JTS空间坐标Geometry使用

Posted 行走于代码中

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JTS空间坐标Geometry使用相关的知识,希望对你有一定的参考价值。

Geometry,WKT,Geotools

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>

休眠空间多边形坐标方向

我正在使用hibernate空间(带有JTS的geolatte)和oracle数据库。我正在创建一个带有多边形的Geometry对象,因此它被保存为数据库中的SDO_GEOMETRY对象。目前,所有多边形都逆时针存储,SDO_ETYPE在SDO_ELEM_INFO_ARRAY中设置为1003(外部多边形环)。如果我反转多边形并保持它,则逆时针方向不会改变。

因此,似乎hibernate正在强制执行此要求,或者一旦创建了SDO_GEOMETRY对象,就会设置方向。

我想控制多边形在数据库中保存的方向。这是一个休眠设置吗?当我创建多边形而不是贝壳时,是否需要将多边形存储为孔(内环)?

谢谢!

答案

Oracle要求特定订购作为SDO规范的一部分。注意我是GeoTools代码的原作者,该代码已在JTS中清理使用。

有关详细信息,请参阅javadoc示例或SDO文档。

另一答案

Oracle Spatial文档在2.2.4节中指定了多边形环方向:

https://docs.oracle.com/cd/A97630_01/appdev.920/a96630/sdo_objrelschema.htm#sthref198

以上是关于JTS空间坐标Geometry使用的主要内容,如果未能解决你的问题,请参考以下文章

hibernate 混合了vividsolutions.jts Geometry 和locationtech.jts.Geometry

JTS 空间数据关系判定

JTS 空间数据关系判定

如何将 JTS-Geometry 转换为 AWT-Shape?

MySQL 空间位置坐标 geometry 的使用

JTS Topology Suite 空间数据处理