From e9c004557352c84629f7d7df529e9599b14d18b9 Mon Sep 17 00:00:00 2001 From: zstadler Date: Sun, 29 Jun 2025 18:33:55 +0300 Subject: [PATCH] Use `min_length` to `setBufferPixels()` (#1277) --- .../custommap/ConfiguredFeature.java | 26 ++++++++++++++++--- .../custommap/ConfiguredProfile.java | 2 +- .../custommap/ConfiguredFeatureTest.java | 23 ++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredFeature.java b/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredFeature.java index ed239688..b1622fbb 100644 --- a/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredFeature.java +++ b/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredFeature.java @@ -5,6 +5,7 @@ import static com.onthegomap.planetiler.expression.Expression.not; import com.onthegomap.planetiler.FeatureCollector; import com.onthegomap.planetiler.FeatureCollector.Feature; +import com.onthegomap.planetiler.custommap.configschema.FeatureLayer; import com.onthegomap.planetiler.custommap.configschema.AttributeDefinition; import com.onthegomap.planetiler.custommap.configschema.FeatureGeometry; import com.onthegomap.planetiler.custommap.configschema.FeatureItem; @@ -20,6 +21,8 @@ import java.util.Objects; import java.util.Set; import java.util.function.BiConsumer; import java.util.function.Function; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * A map feature, configured from a YML configuration file. @@ -28,6 +31,8 @@ import java.util.function.Function; * and {@link #processFeature(Contexts.FeaturePostMatch, FeatureCollector)} processes matching elements. */ public class ConfiguredFeature { + private static final Logger LOGGER = LoggerFactory.getLogger(ConfiguredFeature.class); + private static final double LOG4 = Math.log(4); private final Expression geometryTest; private final Function geometryFactory; @@ -40,7 +45,7 @@ public class ConfiguredFeature { private ScriptEnvironment featurePostMatchContext; - public ConfiguredFeature(String layer, TagValueProducer tagValueProducer, FeatureItem feature, + public ConfiguredFeature(FeatureLayer layer, TagValueProducer tagValueProducer, FeatureItem feature, Contexts.Root rootContext) { sources = Set.copyOf(feature.source()); @@ -81,7 +86,7 @@ public class ConfiguredFeature { tagTest = filter; //Factory to generate the right feature type from FeatureCollector - geometryFactory = geometryType.newGeometryFactory(layer); + geometryFactory = geometryType.newGeometryFactory(layer.id()); //Configure logic for each attribute in the output tile List> processors = new ArrayList<>(); @@ -90,7 +95,22 @@ public class ConfiguredFeature { } processors.add(makeFeatureProcessor(feature.minZoom(), Integer.class, Feature::setMinZoom)); processors.add(makeFeatureProcessor(feature.maxZoom(), Integer.class, Feature::setMaxZoom)); - processors.add(makeFeatureProcessor(feature.minSize(), Double.class, Feature::setMinPixelSize)); + if (layer.postProcess() == null) { + processors.add(makeFeatureProcessor(feature.minSize(), Double.class, Feature::setMinPixelSize)); + } else { + processors.add(makeFeatureProcessor(0, Double.class, Feature::setMinPixelSize)); + if (feature.minSize() != null ) { + LOGGER.info("Ignored min_size settings in layer {} in favour of its tile_post_process settings", + layer.id()); + } + var merge = layer.postProcess().mergeLineStrings(); + if (merge != null) { + var minLength = merge.minLength(); + if (minLength > 4) { + processors.add(makeFeatureProcessor(minLength, Double.class, Feature::setBufferPixels)); + } + } + } featureProcessors = processors.stream().filter(Objects::nonNull).toList(); } diff --git a/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredProfile.java b/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredProfile.java index aa00ae9b..5e84c8ee 100644 --- a/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredProfile.java +++ b/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredProfile.java @@ -47,7 +47,7 @@ public class ConfiguredProfile implements Profile { String layerId = layer.id(); layersById.put(layerId, layer); for (var feature : layer.features()) { - var configuredFeature = new ConfiguredFeature(layerId, tagValueProducer, feature, rootContext); + var configuredFeature = new ConfiguredFeature(layer, tagValueProducer, feature, rootContext); var entry = new Entry<>(configuredFeature, configuredFeature.matchExpression()); configuredFeatureEntries.add(entry); } 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 e811b907..355e91aa 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 @@ -226,6 +226,29 @@ class ConfiguredFeatureTest { assertEquals(List.of(feature), profile.postProcessLayerFeatures("testLayer", 0, List.of(feature))); } + @Test + void testMergeLineStringMinLengthSetsBufferPixels() { + testLinestring(""" + sources: + osm: + type: osm + url: geofabrik:rhode-island + local_path: data/rhode-island.osm.pbf + layers: + - id: testLayer + features: + - source: osm + geometry: line + tile_post_process: + merge_line_strings: + min_length: 10 + tolerance: 5 + buffer: 4 + """, Map.of(), f -> { + assertEquals(10, f.getBufferPixelsAtZoom(14)); + }, 1); + } + @Test void testFeaturePostProcessorMergePolygons() throws GeometryException { var config = """