From a74f274c3162cd547c9b19a9a5e8705982a61755 Mon Sep 17 00:00:00 2001 From: Michael Barry Date: Tue, 2 Jul 2024 07:35:53 -0400 Subject: [PATCH] Allow centroid for other geometry types in yaml configs (#945) --- .../planetiler/geo/GeometryType.java | 7 ++- .../planetiler/geo/GeometryTypeTest.java | 7 +-- planetiler-custommap/README.md | 3 + planetiler-custommap/planetiler.schema.json | 5 +- .../configschema/FeatureGeometry.java | 8 ++- .../custommap/ConfiguredFeatureTest.java | 56 +++++++++++++++++++ 6 files changed, 79 insertions(+), 7 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/GeometryType.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/GeometryType.java index 76df3f40ae..942b82ad3e 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/GeometryType.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/GeometryType.java @@ -13,7 +13,12 @@ import vector_tile.VectorTileProto; public enum GeometryType { - UNKNOWN(VectorTileProto.Tile.GeomType.UNKNOWN, 0, "unknown"), + UNKNOWN(VectorTileProto.Tile.GeomType.UNKNOWN, 0, "unknown") { + @Override + public Expression featureTest() { + return Expression.TRUE; + } + }, @JsonProperty("point") POINT(VectorTileProto.Tile.GeomType.POINT, 1, "point"), @JsonProperty("line") diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/GeometryTypeTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/GeometryTypeTest.java index 374f221ca8..372a15b30f 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/GeometryTypeTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/geo/GeometryTypeTest.java @@ -3,7 +3,6 @@ import static java.util.Collections.emptyList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import com.onthegomap.planetiler.TestUtils; @@ -42,9 +41,9 @@ void testGeometryFactory() { assertFalse(GeometryType.POLYGON.featureTest().evaluate(point)); assertTrue(GeometryType.POLYGON.featureTest().evaluate(poly)); - assertThrows(Exception.class, () -> GeometryType.UNKNOWN.featureTest().evaluate(point)); - assertThrows(Exception.class, () -> GeometryType.UNKNOWN.featureTest().evaluate(line)); - assertThrows(Exception.class, () -> GeometryType.UNKNOWN.featureTest().evaluate(poly)); + assertTrue(GeometryType.UNKNOWN.featureTest().evaluate(point)); + assertTrue(GeometryType.UNKNOWN.featureTest().evaluate(line)); + assertTrue(GeometryType.UNKNOWN.featureTest().evaluate(poly)); } @ParameterizedTest diff --git a/planetiler-custommap/README.md b/planetiler-custommap/README.md index 8515752be6..331533d2de 100644 --- a/planetiler-custommap/README.md +++ b/planetiler-custommap/README.md @@ -219,7 +219,10 @@ A feature is a defined set of objects that meet a specified filter criteria. of: - `point` `line` or `polygon` to pass the original feature through - `polygon_centroid` to match on polygons, and emit a point at the center + - `line_centroid` to match on lines, and emit a point at the center + - `centroid` to match any geometry, and emit a point at the center - `polygon_point_on_surface` to match on polygons, and emit an interior point + - `point_on_line` to match on lines, and emit a point somewhere along the line - `polygon_centroid_if_convex` to match on polygons, and if the polygon is convex emit the centroid, otherwise emit an interior point - `min_tile_cover_size` - Include objects of a certain geometry size, where 1.0 means "is diff --git a/planetiler-custommap/planetiler.schema.json b/planetiler-custommap/planetiler.schema.json index 027fe110f0..0fa3b028ae 100644 --- a/planetiler-custommap/planetiler.schema.json +++ b/planetiler-custommap/planetiler.schema.json @@ -354,8 +354,11 @@ "line", "polygon", "polygon_centroid", + "line_centroid", + "centroid", "polygon_centroid_if_convex", - "polygon_point_on_surface" + "polygon_point_on_surface", + "point_on_line" ] }, "source": { diff --git a/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/configschema/FeatureGeometry.java b/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/configschema/FeatureGeometry.java index b0dc9ea9bf..96f31b66ce 100644 --- a/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/configschema/FeatureGeometry.java +++ b/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/configschema/FeatureGeometry.java @@ -16,10 +16,16 @@ public enum FeatureGeometry { POLYGON(GeometryType.POLYGON, FeatureCollector::polygon), @JsonProperty("polygon_centroid") POLYGON_CENTROID(GeometryType.POLYGON, FeatureCollector::centroid), + @JsonProperty("line_centroid") + LINE_CENTROID(GeometryType.LINE, FeatureCollector::centroid), + @JsonProperty("centroid") + CENTROID(GeometryType.UNKNOWN, FeatureCollector::centroid), @JsonProperty("polygon_centroid_if_convex") POLYGON_CENTROID_IF_CONVEX(GeometryType.POLYGON, FeatureCollector::centroidIfConvex), @JsonProperty("polygon_point_on_surface") - POLYGON_POINT_ON_SURFACE(GeometryType.POLYGON, FeatureCollector::pointOnSurface); + POLYGON_POINT_ON_SURFACE(GeometryType.POLYGON, FeatureCollector::pointOnSurface), + @JsonProperty("point_on_line") + POINT_ON_LINE(GeometryType.LINE, FeatureCollector::pointOnSurface); public final GeometryType geometryType; public final BiFunction geometryFactory; diff --git a/planetiler-custommap/src/test/java/com/onthegomap/planetiler/custommap/ConfiguredFeatureTest.java b/planetiler-custommap/src/test/java/com/onthegomap/planetiler/custommap/ConfiguredFeatureTest.java index 5cabed4330..fc743a8925 100644 --- a/planetiler-custommap/src/test/java/com/onthegomap/planetiler/custommap/ConfiguredFeatureTest.java +++ b/planetiler-custommap/src/test/java/com/onthegomap/planetiler/custommap/ConfiguredFeatureTest.java @@ -34,6 +34,7 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.ValueSource; +import org.locationtech.jts.geom.Puntal; class ConfiguredFeatureTest { private PlanetilerConfig planetilerConfig = PlanetilerConfig.defaults(); @@ -1222,4 +1223,59 @@ void testSchemaPostProcessWithMergePolygons() { ) ), loadConfig(config).findFeatureLayer("testLayer").postProcess()); } + + @Test + void testCentroid() { + var config = """ + sources: + osm: + type: osm + url: geofabrik:rhode-island + local_path: data/rhode-island.osm.pbf + layers: + - id: testLayer + features: + - source: osm + geometry: centroid + """; + this.planetilerConfig = PlanetilerConfig.from(Arguments.of(Map.of())); + testPolygon(config, Map.of( + "natural", "water" + ), feature -> { + assertInstanceOf(Puntal.class, feature.getGeometry()); + }, 1); + testLinestring(config, Map.of( + "natural", "water" + ), feature -> { + assertInstanceOf(Puntal.class, feature.getGeometry()); + }, 1); + testPoint(config, Map.of( + "natural", "water" + ), feature -> { + assertInstanceOf(Puntal.class, feature.getGeometry()); + }, 1); + } + + @ParameterizedTest + @ValueSource(strings = {"line_centroid", "point_on_line"}) + void testLineCentroid(String type) { + var config = """ + sources: + osm: + type: osm + url: geofabrik:rhode-island + local_path: data/rhode-island.osm.pbf + layers: + - id: testLayer + features: + - source: osm + geometry: %s + """.formatted(type); + this.planetilerConfig = PlanetilerConfig.from(Arguments.of(Map.of())); + testLinestring(config, Map.of( + "natural", "water" + ), feature -> { + assertInstanceOf(Puntal.class, feature.getGeometry()); + }, 1); + } }