Esri geometry api java 学习文档 (3) 线、多线 (Polyline)
线要素是是一个1维的空间数据,每个线要素一组单线(paths),每条单线包含着一组点(n≥2)。
这里与OGC、JTS不同的是,Esri更喜欢用一个Polyline表示多线元素,单线 = 只有一个元素的多线,而不喜欢把Line和MultiLine单独分开。
线要素由一组点要素组成,这些点要素被分为三类:
Boundary: 边界点
Interior: 内部点
Exterior: 外部点
如图:
边界点就是 线要素的开始点和结束点;
内部点就是 线要素上非开始点和结束点的其他点;
外部点就是 非内部点且非边界点的点集.
那么比较复杂的线要素是怎么判断这三种点呢?
例如:
这里绿色点连接着4条线段,是偶数,所以它不是边界点
这里绿色点连接着5条线段,是奇数,所以它是边界点。
此外还有一个特殊概念Valid polyline(OGC\JTS里面叫LinearRing)
Valid polyline:
1. Valid polyline是一条线段(n≥2)
2. Valid polyline是简单曲线(线段不会自相交)
上面说了Polyline的几何原理,下面说说它的实现方法:
Polyline 属于 MultiVertexGeometry 下的 MultiPath(多路),Polyline 只是 MultiPath 的一种特殊情况
MultiPath 多路与上一篇 MultiPoint 不同的是:MultiPath 以 MultiVertex(多顶点)为单位,存储线状几何要素,
所有MultiPath 都要对内部的单位的线做操作,同时线的内部还包含着端点(Point)与连接端点的线段(Segment)
所以 MultiPath 里面包含的特殊方法是:对线、端点、线段的增删改查~
对线(Path):
增
以勾勒的方式画一条路径:
startPath(x,y) or (Point point); //开始勾勒单线
lineTo(x,y) or (Point point); //勾勒下一个点
添加一条线
在后面:addPath(MultiPath src, int srcPathIndex, boolean bForward)
在中间:insertPath(int pathIndex, MultiPath src, int srcPathIndex, boolean bForward)
批量添加一组线:
add(MultiPath src, boolean bReversePaths)
都能得到这样的数据:
删
removePath(int pathIndex)
改
改不好改啊,肯定是改Path里面的点位,所以放在下面的Point里面说
查
(没找到 T_T,求找到的大神私信我~ )
对点(Point):
增
虽然是点,但是这个类毕竟是线啊,所以点总要在线里面:
加一个点:insertPoint(int pathIndex, int beforePointIndex, Point pt)
加一堆点:insertPoints(int pathIndex, int beforePointIndex, MultiPath src, int srcPathIndex, int srcPointIndexFrom, int srcPointCount, boolean bForward)
删
removePoint(int pathIndex, int pointIndex)
改
setPoint(int index, Point point)
查
查一个:getPoint(int index)
查一堆:queryCoordinates(Point[] dst)
取一些特殊点:getPathEnd(int pathIndex)
getPathStart(int pathIndex)(流向图经常用他们)
对线段(Segment):
增
加一个:addSegment(Segment segment, boolean bStartNewPath)
加一堆:addSegmentsFromPath(MultiPath src, int srcPathIndex, int srcSegmentFrom, int srcSegmentCount, boolean bStartNewPath)
删
线段由点组成删点就可以了
改
线段由点组成改点就可以了
查
线段的遍历器哦:querySegmentIterator()
除此之外还有几个方法做支撑:
关闭最后一个线(盘它!):closePathWithLine()
关闭所有线(盘它们!):closeAllPaths()(多用于成面)
是否关闭(盘过了吗!):isClosedPath(int pathIndex)
一些计数:
getPathCount()
getPathSize(int pathIndex)
getPointCount()
getSegmentCount()
getSegmentCount(int pathIndex)
连接 Path 与 Point 的方法:
getPathIndexFromPointIndex(int pointIndex)
最后要对这个多线所在的域处理下:
获得域:queryEnvelope(Envelope env)
简单的加一个域:addEnvelope(Envelope envSrc, boolean bReverse)
到这里MultiPath基本操作就都说清了~
上面说了这么多 MultiPath 的基本方法,最后说一下 Polyline 的特殊方法吧!
Polyline 的常用方法:
增:
p1.add(p2, true); //在P1后面新增另一个Polyline(P2)的全部单线
p1.addPath(p2,0,true); //在P1后面新增另一个Polyline(P2)第“0”个单线
p1.addEnvelope(e,true); //在P1后面新增另一个Envelope
p1.addSegmentsFromPath(p2,0,1,2,true); //在p1最后一个单线后面加入p2第0个单线的,从第1个点开始延伸2个点的这个线段
以上true/false表示顺时针/逆时针输入
对线(path)的处理:
查:
p1.getPathIndexFromPointIndex(0); //读取第0条单线
p1.calculatePathLength2D(0); //读取第0条单线的二维长度(°)(WGS84)
Polyline 的特殊定义:(这个跟之前Point大同小异我把它放在最后了)
1 static Polyline createPolyline1() {
2
3 Polyline line = new Polyline();
4
5 // Path 1
6 line.startPath(6.9, 9.1);
7 line.lineTo(7, 8.8);
8
9 // Path 2
10 line.startPath(6.8, 8.8);
11 line.lineTo(7, 9);
12 line.lineTo(7.2, 8.9);
13 line.lineTo(7.4, 9);
14
15 // Path 3
16 line.startPath(7.4, 8.9);
17 line.lineTo(7.25, 8.6);
18 line.lineTo(7.15, 8.8);
19
20 return line;
21 }
2.读取JSON数据
OperatorImportFromJson.local().executeexecute(Geometry.Type type, String string);
Type :Geometry.Type寻找,这里是polyline
string :JSON字符串
1 static Polyline createPolygonFromJson() throws JsonParseException, IOException {
2
3 String jsonString = "{\"paths\":[[[6.8,8.8],[7,9],[7.2,8.9],[7.4,9]],"
4 + "[[7.4,8.9],[7.25,8.6],[7.15,8.8]],[[6.9, 9.1],[7, 8.8]]],"
5 + "\"spatialReference\":{\"wkid\":4326}}";
6
7 MapGeometry mapGeom = OperatorImportFromJson.local().execute(Geometry.Type.Polyline, jsonString);
8
9 return (Polyline)mapGeom.getGeometry();
10 }
3.读取GeoJSON数据
OperatorImportFromGeoJson.local().execute(int importFlags, Geometry.Type type, String geoJsonString, ProgressTracker progressTracker);
importFlags :
geoJsonImportDefaults = 0; 默认WGS84坐标系
geoJsonImportNonTrusted = 2; 不可信点
geoJsonImportSkipCRS = 8; 跳过CRS
geoJsonImportNoWGS84Default = 16; 非WGS84坐标系
type :Geometry.Type寻找,这里是polyline
geoJsonString :GeoJSON字符串
progressTracker :null
1 static Polyline createPolylineFromGeoJson() throws JsonParseException, IOException {
2
3 String geoJsonString = "{\"type\":\"MultiLineString\","
4 + "\"coordinates\":[[[6.8,8.8],[7,9],[7.2,8.9],[7.4,9]],"
5 + "[[7.4,8.9],[7.25,8.6],[7.15,8.8]],[[6.9, 9.1],[7, 8.8]]],"
6 + "\"crs\":\"EPSG:4326\"}";
7
8 MapGeometry mapGeom = OperatorImportFromGeoJson.local().execute(GeoJsonImportFlags.geoJsonImportDefaults, Geometry.Type.Polyline, geoJsonString, null);
9
10 return (Polyline)mapGeom.getGeometry();
11 }
4.读取WKT数据
OperatorImportFromWkt.local().execute(int import_flags, Geometry.Type type,String wkt_string, ProgressTracker progress_tracker);
import_flags :同上
Type :同上
wkt_string :WKT字符串
progressTracker :null
1 static Polyline createPolylineFromWKT() throws JsonParseException, IOException {
2
3 String wktString = "MULTILINESTRING ((6.9 9.1,7 8.8),(6.8 8.8,7 9,7.2 8.9,7.4 9),(7.4 8.9,7.25 8.6,7.15 8.8))";
4 Geometry geom = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polyline, wktString, null);
5
6 return (Polyline)geom;
7 }
8 }
参考:
http://esri.github.io/geometry-api-java/doc/Polyline.html
http://esri.github.io/geometry-api-java/javadoc/com/esri/core/geometry/Polyline.html