org.locationtech.jts:jts-core:1.19.0
链接: github
/**
* 部分重叠
*/
public class GeometryOverlaps {
private final GeometryFactory geometryFactory = new GeometryFactory();
private static final Logger LOGGER = LoggerFactory.getLogger(GeometryOverlaps.class);
private static final WKTWriter WKT_WRITER = new WKTWriter();
private Coordinate[] coordinate1;
private Coordinate[] coordinate2;
/**
* 如果几何图形A和B在空间上重叠,则返回TRUE。
* 如果两个几何图形具有相同的尺寸,每个几何图形至少有一个点不被另一个几何图形共享(或者等价地,两个几何图形都不覆盖另一个),并且它们内部的交点具有相同的尺寸,则两个几何图形重叠。
* 这种重叠关系是对称的。
*/
public static void main(String[] args) {
GeometryOverlaps geometryOverlaps = new GeometryOverlaps();
geometryOverlaps.test02();
}
}
/**
* 多点与多点
*/
public void test00() {
coordinate1 = new Coordinate[] {
new Coordinate(1, 6), new Coordinate(7, 6),new Coordinate(7, 1), new Coordinate(1, 1),
new Coordinate(4, 3), new Coordinate(7, 6)
};
MultiPoint multiPoint1 = geometryFactory.createMultiPointFromCoords(coordinate1);
coordinate2 = new Coordinate[] {
new Coordinate(2, 5), new Coordinate(6, 5),new Coordinate(6, 2), new Coordinate(2, 2),
new Coordinate(4, 3), new Coordinate(7, 6)
};
MultiPoint multiPoint2 = geometryFactory.createMultiPointFromCoords(coordinate2);
LOGGER.info("multiPoint1 - multiPoint2 九交模型值:{}", multiPoint1.relate(multiPoint2).toString());
LOGGER.info("multiPoint1 - multiPoint2 重叠:{}", multiPoint1.overlaps(multiPoint2));
LOGGER.info("multiPoint1: {}, multiPoint2: {}", WKT_WRITER.write(multiPoint1), WKT_WRITER.write(multiPoint2));
}
16:18:38.992 [main] INFO pers.stu.geometry.GeometryOverlaps - multiPoint1 - multiPoint2 九交模型值:0F0FFF0F2
16:18:38.994 [main] INFO pers.stu.geometry.GeometryOverlaps - multiPoint1 - multiPoint2 重叠:true
16:18:39.000 [main] INFO pers.stu.geometry.GeometryOverlaps - multiPoint1: MULTIPOINT ((1 6), (7 6), (7 1), (1 1), (4 3), (7 6)), multiPoint2: MULTIPOINT ((2 5), (6 5), (6 2), (2 2), (4 3), (7 6))
/**
* 线与线
*/
public void test01() {
coordinate1 = new Coordinate[] {
new Coordinate(2, 7), new Coordinate(2, 4),new Coordinate(6, 4), new Coordinate(6, 7)
};
LineString lineString1 = geometryFactory.createLineString(coordinate1);
coordinate2 = new Coordinate[] {
new Coordinate(2, 1), new Coordinate(2, 4),new Coordinate(6, 4), new Coordinate(6, 1)
};
LineString lineString2 = geometryFactory.createLineString(coordinate2);
LOGGER.info("lineString1 - lineString2 九交模型值:{}", lineString1.relate(lineString2).toString());
LOGGER.info("lineString1 - lineString2 重叠:{}", lineString1.overlaps(lineString2));
LOGGER.info("lineString1: {}, lineString2: {}", WKT_WRITER.write(lineString1), WKT_WRITER.write(lineString2));
}
16:54:19.272 [main] INFO pers.stu.geometry.GeometryOverlaps - lineString1 - lineString2 九交模型值:1F1FF0102
16:54:19.274 [main] INFO pers.stu.geometry.GeometryOverlaps - lineString1 - lineString2 重叠:true
16:54:19.280 [main] INFO pers.stu.geometry.GeometryOverlaps - lineString1: LINESTRING (2 7, 2 4, 6 4, 6 7), lineString2: LINESTRING (2 1, 2 4, 6 4, 6 1)
/**
* 面与面
*/
public void test02() {
coordinate1 = new Coordinate[] {
new Coordinate(3, 8), new Coordinate(3, 3),new Coordinate(11, 3), new Coordinate(11, 8),
new Coordinate(3, 8)
};
Polygon polygon1 = geometryFactory.createPolygon(coordinate1);
coordinate2 = new Coordinate[] {
new Coordinate(3, 5), new Coordinate(3, 1),new Coordinate(11, 1), new Coordinate(6, 1),
new Coordinate(3, 5)
};
Polygon polygon2 = geometryFactory.createPolygon(coordinate2);
LOGGER.info("polygon1 - polygon2 九交模型值:{}", polygon1.relate(polygon2).toString());
LOGGER.info("polygon1 - polygon2 重叠:{}", polygon1.overlaps(polygon2));
LOGGER.info("polygon1: {}, polygon2: {}", WKT_WRITER.write(polygon1), WKT_WRITER.write(polygon2));
}
16:55:24.524 [main] INFO pers.stu.geometry.GeometryOverlaps - polygon1 - lineString2 九交模型值:212111212
16:55:24.526 [main] INFO pers.stu.geometry.GeometryOverlaps - polygon1 - lineString2 重叠:true
16:55:24.531 [main] INFO pers.stu.geometry.GeometryOverlaps - polygon1: POLYGON ((3 8, 3 3, 11 3, 11 8, 3 8)), lineString2: POLYGON ((3 5, 3 1, 11 1, 6 1, 3 5))