一些高级操作, 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);
- }
- }
- }
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);
- }
- }
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());
- }
- }
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());
- }
- }
- }
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());
- }
- }
- }
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>
- }
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>
}