The interface is localized. Skill descriptions and documents are machine-translated by AI into your language; code, commands, paths and technical terms stay in the original, and the author's document governs.
Explore skills
Productivity · SKILL.md

jts

jts

Use when performing precise 2D computational geometry in Java — spatial predicates (contains, intersects), overlay operations, buffering, triangulation. JTS (Java Topology Suite): the canonical geometry engine used as blueprint for GEOS, Shapely, and NetTopologySuite.

Independent GitHub snapshot · Automated classification · Original documentation governs

Skill documentation

GitHub

name: jts description: "Use when performing precise 2D computational geometry in Java — spatial predicates (contains, intersects), overlay operations, buffering, triangulation. JTS (Java Topology Suite): the canonical geometry engine used as blueprint for GEOS, Shapely, and NetTopologySuite."

tags:

  • java
  • geometry
  • topology
  • spatial
  • de9im
  • wkt
  • wkb
  • geojson
  • gis

项目地址: https://github.com/locationtech/jts

Maven Central: org.locationtech.jts:jts-core

许可证: Eclipse Public License 2.0 / Eclipse Distribution License 1.0(BSD 风格)

Javadoc: https://locationtech.github.io/jts/javadoc

概述

JTS Topology Suite(简称 JTS)是 LocationTech 项目组下的开源 Java 二维矢量几何库,是 GeoTools、GeoServer 等众多开源 GIS 项目的几何计算核心。它提供:

  • 几何对象模型:Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon、GeometryCollection
  • 空间关系判断:equals、contains、within、intersects、touches、crosses、overlaps、disjoint、relate(DE-9IM)
  • 集合运算:intersection、union、difference、symDifference、buffer
  • 几何分析:convexHull、centroid、area、length、distance、isValid、simplify
  • 线性参考:沿线距离定位、线上插值
  • 空间索引:STRtree、Quadtree,加速批量空间查询
  • 格式读写:WKT、WKB、GeoJSON

环境要求: JDK 8+


快速集成

Maven

<properties>
    <jts.version>1.20.0</jts.version><!-- Maven Central 最新稳定版;列表见 https://central.sonatype.com/artifact/org.locationtech.jts/jts-core -->
</properties>

<dependency>
    <groupId>org.locationtech.jts</groupId>
    <artifactId>jts-core</artifactId>
    <version>${jts.version}</version>
</dependency>

如需 GeoJSON 等 I/O 支持:

<dependency>
    <groupId>org.locationtech.jts.io</groupId>
    <artifactId>jts-io-common</artifactId>
    <version>${jts.version}</version>
</dependency>

Gradle

implementation 'org.locationtech.jts:jts-core:1.20.0'
implementation 'org.locationtech.jts.io:jts-io-common:1.20.0'

项目模块一览

模块artifactId用途
jts-corejts-core★ 核心——几何模型、算法、空间操作、索引
jts-io-commonjts-io-common通用 I/O:WKT、WKB、GeoJSON 读写
jts-io-orajts-io-oraOracle Spatial SDO_GEOMETRY 读写——源码模块,需 -Pall(oracle profile)本地构建,未发布到 Maven Central
jts-io-sdejts-io-sdeArcSDE 几何读写——源码保留,构建中已注释禁用,无发布产物

核心类一览

