/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
List<List<List<Point>>> polys = multiPolygon.coordinates();
@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()); }