Skip to content

Instantly share code, notes, and snippets.

@adeydas
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adeydas/3d22d4e868854fb98ea5 to your computer and use it in GitHub Desktop.
Save adeydas/3d22d4e868854fb98ea5 to your computer and use it in GitHub Desktop.
Find holes in a polygon
private List<PolyDT> temp;
private void findHoles(Geometry geometry) {
String geometryType = geometry.getGeometryType();
geometryType = geometryType.toUpperCase();
if (geometryType.equals("MULTIPOLYGON")) {
MultiPolygon multiPolygon = (MultiPolygon) geometry;
for (int i=0; i<multiPolygon.getNumGeometries(); i++) {
findHoles((Geometry) multiPolygon.getGeometryN(i));
}
} else if (geometryType.equals("POLYGON")) {
Polygon polygon = (Polygon) geometry;
int noOfHoles = polygon.getNumInteriorRing();
if (noOfHoles > 0) {
for (int j = 0; j < noOfHoles; j++) {
LineString hole = polygon.getInteriorRingN(j);
Coordinate[] coordinates = hole.getCoordinates();
GeometryFactory factory = new GeometryFactory();
//Find co-ordinate sequence from line string
CoordinateSequence coordinateSequence = new CoordinateArraySequence(coordinates);
LinearRing linearRing = factory.createLinearRing(coordinateSequence);
Polygon p = new Polygon(linearRing, null, factory);
temp.add(new PolyDT((Geometry)p, true));
}
}
temp.add(new PolyDT((Geometry)polygon, false));
} else if (geometryType.equals("LINESTRING")) {
temp.add(new PolyDT(geometry, false));
} else if (geometryType.equals("MULTILINESTRING")) {
temp.add(new PolyDT(geometry, false));
} else if (geometryType.equals("POINT")) {
temp.add(new PolyDT(geometry, false));
} else if (geometryType.equals("MULTIPOINT")) {
temp.add(new PolyDT(geometry, false));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment