JTS-Geometry 使用说明(五)

26 篇文章 3 订阅
本文详细介绍了org.locationtech.jts.geom.Geometry类在处理平面和线性几何操作时的方法,包括获取长度、判断几何类型、计算中心点、进行几何比较和计算等。示例代码展示了如何使用GeometryFactory和WKTReader读取并操作几何对象,以及如何调用如getEnvelope、intersection、difference等方法。
摘要由CSDN通过智能技术生成

org.locationtech.jts.geom.Geometry 使用说明

示例代码库

Geometry 经纬度操作类

Geometry类继承关系

在这里插入图片描述

说明

平面、线性几何操作抽象类

提供的相关方法:

1.基础方法

1.1 getLength:获取长度,线几何返回点与点之间的长度之和;闭合几何返回周长;其它返回0

1.2 getSRID:获取srid

1.3 isEmpty:判读几何是否是空,判断几何的 point.size == 0 ; 或者几何包含 empty: reader.read(“POINT EMPTY”)

1.4 isRectangle:判断几何是否是Polygon

1.5 isValid:判断几何是否符合OGC SFS规格(OGC SFS specification),例如:Polygon是否自相交等

1.6 getCentroid: 获取中心点

1.7 getCoordinates:获取coordinate数据

1.8 getEnvelope:获取边界

1.9 toText:返回WKT格式数据

2.查询比较方法:

2.1 equals(Geometry g): 判断两个几何是否相等,不用于GeometryCollection,图引用来自sfs标准

在这里插入图片描述


2.2 disjoint(Geometry g): 判断几何是否是不相交(脱节)的

在这里插入图片描述


2.3 intersects(Geometry g): 判断几何是否相交:

在这里插入图片描述


2.4 touches(Geometry g): 判断几何是否接触:

在这里插入图片描述


2.5 crosses(Geometry g): 判断几何是否交叉:

在这里插入图片描述


2.6 within(Geometry g): 判断当前几何是否在指定的几何内部:

在这里插入图片描述


2.7 contains(Geometry g): 判断当前几何包含g几何:

在这里插入图片描述


2.8 overlaps(Geometry g): 判断当前几何与参数g几何是否部分重叠:

在这里插入图片描述


2.9 relate(Geometry g, String intersectionPattern): 判断当前几何与参数几何是否符合输入的 DE-9IM(例如:intersectionPattern=0*01T12F2)

2.10 covers(Geometry g): 判断当前几何是否覆盖参数g几何:例如黄色的几何覆盖内层几何

在这里插入图片描述


2.11 coveredBy(Geometry g): 判断当前几何是否被参数g几何掩盖:例如内层几何被外层黄色几何掩盖

在这里插入图片描述


3.计算方法:

3.1 intersection(Geometry other): 返回当前几何与输入几何相交的几何数据

在这里插入图片描述


3.2 difference(Geometry other): 计算差异几何,差异分析,返回other与当前几何不同的几何数据

在这里插入图片描述


3.3 union(Geometry other): 合并几何

在这里插入图片描述


3.4 symDifference(Geometry other): 对称差异分析,排除一样(重叠)的几何,将当前与other不一样的几何合集返回

在这里插入图片描述


3.5 buffer(double distance): 加buffer

在这里插入图片描述


3.6 buffer(double distance, int quadrantSegments): 加buffer,边界样式

在这里插入图片描述


3.7 convexHull(): 凸包分析,返回当前的几何的覆盖面

在这里插入图片描述


在这里插入图片描述

直接子类

GeometryCollection, LineString, Point, Polygon

操作示例
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import java.io.*;

public class GeometryTest {

    public static void main(String[] args) throws FileNotFoundException, ParseException {

        PrecisionModel precisionModel = new PrecisionModel();
        GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0 ,PackedCoordinateSequenceFactory.DOUBLE_FACTORY);

        //通过geometryFactory 或者 Geometry子类创建 Geometry 实例
        //通过wktReader读取飞机形状wkt数据 (见图一)
        WKTReader wktReader = new WKTReader(geometryFactory);
        InputStream is = GeometryTest.class.getResourceAsStream("/wkt/plane.wkt");
        Geometry geometry = wktReader.read(new InputStreamReader(is));
        System.out.printf("WKT几何形状%s \r\n",geometry.toText());

        //Geometry常用方法示例 (见图一) 红色框代表边界
        Geometry envelopeGeo =  geometry.getEnvelope();
        System.out.printf("WKT边界 geometry.getEnvelope %s", envelopeGeo.toText());
    }
}
操作示例截图(图一)

在这里插入图片描述

可以使用 JTS Topology Suite 提供的 `CircularArc` 类来创建圆弧,具体实现步骤如下: 1. 定义圆心坐标、半径、起点坐标和终点坐标: ```java Coordinate center = new Coordinate(xc, yc); // 圆心坐标 double radius = r; // 半径 Coordinate startPt = new Coordinate(xs, ys); // 弧起点坐标 Coordinate endPt = new Coordinate(xe, ye); // 弧终点坐标 ``` 2. 计算起点角度和终点角度: ```java double startAngle = Math.atan2(startPt.y - center.y, startPt.x - center.x); double endAngle = Math.atan2(endPt.y - center.y, endPt.x - center.x); ``` 3. 创建 `CircularArc` 对象: ```java CircularArc arc = new CircularArc(center, radius, startAngle, endAngle); ``` 完整示例代码: ```java import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.CircularArc; public class ArcCreator { public static void main(String[] args) { // 定义圆心坐标、半径、起点坐标和终点坐标 Coordinate center = new Coordinate(0, 0); double radius = 10; Coordinate startPt = new Coordinate(5, 5); Coordinate endPt = new Coordinate(5, -5); // 计算起点角度和终点角度 double startAngle = Math.atan2(startPt.y - center.y, startPt.x - center.x); double endAngle = Math.atan2(endPt.y - center.y, endPt.x - center.x); // 创建 CircularArc 对象 CircularArc arc = new CircularArc(center, radius, startAngle, endAngle); // 打印圆弧的几何信息 System.out.println(arc.toGeometry()); } } ``` 输出结果: ``` LINESTRING (10.000000000000002 0,9.510565162951535 3.090169943749471,8.090169943749474 5.877852522924732,5.877852522924733 8.090169943749472,3.090169943749472 9.510565162951534,1.8369701987210297e-15 10,-3.090169943749471 9.510565162951534,-5.877852522924732 8.090169943749474,-8.090169943749474 5.877852522924733,-9.510565162951535 3.090169943749475,-10 1.2246467991473533e-15,-9.510565162951535 -3.090169943749474,-8.090169943749474 -5.877852522924732,-5.877852522924733 -8.090169943749472,-3.0901699437494737 -9.510565162951534,-5.510910596163089e-15 -10) ``` 输出结果是圆弧的几何信息,可以通过 `toGeometry()` 方法将 `CircularArc` 对象转换为 JTS 的 `Geometry` 对象,并进行后续操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值