JTS geometry 常用方法使用记录(一)
1.描述几何的2种表达方式:WKT 、WKB
WKT:(Well-known Text)可以通过文本来描述几何对象。
WKB(Well-known Binary)通过序列化的字节对象来描述几何对象(一般在数据库中使用)。
参考sfs标准:
几何类型 |
WKT 例子 |
说明 |
Point |
Point (10 10) |
点 |
LineString |
LineString ( 10 10, 20 20, 30 40) |
有 3 个节点的线 |
Polygon |
Polygon ((10 10, 10 20, 20 20, 20 15, 10 10)) |
叧有 1 个外环的多边形 |
MultiPoint |
MultiPoint ( (10 10), (20 20) |
多点 |
) |
||
MultiLineString |
MultiLineString ( (10 10, 20 20), (15 15, 30 15) ) |
多线 |
MultiPolygon |
MultiPolygon ( ((10 10, 10 20, 20 20, 20 15, 10 10)), ((60 60, 70 70, 80 60, 60 60 )) ) |
多面 |
GeometryCollection |
GeometryCollection ( POINT (10 10), POINT (30 30), LINESTRING (15 15, 20 20) ) |
几何集合 |
PolyhedralSurface |
PolyhedralSurface Z ( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)), ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)), ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)), ((0 0 1, 1 0 1, 1 1 1, 0 1 1. 0 0 1)) ) |
多个表面构成的立方体 |
Tin |
Tin Z ( ((0 0 0, 0 0 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 0 0, 0 0 0)), ((0 0 0, 1 0 0, 0 0 1, 0 0 0)), ((1 0 0, 0 1 0, 0 0 1, 1 0 0)), ) |
4 个三觇形构成的 TIN 网格 |
Point |
Point Z (10 10 5) |
三维点 |
Point |
Point ZM (10 10 5 40) |
带 M 值的三维点 |
Point |
Point M (10 10 40) |
带 M 值的二维点 |
2. geometry 类继承关系
geometry 类:几何对象操作方法如下图:
3. geometry 基础类提供的操作方法
1. 获取长度。
1. 线几何返回点与点之间的长度之和,例如:lineString
2. 闭合几何返回周长,例如:ploygon
3. 其它返回0,例如:point
public double getLength() { return 0.0D; }
2. 获取srid
public int getSRID() { return this.SRID; }
3.判读几何是否是空
1. 判断几何的 point.size == 0 ;
2.几何包含 empty: reader.read(“POINT EMPTY”)
public abstract boolean isEmpty();
4.返回字符串形式图形数据,WKT 格式数据
public String toString() { return this.toText(); }
5. 判断几何是否有效,符合sfs标准。
1.可以判断Polygon是否自相交等
public boolean isValid() { return IsValidOp.isValid(this); }
6. 判断几何是否是Polygon
public boolean isRectangle() { return false; }
7. 判断两个几何是否相等
public boolean equals(Object o) { if (!(o instanceof Geometry)) { return false; } else { Geometry g = (Geometry)o; return this.equalsExact(g); } }
8. 判断几何是否是不相交(脱节)
public boolean disjoint(Geometry g) { return !this.intersects(g); }
9.两个几何是否相交
public boolean intersects(Geometry g) { if (!this.getEnvelopeInternal().intersects(g.getEnvelopeInternal())) { return false; } else if (this.isRectangle()) { return RectangleIntersects.intersects((Polygon)this, g); } else if (g.isRectangle()) { return RectangleIntersects.intersects((Polygon)g, this); } else if (!this.isGeometryCollection() && !g.isGeometryCollection()) { return this.relate(g).isIntersects(); } else { boolean r = false; for(int i = 0; i < this.getNumGeometries(); ++i) { for(int j = 0; j < g.getNumGeometries(); ++j) { if (this.getGeometryN(i).intersects(g.getGeometryN(j))) { return true; } } } return false; } }
任意部分有相交,等价于判断空间关系的 DE-9IM 字符串表达是否是以下之一:
T********
*T*******
***T*****
****T****
10.判断几何是否接触
public boolean touches(Geometry g) { return !this.getEnvelopeInternal().intersects(g.getEnvelopeInternal()) ? false : this.relate(g).isTouches(this.getDimension(), g.getDimension()); }
11.判断几何是否交叉
public boolean crosses(Geometry g) { return !this.getEnvelopeInternal().intersects(g.getEnvelopeInternal()) ? false : this.relate(g).isCrosses(this.getDimension(), g.getDimension()); }
12.判断当前几何是否在指定的几何内部
public boolean within(Geometry g) { return g.contains(this); }
13.判断当前几何包含g几何
public boolean contains(Geometry g) { if (g.getDimension() == 2 && this.getDimension() < 2) { return false; } else if (g.getDimension() == 1 && this.getDimension() < 1 && g.getLength() > 0.0D) { return false; } else if (!this.getEnvelopeInternal().contains(g.getEnvelopeInternal())) { return false; } else { return this.isRectangle() ? RectangleContains.contains((Polygon)this, g) : this.relate(g).isContains(); } }
14. 判断当前几何与参数g几何是否部分重叠
public boolean overlaps(Geometry g) { return !this.getEnvelopeInternal().intersects(g.getEnvelopeInternal()) ? false : this.relate(g).isOverlaps(this.getDimension(), g.getDimension()); }
15.返回两个几何体相交部分图形数据
public Geometry intersection(final Geometry other) { if (!this.isEmpty() && !other.isEmpty()) { return (Geometry)(this.isGeometryCollection() ? GeometryCollectionMapper.map((GeometryCollection)this, new MapOp() { public Geometry map(Geometry g) { return g.intersection(other); } }) : SnapIfNeededOverlayOp.overlayOp(this, other, 1)); } else { return OverlayOp.createEmptyResult(1, this, other, this.factory); } }
16.返回两个几何体不相交集合
public Geometry difference(Geometry other) { if (this.isEmpty()) { return OverlayOp.createEmptyResult(3, this, other, this.factory); } else if (other.isEmpty()) { return this.copy(); } else { checkNotGeometryCollection(this); checkNotGeometryCollection(other); return SnapIfNeededOverlayOp.overlayOp(this, other, 3); } }
17. 返回两个几何的并集
public Geometry union() { return UnaryUnionOp.union(this); }