I have a list of 2D polygons represented as List<double[]> in Java defining gemetries of streets. Each array represents a polygon with coordinates in pairs (x1, y1, x2, y2, …). For example, a polygon might be represented as {x1, y1, x2, y2, x3, y3, x1, y1}.
I want to obtain a simplified list of polygons by merging polygons that can be combined into a single polygon. All polygons in the list are simple, with no oriented rings or self-intersections.
I’ve tried using JTS (Java Topology Suite), but the results are not as expected. Here are the images
Before polygon merging
After polygon merging
I’ve ensured the polygons are correctly defined multiple times, but as shown in the images, the merging produces unexpected results.
**Here’s the code I’ve tried: **
public class PolygonMerger {
public static List<double[]> mergePolygons(List<double[]> polygons) {
List<Geometry> jtsPolygons = new ArrayList<>();
GeometryFactory factory = new GeometryFactory();
for (double[] coords : polygons) {
Coordinate[] coordinates = new Coordinate[coords.length / 2];
for (int i = 0; i < coords.length; i += 2) {
coordinates[i / 2] = new Coordinate(coords[i], coords[i + 1]);
}
// Ensure the polygon is closed
if (!coordinates[0].equals(coordinates[coordinates.length - 1])) {
Coordinate[] closedCoordinates = new Coordinate[coordinates.length + 1];
System.arraycopy(coordinates, 0, closedCoordinates, 0, coordinates.length);
closedCoordinates[coordinates.length] = coordinates[0]; // Close the polygon
coordinates = closedCoordinates;
}
// Create the polygon
Polygon polygon = factory.createPolygon(coordinates);
// Validate and repair the polygon if needed
if (!new IsValidOp(polygon).isValid()) {
polygon = (Polygon) polygon.buffer(0); // Repair polygon
}
jtsPolygons.add(polygon);
}
// Union all polygons
Geometry mergedGeometry = CascadedPolygonUnion.union(jtsPolygons);
List<Polygon> mergedPolygons = new ArrayList<>();
if (mergedGeometry instanceof Polygon) {
mergedPolygons.add((Polygon) mergedGeometry);
} else if (mergedGeometry instanceof MultiPolygon) {
for (int i = 0; i < mergedGeometry.getNumGeometries(); i++) {
mergedPolygons.add((Polygon) mergedGeometry.getGeometryN(i));
}
}
return convertToDoubleArrays(mergedPolygons);
}
private static List<double[]> convertToDoubleArrays(List<Polygon> polygons) {
List<double[]> polygonCoordsList = new ArrayList<>();
for (Polygon polygon : polygons) {
Coordinate[] coordinates = polygon.getExteriorRing().getCoordinates();
double[] coordsArray = new double[coordinates.length * 2];
for (int i = 0; i < coordinates.length; i++) {
coordsArray[i * 2] = coordinates[i].x;
coordsArray[i * 2 + 1] = coordinates[i].y;
}
polygonCoordsList.add(coordsArray);
}
return polygonCoordsList;
}
}
What could be causing the unexpected results, and how can I fix them? Are there potential issues with the merging process or the polygon definitions that I might have overlooked?
1