JTS Geometry Operations(二)

本文深入解析GIS中的高级操作,包括Buffer、Polygonization、LineMerger等,详细介绍了这些操作的功能、实现方法及应用案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一些高级操作, Buffer,LineMerger,Polygonization,UnionLine,凹壳分析,Overlays

(1)、Buffer,返回的结果是一个Polygon或者 MultiPolygon

buffering is an operation which in GIS is used to compute the area containing all
 points within a given distance of a Geometry.
 You can use JTS to compute the buffer of a Geometry using the Geometry buffer method
 or the BufferOp class.  The input Geometry to the buffer operation may be of any type
 (including arbitrary GeometryCollections).  The result of a buffer operation is always an area
 type (Polygon or MultiPolygon)
.  The result may be empty (for example, a negative buffer
 of a LineString).

GeometryFactory.java

package com.mapbar.jst;


import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;

public class GeometryFactory {
	
	private WKTReader reader;
	
	private  GeometryFactory instance = null;
	
	public static synchronized GeometryFactory getInstance(){
		if(instance==null){
			instance = new GeometryFactory();
		}
		return instance;
	}
	
	public void getReader(){
		reader = new WKTReader();
	}
	
	public Geometry buildGeo(String str){
		try {
			if(reader==null){
				reader = new WKTReader();
			}
			return reader.read(str);
		} catch (ParseException e) {
			throw new RuntimeException("buildGeometry Error",e);
		}
	}

}

Buffers.java

package com.mapbar.jst;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.operation.buffer.BufferOp;

public class Buffers {

	private GeometryFactory factory = GeometryFactory.getInstance();

	public Geometry buildGeo(String str) {
		return factory.buildGeo(str);
	}

	public static void main(String[] args) {
		Buffers bs = new Buffers();
		String line1 = "LINESTRING (0 0, 1 1, 2 2,3 3)";
		Geometry g1 = bs.buildGeo(line1);
		//方式(一)
		Geometry g = g1.buffer(2);

		////方式(二) BufferOP
		BufferOp bufOp = new BufferOp(g1);
		bufOp.setEndCapStyle(BufferOp.CAP_BUTT);
		Geometry bg = bufOp.getResultGeometry(2);
	}
}

注意:bufOp.setEndCapStyle 缓冲样式的设置,总共有三种CAP_ROUND,CAP_BUTT,CAP_SQUARE 对应如下三种情况

 

(2)、Polygonization 面处理类

Polygonization is the process of forming polygons from linework which
encloses areas. Linework to be formed into polygons must be fully noded –
that is, linestrings must not cross and must touch only at endpoints.
  JTS provides the Polygonizer class to perform Polygonization. The Polygonizer
takes a set of fully noded LineStrings and forms all the polygons which are
enclosed by the lines. Polygonization errors such as dangling lines or cut
 lines can be identified and reported.

Polygonization.java

package com.mapbar.jst;

import java.util.ArrayList;
import java.util.List;
import java.util.Collection;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.operation.polygonize.Polygonizer;

public class Polygonization {

	private static GeometryFactory factory = GeometryFactory.getInstance();

	public static void main(String[] args) {
		List<Geometry> list = new ArrayList<Geometry>();
		list.add(factory.buildGeo("LINESTRING (0 0,1 1)")); 
		list.add(factory.buildGeo("LINESTRING (6 3,6 10)"));
		list.add(factory.buildGeo("LINESTRING (2 2,4 4,6 3)"));
		list.add(factory.buildGeo("LINESTRING (2 2,5 1,6 3)"));
		list.add(factory.buildGeo("LINESTRING (6 3,6 4)"));
		list.add(factory.buildGeo("LINESTRING (9 5,7 1,6 4)"));
		list.add(factory.buildGeo("LINESTRING (9 5,8 8,6 4)"));
		Polygonizer p = new Polygonizer();
		p.add(list);
		Collection<Geometry> polys = p.getPolygons(); //面
		Collection<Geometry> dangles = p.getDangles();//悬挂线
		Collection<Geometry> cuts = p.getCutEdges(); //面和面的连接线
		System.out.println(polys.size()+":"+polys.toString());
		System.out.println(dangles.size()+":"+dangles.toString());
		System.out.println(cuts.size()+":"+cuts.toString());
	}
}

输出结果:

2:[POLYGON ((2 2, 4 4, 6 3, 5 1, 2 2)), POLYGON ((6 4, 8 8, 9 5, 7 1, 6 4))]
2:[LINESTRING (6 3, 6 10), LINESTRING (0 0, 1 1)]
1:[LINESTRING (6 3, 6 4)]

