1

I need to do some surgery on polygons in mongodb

I have a polygon I want to cut from

            //this is a test case, in reality i am using mongodb to store geoJsonPolygons..so converting to JTS Geometry here.
                Geometry parent = GeoJsonPolygonUtil.toGeometry(geoJsonPolygonParent);
                
                //Cookie cut this polygon from parent.
                Geometry child = GeoJsonPolygonUtil.toGeometry(geoJsonPolygon);
                
                //create line segments from the polygon we want to cut out..
                Geometry lineSegements = PolygonUtil.polygonize(child);
                
                //split?(cut) out the polygon..
                Geometry parentAfterChildRemoved = PolygonUtil.splitPolygon(parent, lineSegements);
                
                //create a new polygon minus the child to cut out...
                geoJsonPolygonParent = GeoJsonConversionUtil.extract(parentAfterChildRemoved.getCoordinates());

I am using the PolygonUtil polygonize, splitPolygon from another gis stack overflow answer thinking it would work...but it does not, the parent polygon has no changes...

JTS: split arbitrary polygon by a line

Parent Polygon - zipcode 92683 https://gist.github.com/boundaries-io/4769bc85338e7398dfdf27307b3d381d parent poygon

Child Polygon - zipcode 92655 https://gist.github.com/boundaries-io/139e7645a59ef7a36aecfe7d42f9beac enter image description here

my PolygonUtil code: public class PolygonUtil {

    public static Geometry polygonize(Geometry geometry) {
        List lines = LineStringExtracter.getLines(geometry);
        Polygonizer polygonizer = new Polygonizer();
        polygonizer.add(lines);
        Collection polys = polygonizer.getPolygons();
        Polygon[] polyArray = GeometryFactory.toPolygonArray(polys);
        return geometry.getFactory().createGeometryCollection(polyArray);
    }

    public static Geometry splitPolygon(Geometry poly, Geometry line) {
        Geometry nodedLinework = poly.getBoundary().union(line);
        Geometry polys = polygonize(nodedLinework);

        // Only keep polygons which are inside the input
        List output = new ArrayList();
        for (int i = 0; i < polys.getNumGeometries(); i++) {
            Polygon candpoly = (Polygon) polys.getGeometryN(i);
            if (poly.contains(candpoly.getInteriorPoint())) {
                output.add(candpoly);
            }
        }
        return poly.getFactory().createGeometryCollection(GeometryFactory.toGeometryArray(output));
    }
}
  • 1
    Is your data in a database, or all geojson files? – DPSSpatial Aug 4 '20 at 16:42
  • Mongodb, stored as geospatial objects – Jeryl Cook Aug 4 '20 at 19:30
1

use the difference method of JTS Geometry.

public  Geometry getDifferenceGeometry(Geometry parentGeom, Geometry childGeom ) throws ParseException {
        return  parentGeom.difference(childGeom);
 
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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