JTS空间坐标Geometry使用
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 holes
Polygon 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 datums
MathTransform 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转geometry
WKTReader2 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
<dependencies>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>osgeo</id>
<name>OSGeo Release Repository</name>
<url>https://repo.osgeo.org/repository/release/</url>
<snapshots><enabled>false</enabled></snapshots>
<releases><enabled>true</enabled></releases>
</repository>
<repository>
<id>osgeo-snapshot</id>
<name>OSGeo Snapshot Repository</name>
<url>https://repo.osgeo.org/repository/snapshot/</url>
<snapshots><enabled>true</enabled></snapshots>
<releases><enabled>false</enabled></releases>
</repository>
</repositories>
热门相关:首席的独宠新娘 第一神算:纨绔大小姐 网游之逆天飞扬 霸皇纪 网游之逆天飞扬