您的位置:首页 > 编程语言 > Java开发

geometry-api-java 学习笔记(五)多边形 Polygons

2017-07-05 13:41 489 查看

Polygons

API Reference

A 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 rings4 outer rings, no inner rings1 outer ring, 1 inner ring2 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 the
polygon. 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-intersectionSelf-intersectionDangling segmentDangling segmentOverlapping rings
Simple polygons





No self-intersectionSelf-intersection at vertexNo dangling segmentNo dangling segmentNo overlapping rings
When drawing a polygon, use the even-odd fill rule. The even-odd fill rule will guarantee that the polygonwill draw correctly even if the ring orientation is not as described above for simple polygons.

JSON format

A polygon can be represented as a JSON string. A polygon in JSON format contains an array of
rings
and an optional
spatialReference
. A polygon can also have boolean-valued
hasM
and
hasZ
fields. The default value is falsefor both the
hasM
and
hasZ
fields.

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
rings
field.

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 the
Polygon
class 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
Polygon
class 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
startPath
method and then successive calls to the
lineTo
method. 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 the
Polygon
class methods, when using
OperatorImportFromJson
to create a polygon the order in which the rings are created doesn't matter. What matters is thering orientation. Unlike the
Polygon
class 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 the
Polygon
class methods and
OperatorImportFromJson
, when using
OperatorImportFromGeoJson
to 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 with
OperatorImportFromGeoJson
, when using
OperatorImportFromWkt
to 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
getAllOuterRings
extracts all the outer rings of a polygon. The call to
OperatorSimplifyOGC
makes sure there are no self-intersections, all outer rings areclockwise, and holes are counterclockwise. You can skip the call
to
OperatorSimplifyOGC
if 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
getOutermostRings
extracts 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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: