JTS

常用的Geometry数据结构

Geometry数据结构

GeometryFactory

GeometryFactory是geometry的工厂类,提供了Geometry子类的创建方法的统一入口。在创建Geometry的时候建议用GeometryFactory去创建,不要单独自己去new一个子类。

数据结构

private PrecisionModel precisionModel : 精度模式,默认为FLOAT
private CoordinateSequenceFactory coordinateSequenceFactory : 坐标序列工厂类,创CoordinateList

PrecisionModel

创建Geometry的时候通过PrecisionModel可以控制geometry坐标的精度。
三种模式:
FLOATING: 等同于Java的双精度double类型
FLOATING_SINGLE: 等同于Java的单精度float类型
FIXED: 固定精度模式(固定小数点后几位)。通过public PrecisionModel(double scale)构造方法构造。
FIXED去固定精度并不会改变geometry里的点的真实坐标,只会去改变toString出来的wkt格式的坐标精度。慎用。

Geometry类常用工具方法

LineString

数据结构

CoordinateSequence coordinates: 一堆点的集合。

常用方法
method description
boolean isClosed() 判断LineString是否闭合,第一个点和最后一个点是否相等
boolean isRing() 在判断isClosed()的同时判断是否是简单图形
LineString copyInternal() 深拷贝一个LineString
LinearRing

可以理解成封闭的LineString,至少需要3个点。

Polygon
数据结构:

LinearRing shell : 表示外轮廓。一个Polygon只能有一个shell。
LinearRing[] holes: 表示洞。一个Polygon可有多个hole。

常用方法
method description
Coordinate[] getCoordinates() 获取Polygon所有的点,polygon中洞的点会排在外轮廓的点之后
boolean isRectangle() 判断polygon是不是矩形
LinearRing getExteriorRing() 获取polygon外轮廓
LinearRing getInteriorRingN(int n) 获取某一个洞
void apply(CoordinateFilter filter) 直接修改一个polygon的值(比如精度修改,放大缩小之类的操作)
Polygon copyInternal() 等同于深拷贝
Geometry convexHull() 拿到Polygon的凸包
void normalize() 将polygon格式化为逆时针构建,一般equal之前会先normalize一下
Geometry normalize() 同上,只是返回结果为一个Polygon
GeometryCollection
数据结构:

Geometry[] geometries: geometry的集合。子类有MultiPoint MultiLineString MultiPolygon.

常用方法:

实现了所有的Geometry的抽象方法,只是改成了对集合整体的操作

Geometry位置关系

method description example
Intersects 两个Geometry至少有一个共同的点。可以理解为相交
disjoint 与Intersect功能相反,两个Geometry没有共同的点
touches 两个Geometry至少有一个共同点,且只能是外轮廓有相交,内部不能相交。对于LineString来说,只有当交点为两条线的端点的时候,touches才能为true
A.touches(B) == true
A.touches(B) == true
crosses A.crosses(B) == B.crosses(A)可以理解为穿过,两个Geometry有共同点且不是所有的点都为共同点。多用于对lineString的判断
A.crosses(B) == true.
A.crosses(B) == true
contains/withIn A.contains(B),B所有的点都在A里且两个geometry的内部至少有一个共同点。简单理解为A包含B。相等的时候contains也为true。A.contains(B) == B.withIn(A)
covers/coveredBy A.covers(B)==B.coveredBy(A)。官方的说明是:类似于contains,但更具包容性(即对于更多情况,返回true)。特别是,与包含不同,它不区分几何体边界和内部的点
overlaps 至少有一个点彼此不共享,或者等价于两个Geometry互相不cover。理解为有重叠
A.overlaps(B)==true
A.overlaps(B)==true

Geometry的一些计算

集合运算

method description example
intersection A.intersection(B) == B.intersection(A)。获取两个Geometry的交集
黄色区域为intersection
union A.union(B) == B.union(A)。获取两个Geometry的合集
黄色部分为A.union(B) 的结果
difference A.difference(B)获取的是A中不包含B的部分,B.difference(A)是获取B中不包含A的部分
黄色部分为A.difference(B)的结果
symDifference A.symDifference(B) == B.symDifference(A).获取的是两个Geometry union的结果取intersection的补集
黄色部分为A.symDifference(B)的结果

Buffer

参数说明BufferParams
parameter description
quadrantSegments 连接两个点为一个1/4圆弧的控制点数,默认为8
endCapStyle 表示不同的线端
CAP_ROUND = 1 圆弧形状的线端样式
CAP_FLAT = 2 平的线端样式
CAP_SQUARE = 3 矩形线端样式,主要是用在LineString,对于Polygon与FLat似乎没区别
joinStyle 端点连接方式
JOIN_ROUND = 1 圆弧连接
JOIN_MITRE = 2 斜角连接
JOIN_BEVEL = 3 斜切连接
mitreLimit 使用JOIN_MITER这种策略来表示连接处形状的时候,由于两根线的连接角度可能很小,那么, 就会导致延伸出来的那个角特别长。mitreLimit用来限制那个尖角的最大长度,默认为5
连接方式说明
示例
Polygon
image

image

image

image

image
LineString
image

image

image

其他有用的类

Orientation

主要用来计算点集合的构建顺序

method description
boolean isCCW(Coordinate[] ring) 判断点集合是否是逆时针构建
MinimumDiameter

包含几何图形的最小直径

method description
Geometry getMinimumRectangle() 计算geometry的最小外接矩形
AffineTransformation
method description
AffineTransformation rotationInstance(double theta) 旋转几何图形theta为弧度 默认以(0,0)为旋转中心
AffineTransformation rotationInstance(double sinTheta, double cosTheta) 旋转几何图形 sinTheta cosTheta组合表示一个弧度 默认以(0,0)为旋转中心
AffineTransformation rotationInstance(double theta, double x, double y) 旋转 theta为旋转角度,(x,y)为旋转中心
AffineTransformation translationInstance(double x, double y) 以(x,y)向量做平移
用法示例
 // 以(x,y)为旋转点将geometry旋转rotateAngle角度
AffineTransformation affineTransformation =
AffineTransformation.rotationInstance(Math.toRadians(rotateAngle), point.getX(), point.getY());
affineTransformation.transform(geometry);
LineSegment

表示由两个坐标定义的线段。提供计算各种几何特性的方法以及线段之间的关系的方法。

method description
boolean isHorizontal() 判断线段是否水平
boolean isVertical() 判断线段是否垂直
int orientationIndex(LineSegment seg) 判断两个线段的位置关系。1表示seg在左侧,-1表示在右侧,0表示与该段共线或交叉。如果A完全位于L左侧的闭合半平面内,则A位于L段的左侧。如果A完全位于L右侧的闭合半平面内,则A位于L段的右侧。如果A与L共线或A与L确定的线相交,则,A相对于L有不确定的方向。
double distancePerpendicular(Coordinate p) 点与线的垂线距离
Coordinate pointAlong(double segmentLengthFraction) 按一定的比例找到线段上的点
LineSegment project(LineSegment seg) 将seg线段投影到该线段上
Coordinate reflect(Coordinate p) 点关于线做镜像
project示例
l1.project(l2)

l2.project(l1)

你可能感兴趣的:(JTS)