JTS求多边形重合面积

前言

通过JTS可以实现多边形求面积以及多个多边形求重合度,本文章只列举几个简单的使用例子。
详细的文档使用链接:http://www.tsusiatsoftware.net/jts/main.html

JTS

1.依赖

<dependency>
	<groupId>com.vividsolutions</groupId>
	<artifactId>jts-core</artifactId>
	<version>1.14.0</version>
</dependency>

2.求单个多边形面积

public static void main(String[] args) throws ParseException {
    GeometryFactory geometryFactory = new GeometryFactory();
    WKTReader reader = new WKTReader(geometryFactory);
    Polygon geometry = (Polygon) reader.read("POLYGON((0 0, 10 0, 9 9.5, 0 10,0 0))");
    System.out.println(geometry.getArea());
}

3.求两个多边形合并面积

public static void main(String[] args) throws ParseException {
    GeometryFactory geometryFactory = new GeometryFactory();
    WKTReader reader = new WKTReader(geometryFactory);
    Polygon geometry1 = (Polygon) reader.read("POLYGON((0 0, 2 0 ,2 2, 0 2,0 0))");
    Polygon geometry2 = (Polygon) reader.read("POLYGON((0 0, 4 0 , 4 1, 0 1, 0 0))");
    OverlayOp op = new OverlayOp(geometry1,geometry2);
    Geometry g =op.getResultGeometry(OverlayOp.UNION);
    System.out.println(g.getArea());
}

4.求两个多边形相交面积

public static void main(String[] args) throws ParseException {
    GeometryFactory geometryFactory = new GeometryFactory();
    WKTReader reader = new WKTReader(geometryFactory);
    Polygon geometry1 = (Polygon) reader.read("POLYGON((0 0, 2 0 ,2 2, 0 2,0 0))");
    Polygon geometry2 = (Polygon) reader.read("POLYGON((0 0, 4 0 , 4 1, 0 1, 0 0))");
    OverlayOp op = new OverlayOp(geometry1,geometry2);
    Geometry g = op.getResultGeometry(OverlayOp.INTERSECTION);
    System.out.println(g.getArea());
}

你可能感兴趣的:(java,后端,jts,maven)