JTS空间坐标Geometry使用

打印 上一主题 下一主题

主题 766|帖子 766|积分 2298

Geomtery子类图


创建Geometry
  1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  2. // 点
  3. Coordinate coord = new Coordinate(1, 1);
  4. Point point = geometryFactory.createPoint(coord);
  5. // 线
  6. Coordinate[] coordinates =  new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) };
  7. LineString line = geometryFactory.createLineString(coordinates);
  8. // 面
  9. Coordinate[] coords  =  new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2), new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) };
  10. LinearRing ring = geometryFactory.createLinearRing(coords);
  11. LinearRing holes[] = null; // use LinearRing[] to represent holes
  12. Polygon polygon = geometryFactory.createPolygon(ring, holes);
  13. // 圆
  14. CurvedGeometryFactory curvedFactory = new CurvedGeometryFactory(geometryFactory, Double.MAX_VALUE);
  15. CoordinateSequence coords =  PackedCoordinateSequenceFactory.DOUBLE_FACTORY.create(new double[] {10, 14, 6, 10, 14, 10}, 2);
  16. CircularString arc = (CircularString) curvedFactory.createCurvedGeometry(coords);
复制代码
Geometry转换坐标系
  1. CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");
  2. CoordinateReferenceSystem targetCrs = CRS.decode("EPSG:4490");
  3. boolean lenient = true; // allow for some error due to different datums
  4. MathTransform transform = CRS.findMathTransform(sourceCRS, targetCrs, lenient);
  5. Geometry geometry2 = JTS.transform(geometry, transform);
复制代码
Geometry方法
  1. // 空间判断
  2. // 不相交
  3. boolean disjoint = geometry.disjoint(geometry2);
  4. // 相交
  5. boolean intersects = geometry.intersects(geometry2);
  6. // 相切,内部不相交
  7. boolean touches = geometry.touches(geometry2);
  8. // 被包含
  9. boolean within = geometry.within(geometry2);
  10. //包含,只针对几何内部而言,不计算边界
  11. boolean contains = geometry.contains(geometry2);
  12. //覆盖,不区分集合边界与内部
  13. boolean covers = geometry.covers(geometry);
  14. //相交,不能是相切或者包含
  15. boolean crosses = geometry.crosses(geometry);
  16. //相交
  17. boolean overlaps = geometry.overlaps(geometry2);
  18. // 两个几何的空间关系
  19. IntersectionMatrix relate1 = geometry.relate(geometry2);
  20. //空间计算
  21. //求交集
  22. Geometry intersection = geometry.intersection(geometry2);
  23. //求并集
  24. Geometry union = geometry.union(geometry);
  25. //geometry-交集
  26. Geometry difference = geometry.difference(geometry2);
  27. // 并集-交集
  28. Geometry symDifference = geometry.symDifference(geometry);
  29. // 几何缓冲生成新几何
  30. Geometry buffer1 = geometry.buffer(2);
  31. // 生成包含几何的最小凸多边形
  32. Geometry convexHull = geometry.convexHull();
  33. // 两个几何的最小距离
  34. double distance = geometry.distance(geometry);
  35. // 面积
  36. double area = geometry.getArea();
  37. //几何类型
  38. String geometryType = geometry.getGeometryType();
  39. // 边界
  40. Geometry boundary = geometry.getBoundary();
  41. // 获取中心点
  42. Point centroid = geometry.getCentroid();
复制代码
Geometry 与 WKT 转换
  1. // geometry转wkt
  2. //方法一
  3. WKTWriter2 wktWriter2 = new WKTWriter2()
  4. String wkt = wktWriter2.write(geometry)
  5. //方法二
  6. String text = geometry.toText();
  7. // wkt转geometry
  8. WKTReader2 wktReader2 = WKTReader2()
  9. Point point = (Point) wktReader2.read("POINT (1 1)");
  10. LineString line = (LineString) wktReader2.read("LINESTRING(0 2, 2 0, 8 6)");
  11. Polygon polygon = (Polygon) wktReader2.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
复制代码
pom.xml
  1. <dependencies>
  2.         <dependency>
  3.             <groupId>org.geotools</groupId>
  4.             <artifactId>gt-shapefile</artifactId>
  5.             <version>${geotools.version}</version>
  6.         </dependency>
  7.         <dependency>
  8.             <groupId>org.geotools</groupId>
  9.             <artifactId>gt-epsg-hsql</artifactId>
  10.             <version>${geotools.version}</version>
  11.         </dependency>
  12.     </dependencies>
  13.   <repositories>
  14.     <repository>
  15.       <id>osgeo</id>
  16.       <name>OSGeo Release Repository</name>
  17.       <url>https://repo.osgeo.org/repository/release/</url>
  18.       <snapshots><enabled>false</enabled></snapshots>
  19.       <releases><enabled>true</enabled></releases>
  20.     </repository>
  21.     <repository>
  22.       <id>osgeo-snapshot</id>
  23.       <name>OSGeo Snapshot Repository</name>
  24.       <url>https://repo.osgeo.org/repository/snapshot/</url>
  25.       <snapshots><enabled>true</enabled></snapshots>
  26.       <releases><enabled>false</enabled></releases>
  27.     </repository>
  28.   </repositories>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表