From f8e64a4beba00fc881ed8ca01a9475f8dd14ad5a Mon Sep 17 00:00:00 2001 From: Michael Barry Date: Sun, 26 May 2024 06:51:10 -0400 Subject: [PATCH] Make VectorTile.Feature implement WithTags (#896) --- .../onthegomap/planetiler/FeatureMerge.java | 4 ++-- .../com/onthegomap/planetiler/VectorTile.java | 19 +++++++++++++------ .../planetiler/collection/FeatureGroup.java | 2 +- .../planetiler/mbtiles/Compare.java | 4 ++-- .../onthegomap/planetiler/mbtiles/Verify.java | 2 +- .../com/onthegomap/planetiler/TestUtils.java | 4 ++-- .../onthegomap/planetiler/VectorTileTest.java | 8 ++++---- .../collection/FeatureGroupTest.java | 4 ++-- 8 files changed, 27 insertions(+), 20 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureMerge.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureMerge.java index 6e0a54824f..012ddd0e06 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureMerge.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureMerge.java @@ -161,7 +161,7 @@ public static List mergeLineStrings(List var groupedByAttrs = groupByAttrs(features, result, GeometryType.LINE); for (List groupedFeatures : groupedByAttrs) { VectorTile.Feature feature1 = groupedFeatures.getFirst(); - double lengthLimit = lengthLimitCalculator.apply(feature1.attrs()); + double lengthLimit = lengthLimitCalculator.apply(feature1.tags()); // as a shortcut, can skip line merging only if: // - only 1 element in the group @@ -401,7 +401,7 @@ public static Collection> groupByAttrs( others.add(feature); } else { groupedByAttrs - .computeIfAbsent(feature.attrs(), k -> new ArrayList<>()) + .computeIfAbsent(feature.tags(), k -> new ArrayList<>()) .add(feature); } } diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/VectorTile.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/VectorTile.java index 2d3d3efb71..c6b54d409d 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/VectorTile.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/VectorTile.java @@ -26,6 +26,7 @@ import com.onthegomap.planetiler.geo.GeometryException; import com.onthegomap.planetiler.geo.GeometryType; import com.onthegomap.planetiler.geo.MutableCoordinateSequence; +import com.onthegomap.planetiler.reader.WithTags; import com.onthegomap.planetiler.util.Hilbert; import com.onthegomap.planetiler.util.LayerAttrStats; import java.util.ArrayList; @@ -484,7 +485,7 @@ public VectorTile addLayerFeatures(String layerName, List features) { if (inFeature != null && inFeature.geometry().commands().length > 0) { EncodedFeature outFeature = new EncodedFeature(inFeature); - for (Map.Entry e : inFeature.attrs().entrySet()) { + for (Map.Entry e : inFeature.tags().entrySet()) { // skip attribute without value if (e.getValue() != null) { String key = e.getKey(); @@ -1010,7 +1011,7 @@ public CoordinateXY firstCoordinate() { * @param layer the layer the feature was in * @param id the feature ID * @param geometry the encoded feature geometry (decode using {@link VectorGeometry#decode()}) - * @param attrs tags for the feature to output + * @param tags tags for the feature to output * @param group grouping key used to limit point density or {@link #NO_GROUP} if not in a group. NOTE: this is only * populated when this feature was deserialized from {@link FeatureGroup}, not when parsed from a tile * since vector tile schema does not encode group. @@ -1019,9 +1020,9 @@ public record Feature( String layer, long id, VectorGeometry geometry, - Map attrs, + Map tags, long group - ) { + ) implements WithTags { public static final long NO_GROUP = Long.MIN_VALUE; @@ -1054,7 +1055,7 @@ public Feature copyWithNewGeometry(VectorGeometry newGeometry) { layer, id, newGeometry, - attrs, + tags, group ); } @@ -1065,11 +1066,17 @@ public Feature copyWithExtraAttrs(Map extraAttrs) { layer, id, geometry, - Stream.concat(attrs.entrySet().stream(), extraAttrs.entrySet().stream()) + Stream.concat(tags.entrySet().stream(), extraAttrs.entrySet().stream()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)), group ); } + + /** @deprecated use {@link #tags()} instead. */ + @Deprecated(forRemoval = true) + public Map attrs() { + return tags; + } } /** diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/FeatureGroup.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/FeatureGroup.java index 619c26f6a0..15f1784a81 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/FeatureGroup.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/FeatureGroup.java @@ -202,7 +202,7 @@ private byte[] encodeValue(VectorTile.Feature vectorTileFeature, RenderedFeature } packer.packLong(vectorTileFeature.id()); packer.packByte(encodeGeomTypeAndScale(vectorTileFeature.geometry())); - var attrs = vectorTileFeature.attrs(); + var attrs = vectorTileFeature.tags(); packer.packMapHeader((int) attrs.values().stream().filter(Objects::nonNull).count()); for (Map.Entry entry : attrs.entrySet()) { Object value = entry.getValue(); diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Compare.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Compare.java index 964d231f06..0fd2360754 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Compare.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Compare.java @@ -116,7 +116,7 @@ private static long getTilesCount(Mbtiles db) throws SQLException { *
  • {@link VectorTile.Feature#id()} won't be compared *
  • {@link VectorTile.Feature#layer()} will be compared *
  • {@link VectorTile.Feature#geometry()} gets normalized for comparing - *
  • {@link VectorTile.Feature#attrs()} will be compared except for the rank attribute since the value produced by + *
  • {@link VectorTile.Feature#tags()} will be compared except for the rank attribute since the value produced by * planetiler is not stable and differs on every run (at least for parks) *
  • {@link VectorTile.Feature#group()} will be compared * @@ -129,7 +129,7 @@ private record VectorTileFeatureForCmp( ) { static VectorTileFeatureForCmp fromActualFeature(VectorTile.Feature f) { try { - var attrs = new HashMap<>(f.attrs()); + var attrs = new HashMap<>(f.tags()); attrs.remove("rank"); return new VectorTileFeatureForCmp(f.layer(), f.geometry().decode().norm(), attrs, f.group()); } catch (GeometryException e) { diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Verify.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Verify.java index 99a43f15de..9d71b44706 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Verify.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/mbtiles/Verify.java @@ -68,7 +68,7 @@ public static int getNumFeatures(Mbtiles db, String layer, int zoom, Map> getTileMap(ReadableTileArc case UNKNOWN -> throw new IllegalArgumentException("cannot decompress \"UNKNOWN\""); }; var decoded = VectorTile.decode(bytes).stream() - .map(feature -> feature(decodeSilently(feature.geometry()), feature.layer(), feature.attrs())).toList(); + .map(feature -> feature(decodeSilently(feature.geometry()), feature.layer(), feature.tags())).toList(); tiles.put(tile.coord(), decoded); } return tiles; @@ -738,7 +738,7 @@ public static void assertFeatureNear(Mbtiles db, String layer, Map tags = feature.attrs(); + Map tags = feature.tags(); containedInLayerFeatures.add(tags.toString()); if (tags.entrySet().containsAll(attrs.entrySet())) { // found a match diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/VectorTileTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/VectorTileTest.java index ab51a39259..203ac7e41d 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/VectorTileTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/VectorTileTest.java @@ -160,7 +160,7 @@ void testAttributeTypes() { List decoded = VectorTile.decode(encoded); assertEquals(1, decoded.size()); - Map decodedAttributes = decoded.getFirst().attrs(); + Map decodedAttributes = decoded.getFirst().tags(); assertEquals("value1", decodedAttributes.get("key1")); assertEquals(123L, decodedAttributes.get("key2")); assertEquals(234.1f, decodedAttributes.get("key3")); @@ -330,13 +330,13 @@ void testMultipleFeaturesMultipleLayer() { )).encode(); List decoded = VectorTile.decode(encoded); - assertEquals(attrs1, decoded.get(0).attrs()); + assertEquals(attrs1, decoded.get(0).tags()); assertEquals("layer1", decoded.get(0).layer()); - assertEquals(attrs2, decoded.get(1).attrs()); + assertEquals(attrs2, decoded.get(1).tags()); assertEquals("layer1", decoded.get(1).layer()); - assertEquals(attrs1, decoded.get(2).attrs()); + assertEquals(attrs1, decoded.get(2).tags()); assertEquals("layer2", decoded.get(2).layer()); } diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/FeatureGroupTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/FeatureGroupTest.java index 7543269cc1..81a721607f 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/FeatureGroupTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/FeatureGroupTest.java @@ -95,7 +95,7 @@ private Map>> getFeatures() { for (var feature : VectorTile.decode(tile.getVectorTile().encode())) { map.computeIfAbsent(tile.tileCoord().encoded(), (i) -> new TreeMap<>()) .computeIfAbsent(feature.layer(), l -> new ArrayList<>()) - .add(new Feature(feature.attrs(), decodeSilently(feature.geometry()))); + .add(new Feature(feature.tags(), decodeSilently(feature.geometry()))); } } return map; @@ -109,7 +109,7 @@ private Map>> getFeaturesParallel() { for (var feature : VectorTile.decode(tile.getVectorTile().encode())) { map.computeIfAbsent(tile.tileCoord().encoded(), (i) -> new TreeMap<>()) .computeIfAbsent(feature.layer(), l -> new ArrayList<>()) - .add(new Feature(feature.attrs(), decodeSilently(feature.geometry()))); + .add(new Feature(feature.tags(), decodeSilently(feature.geometry()))); } } return map;