geometry-api-java 学习笔记(五)多边形 Polygons
2017-07-05 13:41
489 查看
Polygons
API ReferenceA polygon is defined by a collection of rings. Each ring is a collection of contiguous line segments suchthat the start point and the end point are the same.
看下图,多边形的起点和终点相等。
Ring
Boundary and Rings
The boundary of a polygon is the collection of rings by which the polygon is defined. The boundarycontains one or more outer rings and zero or more inner rings. An outer ring is oriented clockwise whilean inner ring is oriented counterclockwise.Imagine walking clockwise along an outer ring. The area toyour immediate right is the
interior of the polygon and to your left is the exterior.
Outer Ring
Similarly, if you were to walk counter-clockwise along an inner ring, the area to your immediateright is the interior of the polygon and to your left is the exterior.
Outer & Inner Ring
It is important to understand the boundary, interior and exterior of a geometry when using the variousoperators. The relational operators, for example, rely heavily on these concepts.
If a polygon has an inner ring, the inner ring looks like a hole. If the hole containsanother outer ring, that outer ring looks like an island.
1 outer ring, no inner rings | 4 outer rings, no inner rings | 1 outer ring, 1 inner ring | 2 outer rings, 1 inner ring |
Valid polygons
A valid polygon has no overlapping rings, no self-intersections except possiblyat vertices, no dangling segments and, in general, an arbitrary point can always beclassified unambiguously as either in the exterior, in the interior or on theboundary of thepolygon. A valid polygon is said to be simple.See the
Simplify operator for a more detailed specificationof simple polygons. Note that a simple polygon may not be OGC compliant. See the
Simplify operator with OGC restrictions.
Examples
Let's look at some examples of non-simple vs. simple polygons. The green circles are the vertices of the polygon,and the lavender colored area represents the interior of the polygon.Non-simple polygons | ||||
---|---|---|---|---|
Self-intersection | Self-intersection | Dangling segment | Dangling segment | Overlapping rings |
Simple polygons | ||||
---|---|---|---|---|
No self-intersection | Self-intersection at vertex | No dangling segment | No dangling segment | No overlapping rings |
JSON format
A polygon can be represented as a JSON string. A polygon in JSON format contains an array ofringsand an optional
spatialReference. A polygon can also have boolean-valued
hasMand
hasZfields. The default value is falsefor both the
hasMand
hasZfields.
Each ring is represented by an array of points. Exterior rings are oriented clockwise, while interior ringsare oriented counter-clockwise. The order of the rings is irrelevant. The first point of each ring is always the same as the last point.Each point
in a ring is represented as an array of numbers. See the description ofJSON multipoints for details on the interpretation of the point arrays.
An empty polygon is represented with an empty array for the
ringsfield.
Syntax
{ "hasZ" : true | false, "hasM" : true | false, "rings": [ [[<x11>,<y11>,<z11>,<m11>],[<x12>,<y12>,<z12>,<m12>], ... ,[<x1j>,<y1j>,<z1j>,<m1j>]], ... , [[<xn1>,<yn1>,<zn1>,<mn1>],[<xn2>,<yn2>,<zn2>,<mn2>], ... ,[<xnk>,<ynk>,<znk>,<mnk>]] ], "spatialReference" : {"wkid" : <wkid>} }
2D Polygon
{ "rings": [ [[6453,16815],[10653,16423],[14549,5204],[-7003,6939],[6453,16815]], [[914,7992],[3140,11429],[1510,10525],[914,7992]] ], "spatialReference" : {"wkid" : 54004} }
3D Polygon with Ms
Note that the third point does not have a z-value, and the second ring does not have any m-values.{ "hasZ" : true, "hasM" : true, "rings": [ [[6453,16815,35,1],[10653,16423,36,2],[14549,5204,null,3],[-7003,6939,37,4],[6453,16815,35,1]], [[914,7992,30],[3140,11429,29],[1510,10525,28],[914,7992,30]] ], "spatialReference" : {"wkid" : 54004} }
Empty Polygon
{"rings": []}
Creating a polygon
To create a polygon, we can use thePolygonclass methods or one of the import operators.
Each of the code samples below creates a polygon with two outer rings and one inner ring.
The polygon looks like this:
Create this polygon |
Polygon
class methods
When using the Polygonclass methods to create a polygon, the order in which the rings are createddoesn't matter. What matters is the ring orientation. Remember, clockwise implies an outer ring whereascounter-clockwise implies an inner ring.
To begin each ring, we call the
startPathmethod and then successive calls to the
lineTomethod. We don't need to repeat the start point as the end point.
这里同样用到startPath和lineTo方法,不过不用重复把起点去做终点。
public static Polygon createPolygon1() { Polygon poly = new Polygon(); // clockwise => outer ring poly.startPath(0, 0); poly.lineTo(-0.5, 0.5); poly.lineTo(0.5, 1); poly.lineTo(1, 0.5); poly.lineTo(0.5, 0); // hole poly.startPath(0.5, 0.2); poly.lineTo(0.6, 0.5); poly.lineTo(0.2, 0.9); poly.lineTo(-0.2, 0.5); poly.lineTo(0.1, 0.2); poly.lineTo(0.2, 0.3); // island poly.startPath(0.1, 0.7); poly.lineTo(0.3, 0.7); poly.lineTo(0.3, 0.4); poly.lineTo(0.1, 0.4); return poly; }
Import from JSON
As with thePolygonclass methods, when using
OperatorImportFromJsonto create a polygon the order in which the rings are created doesn't matter. What matters is thering orientation. Unlike the
Polygonclass methods, the start point of each ring mustbe repeated to specify the end point.
The code shown below creates the same polygon as before, but notice that the inner ring that forms the holeis given before the outer ring. This was done just to drive home the point that the order of the ringsdoesn't matter when the polygon is in JSON format.
static Polygon createPolygonFromJson() throws JsonParseException, IOException { String jsonString = "{\"rings\":[[[0.5,0.2],[0.6,0.5],[0.2,0.9],[-0.2,0.5],[0.1,0.2],[0.2,0.3],[0.5,0.2]]," + "[[0.0,0.0],[-0.5,0.5],[0.0,1.0],[0.5,1.0],[1.0,0.5],[0.5,0.0],[0.0,0.0]]," + "[[0.1,0.7],[0.3,0.7],[0.3,0.4],[0.1,0.4],[0.1,0.7]]]," + " \"spatialReference\":{\"wkid\":4326}}"; MapGeometry mapGeom = OperatorImportFromJson.local().execute(Geometry.Type.Polygon, jsonString); return (Polygon)mapGeom.getGeometry(); }
Import from GeoJSON
Unlike thePolygonclass methods and
OperatorImportFromJson, when using
OperatorImportFromGeoJsonto create a polygon the order in which the rings are givendoes matter. Within an array of rings, the outer ring is always
first followed by zero or moreinner rings. However, the order of the arrays of rings doesn't matter. The start point of eachring must be repeated to specify the end point.
The code shown below creates the same polygon as in the previous examples.
static Polygon createPolygonFromGeoJson() throws JsonParseException, IOException { String geoJsonString = "{\"type\":\"MultiPolygon\"," + "\"coordinates\":[[[[0.0,0.0],[-0.5,0.5],[0.0,1.0],[0.5,1.0],[1.0,0.5],[0.5,0.0],[0.0,0.0]]," + "[[0.5,0.2],[0.6,0.5],[0.2,0.9],[-0.2,0.5],[0.1,0.2],[0.2,0.3],[0.5,0.2]]]," + "[[[0.1,0.7],[0.3,0.7],[0.3,0.4],[0.1,0.4],[0.1,0.7]]]]," + "\"crs\":\"EPSG:4326\"}"; MapGeometry mapGeom = OperatorImportFromGeoJson.local().execute(GeoJsonImportFlags.geoJsonImportDefaults, Geometry.Type.Polygon, geoJsonString, null); return (Polygon)mapGeom.getGeometry(); }
Import from WKT
As withOperatorImportFromGeoJson, when using
OperatorImportFromWktto create a polygon the order in which the rings are given does matter. Within an array of rings,the outer ring is always first followed by zero or more inner rings.
However, the order of the arraysof rings doesn't matter. The start point of each ring must be repeated to specify the end point.
The code shown below creates the same polygon as in the previous examples, but notice that the outer ringthat forms the island is given first. This was done for illustrative purposes. It could have been given last,which probably would be more understandable
to the reader.
static Polygon createPolygonFromWKT() throws JsonParseException, IOException { String wktString = "MULTIPOLYGON (((0.1 0.7, 0.1 0.4, 0.3 0.4, 0.3 0.7, 0.1 0.7))," + "((0 0, 0.5 0, 1 0.5, 0.5 1, 0 1, -0.5 0.5, 0 0)," + "(0.5 0.2, 0.2 0.3, 0.1 0.2, -0.2 0.5, 0.2 0.9, 0.6 0.5, 0.5 0.2)))"; Geometry geom = OperatorImportFromWkt.local().execute(WktImportFlags.wktImportDefaults, Geometry.Type.Polygon, wktString, null); return (Polygon)geom; }
Extracting Outer Rings
Many workflows require getting only the outer rings of a polygon.The method
getAllOuterRingsextracts all the outer rings of a polygon. The call to
OperatorSimplifyOGCmakes sure there are no self-intersections, all outer rings areclockwise, and holes are counterclockwise. You can skip the call
to
OperatorSimplifyOGCif you trust your input polygon.
static List<Polygon> getAllOuterRings(Polygon polygon) { Polygon simplePolygon = (Polygon)OperatorSimplifyOGC.local().execute(polygon, null, true, null); ArrayList<Polygon> rings = new ArrayList<Polygon>(); int n = simplePolygon.getPathCount(); for (int i = 0; i < n; i++) { if (simplePolygon.calculateRingArea2D(i) <= 0) continue; Polygon ring = new Polygon(); ring.addPath(simplePolygon, i, true); rings.add(ring); } return rings; }
The method
getOutermostRingsextracts the outermost rings, that is,only rings that are not contained inside of any hole.
static List<Polygon> getOutermostRings(Polygon polygon) { List<Polygon> allOuterRings = getAllOuterRings(polygon); GeometryCursor outerRingsCursor = new SimpleGeometryCursor(allOuterRings); GeometryCursor unionCursor = OperatorUnion.local().execute(outerRingsCursor, null, null); Geometry unionPoly = unionCursor.next(); // Holes could have been produced during the union, so rerun just in case. return getAllOuterRings((Polygon)unionPoly); }
相关文章推荐
- geometry-api-java 学习笔记(六)多边形 Envelope
- geometry-api-java 学习笔记(七)拓扑运算之cut
- geometry-api-java 学习笔记(二)点 Point
- geometry-api-java 学习笔记(一)概述
- geometry-api-java 学习笔记(四)多线段 polyline
- geometry-api-java 学习笔记(八)分割Intersection
- geometry-api-java 学习笔记(三)多点 multipoint
- Java Sound API 学习笔记[转载]
- ant学习笔记(四)ant整合javadoc直接生成java文档api并将其打包之后上传到FTP服务器上面
- Android(java)学习笔记143:android提供打开各种文件的API接口:setDataAndType
- Core Java 学习笔记——1.术语/环境配置/Eclipse汉化字体快捷键/API文档
- Java学习笔记之常用API学习笔记2
- 黑马程序员_学习笔记:12) 其他API:java.lang.Runtime、java.util.Math、java.util.Date、java.util. Calendar
- Java基础班学习笔记(11)API常用类(三)
- Java学习笔记之常用API学习笔记3
- java学习笔记——API中的常用类和StringBuffer
- JavaWeb--学习笔记(一)--API
- Java-API学习笔记---Object
- JAVA学习笔记(十八)- API常用类
- Java-API学习笔记---Integer