From 88c07460782e14b7d6ce25ae46354be8159a3ea0 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 16 Jan 2024 15:31:21 +0800 Subject: [PATCH] add extraMetadata to profile interface [#794] --- .../com/onthegomap/planetiler/Profile.java | 3 +++ .../archive/TileArchiveMetadata.java | 12 ++++++++- .../archive/TileArchiveMetadataTest.java | 27 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/Profile.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/Profile.java index 8bb2977bbe..99a2db7ee1 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/Profile.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/Profile.java @@ -7,6 +7,7 @@ import com.onthegomap.planetiler.reader.osm.OsmRelationInfo; import com.onthegomap.planetiler.util.Wikidata; import java.util.List; +import java.util.Map; import java.util.function.Consumer; /** @@ -157,6 +158,8 @@ default boolean isOverlay() { return false; } + default Map extraMetadata() { return Map.of(); } + /** * Defines whether {@link Wikidata} should fetch wikidata translations for the input element. *

diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/archive/TileArchiveMetadata.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/archive/TileArchiveMetadata.java index 1c5c83a14f..81749e0e01 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/archive/TileArchiveMetadata.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/archive/TileArchiveMetadata.java @@ -17,6 +17,8 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Envelope; import org.slf4j.Logger; @@ -93,7 +95,7 @@ public TileArchiveMetadata(Profile profile, PlanetilerConfig config, List mergeMaps(Map m1, Map m2) { + return Stream.concat(m1.entrySet().stream(), m2.entrySet().stream()) + .collect(Collectors.toMap( + Map.Entry::getKey, + Map.Entry::getValue, + (value1, value2) -> value2)); + } + @JsonAnySetter private void putUnknownFieldsToOthers(String name, String value) { others.put(name, value); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/archive/TileArchiveMetadataTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/archive/TileArchiveMetadataTest.java index e672934b7d..7ba9e87dde 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/archive/TileArchiveMetadataTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/archive/TileArchiveMetadataTest.java @@ -6,11 +6,14 @@ import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.onthegomap.planetiler.FeatureCollector; import com.onthegomap.planetiler.Profile; import com.onthegomap.planetiler.TestUtils; +import com.onthegomap.planetiler.VectorTile; import com.onthegomap.planetiler.config.Arguments; import com.onthegomap.planetiler.config.PlanetilerConfig; import com.onthegomap.planetiler.geo.GeoUtils; +import com.onthegomap.planetiler.reader.SourceFeature; import com.onthegomap.planetiler.util.LayerAttrStats; import java.util.List; import java.util.Map; @@ -191,6 +194,30 @@ void testAddMetadataSmallBounds() { assertEquals(7, Math.ceil(metadata.zoom())); } + @Test + void testAddExtraMetadata() { + class TestingProfile extends Profile.NullProfile { + + @Override + public String name() { + return "My Name"; + } + + @Override + public Map extraMetadata() { + return Map.of("FooVersion","2.0"); + } + } + + var metadata = new TileArchiveMetadata(new TestingProfile(), PlanetilerConfig.from(Arguments.of(Map.of( + "bounds", "-73.6632,41.1274,-69.7598,43.0185" + )))); + + var map = new TreeMap<>(metadata.toMap()); + assertEquals("My Name", map.get("name")); + assertEquals("2.0", map.get("FooVersion")); + } + @Test void testToMap() throws JsonProcessingException { var bounds = "-73.6632,41.1274,-69.7598,43.0185";