类包用途
GeometryFactoryorg.locationtech.jts.geom★ 推荐入口——创建所有几何对象的工厂
Geometryorg.locationtech.jts.geom几何抽象基类,提供空间操作与关系判断方法
Pointorg.locationtech.jts.geom点(0 维)
LineStringorg.locationtech.jts.geom线串(1 维)
LinearRingorg.locationtech.jts.geom闭合线环(用于构建 Polygon)
Polygonorg.locationtech.jts.geom面(2 维,含外环与可选内环)
MultiPointorg.locationtech.jts.geom多点集合
MultiLineStringorg.locationtech.jts.geom多线集合
MultiPolygonorg.locationtech.jts.geom多面集合
GeometryCollectionorg.locationtech.jts.geom任意几何集合
Coordinateorg.locationtech.jts.geom坐标值(x, y, z)
Envelopeorg.locationtech.jts.geom外接矩形(MBR)
PrecisionModelorg.locationtech.jts.geom坐标精度模型
WKTReaderorg.locationtech.jts.ioWKT 格式解析
WKTWriterorg.locationtech.jts.ioWKT 格式输出
WKBReaderorg.locationtech.jts.ioWKB 格式解析
WKBWriterorg.locationtech.jts.ioWKB 格式输出
GeoJsonReaderorg.locationtech.jts.io.geojsonGeoJSON 解析
GeoJsonWriterorg.locationtech.jts.io.geojsonGeoJSON 输出
STRtreeorg.locationtech.jts.index.strtreeR-tree 空间索引(批量加载,查询高效)
Quadtreeorg.locationtech.jts.index.quadtree四叉树空间索引(支持动态插入删除)
PreparedGeometryorg.locationtech.jts.geom.prep预处理几何,加速重复空间关系判断
PreparedGeometryFactoryorg.locationtech.jts.geom.prep创建 PreparedGeometry 的工厂
BufferOporg.locationtech.jts.operation.buffer缓冲区运算(可精细控制端头、连接样式)
OverlayNGRobustorg.locationtech.jts.operation.overlayng鲁棒叠加运算(推荐使用的新一代叠加引擎)
TopologyPreservingSimplifierorg.locationtech.jts.simplify保持拓扑的几何简化
DouglasPeuckerSimplifierorg.locationtech.jts.simplifyDouglas-Peucker 几何简化
IsValidOporg.locationtech.jts.operation.valid几何有效性检测
LengthIndexedLineorg.locationtech.jts.linearref按长度进行线性参考
LocationIndexedLineorg.locationtech.jts.linearref按位置进行线性参考

几何对象创建

import org.locationtech.jts.geom.*;

GeometryFactory gf = new GeometryFactory();

// 点
Point point = gf.createPoint(new Coordinate(116.4, 39.9));

// 带 Z 值的点
Point point3d = gf.createPoint(new Coordinate(116.4, 39.9, 50.0));

// 线串
LineString line = gf.createLineString(new Coordinate[]{
    new Coordinate(0, 0),
    new Coordinate(10, 10),
    new Coordinate(20, 0)
});

// 线环(必须闭合)
LinearRing ring = gf.createLinearRing(new Coordinate[]{
    new Coordinate(0, 0), new Coordinate(10, 0),
    new Coordinate(10, 10), new Coordinate(0, 10),
    new Coordinate(0, 0)  // 首尾相同
});

// 面(无洞)
Polygon polygon = gf.createPolygon(new Coordinate[]{
    new Coordinate(0, 0), new Coordinate(10, 0),
    new Coordinate(10, 10), new Coordinate(0, 10),
    new Coordinate(0, 0)
});

// 面(带洞)
LinearRing shell = gf.createLinearRing(new Coordinate[]{
    new Coordinate(0, 0), new Coordinate(20, 0),
    new Coordinate(20, 20), new Coordinate(0, 20),
    new Coordinate(0, 0)
});
LinearRing hole = gf.createLinearRing(new Coordinate[]{
    new Coordinate(5, 5), new Coordinate(15, 5),
    new Coordinate(15, 15), new Coordinate(5, 15),
    new Coordinate(5, 5)
});
Polygon polygonWithHole = gf.createPolygon(shell, new LinearRing[]{hole});

// 多点
MultiPoint multiPoint = gf.createMultiPointFromCoords(new Coordinate[]{
    new Coordinate(1, 2), new Coordinate(3, 4)
});

// 多线
MultiLineString multiLine = gf.createMultiLineString(new LineString[]{line});

// 多面
MultiPolygon multiPolygon = gf.createMultiPolygon(new Polygon[]{polygon});

// 几何集合
GeometryCollection gc = gf.createGeometryCollection(new Geometry[]{point, line, polygon});

空间关系、集合运算、几何分析、格式读写与空间索引的完整参考见 reference/geometry-operations.md

典型应用场景

场景关键类 / 方法
创建几何对象GeometryFactory.createPoint() / createLineString() / createPolygon()
地理围栏 / 点在面内判断Geometry.contains() / within(),高频场景用 PreparedGeometry
计算两个几何的距离Geometry.distance()
缓冲区分析Geometry.buffer() 或 BufferOp
面叠加分析(交并差)Geometry.intersection() / union() / difference()
鲁棒叠加运算OverlayNGRobust.overlay()
几何格式互转WKTReader / WKTWriter / WKBReader / WKBWriter / GeoJsonReader / GeoJsonWriter
批量空间查询加速STRtree / Quadtree
最近邻搜索STRtree.nearestNeighbour()
几何简化(抽稀)TopologyPreservingSimplifier / DouglasPeuckerSimplifier
几何有效性校验与修复IsValidOp / GeometryFixer
线性参考 / 沿线定位LengthIndexedLine / LocationIndexedLine
凸包计算Geometry.convexHull()
几何仿射变换AffineTransformation
数据库 WKB 交互WKBReader.hexToBytes() / WKBWriter.toHex()

常见问题

  1. 使用 GeometryFactory 创建几何:不要直接 new Point(),始终通过 GeometryFactory 的工厂方法创建几何对象。
  2. 面必须闭合:Polygon 的外环和内环坐标数组的首尾坐标必须相同。
  3. 坐标顺序:JTS 使用 (x, y) 即 (经度, 纬度) 的顺序,注意与某些 GIS 系统的 (纬度, 经度) 区分。
  4. 几何有效性:从外部导入的几何数据应使用 geometry.isValid() 检查有效性,无效几何可用 GeometryFixer.fix() 修复。
  5. SRID 不参与计算:JTS 的 SRID 仅作为元数据标记,不影响空间运算;JTS 所有计算都在笛卡尔平面上进行,不处理投影。
  6. 性能优化:批量空间查询使用 STRtree;重复空间关系判断使用 PreparedGeometry;大量几何合并使用 UnaryUnionOp。
  7. 线程安全:GeometryFactory 是线程安全的;Geometry 对象本身不可变,可安全共享;但 Reader/Writer 实例非线程安全,需为每个线程创建独立实例。
  8. 包名迁移:JTS 1.15+ 包名由 com.vividsolutions.jts 迁移为 org.locationtech.jts,注意旧代码升级。
  9. OverlayNG:对于叠加运算(intersection / union / difference),推荐使用 OverlayNGRobust,它比传统叠加引擎更加鲁棒,能处理更多边界情况。

AI 使用建议

推荐工作流

  1. 创建几何对象:始终通过 GeometryFactory 工厂方法创建,不要直接 new
  2. 格式解析:从 WKT/WKB/GeoJSON 读入外部数据,使用对应的 Reader
  3. 空间运算:用 PreparedGeometry 加速批量 contains/intersects 判断
  4. 批量查询:用 STRtree 建空间索引后查询
  5. 结果导出:用 Writer 输出为 WKT/WKB/GeoJSON

关键注意事项

  • 坐标顺序:JTS 使用 (x, y) 即 (经度, 纬度),注意与部分 GIS 系统的 (y, x) 区分
  • SRID 不参与计算:JTS 所有计算在笛卡尔平面进行,不处理地球曲率
  • OverlayNG 优先:叠加运算优先使用 OverlayNGRobust.overlay(),比传统方法更鲁棒
  • 线程安全:GeometryFactory 和 Geometry 线程安全;Reader/Writer 非线程安全
  • 有效性检查:外部导入的几何用 isValid() 检查后用 GeometryFixer.fix() 修复

相关技能

参考资源

Saved skills