首页 > 其他分享 >JTS空间坐标Geometry使用

JTS空间坐标Geometry使用

时间:2023-04-22 23:55:31浏览次数:40  
标签:Geometry JTS geometry boolean 坐标 Coordinate new geometry2

Geomtery子类图

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>

标签:Geometry,JTS,geometry,boolean,坐标,Coordinate,new,geometry2
From: https://www.cnblogs.com/walkAlwaysInCode/p/17331474.html

相关文章

  • matplotlib这个图横坐标上的文本如何调整为竖着显示呢?
    大家好,我是皮皮。一、前言前几天在Python白银交流群【知足】问了一个matplotlib处理的问题,这里拿出来给大家分享下。二、实现过程这里【甯同学】给了一个解决思路,直接plt.xticks(rotation=270),如下图所示:顺利地解决了粉丝的问题。最后补充一个设置画布大小的问题,设置方法......
  • GPS定位坐标纠偏
    做过地图相关开发的同学肯定会遇到这样一个问题:同样的经纬度坐标,在不同的地图上位置不一样。那是因为大家用的不是同一个坐标系。不同的坐标系之间可能有几十到几百米的偏移,所以在开发基于地图的产品,或者做地理数据可视化时,我们需要修正不同坐标系之间的偏差。WGS-84世界大地测量......
  • 矩阵左乘与右乘和旋转矩阵、坐标转换
    (1封私信/82条消息)如何通俗理解矩阵左乘和右乘的区别?-知乎(zhihu.com) ......
  • Three.js教程:自定义顶点UV坐标
    推荐:将NSDT场景编辑器加入你的3D工具链其他工具集:NSDT简石数字孪生自定义顶点UV坐标学习自定义顶点UV坐标之前,首先保证你对BufferGeometry的顶点数据、纹理贴图都有一定的理解。#顶点UV坐标的作用顶点UV坐标的作用是从纹理贴图上提取像素映射到网格模型Mesh的几何体表面上。浏览器......
  • 地图开发中的坐标系系统(二)
    04-地图开发中的坐标系系统(二)因为投影方式的不同,所以并没有一个严格的标准且统一的方式来表述某个点的位置。当没有统一标准的时候,就会存在很多体系标准,而当各种体系标准变得庞大且失去统一管理的时候,人们想将不同体系之间的坐标互相转化的话就会变得异常困难。EPSG就是来管理......
  • 将坐标轴设定到原点位置
    (47条消息)matlabxaxislocation,x轴和y轴位置_云筱竹的博客-CSDN博客x轴位置set(gca,'XAxisLocation','bottom'); %将x轴的位置设置在底部(默认)。set(gca,'XAxisLocation','top');  %将x轴的位置设置在顶部。set(gca,'XAxisLocation','origin');%将......
  • 【ArcPy】如何在ArcPy创建要素中生成精准的XY坐标?解决精度损失问题
    使用ArcPy创建要素的代码段前面有发布,【arcpy】创建点、线、面(孔洞、环、多部件)要素、要素类Q:这些代码里创建要素后会存在XY精度损失的问题,如何解决?A:解决方案是在创建要素过程中指定正确的空间参考。答案来自 geometry-HowtohandlecoordinatesaccuracyinArcGIS-Geog......
  • 【位姿估计】相机坐标系转换
    ConvertingcameraposesfromOpenCVtoOpenGLcanbeeasy参考链接https://medium.com/check-visit-computer-vision/converting-camera-poses-from-opencv-to-opengl-can-be-easy-27ff6c413bdb......
  • 12 Geometry
    关键点MeshSubdivision(LoopSubdivision,Catmull-ClarkSubdivision)MeshSimplification(EdgeCollapsing)MeshRegularization1.MeshOperarions1.1MeshSubdivision--Upsampling细分细分:把三角形数量增多调整:改变三角形的位置1.1.1LoopSubdivision每个三角形......
  • 11 Geometry(Curves and Surface)
    关键点BezierCurvesBezierSurfaces1.BezierCurves贝塞尔曲线用一系列的控制点定义一个曲线,并且定义了一系列控制。如下定义起止点为p0、p3,且定义起始切线,则绘制出一条曲线。1.1deCasteljauAlogrithmThreeinputpoints定义一个时间t,然后取b0b1与b1b2的相应比......