参考文章:
https://blog.csdn.net/kone0611/article/details/83781484
https://wenku.baidu.com/view/839cbdb1fd0a79563c1e726c.html
https://www.cnblogs.com/duanxingxing/p/5144257.html
网盘jar包下载
提取码:qw23
Geometry 空间分析方法几何图形操作包,在operation包内,包含buffer、distance、linemerge、overlap、polygonize、predicate、relate、valide八个子包。分别对应着计算图形的缓冲、距离、线段融合、图形覆盖、多边形化、断言、关联、有效性等的操作。所有的操作针对的都是在geom包中定义的Geometry对象。
由于在计算机中,所有的图形都是离散的点组成,所以所有的操作都是在组成图形的点的集合上进行的,一个图形(Geometry)的
缓冲(buffer)距离操作(distance)是个二元操作,操作对象Geometry A、B,返回(A)与(B)中距离最近的两个点的距离。
线段的融合(linemerge)是将Geometry A中相互连接的线段进行连接。
多边形化操作(polygonize)对Geometry A进行计算,返回一个多边形(Polygon)。将由许多个点表示的图形,用少量的点来表示,减少图形的信息,即对图形进行降维。
断言(predicate)是一个二维的操作,对Geometry之间的关系进行判断的操作。
关联(relate) 根据DE-9IM(The Dimensionally Extended Nine-Intersection Model),该方法返回两个Geometry A与B的相交矩阵IM(Intersections Matrix)。这个矩阵在计算图形关系上用到。
1.distance,intersection,union,difference 示例代码:
package com.mapbar.geo.jts.operation;
import java.util.ArrayList;
import java.util.List;
import org.geotools.geometry.jts.JTSFactoryFinder;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
public class Operation {
private GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
public Coordinate point(double x,double y){
return new Coordinate(x,y);
}
public LineString createLine(List<Coordinate> points){
Coordinate[] coords = (Coordinate[]) points.toArray(new Coordinate[points.size()]);
LineString line = geometryFactory.createLineString(coords);
return line;
}
public double distanceGeo(Geometry a,Geometry b){
return a.distance(b);
}
public Geometry intersectionGeo(Geometry a,Geometry b){
return a.intersection(b);
}
public Geometry unionGeo(Geometry a,Geometry b){
return a.union(b);
}
public Geometry differenceGeo(Geometry a,Geometry b){
return a.difference(b);
}
public static void main(String[] args){
Operation op = new Operation();
List<Coordinate> points1 = new ArrayList<Coordinate>();
points1.add(op.point(0,0));
points1.add(op.point(1,3));
points1.add(op.point(2,3));
LineString line1 = op.createLine(points1);
List<Coordinate> points2 = new ArrayList<Coordinate>();
points2.add(op.point(3,0));
points2.add(op.point(3,3));
points2.add(op.point(5,6));
LineString line2 = op.createLine(points2);
System.out.println(op.distanceGeo(line1,line2));
System.out.println(op.intersectionGeo(line1,line2));
System.out.println(op.unionGeo(line1,line2));
System.out.println(op.differenceGeo(line1,line2));
}
}
2.一些高级操作, Buffer,LineMerger,Polygonization,UnionLine,凹壳分析,Overlays
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);
}
}
}
2.1 缓冲区操作:
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 bufOp = new BufferOp(g1);
bufOp.setEndCapStyle(BufferOp.CAP_BUTT);
Geometry bg = bufOp.getResultGeometry(2);
}
}
bufOp.setEndCapStyle 缓冲样式的设置,总共有三种CAP_ROUND,CAP_BUTT,CAP_SQUARE 对应如下三种情况
2.2 Polygonization
多边形化是由线条包围区域形成多边形的过程,各线段不能交叉,只能在端点接触,且完全闭合。
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)]
2.3 LineMerger 线路合并,线路之间不能有交点,并且只在线路末尾有公共交点
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)
2.4 union 线路合并,并且生成交叉点
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());
}
}
}
2.5 凹壳分析 包含几何形体的所有点的最小凸壳多边形(外包多边形)
2.6 叠加操作 叠加可以用来确定任何几何图形的布尔组合
通过对两个数据进行的一系列集合运算,产生新数据的过程。叠加分析的目的就是通过对空间数据的加工或分析,提取用户需要的新的空间几何信息。
叠加分析类型包括:
交叉分析(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);
Geometry g2 = op.getResultGeometry(OverlayOp.UNION);
Geometry g3 = op.getResultGeometry(OverlayOp.DIFFERENCE);
Geometry g4 = op.getResultGeometry(OverlayOp.SYMDIFFERENCE);
PlanarGraph p = op.getGraph();
}
3. 快速合并多个多边形并计算面积
List<Polygon> polygons = new ArrayList<>();
Polygon[] pgs = polygons.toArray(new Polygon[polygons.size()]);
GeometryCollection polygonCollection = geometryFactory.createGeometryCollection(pgs);
Geometry union = polygonCollection.buffer(0);
System.out.println(union);
String type = union.getGeometryType();
System.out.println(type);
if (type == "Polygon"){
Polygon polygon = (Polygon) union;
System.out.println(polygon.getArea());
return polygon.getArea();
}else if (type == "MultiPolygon"){
MultiPolygon multiPolygon = (MultiPolygon) union;
System.out.println(multiPolygon.getArea());
return multiPolygon.getArea();
}