From 91c377a48242124cb3fa806001fe28a872082c94 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 14 Mar 2023 17:18:51 +0800 Subject: [PATCH 1/3] Expose setSourceId in FeatureCollector [#377] --- .../com/onthegomap/planetiler/FeatureCollector.java | 7 ++++++- .../onthegomap/planetiler/FeatureCollectorTest.java | 10 +++++++--- .../test/java/com/onthegomap/planetiler/TestUtils.java | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java index e59a8cc89c..79a4437ee1 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java @@ -201,7 +201,7 @@ public final class Feature { private final Geometry geom; private final Map attrs = new TreeMap<>(); private final GeometryType geometryType; - private final long sourceId; + private long sourceId; private int sortKey = 0; @@ -240,6 +240,11 @@ public long getSourceId() { return sourceId; } + public Feature setSourceId(long sourceId) { + this.sourceId = sourceId; + return this; + } + GeometryType getGeometryType() { return geometryType; } diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java index 7f91bb7dbf..c5657be345 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java @@ -58,7 +58,8 @@ void testPoint() { .setAttr("attr1", 2) .setBufferPixels(10d) .setBufferPixelOverrides(ZoomFunction.maxZoom(12, 100d)) - .setPointLabelGridSizeAndLimit(12, 100, 10); + .setPointLabelGridSizeAndLimit(12, 100, 10) + .setSourceId(123456789); assertFeatures(14, List.of( Map.of( "_layer", "layername", @@ -69,16 +70,19 @@ void testPoint() { "_labelgrid_limit", 0, "attr1", 2, "_type", "point", - "_buffer", 10d + "_buffer", 10d, + "_source_id", 123456789L ) ), collector); assertFeatures(12, List.of( Map.of( "_labelgrid_size", 100d, "_labelgrid_limit", 10, - "_buffer", 100d + "_buffer", 100d, + "_source_id", 123456789L ) ), collector); + } @Test diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java index 13defbe57b..60903c2289 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java @@ -414,6 +414,7 @@ public static ComparableFeature feature(Geometry geom, Map attrs public static Map toMap(FeatureCollector.Feature feature, int zoom) { TreeMap result = new TreeMap<>(feature.getAttrsAtZoom(zoom)); Geometry geom = feature.getGeometry(); + result.put("_source_id", feature.getSourceId()); result.put("_minzoom", feature.getMinZoom()); result.put("_maxzoom", feature.getMaxZoom()); result.put("_buffer", feature.getBufferPixelsAtZoom(zoom)); From 2f5668ca62e431f66786c17e15382017cbeee7e7 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 14 Mar 2023 17:56:40 +0800 Subject: [PATCH 2/3] pass sourceIds to vector tile serializer [#377] --- .../onthegomap/planetiler/render/FeatureRenderer.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java index 25e78698b3..b06e801100 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java @@ -16,7 +16,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.concurrent.atomic.AtomicLong; import java.util.function.Consumer; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.CoordinateSequence; @@ -39,9 +38,6 @@ * profile (like zoom range, min pixel size, output attributes and their zoom ranges). */ public class FeatureRenderer implements Consumer, Closeable { - - // generate globally-unique IDs shared by all vector tile features representing the same source feature - private static final AtomicLong idGenerator = new AtomicLong(0); private static final Logger LOGGER = LoggerFactory.getLogger(FeatureRenderer.class); private static final VectorTile.VectorGeometry FILL = VectorTile.encodeGeometry(GeoUtils.JTS_FACTORY .createPolygon(GeoUtils.JTS_FACTORY.createLinearRing(new PackedCoordinateSequence.Double(new double[]{ @@ -95,7 +91,6 @@ private void renderGeometry(Geometry geom, FeatureCollector.Feature feature) { } private void renderPoint(FeatureCollector.Feature feature, Coordinate... origCoords) { - long id = idGenerator.incrementAndGet(); boolean hasLabelGrid = feature.hasLabelGrid(); Coordinate[] coords = new Coordinate[origCoords.length]; for (int i = 0; i < origCoords.length; i++) { @@ -132,7 +127,7 @@ private void renderPoint(FeatureCollector.Feature feature, Coordinate... origCoo TileCoord tile = entry.getKey(); List> result = entry.getValue(); Geometry geom = GeometryCoordinateSequences.reassemblePoints(result); - encodeAndEmitFeature(feature, id, attrs, tile, geom, groupInfo, 0); + encodeAndEmitFeature(feature, feature.getSourceId(), attrs, tile, geom, groupInfo, 0); emitted++; } stats.emittedFeatures(zoom, feature.getLayer(), emitted); @@ -173,7 +168,6 @@ private void renderPoint(FeatureCollector.Feature feature, MultiPoint points) { } private void renderLineOrPolygon(FeatureCollector.Feature feature, Geometry input) { - long id = idGenerator.incrementAndGet(); boolean area = input instanceof Polygonal; double worldLength = (area || input.getNumGeometries() > 1) ? 0 : input.getLength(); String numPointsAttr = feature.getNumPointsAttr(); @@ -217,7 +211,7 @@ private void renderLineOrPolygon(FeatureCollector.Feature feature, Geometry inpu attrs = new HashMap<>(attrs); attrs.put(numPointsAttr, geom.getNumPoints()); } - writeTileFeatures(z, id, feature, sliced, attrs); + writeTileFeatures(z, feature.getSourceId(), feature, sliced, attrs); } stats.processedElement(area ? "polygon" : "line", feature.getLayer()); From 8897c6a4eedf6f63cd3ff7357be97320ac0e341e Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Wed, 15 Mar 2023 08:49:47 +0800 Subject: [PATCH 3/3] refactor get/setSourceId -> get/setId for FeatureCollector --- .../onthegomap/planetiler/FeatureCollector.java | 14 +++++++------- .../planetiler/render/FeatureRenderer.java | 4 ++-- .../planetiler/FeatureCollectorTest.java | 6 +++--- .../java/com/onthegomap/planetiler/TestUtils.java | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java index 79a4437ee1..efe950d1d9 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java @@ -201,7 +201,7 @@ public final class Feature { private final Geometry geom; private final Map attrs = new TreeMap<>(); private final GeometryType geometryType; - private long sourceId; + private long id; private int sortKey = 0; @@ -228,20 +228,20 @@ public final class Feature { private String numPointsAttr = null; - private Feature(String layer, Geometry geom, long sourceId) { + private Feature(String layer, Geometry geom, long id) { this.layer = layer; this.geom = geom; this.geometryType = GeometryType.typeOf(geom); - this.sourceId = sourceId; + this.id = id; } /** Returns the original ID of the source feature that this feature came from (i.e. OSM node/way ID). */ - public long getSourceId() { - return sourceId; + public long getId() { + return id; } - public Feature setSourceId(long sourceId) { - this.sourceId = sourceId; + public Feature setId(long id) { + this.id = id; return this; } diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java index b06e801100..6b2c18ff48 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/render/FeatureRenderer.java @@ -127,7 +127,7 @@ private void renderPoint(FeatureCollector.Feature feature, Coordinate... origCoo TileCoord tile = entry.getKey(); List> result = entry.getValue(); Geometry geom = GeometryCoordinateSequences.reassemblePoints(result); - encodeAndEmitFeature(feature, feature.getSourceId(), attrs, tile, geom, groupInfo, 0); + encodeAndEmitFeature(feature, feature.getId(), attrs, tile, geom, groupInfo, 0); emitted++; } stats.emittedFeatures(zoom, feature.getLayer(), emitted); @@ -211,7 +211,7 @@ private void renderLineOrPolygon(FeatureCollector.Feature feature, Geometry inpu attrs = new HashMap<>(attrs); attrs.put(numPointsAttr, geom.getNumPoints()); } - writeTileFeatures(z, feature.getSourceId(), feature, sliced, attrs); + writeTileFeatures(z, feature.getId(), feature, sliced, attrs); } stats.processedElement(area ? "polygon" : "line", feature.getLayer()); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java index c5657be345..672fe9ef36 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java @@ -59,7 +59,7 @@ void testPoint() { .setBufferPixels(10d) .setBufferPixelOverrides(ZoomFunction.maxZoom(12, 100d)) .setPointLabelGridSizeAndLimit(12, 100, 10) - .setSourceId(123456789); + .setId(123456789); assertFeatures(14, List.of( Map.of( "_layer", "layername", @@ -71,7 +71,7 @@ void testPoint() { "attr1", 2, "_type", "point", "_buffer", 10d, - "_source_id", 123456789L + "_id", 123456789L ) ), collector); assertFeatures(12, List.of( @@ -79,7 +79,7 @@ void testPoint() { "_labelgrid_size", 100d, "_labelgrid_limit", 10, "_buffer", 100d, - "_source_id", 123456789L + "_id", 123456789L ) ), collector); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java index 60903c2289..819eac6bc9 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/TestUtils.java @@ -414,7 +414,7 @@ public static ComparableFeature feature(Geometry geom, Map attrs public static Map toMap(FeatureCollector.Feature feature, int zoom) { TreeMap result = new TreeMap<>(feature.getAttrsAtZoom(zoom)); Geometry geom = feature.getGeometry(); - result.put("_source_id", feature.getSourceId()); + result.put("_id", feature.getId()); result.put("_minzoom", feature.getMinZoom()); result.put("_maxzoom", feature.getMaxZoom()); result.put("_buffer", feature.getBufferPixelsAtZoom(zoom));