Last active
August 29, 2015 14:14
-
-
Save adeydas/3d22d4e868854fb98ea5 to your computer and use it in GitHub Desktop.
Find holes in a polygon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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