Geographic Information Systems Asked on June 12, 2021
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
Child Polygon – zipcode 92655
https://gist.github.com/boundaries-io/139e7645a59ef7a36aecfe7d42f9beac
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));
}
}
use the difference method of JTS Geometry.
public Geometry getDifferenceGeometry(Geometry parentGeom, Geometry childGeom ) throws ParseException {
return parentGeom.difference(childGeom);
}
Correct answer by Jeryl Cook on June 12, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP