/** * Returns a list of polygons which make up this MultiPolygon instance. * * @return a list of {@link Polygon}s which make up this MultiPolygon instance * @since 3.0.0 */ public List<Polygon> polygons() { List<List<List<Point>>> coordinates = coordinates(); List<Polygon> polygons = new ArrayList<>(coordinates.size()); for (List<List<Point>> points : coordinates) { polygons.add(Polygon.fromLngLats(points)); } return polygons; }
/** * Convenience method to get the outer {@link LineString} which defines the outer perimeter of * the polygon. * * @return a {@link LineString} defining the outer perimeter of this polygon * @since 3.0.0 */ @Nullable public LineString outer() { return LineString.fromLngLats(coordinates().get(0)); }
/** * Create a new instance of this class by passing in an outer {@link LineString} and optionally * one or more inner LineStrings. Each of these LineStrings should follow the linear ring rules. * <p> * Note that if a LineString breaks one of the linear ring rules, a {@link RuntimeException} will * be thrown. * * @param outer a LineString which defines the outer perimeter of the polygon * @param inner one or more LineStrings representing holes inside the outer perimeter * @return a new instance of this class defined by the values passed inside this static factory * method * @since 3.0.0 */ public static Polygon fromOuterInner(@NonNull LineString outer, @Nullable LineString... inner) { isLinearRing(outer); List<List<Point>> coordinates = new ArrayList<>(); coordinates.add(outer.coordinates()); // If inner rings are set to null, return early. if (inner == null) { return new AutoValue_Polygon(TYPE, null, coordinates); } for (LineString lineString : inner) { isLinearRing(lineString); coordinates.add(lineString.coordinates()); } return new AutoValue_Polygon(TYPE, null, coordinates); }
@Test public void fromLngLats_tripleDoubleArray() throws Exception { double[][][] coordinates = new double[][][] { {{100.0, 0.0}, {101.0, 0.0}, {101.0, 1.0}, {100.0, 1.0}, {100.0, 0.0}} }; Polygon polygon = Polygon.fromLngLats(coordinates); assertEquals(0, polygon.inner().size()); assertEquals(Point.fromLngLat(100.0, 0.0), polygon.coordinates().get(0).get(0)); }
@Test public void passingInSinglePolygon_doesHandleCorrectly() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(3.0, 4.0)); List<List<Point>> pointsList = new ArrayList<>(); pointsList.add(points); Polygon geometry = Polygon.fromLngLats(pointsList); MultiPolygon multiPolygon = MultiPolygon.fromPolygon(geometry); assertNotNull(multiPolygon); assertEquals(1, multiPolygon.polygons().size()); assertEquals(2.0, multiPolygon.polygons().get(0).coordinates().get(0).get(0).latitude(), DELTA); }
@Test public void fromJsonHoles() throws IOException { final String json = loadJsonFixture(SAMPLE_POLYGON_HOLES); Polygon geo = Polygon.fromJson(json); assertEquals("Polygon", geo.type()); assertEquals(100.0, geo.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(0.0, geo.coordinates().get(0).get(0).latitude(), DELTA); assertEquals(2, geo.coordinates().size()); assertEquals(100.8, geo.coordinates().get(1).get(0).longitude(), DELTA); assertEquals(0.8, geo.coordinates().get(1).get(0).latitude(), DELTA); assertFalse(geo.coordinates().get(0).get(0).hasAltitude()); }
@Test public void fromOuterInner_handlesOuterAndInnerLineStringCorrectly() throws Exception { List<Point> outer = new ArrayList<>(); outer.add(Point.fromLngLat(10.0, 2.0)); outer.add(Point.fromLngLat(5.0, 2.0)); outer.add(Point.fromLngLat(3.0, 2.0)); outer.add(Point.fromLngLat(10.0, 2.0)); LineString outerLineString = LineString.fromLngLats(outer); List<Point> inner = new ArrayList<>(); inner.add(Point.fromLngLat(5.0, 2.0)); inner.add(Point.fromLngLat(2.5, 2.0)); inner.add(Point.fromLngLat(1.5, 2.0)); inner.add(Point.fromLngLat(5.0, 2.0)); LineString innerLineString = LineString.fromLngLats(inner); Polygon polygon = Polygon.fromOuterInner(outerLineString, innerLineString); assertEquals(Point.fromLngLat(10.0, 2.0), polygon.coordinates().get(0).get(0)); assertEquals(outerLineString, polygon.outer()); assertEquals(1, polygon.inner().size()); assertEquals(innerLineString, polygon.inner().get(0)); }
@Test public void fromOuterInner_handlesSingleLineStringCorrectly() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(10.0, 2.0)); points.add(Point.fromLngLat(5.0, 2.0)); points.add(Point.fromLngLat(3.0, 2.0)); points.add(Point.fromLngLat(10.0, 2.0)); LineString lineString = LineString.fromLngLats(points); Polygon polygon = Polygon.fromOuterInner(lineString); assertEquals(Point.fromLngLat(10.0, 2.0), polygon.coordinates().get(0).get(0)); }
@Test public void fromOuterInner_withABoundingBox() throws Exception { List<Point> outer = new ArrayList<>(); outer.add(Point.fromLngLat(10.0, 2.0)); outer.add(Point.fromLngLat(5.0, 2.0)); outer.add(Point.fromLngLat(3.0, 2.0)); outer.add(Point.fromLngLat(10.0, 2.0)); LineString outerLineString = LineString.fromLngLats(outer); List<Point> inner = new ArrayList<>(); inner.add(Point.fromLngLat(5.0, 2.0)); inner.add(Point.fromLngLat(2.5, 2.0)); inner.add(Point.fromLngLat(1.5, 2.0)); inner.add(Point.fromLngLat(5.0, 2.0)); LineString innerLineString = LineString.fromLngLats(inner); BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0); Polygon polygon = Polygon.fromOuterInner(outerLineString, bbox, innerLineString); assertEquals(bbox, polygon.bbox()); assertEquals(outerLineString, polygon.outer()); assertEquals(1, polygon.inner().size()); assertEquals(innerLineString, polygon.inner().get(0)); }
@Test public void bbox_doesNotSerializeWhenNotPresent() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); points.add(Point.fromLngLat(3.0, 4.0)); points.add(Point.fromLngLat(1.0, 2.0)); LineString outer = LineString.fromLngLats(points); List<LineString> inner = new ArrayList<>(); inner.add(LineString.fromLngLats(points)); inner.add(LineString.fromLngLats(points)); Polygon polygon = Polygon.fromOuterInner(outer, inner); compareJson(polygon.toJson(), "{\"type\":\"Polygon\",\"coordinates\":" + "[[[1,2],[2,3],[3,4],[1,2]],[[1,2],[2,3],[3,4],[1,2]],[[1,2],[2,3],[3,4],[1,2]]]}"); }
@Test public void fromJson_coordinatesPresent() throws Exception { thrown.expect(NullPointerException.class); Polygon.fromJson("{\"type\":\"Polygon\",\"coordinates\":null}"); } }
@Test public void bbox_nullWhenNotSet() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); points.add(Point.fromLngLat(3.0, 4.0)); points.add(Point.fromLngLat(1.0, 2.0)); LineString outer = LineString.fromLngLats(points); List<LineString> inner = new ArrayList<>(); inner.add(LineString.fromLngLats(points)); inner.add(LineString.fromLngLats(points)); Polygon polygon = Polygon.fromOuterInner(outer, inner); assertNull(polygon.bbox()); }
@Test @Ignore public void name() throws Exception { Feature feature = Feature.fromJson(loadJsonFixture(CIRCLE_IN)); Polygon polygon = TurfTransformation.circle((Point) feature.geometry(), feature.getNumberProperty("radius").doubleValue()); FeatureCollection featureCollection = FeatureCollection.fromJson(loadJsonFixture(CIRCLE_OUT)); compareJson(featureCollection.features().get(1).geometry().toJson(), polygon.toJson()); } }
@Test public void fromJson() throws IOException { final String json = loadJsonFixture(SAMPLE_POLYGON); Polygon geo = Polygon.fromJson(json); assertEquals("Polygon", geo.type()); assertEquals(100.0, geo.coordinates().get(0).get(0).longitude(), DELTA); assertEquals(0.0, geo.coordinates().get(0).get(0).latitude(), DELTA); assertFalse(geo.coordinates().get(0).get(0).hasAltitude()); }
@Test public void bbox_doesSerializeWhenPresent() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); points.add(Point.fromLngLat(3.0, 4.0)); points.add(Point.fromLngLat(1.0, 2.0)); LineString outer = LineString.fromLngLats(points); BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0); List<LineString> inner = new ArrayList<>(); inner.add(LineString.fromLngLats(points)); inner.add(LineString.fromLngLats(points)); Polygon polygon = Polygon.fromOuterInner(outer, bbox, inner); compareJson(polygon.toJson(), "{\"type\":\"Polygon\",\"bbox\":[1.0,2.0,3.0,4.0],\"coordinates\":" + "[[[1,2],[2,3],[3,4],[1,2]],[[1,2],[2,3],[3,4],[1,2]],[[1,2],[2,3],[3,4],[1,2]]]}"); }
@Test public void coordAllPolygonExcludeWrapCoord() throws TurfException { String polygonString = "{type: 'Polygon', coordinates: [[[0, 0], [1, 1], [0, 1], [0, 0]]]}"; Polygon polygonGeometry = Polygon.fromJson(polygonString); List<Point> resultList = TurfMeta.coordAll(polygonGeometry, true); assertEquals(resultList.size(), 3, DELTA); assertEquals(resultList.get(0), Point.fromLngLat(0, 0)); assertEquals(resultList.get(1), Point.fromLngLat(1, 1)); assertEquals(resultList.get(2), Point.fromLngLat(0, 1)); }
@Test public void bbox_returnsCorrectBbox() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); points.add(Point.fromLngLat(3.0, 4.0)); points.add(Point.fromLngLat(1.0, 2.0)); LineString outer = LineString.fromLngLats(points); BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0); List<LineString> inner = new ArrayList<>(); inner.add(LineString.fromLngLats(points)); inner.add(LineString.fromLngLats(points)); Polygon polygon = Polygon.fromOuterInner(outer, bbox, inner); assertNotNull(polygon.bbox()); assertEquals(1.0, polygon.bbox().west(), DELTA); assertEquals(2.0, polygon.bbox().south(), DELTA); assertEquals(3.0, polygon.bbox().east(), DELTA); assertEquals(4.0, polygon.bbox().north(), DELTA); }
/** * Convenience method to get the outer {@link LineString} which defines the outer perimeter of * the polygon. * * @return a {@link LineString} defining the outer perimeter of this polygon * @since 3.0.0 */ @Nullable public LineString outer() { return LineString.fromLngLats(coordinates().get(0)); }