Geomtery子类图
创建Geometry
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();// 点Coordinate coord = new Coordinate(1, 1);Point point = geometryFactory.createPoint(coord);// 线Coordinate[] coordinates = new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) };LineString line = geometryFactory.createLineString(coordinates);// 面Coordinate[] coords = new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2), new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) };LinearRing ring = geometryFactory.createLinearRing(coords);LinearRing holes[] = null; // use LinearRing[] to represent holesPolygon polygon = geometryFactory.createPolygon(ring, holes);// 圆CurvedGeometryFactory curvedFactory = new CurvedGeometryFactory(geometryFactory, Double.MAX_VALUE);CoordinateSequence coords = PackedCoordinateSequenceFactory.DOUBLE_FACTORY.create(new double[] {10, 14, 6, 10, 14, 10}, 2);CircularString arc = (CircularString) curvedFactory.createCurvedGeometry(coords);
Geometry转换坐标系
CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326");CoordinateReferenceSystem targetCrs = CRS.decode("EPSG:4490");boolean lenient = true; // allow for some error due to different datumsMathTransform transform = CRS.findMathTransform(sourceCRS, targetCrs, lenient);Geometry geometry2 = JTS.transform(geometry, transform);
Geometry方法
// 空间判断// 不相交boolean disjoint = geometry.disjoint(geometry2);// 相交boolean intersects = geometry.intersects(geometry2);// 相切,内部不相交boolean touches = geometry.touches(geometry2);// 被包含boolean within = geometry.within(geometry2);//包含,只针对几何内部而言,不计算边界boolean contains = geometry.contains(geometry2);//覆盖,不区分集合边界与内部boolean covers = geometry.covers(geometry);//相交,不能是相切或者包含boolean crosses = geometry.crosses(geometry);//相交boolean overlaps = geometry.overlaps(geometry2);// 两个几何的空间关系IntersectionMatrix relate1 = geometry.relate(geometry2);//空间计算//求交集Geometry intersection = geometry.intersection(geometry2);//求并集Geometry union = geometry.union(geometry);//geometry-交集Geometry difference = geometry.difference(geometry2);// 并集-交集Geometry symDifference = geometry.symDifference(geometry);// 几何缓冲生成新几何Geometry buffer1 = geometry.buffer(2);// 生成包含几何的最小凸多边形Geometry convexHull = geometry.convexHull();// 两个几何的最小距离double distance = geometry.distance(geometry);// 面积double area = geometry.getArea();//几何类型String geometryType = geometry.getGeometryType();// 边界Geometry boundary = geometry.getBoundary();// 获取中心点Point centroid = geometry.getCentroid();
Geometry 与 WKT 转换
// geometry转wkt//方法一WKTWriter2 wktWriter2 = new WKTWriter2()String wkt = wktWriter2.write(geometry)//方法二String text = geometry.toText();// wkt转geometryWKTReader2 wktReader2 = WKTReader2()Point point = (Point) wktReader2.read("POINT (1 1)");LineString line = (LineString) wktReader2.read("LINESTRING(0 2, 2 0, 8 6)");Polygon polygon = (Polygon) wktReader2.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
pom.xml
org.geotools gt-shapefile ${geotools.version} org.geotools gt-epsg-hsql ${geotools.version} osgeo OSGeo Release Repository https://repo.osgeo.org/repository/release/ false true osgeo-snapshot OSGeo Snapshot Repository https://repo.osgeo.org/repository/snapshot/ true false