JTS geometry 常用方法使用记录(一)

本文介绍了几何对象的两种表达方式:WKT(Well-known Text)和WKB(Well-known Binary),并通过实例展示了不同类型的几何对象如何用WKT表示。此外,还详细解释了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);
    }

JTSJava Topology Suite)是个广泛使用的开源 Java 库,用于处理几何对象和执行空间操作。`Geometry` 是 JTS 中所有几何对象的基类,它定义了所有几何类型共有的方法和属性。根据 JTS 的设计,`Geometry` 类有多个直接和间接子类,这些子类代表了不同的几何类型,并支持多种空间操作。 ### JTS Geometry 类的主要子类包括: - **Point** 表示个单的点,即零维几何对象,用于表示空间中的个位置。`Point` 是 `Geometry` 的直接子类,它由个单独的 `Coordinate` 实例定义 [^2]。 - **MultiPoint** 表示由多个点组成的集合,是 `GeometryCollection` 的子类。`MultiPoint` 用于表示多个不相连的点对象 [^2]。 - **LineString** 表示由系列坐标点组成的线性对象,是维几何类型,用于表示线段或路径。`LineString` 是 `Geometry` 的直接子类 [^2]。 - **LinearRing** 表示封闭的线性环,是 `LineString` 的子类,专门用于定义 `Polygon` 的边界。`LinearRing` 的起点和终点必须相同,以形成闭合的环 [^2]。 - **MultiLineString** 表示由多条 `LineString` 组成的集合,是 `GeometryCollection` 的子类,用于表示多个不相连的线段 。 - **Polygon** 表示二维几何对象,通常由个外环(`LinearRing`)和零个或多个内环(孔)组成,用于表示面状实体 。 - **MultiPolygon** 表示由多个 `Polygon` 组成的集合,是 `GeometryCollection` 的子类,用于表示多个不相连的多边形 。 - **GeometryCollection** 表示几何对象的集合,可以包含不同类型的几何对象(如点、线、面等),是 `Geometry` 的子类。`GeometryCollection` 是个通用容器,用于组织多种几何类型 [^2]。 这些几何类之间存在清晰的继承关系,例如 `Polygon` 继承自 `Geometry`,而 `MultiPolygon` 继承自 `GeometryCollection`。这种结构使得 JTS 能够灵活地支持多种空间数据建模和分析需求。 ### 示例:创建 Point 对象 以下是使用 `GeometryFactory` 创建 `Point` 对象的代码示例: ```java import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.Point; public class GeometryDemo { private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); public Point createPoint(double x, double y) { Coordinate coordinate = new Coordinate(x, y); return geometryFactory.createPoint(coordinate); } } ``` 该代码展示了如何使用 `GeometryFactory` 构造个 `Point` 实例。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值