From 2cc1e2bd3280111132aa6954550eaa58a52f8524 Mon Sep 17 00:00:00 2001 From: Michael Barry Date: Thu, 30 May 2024 05:37:09 -0400 Subject: [PATCH] Make sure feature collector can handle structs (#904) --- .../planetiler/FeatureCollector.java | 19 ++++++---- .../planetiler/FeatureCollectorTest.java | 35 +++++++++++++++++++ 2 files changed, 48 insertions(+), 6 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 54e315381f..71862fab50 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/FeatureCollector.java @@ -7,6 +7,7 @@ import com.onthegomap.planetiler.geo.GeometryException; import com.onthegomap.planetiler.geo.GeometryType; import com.onthegomap.planetiler.reader.SourceFeature; +import com.onthegomap.planetiler.reader.Struct; import com.onthegomap.planetiler.render.FeatureRenderer; import com.onthegomap.planetiler.stats.Stats; import com.onthegomap.planetiler.util.CacheByZoom; @@ -807,10 +808,7 @@ public Feature setPointLabelGridSizeAndLimit(int maxzoom, double size, int limit private Map computeAttrsAtZoom(int zoom) { Map result = new TreeMap<>(); for (var entry : attrs.entrySet()) { - Object value = entry.getValue(); - if (value instanceof ZoomFunction fn) { - value = fn.apply(zoom); - } + Object value = unwrap(entry.getValue(), zoom); if (value != null && !"".equals(value)) { result.put(entry.getKey(), value); } @@ -818,6 +816,16 @@ private Map computeAttrsAtZoom(int zoom) { return result; } + private static Object unwrap(Object object, int zoom) { + if (object instanceof ZoomFunction fn) { + object = fn.apply(zoom); + } + if (object instanceof Struct struct) { + object = struct.rawValue(); + } + return object; + } + /** Returns the attribute to put on all output vector tile features at a zoom level. */ public Map getAttrsAtZoom(int zoom) { if (!attrsChangeByZoom) { @@ -952,8 +960,7 @@ Partial withAttr(String key, Object value) { MergingRangeMap result = MergingRangeMap.unit(new Partial(false, attrs), Partial::merge); for (var override : partialOverrides) { result.update(override.range(), m -> switch (override) { - case Attr attr -> - m.withAttr(attr.key, attr.value instanceof ZoomFunction fn ? fn.apply(zoom) : attr.value); + case Attr attr -> m.withAttr(attr.key, unwrap(attr.value, zoom)); case Maxzoom mz -> m.withOmit(mz.maxzoom < zoom); case Minzoom mz -> m.withOmit(mz.minzoom > zoom); case Omit ignored -> m.withOmit(true); 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 ab889de397..7d894717f6 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/FeatureCollectorTest.java @@ -10,6 +10,7 @@ import com.onthegomap.planetiler.geo.GeometryException; import com.onthegomap.planetiler.geo.GeometryType; import com.onthegomap.planetiler.reader.SimpleFeature; +import com.onthegomap.planetiler.reader.Struct; import com.onthegomap.planetiler.stats.Stats; import com.onthegomap.planetiler.util.ZoomFunction; import java.util.Arrays; @@ -748,4 +749,38 @@ void testPartialOmit() { new FeatureCollector.RangeWithTags(0.75, 1, roundTrip(newLineString(0.75, 0, 1, 0)), Map.of()) ), feature.getLinearRangesAtZoom(14)); } + + @Test + void testSetAttrStruct() { + var collector = factory.get(newReaderFeature(newPoint(0, 0), Map.of())); + collector.point("layername") + .setAttr("a", Struct.of(1)) + .setAttrWithMinzoom("b", Struct.of(2d), 9) + .putAttrs(Map.of("c", Struct.of("3"), "d", ZoomFunction.minZoom(9, Struct.of(true)))); + var feature = collector.iterator().next(); + assertEquals(Map.of( + "a", 1, + "b", 2d, + "c", "3", + "d", true + ), feature.getAttrsAtZoom(14)); + } + + @Test + void testSetAttrPartial() { + var collector = factory.get(newReaderFeature(newLineString(0, 0, 1, 1), Map.of())); + collector.line("layername") + .linearRange(0, 0.5) + .setAttr("a", Struct.of(1)) + .setAttrWithMinzoom("b", Struct.of(2d), 9) + .putAttrs(Map.of("c", Struct.of("3"), "d", ZoomFunction.minZoom(9, Struct.of(true)))); + var feature = collector.iterator().next(); + var subFeature = feature.getLinearRangesAtZoom(14).getFirst(); + assertEquals(Map.of( + "a", 1, + "b", 2d, + "c", "3", + "d", true + ), subFeature.attrs()); + } }