GeoTools测试几何合并union
一 合并测试
@Test
public void unionTest() {
GeometryFactory factory = new GeometryFactory();
Polygon polygon_1 = factory.createPolygon(new Coordinate[]{
new Coordinate(0, 0),
new Coordinate(10, 0),
new Coordinate(10, 10),
new Coordinate(0, 10),
new Coordinate(0, 0),});
System.out.println(polygon_1.toString());
Polygon polygon_2 = factory.createPolygon(new Coordinate[]{
new Coordinate(10, 0),
new Coordinate(20, 0),
new Coordinate(20, 10),
new Coordinate(10, 10),
new Coordinate(10, 0),});
System.out.println(polygon_2.toString());
Polygon polygon_3 = factory.createPolygon(new Coordinate[]{
new Coordinate(0, 11),
new Coordinate(10, 11),
new Coordinate(10, 21),
new Coordinate(0, 21),
new Coordinate(0, 11),});
System.out.println(polygon_3.toString());
Polygon polygon_4 = factory.createPolygon(new Coordinate[]{
new Coordinate(10, 11),
new Coordinate(20, 11),
new Coordinate(20, 21),
new Coordinate(10, 21),
new Coordinate(10, 11),});
System.out.println(polygon_4.toString());
// 相邻多边形合并
Geometry union_12 = polygon_1.union(polygon_2);
System.out.println(union_12.toString());
// 不相邻多边形合并
Geometry union_13 = polygon_1.union(polygon_3);
System.out.println(union_13.toString());
// 多多边形中某个多边形与另外一个多边形相邻进行合并
Geometry union_134_ = union_13.union(polygon_4);
System.out.println(union_134_.toString());
}
# union_12
POLYGON ((10 0, 0 0, 0 10, 10 10, 20 10, 20 0, 10 0))
# union_13
MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0)), ((0 11, 0 21, 10 21, 10 11, 0 11)))
# union_134
MULTIPOLYGON (((0 0, 0 10, 10 10, 10 0, 0 0)), ((0 11, 0 21, 10 21, 20 21, 20 11, 10 11, 0 11)))
二 结论
- 两个相交的多边形合并后会成为一个多边形
- 两个不相交的多边形合并后会成为一个多多边形
- 一个多多边形中某个多边形和另外一个多边形相交,合并后内部相交的多边形几何成为一个合并后的多边形
- 其他几何类型相似
三 其他
- 可以在线渲染WKT的网站openlayers-example
- 可以在线渲染GeoJson的网站http://geojson.io
var code = “7dc8a9e3-e41b-4a88-8220-0aab59e36986”