TransWikia.com

How to I recreate MultiPolygon with Rings/Holes using org.geojson

Geographic Information Systems Asked by Jeryl Cook on October 10, 2020

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…

One Answer

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;
  }

Correct answer by Ian Turton on October 10, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP