diff --git a/tiles/src/main/java/com/protomaps/basemap/feature/Matcher.java b/tiles/src/main/java/com/protomaps/basemap/feature/Matcher.java index f162892e..b3de3e9a 100644 --- a/tiles/src/main/java/com/protomaps/basemap/feature/Matcher.java +++ b/tiles/src/main/java/com/protomaps/basemap/feature/Matcher.java @@ -55,7 +55,7 @@ public static String getString(SourceFeature sf, List> match if (value instanceof String stringValue) { return stringValue; } else if (value instanceof FromTag fromTag) { - return sf.getString(fromTag.key); + return sf.getString(fromTag.key, defaultValue); } else { return defaultValue; } @@ -72,7 +72,31 @@ public static Integer getInteger(SourceFeature sf, List> mat if (value instanceof Integer integerValue) { return integerValue; } else if (value instanceof FromTag fromTag) { - return Integer.valueOf(sf.getString(fromTag.key, String.valueOf(defaultValue))); + try { + return sf.hasTag(fromTag.key) ? Integer.valueOf(sf.getString(fromTag.key)) : defaultValue; + } catch (NumberFormatException e) { + return defaultValue; + } + } else { + return defaultValue; + } + } + } + return defaultValue; + } + + public static Double getDouble(SourceFeature sf, List> matches, String key, Double defaultValue) { + for (var match : matches.reversed()) { + if (match.containsKey(key)) { + Object value = match.get(key); + if (value instanceof Double doubleValue) { + return doubleValue; + } else if (value instanceof FromTag fromTag) { + try { + return sf.hasTag(fromTag.key) ? Double.valueOf(sf.getString(fromTag.key)) : defaultValue; + } catch (NumberFormatException e) { + return defaultValue; + } } else { return defaultValue; } diff --git a/tiles/src/main/java/com/protomaps/basemap/layers/Landuse.java b/tiles/src/main/java/com/protomaps/basemap/layers/Landuse.java index ca953783..e0a891b5 100644 --- a/tiles/src/main/java/com/protomaps/basemap/layers/Landuse.java +++ b/tiles/src/main/java/com/protomaps/basemap/layers/Landuse.java @@ -1,8 +1,8 @@ package com.protomaps.basemap.layers; -import static com.protomaps.basemap.feature.Matcher.rule; import static com.protomaps.basemap.feature.Matcher.fromTag; import static com.protomaps.basemap.feature.Matcher.getString; +import static com.protomaps.basemap.feature.Matcher.rule; import static com.protomaps.basemap.feature.Matcher.use; import static com.protomaps.basemap.feature.Matcher.with; import static com.protomaps.basemap.feature.Matcher.without; @@ -62,7 +62,8 @@ public class Landuse implements ForwardingProfile.LayerPostProcessor { use("kind", fromTag("military")) ), rule( - with("leisure", "golf_course", "marina", "park", "stadium", "playground", "garden", "dog_park", "pitch", "nature_reserve"), + with("leisure", "golf_course", "marina", "park", "stadium", "playground", "garden", "dog_park", "pitch", + "nature_reserve"), use("kind", fromTag("leisure")) ), rule( diff --git a/tiles/src/test/java/com/protomaps/basemap/feature/MatcherTest.java b/tiles/src/test/java/com/protomaps/basemap/feature/MatcherTest.java index 05e7b895..224ca35b 100644 --- a/tiles/src/test/java/com/protomaps/basemap/feature/MatcherTest.java +++ b/tiles/src/test/java/com/protomaps/basemap/feature/MatcherTest.java @@ -1,15 +1,15 @@ package com.protomaps.basemap.feature; import static com.onthegomap.planetiler.TestUtils.newPoint; +import static com.protomaps.basemap.feature.Matcher.fromTag; +import static com.protomaps.basemap.feature.Matcher.getBoolean; +import static com.protomaps.basemap.feature.Matcher.getDouble; +import static com.protomaps.basemap.feature.Matcher.getInteger; +import static com.protomaps.basemap.feature.Matcher.getString; import static com.protomaps.basemap.feature.Matcher.rule; import static com.protomaps.basemap.feature.Matcher.use; import static com.protomaps.basemap.feature.Matcher.with; import static com.protomaps.basemap.feature.Matcher.without; -import static com.protomaps.basemap.feature.Matcher.getString; -import static com.protomaps.basemap.feature.Matcher.getInteger; -import static com.protomaps.basemap.feature.Matcher.getDouble; -import static com.protomaps.basemap.feature.Matcher.getBoolean; -import static com.protomaps.basemap.feature.Matcher.fromTag; import static org.junit.jupiter.api.Assertions.assertEquals; import com.onthegomap.planetiler.expression.Expression; @@ -115,7 +115,7 @@ void testGetStringWithA() { ); var matches = index.getMatches(sf); assertEquals("d", getString(sf, matches, "b", "d")); - + sf = SimpleFeature.create( newPoint(0, 0), Map.of("a", "something"), @@ -522,5 +522,5 @@ void testGetBooleanFromTag() { matches = index.getMatches(sf); assertEquals(true, getBoolean(sf, matches, "a", false)); } - + }