2022-06-07 21:34:21 +00:00
|
|
|
package com.onthegomap.planetiler.custommap;
|
|
|
|
|
|
|
|
import static com.onthegomap.planetiler.expression.MultiExpression.Entry;
|
2022-09-23 10:49:09 +00:00
|
|
|
import static java.util.Map.entry;
|
2022-06-07 21:34:21 +00:00
|
|
|
|
|
|
|
import com.onthegomap.planetiler.FeatureCollector;
|
|
|
|
import com.onthegomap.planetiler.Profile;
|
|
|
|
import com.onthegomap.planetiler.custommap.configschema.FeatureLayer;
|
|
|
|
import com.onthegomap.planetiler.custommap.configschema.SchemaConfig;
|
|
|
|
import com.onthegomap.planetiler.expression.MultiExpression;
|
|
|
|
import com.onthegomap.planetiler.expression.MultiExpression.Index;
|
|
|
|
import com.onthegomap.planetiler.reader.SourceFeature;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2022-09-23 10:49:09 +00:00
|
|
|
import java.util.HashMap;
|
2022-06-07 21:34:21 +00:00
|
|
|
import java.util.List;
|
2022-09-23 10:49:09 +00:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.stream.Collectors;
|
2022-06-07 21:34:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A profile configured from a yml file.
|
|
|
|
*/
|
|
|
|
public class ConfiguredProfile implements Profile {
|
|
|
|
|
|
|
|
private final SchemaConfig schemaConfig;
|
|
|
|
|
2022-09-23 10:49:09 +00:00
|
|
|
private final Map<String, Index<ConfiguredFeature>> featureLayerMatcher;
|
|
|
|
private final TagValueProducer tagValueProducer;
|
2022-06-07 21:34:21 +00:00
|
|
|
|
|
|
|
public ConfiguredProfile(SchemaConfig schemaConfig) {
|
|
|
|
this.schemaConfig = schemaConfig;
|
|
|
|
|
|
|
|
Collection<FeatureLayer> layers = schemaConfig.layers();
|
|
|
|
if (layers == null || layers.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("No layers defined");
|
|
|
|
}
|
|
|
|
|
2022-09-23 10:49:09 +00:00
|
|
|
tagValueProducer = new TagValueProducer(schemaConfig.inputMappings());
|
2022-06-07 21:34:21 +00:00
|
|
|
|
2022-09-23 10:49:09 +00:00
|
|
|
Map<String, List<MultiExpression.Entry<ConfiguredFeature>>> configuredFeatureEntries = new HashMap<>();
|
2022-06-07 21:34:21 +00:00
|
|
|
|
|
|
|
for (var layer : layers) {
|
2022-09-23 10:49:09 +00:00
|
|
|
String layerId = layer.id();
|
2022-06-07 21:34:21 +00:00
|
|
|
for (var feature : layer.features()) {
|
2022-09-23 10:49:09 +00:00
|
|
|
var configuredFeature = new ConfiguredFeature(layerId, tagValueProducer, feature);
|
|
|
|
var entry = new Entry<>(configuredFeature, configuredFeature.matchExpression());
|
|
|
|
for (var source : feature.source()) {
|
|
|
|
var list = configuredFeatureEntries.computeIfAbsent(source, s -> new ArrayList<>());
|
|
|
|
list.add(entry);
|
|
|
|
}
|
2022-06-07 21:34:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 10:49:09 +00:00
|
|
|
featureLayerMatcher = configuredFeatureEntries.entrySet().stream()
|
|
|
|
.map(entry -> entry(entry.getKey(), MultiExpression.of(entry.getValue()).index()))
|
|
|
|
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
|
2022-06-07 21:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String name() {
|
|
|
|
return schemaConfig.schemaName();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String attribution() {
|
|
|
|
return schemaConfig.attribution();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void processFeature(SourceFeature sourceFeature, FeatureCollector featureCollector) {
|
2022-09-23 10:49:09 +00:00
|
|
|
var context = new Contexts.ProcessFeature(sourceFeature, tagValueProducer);
|
|
|
|
var index = featureLayerMatcher.get(sourceFeature.getSource());
|
|
|
|
if (index != null) {
|
|
|
|
var matches = index.getMatchesWithTriggers(context);
|
|
|
|
for (var configuredFeature : matches) {
|
|
|
|
configuredFeature.match().processFeature(
|
|
|
|
context.createPostMatchContext(configuredFeature.keys()),
|
|
|
|
featureCollector
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 21:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String description() {
|
|
|
|
return schemaConfig.schemaDescription();
|
|
|
|
}
|
|
|
|
}
|