@Test public void fromJson() throws IOException { final String json = loadJsonFixture(SAMPLE_MULTIPOLYGON); MultiPolygon geo = MultiPolygon.fromJson(json); assertEquals(geo.type(), "MultiPolygon"); assertEquals(geo.coordinates().get(0).get(0).get(0).longitude(), 102.0, DELTA); assertEquals(geo.coordinates().get(0).get(0).get(0).latitude(), 2.0, DELTA); assertFalse(geo.coordinates().get(0).get(0).get(0).hasAltitude()); }
/** * Takes a {@link Point} and a {@link Polygon} and determines if the point resides inside the * polygon. The polygon can be convex or concave. The function accounts for holes. * * @param point which you'd like to check if inside the polygon * @param polygon which you'd like to check if the points inside * @return true if the Point is inside the Polygon; false if the Point is not inside the Polygon * @see <a href="http://turfjs.org/docs/#inside">Turf Inside documentation</a> * @since 1.3.0 */ public static boolean inside(Point point, Polygon polygon) { // This API needs to get better List<List<Point>> coordinates = polygon.coordinates(); List<List<List<Point>>> multiCoordinates = new ArrayList<>(); multiCoordinates.add(coordinates); return inside(point, MultiPolygon.fromLngLats(multiCoordinates)); }
/** * 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; }
@Test public void fromJson_coordinatesPresent() throws Exception { thrown.expect(NullPointerException.class); MultiPolygon.fromJson("{\"type\":\"MultiPolygon\",\"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<Polygon> polygons = new ArrayList<>(); polygons.add(Polygon.fromOuterInner(outer)); polygons.add(Polygon.fromOuterInner(outer)); MultiPolygon multiPolygon = MultiPolygon.fromPolygons(polygons); assertNull(multiPolygon.bbox()); }
@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<Polygon> polygons = new ArrayList<>(); polygons.add(Polygon.fromOuterInner(outer)); polygons.add(Polygon.fromOuterInner(outer)); MultiPolygon multiPolygon = MultiPolygon.fromPolygons(polygons); compareJson(multiPolygon.toJson(), "{\"type\":\"MultiPolygon\"," + "\"coordinates\":[[[[1,2],[2,3],[3,4],[1,2]]],[[[1,2],[2,3],[3,4],[1,2]]]]}"); }
@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 sanity() 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<Polygon> polygons = new ArrayList<>(); polygons.add(Polygon.fromOuterInner(outer)); polygons.add(Polygon.fromOuterInner(outer)); MultiPolygon multiPolygon = MultiPolygon.fromPolygons(polygons); assertNotNull(multiPolygon); }
/** * 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; }
@Test public void coordAllMultiPolygon() throws TurfException { String multipolygonString = "{type: 'MultiPolygon', coordinates: [[[[0, 0], [1, 1], [0, 1], [0, 0]]]]}"; MultiPolygon multiPolygonGeometry = MultiPolygon.fromJson(multipolygonString); List<Point> resultList = TurfMeta.coordAll(multiPolygonGeometry, false); assertEquals(resultList.size(), 4, 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)); assertEquals(resultList.get(3), Point.fromLngLat(0, 0)); }
@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<Polygon> polygons = new ArrayList<>(); polygons.add(Polygon.fromOuterInner(outer)); polygons.add(Polygon.fromOuterInner(outer)); MultiPolygon multiPolygon = MultiPolygon.fromPolygons(polygons, bbox); assertNotNull(multiPolygon.bbox()); assertEquals(1.0, multiPolygon.bbox().west(), DELTA); assertEquals(2.0, multiPolygon.bbox().south(), DELTA); assertEquals(3.0, multiPolygon.bbox().east(), DELTA); assertEquals(4.0, multiPolygon.bbox().north(), DELTA); }
@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<Polygon> polygons = new ArrayList<>(); polygons.add(Polygon.fromOuterInner(outer)); polygons.add(Polygon.fromOuterInner(outer)); MultiPolygon multiPolygon = MultiPolygon.fromPolygons(polygons, bbox); compareJson(multiPolygon.toJson(), "{\"type\":\"MultiPolygon\",\"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]]]]}"); }
@Test public void testSerializable() 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<Polygon> polygons = new ArrayList<>(); polygons.add(Polygon.fromOuterInner(outer)); polygons.add(Polygon.fromOuterInner(outer)); MultiPolygon multiPolygon = MultiPolygon.fromPolygons(polygons, bbox); byte[] bytes = serialize(multiPolygon); assertEquals(multiPolygon, deserialize(bytes, MultiPolygon.class)); }
/** * Get all coordinates from a {@link MultiPolygon} object, returning a {@code List} of Point * objects. If you have a geometry collection, you need to break it down to individual geometry * objects before using {@link #coordAll}. * * @param multiPolygon any {@link MultiPolygon} object * @param excludeWrapCoord whether or not to include the final coordinate of LinearRings that * wraps the ring in its iteration * @return a {@code List} made up of {@link Point}s * @since 2.0.0 */ @NonNull public static List<Point> coordAll(@NonNull MultiPolygon multiPolygon, boolean excludeWrapCoord) { List<Point> coords = new ArrayList<>(); int wrapShrink = excludeWrapCoord ? 1 : 0; for (int i = 0; i < multiPolygon.coordinates().size(); i++) { for (int j = 0; j < multiPolygon.coordinates().get(i).size(); j++) { for (int k = 0; k < multiPolygon.coordinates().get(i).get(j).size() - wrapShrink; k++) { coords.add(multiPolygon.coordinates().get(i).get(j).get(k)); } } } return coords; }
@Test public void bboxFromGeometryCollection() throws IOException, TurfException { // Check that geometry collection and direct bbox are equal MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON)); assertArrayEquals(TurfMeasurement.bbox(multiPolygon), TurfMeasurement.bbox(GeometryCollection.fromGeometry(multiPolygon)), DELTA); // Check all geometry types List<Geometry> geometries = new ArrayList<>(); geometries.add(Feature.fromJson(loadJsonFixture(TURF_BBOX_POINT)).geometry()); geometries.add(MultiPoint.fromJson(loadJsonFixture(TURF_BBOX_MULTI_POINT))); geometries.add(LineString.fromJson(loadJsonFixture(TURF_BBOX_LINESTRING))); geometries.add(MultiLineString.fromJson(loadJsonFixture(TURF_BBOX_MULTILINESTRING))); geometries.add(Feature.fromJson(loadJsonFixture(TURF_BBOX_POLYGON)).geometry()); geometries.add(MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON))); geometries.add(GeometryCollection.fromGeometry(Point.fromLngLat(-1., -1.))); double[] bbox = TurfMeasurement.bbox(GeometryCollection.fromGeometries(geometries)); assertEquals(4, bbox.length); assertEquals(-1, bbox[0], DELTA); assertEquals(-10, bbox[1], DELTA); assertEquals(130, bbox[2], DELTA); assertEquals(4, bbox[3], DELTA); } }
/** * Takes a {@link MultiPolygon} and measures each polygons perimeter in the specified units. if * one of the polygons contains holes, the perimeter will also be included. * * @param multiPolygon geometry to measure * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria} * @return total perimeter of the input polygons combined, in the units specified * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> * @since 1.2.0 */ public static double length(@NonNull MultiPolygon multiPolygon, @NonNull @TurfConstants.TurfUnitCriteria String units) { double len = 0; List<List<List<Point>>> coordinates = multiPolygon.coordinates(); for (List<List<Point>> coordinate : coordinates) { for (List<Point> theCoordinate : coordinate) { len += length(theCoordinate, units); } } return len; }
@Test public void bboxFromMultiPolygon() throws IOException, TurfException { MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON)); double[] bbox = TurfMeasurement.bbox(multiPolygon); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(103, bbox[2], DELTA); assertEquals(3, bbox[3], DELTA); }
List<List<List<Point>>> polys = multiPolygon.coordinates();
@Test public void bboxFromGeometry() throws IOException, TurfException { Geometry geometry = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON)); double[] bbox = TurfMeasurement.bbox(geometry); assertEquals(4, bbox.length); assertEquals(100, bbox[0], DELTA); assertEquals(0, bbox[1], DELTA); assertEquals(103, bbox[2], DELTA); assertEquals(3, bbox[3], DELTA); }