I am retrieving a List<Coordinate>
from an external source, and it is known to contain one or more polygons.
Here's an example of the coordinates. As you can see, it contains two squares which do not overlap and are also not adjacent.
(0, 0)
(0, 5)
(5, 5)
(5, 0)
(0, 0)
(12, 12)
(12, 17)
(17, 17)
(17, 12)
(12, 12)
Now how do I get polygons from a list of coordinates? Of course, I could traverse all coordinates manually and check if they are a closed ring, but is there a more elegant way?
I've tried to look at the methods of GeometryFactory
and Geometry
, but I couldn't find anything similar to what I'm trying to achieve.
Note: one may assume that the received coordinates always represent a sequence of polygons without holes.
I am retrieving a List<Coordinate>
from an external source, and it is known to contain one or more polygons.
Here's an example of the coordinates. As you can see, it contains two squares which do not overlap and are also not adjacent.
(0, 0)
(0, 5)
(5, 5)
(5, 0)
(0, 0)
(12, 12)
(12, 17)
(17, 17)
(17, 12)
(12, 12)
Now how do I get polygons from a list of coordinates? Of course, I could traverse all coordinates manually and check if they are a closed ring, but is there a more elegant way?
I've tried to look at the methods of GeometryFactory
and Geometry
, but I couldn't find anything similar to what I'm trying to achieve.
Note: one may assume that the received coordinates always represent a sequence of polygons without holes.
Share Improve this question edited Mar 31 at 13:26 MC Emperor asked Mar 31 at 8:22 MC EmperorMC Emperor 23.1k16 gold badges88 silver badges137 bronze badges 1- Is the specified example list one sequence of 10 coordinates from which 2 polygons should result? If so, in general, will the sequence always be grouped as depicted (i.e. 1 subsequence per geometry)?. – Computable Commented Mar 31 at 13:04
1 Answer
Reset to default 0In JTS, a Polygon consists of an outer shell and inner holes. Both are represented as LinearRings.
Depending on the situation, you could represent those coordinates using different geometries (e.g., two Polygons, a MultiPolygon, or a GeometryCollection).
From the two lists of coordinates, which in your case make up a closed LinearRings (that's good, we need that!).
A
LinearRing
is aLineString
which is both closed and simple. In other words, the first and last coordinate in the ring must be equal, and the ring must not self-intersect.
With the GeometryFactory, you were on the right track. There is the method geometryFactory.createPolygon(coordinates),
that you could use to construct the polygons, but first, you would need to build each JTS Coordinate var c = new Coordinate(x, y)
.