(3)、LineMerger 线路合并,线路之间不能有交点,并且只在线路末尾有公共交点

Sometimes a spatial operation such as #union will produce
chains of small LineStrings. The JTS LineMerger is a simple utility
to sew these small LineStrings together. NOTE:they do not cross; only
their endpoints can touch. If LineStrings to be merged do not have
 the same direction, the direction of the resulting LineString will be
 that of the majority.

MergerLine.java

package com.mapbar.jst;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.operation.linemerge.LineMerger;

public class MergerLine {

	private static GeometryFactory factory = GeometryFactory.getInstance();

	public static void main(String[] args) {
		LineMerger lineMerger = new LineMerger();
		List<Geometry> list = new ArrayList<Geometry>();
		list.add(factory.buildGeo("LINESTRING (3 3,2 2,0 0)"));
		list.add(factory.buildGeo("LINESTRING (3 3,6 6,0 10)"));
		list.add(factory.buildGeo("LINESTRING (0 10,3 1,10 1)"));
		lineMerger.add(list);
		Collection<Geometry> mergerLineStrings = lineMerger.getMergedLineStrings();
		for (Geometry g : mergerLineStrings) {
			System.out.println(g.toText());
		}
	}
}

输出结果:LINESTRING (0 0, 2 2, 3 3, 6 6, 0 10, 3 1, 10 1)

lineMerger 和union区别,union可以在两条相交的线中生成交点(noded)

(4)、union 线路合并,并且生成交叉点。

The noding process splits LineStrings that cross into smaller LineStrings  that meet at a point, or node

package com.mapbar.jst;

import java.util.ArrayList;
import java.util.List;

import com.vividsolutions.jts.geom.Geometry;

public class UnionLine {

	private static GeometryFactory factory = GeometryFactory.getInstance();

	public static void main(String[] args) {
		List<Geometry> list = new ArrayList<Geometry>();
		list.add(factory.buildGeo("LINESTRING (10 10,2 2,0 0)"));
		list.add(factory.buildGeo("LINESTRING (10 0,6 6,0 10)"));
		list.add(factory.buildGeo("LINESTRING (1 1,3 1,10 1)"));
		Geometry nodedLine = list.get(0);
		for (int i = 1; i < list.size(); i++) {
			nodedLine = nodedLine.union(list.get(i));
		}
		int num = nodedLine.getNumGeometries();
		for (int j = 0; j < num; j++) {
			Geometry eachG = nodedLine.getGeometryN(j);
			System.out.println(eachG.toText());
		}
	}
}


(5)、凹壳分析  包含几何形体的所有点的最小凸壳多边形(外包多边形)



(6)、叠加操作  叠加可以用来确定任何几何图形的布尔组合。
The overlay can be used to determine any boolean combination of the geometries.
通过对两个数据进行的一系列集合运算,产生新数据的过程。叠加分析的目的就是通过对空间数据的加工或分析,提取用户需要的新的空间几何信息。
叠加分析类型包括:
交叉分析(Intersection) 交叉操作就是多边形AB中所有共同点的集合。
联合分析(Union) AB的联合操作就是AB所有点的集合。
差异分析(Difference) AB形状的差异分析就是A里有B里没有的所有点的集合。
对称差异分析(SymDifference) AB形状的对称差异分析就是位于A中或者B中但不同时在AB中的所有点的集合

	public void overlaps() throws ParseException, FileNotFoundException{
		WKTReader reader = new WKTReader(geometryFactory);
		Polygon geometry1 = (Polygon) reader.read("POLYGON((0 0, 2 0 ,2 2, 0 2,0 0))");
		Polygon geometry2 = (Polygon) reader.read("POLYGON((0 0, 4 0 , 4 1, 0 1, 0 0))");
	    OverlayOp op = new OverlayOp(geometry1,geometry2);
	    Geometry g =op.getResultGeometry(OverlayOp.INTERSECTION);//POLYGON ((2 0, 0 0, 0 1, 2 1, 2 0))
	    Geometry g2 = op.getResultGeometry(OverlayOp.UNION);
	    Geometry g3 = op.getResultGeometry(OverlayOp.DIFFERENCE);
	    Geometry g4 = op.getResultGeometry(OverlayOp.SYMDIFFERENCE);
	    PlanarGraph p = op.getGraph(); //图<v,e>
	}


 

JTS(Java 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` 实例。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值