1

I have a few original MultiPolygons that contain holes...I would like to extract the Polygons and recreate them WOTHOUT the holes. how is this done?

Original MultiPolygon. https://gist.github.com/boundaries-io/978eaa4a10df9467638a5eb9259c84e6 enter image description here

when breaking up the Multipolygon

   org.geojson.MultiPolygon reCreateMultiPolygon = new org.geojson.MultiPolygon();
             for (List<List<LngLatAlt>> polygonPoints : multiPolygon.getCoordinates()) {
                for (List<LngLatAlt> point : polygonPoints) {
                    Polygon p = new Polygon(point);
                    reCreateMultiPolygon.add(p);
                }
            }
             
             String fileName2 =   saveToFileSystem(reCreateMultiPolygon);

The this is treating each polygon as its own polygon...I am unclear how to recreate this with the holes intact...not treat them as normal polygons. results:/ Incorrect recreated MultiPolygon https://gist.github.com/boundaries-io/89e51458e9f5509b5457f9033474bc2d enter image description here

my end goal is to create a convert that does a org.geojson.MultiPolygon to rg.springframework.data.mongodb.core.geo.GeoJsonMultiPolygon to save in Mongodb via spring data mongodb. if this can be done via JTS,geotools,etc. thats fine...

1 Answer 1

2

If you just want the individual polygons (including the holes) then something like:

  MultiPolygon mp = (MultiPolygon) g;
  List<Polygon> polys = new ArrayList<>();
  for (int i = 0; i < mp.getNumGeometries(); i++) {
    Polygon poly = (Polygon) mp.getGeometryN(i);
    polys.add(poly);
  }

Will give you a List of Polygons. Note, these are JTS geometries not geojson ones.

Update

For just the holes you need to do something like:

private List<Polygon> getHoles(Geometry mp) {
    GeometryFactory gf = new GeometryFactory();
    List<Polygon> holes = new ArrayList<>();
    if (mp instanceof MultiPolygon || mp instanceof Polygon) {
      for (int i = 0; i < mp.getNumGeometries(); i++) {
        Polygon poly = (Polygon) mp.getGeometryN(i);
        for (int h = 0; h < poly.getNumInteriorRing(); h++) {
          LinearRing hole = poly.getInteriorRingN(h);
          holes.add(gf.createPolygon(hole));
        }
      }
    }
    return holes;
  }
6
  • i want to re-create the MultiPolygon object so when saving it will contain holes.... using what you have will recreate the MultiPolygon treating holes as normal polygons. basically how do i create a multiPolygon that will describe the 'holes'
    – Jeryl Cook
    Sep 3, 2020 at 14:30
  • updated question..with my end goal.
    – Jeryl Cook
    Sep 3, 2020 at 15:21
  • now that i can get the holes....how do i use them to make the original MultiPolygon(that contain the holes?
    – Jeryl Cook
    Sep 3, 2020 at 15:44
  • 1
    What do you mean? if you want the holes in the original filled - see gis.stackexchange.com/questions/364571/…
    – Ian Turton
    Sep 3, 2020 at 15:49
  • Ahh my mistake I meant recreate without holes...like the original.
    – Jeryl Cook
    Sep 3, 2020 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.