Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expression issues #1030

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion planetiler-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
<geotools.version>32.0</geotools.version>
<log4j.version>2.24.0</log4j.version>
<prometheus.version>0.16.0</prometheus.version>
<protobuf.version>4.28.2</protobuf.version>
<!-- needs to match CEL -->
<protobuf.version>4.28.1</protobuf.version>
<geopackage.version>6.6.5</geopackage.version>
</properties>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.api.expr.v1alpha1.Constant;
import com.google.api.expr.v1alpha1.Decl;
import com.google.api.expr.v1alpha1.Type;
import com.google.common.collect.ForwardingMap;
import com.google.protobuf.NullValue;
import com.onthegomap.planetiler.config.Arguments;
import com.onthegomap.planetiler.config.PlanetilerConfig;
Expand Down Expand Up @@ -370,7 +371,7 @@ public static ScriptEnvironment<ProcessFeature> description(Root root) {
public Object apply(String key) {
if (key != null) {
return switch (key) {
case FEATURE_TAGS -> tagValueProducer.mapTags(feature);
case FEATURE_TAGS -> mapWithDefault(tagValueProducer.mapTags(feature), NullValue.NULL_VALUE);
case FEATURE_ID -> feature.id();
case FEATURE_SOURCE -> feature.getSource();
case FEATURE_SOURCE_LAYER -> wrapNullable(feature.getSourceLayer());
Expand All @@ -395,6 +396,20 @@ public Object apply(String key) {
}
}

private static <K, V> Map<K, V> mapWithDefault(Map<K, V> map, Object nullValue) {
return new ForwardingMap<>() {
@Override
protected Map<K, V> delegate() {
return map;
}

@Override
public V get(Object key) {
return map.getOrDefault(key, (V) nullValue);
}
};
}

public FeaturePostMatch createPostMatchContext(List<String> matchKeys) {
return new FeaturePostMatch(this, matchKeys);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1287,4 +1287,38 @@ void testLineCentroid(String type) {
assertInstanceOf(Puntal.class, feature.getGeometry());
}, 1);
}

@Test
void testWikidataParse() {
var config = """
sources:
osm:
type: osm
url: geofabrik:rhode-island
local_path: data/rhode-island.osm.pbf
layers:
- id: testLayer
features:
- source: osm
geometry: point
attributes:
- key: wikidata
value: "${feature.tags.wikidata != null ? int(feature.tags.wikidata.replace('Q', '')) : 0}"
""";
this.planetilerConfig = PlanetilerConfig.from(Arguments.of(Map.of()));
testPoint(config, Map.of(
"wikidata", "Q235"
), feature -> {
assertEquals(Map.of("wikidata", 235L), feature.getAttrsAtZoom(14));
}, 1);
testPoint(config, Map.of(
"wikidata", "235"
), feature -> {
assertEquals(Map.of("wikidata", 235L), feature.getAttrsAtZoom(14));
}, 1);
testPoint(config, Map.of(
), feature -> {
assertEquals(Map.of("wikidata", 0L), feature.getAttrsAtZoom(14));
}, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ExpressionTests {
"{'a': 2}.has('a', 1, 2)|true|boolean",
"{'a': 2}.has('a', 3)|false|boolean",
"{'a': 1}.has('b')|false|boolean",
"int({'tags': {'wikidata': 'Q1'}}.tags.wikidata.replace('Q', ''))|1|long",

"coalesce({'a': 1}.get('a'), 2)|1|long",
"coalesce({'a': 1}.get('b'), 2)|2|long",
Expand Down
Loading