Skip to content

Commit

Permalink
Allow centroid for other geometry types in yaml configs (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Jul 2, 2024
1 parent bbea298 commit a74f274
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions planetiler-custommap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion planetiler-custommap/planetiler.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeatureCollector, String, FeatureCollector.Feature> geometryFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

0 comments on commit a74f274

Please sign in to comment.