Skip to content

Commit

Permalink
order polygons in Multipolygon also when creating final result, for d…
Browse files Browse the repository at this point in the history
…eterministic output
  • Loading branch information
phanecak-maptiler committed Mar 14, 2023
1 parent 922f307 commit ef14e59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private static Geometry doBuild(
return shells.iterator().next().toPolygon();
} else {
Polygon[] finished = shells.stream()
.sorted(BY_AREA_DESCENDING)
.map(Ring::toPolygon)
.toArray(Polygon[]::new);
return GeoUtils.JTS_FACTORY.createMultiPolygon(finished);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateXY;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.MultiPolygon;

/**
* This class is ported to Java from https://github.com/omniscale/imposm3/blob/master/geom/multipolygon_test.go
Expand Down Expand Up @@ -107,7 +108,7 @@ private Node node(double x, double y) {
return new Node(id++, x, y);
}

private void testBuildMultipolygon(List<List<Node>> ways, Geometry expected) throws GeometryException {
private void testBuildMultipolygon(List<List<Node>> ways, Geometry expected, boolean withOrdering) throws GeometryException {
Map<Long, Coordinate> coords = new HashMap<>();
List<LongArrayList> rings = new ArrayList<>();
for (List<Node> way : ways) {
Expand All @@ -121,6 +122,13 @@ private void testBuildMultipolygon(List<List<Node>> ways, Geometry expected) thr
OsmReader.NodeLocationProvider nodeLocs = coords::get;
Geometry actual = OsmMultipolygon.build(rings, nodeLocs, 0);
assertSameNormalizedFeature(expected, actual);
if (withOrdering) {
assertEquals(expected.toString(), actual.toString());
}
}

private void testBuildMultipolygon(List<List<Node>> ways, Geometry expected) throws GeometryException {
testBuildMultipolygon(ways, expected, false);
}

@Test
Expand Down Expand Up @@ -222,6 +230,21 @@ void testSimplePolygonWithHole() throws GeometryException {
);
}

@Test
void testSimplePolygonOrdering() throws GeometryException {
testBuildMultipolygon(
List.of(
rectangleNodes(8, 10),
rectangleNodes(0, 7)
),
newMultiPolygon(
rectangle(0, 7),
rectangle(8, 10)
),
true
);
}

@Test
void testSimplePolygonWithMultipleHoles() throws GeometryException {
testBuildMultipolygon(
Expand Down

0 comments on commit ef14e59

Please sign in to comment.