Skip to content

Commit

Permalink
Add tile post-process hook to forwarding profile (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored Feb 3, 2024
1 parent f97b5f9 commit 7c03592
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.onthegomap.planetiler;

import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.geo.TileCoord;
import com.onthegomap.planetiler.reader.SourceFeature;
import com.onthegomap.planetiler.reader.osm.OsmElement;
import com.onthegomap.planetiler.reader.osm.OsmRelationInfo;
Expand All @@ -20,7 +21,8 @@
* <li>{@link FeatureProcessor} to handle features from a particular source (added through
* {@link #registerSourceHandler(String, FeatureProcessor)})</li>
* <li>{@link FinishHandler} to be notified whenever we finish processing each source</li>
* <li>{@link FeaturePostProcessor} to post-process features in a layer before rendering the output tile</li>
* <li>{@link LayerPostProcesser} to post-process features in a layer before rendering the output tile</li>
* <li>{@link TilePostProcessor} to post-process features in a tile before rendering the output tile</li>
* </ul>
* See {@code OpenMapTilesProfile} for a full implementation using this framework.
*/
Expand All @@ -35,8 +37,10 @@ public abstract class ForwardingProfile implements Profile {
private final List<OsmRelationPreprocessor> osmRelationPreprocessors = new ArrayList<>();
/** Handlers that get a callback when each source is finished reading. */
private final List<FinishHandler> finishHandlers = new ArrayList<>();
/** Map from layer name to its handler if it implements {@link FeaturePostProcessor}. */
private final Map<String, List<FeaturePostProcessor>> postProcessors = new HashMap<>();
/** Map from layer name to its handler if it implements {@link LayerPostProcesser}. */
private final Map<String, List<LayerPostProcesser>> layerPostProcessors = new HashMap<>();
/** List of handlers that implement {@link TilePostProcessor}. */
private final List<TilePostProcessor> tilePostProcessors = new ArrayList<>();
/** Map from source ID to its handler if it implements {@link FeatureProcessor}. */
private final Map<String, List<FeatureProcessor>> sourceElementProcessors = new HashMap<>();

Expand All @@ -53,7 +57,7 @@ public void registerSourceHandler(String source, FeatureProcessor processor) {

/**
* Call {@code handler} for different events based on which interfaces {@code handler} implements:
* {@link OsmRelationPreprocessor}, {@link FinishHandler}, or {@link FeaturePostProcessor}.
* {@link OsmRelationPreprocessor}, {@link FinishHandler}, {@link TilePostProcessor} or {@link LayerPostProcesser}.
*/
public void registerHandler(Handler handler) {
this.handlers.add(handler);
Expand All @@ -69,10 +73,13 @@ public void registerHandler(Handler handler) {
if (handler instanceof FinishHandler finishHandler) {
finishHandlers.add(finishHandler);
}
if (handler instanceof FeaturePostProcessor postProcessor) {
postProcessors.computeIfAbsent(postProcessor.name(), name -> new ArrayList<>())
if (handler instanceof LayerPostProcesser postProcessor) {
layerPostProcessors.computeIfAbsent(postProcessor.name(), name -> new ArrayList<>())
.add(postProcessor);
}
if (handler instanceof TilePostProcessor postProcessor) {
tilePostProcessors.add(postProcessor);
}
}

@Override
Expand Down Expand Up @@ -129,10 +136,10 @@ public boolean caresAboutSource(String name) {
public List<VectorTile.Feature> postProcessLayerFeatures(String layer, int zoom, List<VectorTile.Feature> items)
throws GeometryException {
// delegate feature post-processing to each layer, if it implements FeaturePostProcessor
List<FeaturePostProcessor> handlers = postProcessors.get(layer);
List<LayerPostProcesser> postProcessers = layerPostProcessors.get(layer);
List<VectorTile.Feature> result = items;
if (handlers != null) {
for (FeaturePostProcessor handler : handlers) {
if (postProcessers != null) {
for (var handler : postProcessers) {
var thisResult = handler.postProcess(zoom, result);
if (thisResult != null) {
result = thisResult;
Expand All @@ -142,6 +149,20 @@ public List<VectorTile.Feature> postProcessLayerFeatures(String layer, int zoom,
return result;
}

@Override
public Map<String, List<VectorTile.Feature>> postProcessTileFeatures(TileCoord tileCoord,
Map<String, List<VectorTile.Feature>> layers) throws GeometryException {
var result = layers;
for (TilePostProcessor postProcessor : tilePostProcessors) {
// TODO catch failures to isolate from other tile postprocessors?
var thisResult = postProcessor.postProcessTile(tileCoord, result);
if (thisResult != null) {
result = thisResult;
}
}
return result;
}

@Override
public void finish(String sourceName, FeatureCollector.Factory featureCollectors,
Consumer<FeatureCollector.Feature> next) {
Expand Down Expand Up @@ -217,18 +238,38 @@ public interface OsmRelationPreprocessor extends Handler {
List<OsmRelationInfo> preprocessOsmRelation(OsmElement.Relation relation);
}

/** Handlers should implement this interface to post-process vector tile features before emitting an output tile. */
public interface FeaturePostProcessor extends HandlerForLayer {
/** Handlers should implement this interface to post-process vector tile features before emitting an output layer. */
public interface LayerPostProcesser extends HandlerForLayer {

/**
* Apply any post-processing to features in this output layer of a tile before writing it to the output file.
* Apply any post-processing to features in this output layer of a tile before writing it to the output archive.
*
* @throws GeometryException if the input elements cannot be deserialized, or output elements cannot be serialized
* @see Profile#postProcessLayerFeatures(String, int, List)
*/
List<VectorTile.Feature> postProcess(int zoom, List<VectorTile.Feature> items) throws GeometryException;
}

/** @deprecated use {@link LayerPostProcesser} or {@link TilePostProcessor} instead */
@Deprecated(forRemoval = true)
public interface FeaturePostProcessor extends LayerPostProcesser {}

/**
* Handlers should implement this interface to post-process all features in a vector tile before writing to an
* archive.
*/
public interface TilePostProcessor extends Handler {

/**
* Apply any post-processing to features in layers in this output tile before writing it to the output archive.
*
* @throws GeometryException if the input elements cannot be deserialized, or output elements cannot be serialized
* @see Profile#postProcessTileFeatures(TileCoord, Map)
*/
Map<String, List<VectorTile.Feature>> postProcessTile(TileCoord tileCoord,
Map<String, List<VectorTile.Feature>> layers) throws GeometryException;
}

/** Handlers should implement this interface to process input features from a given source ID. */
public interface FeatureProcessor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import com.onthegomap.planetiler.geo.GeoUtils;
import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.geo.TileCoord;
import com.onthegomap.planetiler.reader.SimpleFeature;
import com.onthegomap.planetiler.reader.SourceFeature;
import com.onthegomap.planetiler.reader.osm.OsmElement;
import com.onthegomap.planetiler.reader.osm.OsmRelationInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -130,7 +132,7 @@ void testFinishHandler() {
}

@Test
void testFeaturePostProcessor() throws GeometryException {
void testLayerPostProcesser() throws GeometryException {
VectorTile.Feature feature = new VectorTile.Feature(
"layer",
1,
Expand All @@ -140,7 +142,7 @@ void testFeaturePostProcessor() throws GeometryException {
assertEquals(List.of(feature), profile.postProcessLayerFeatures("layer", 0, List.of(feature)));

// ignore null response
profile.registerHandler(new ForwardingProfile.FeaturePostProcessor() {
profile.registerHandler(new ForwardingProfile.LayerPostProcesser() {
@Override
public List<VectorTile.Feature> postProcess(int zoom, List<VectorTile.Feature> items) {
return null;
Expand All @@ -154,7 +156,7 @@ public String name() {
assertEquals(List.of(feature), profile.postProcessLayerFeatures("a", 0, List.of(feature)));

// empty list removes
profile.registerHandler(new ForwardingProfile.FeaturePostProcessor() {
profile.registerHandler(new ForwardingProfile.LayerPostProcesser() {
@Override
public List<VectorTile.Feature> postProcess(int zoom, List<VectorTile.Feature> items) {
return List.of();
Expand All @@ -170,7 +172,7 @@ public String name() {
assertEquals(List.of(feature), profile.postProcessLayerFeatures("b", 0, List.of(feature)));

// 2 handlers for same layer run one after another
var skip1 = new ForwardingProfile.FeaturePostProcessor() {
var skip1 = new ForwardingProfile.LayerPostProcesser() {
@Override
public List<VectorTile.Feature> postProcess(int zoom, List<VectorTile.Feature> items) {
return items.stream().skip(1).toList();
Expand All @@ -183,7 +185,7 @@ public String name() {
};
profile.registerHandler(skip1);
profile.registerHandler(skip1);
profile.registerHandler(new ForwardingProfile.FeaturePostProcessor() {
profile.registerHandler(new ForwardingProfile.LayerPostProcesser() {
@Override
public List<VectorTile.Feature> postProcess(int zoom, List<VectorTile.Feature> items) {
return null; // ensure that returning null after initial post-processors run keeps the postprocessed result
Expand All @@ -199,4 +201,63 @@ public String name() {
assertEquals(List.of(feature, feature, feature, feature),
profile.postProcessLayerFeatures("c", 0, List.of(feature, feature, feature, feature)));
}

@Test
void testTilePostProcesser() throws GeometryException {
VectorTile.Feature feature = new VectorTile.Feature(
"layer",
1,
VectorTile.encodeGeometry(GeoUtils.point(0, 0)),
Map.of()
);
assertEquals(Map.of("layer", List.of(feature)), profile.postProcessTileFeatures(TileCoord.ofXYZ(0, 0, 0), Map.of(
"layer", List.of(feature)
)));

// ignore null response
profile.registerHandler((ForwardingProfile.TilePostProcessor) (tileCoord, layers) -> null);
assertEquals(Map.of("a", List.of(feature)),
profile.postProcessTileFeatures(TileCoord.ofXYZ(0, 0, 0), Map.of("a", List.of(feature))));

// empty map removes
profile.registerHandler((ForwardingProfile.TilePostProcessor) (tileCoord, layers) -> Map.of());
assertEquals(Map.of(),
profile.postProcessTileFeatures(TileCoord.ofXYZ(0, 0, 0), Map.of("a", List.of(feature))));
// also touches elements in another layer
assertEquals(Map.of(),
profile.postProcessTileFeatures(TileCoord.ofXYZ(0, 0, 0), Map.of("b", List.of(feature))));
}

@Test
void testStackedTilePostProcessors() throws GeometryException {
VectorTile.Feature feature = new VectorTile.Feature(
"layer",
1,
VectorTile.encodeGeometry(GeoUtils.point(0, 0)),
Map.of()
);
var skip1 = new ForwardingProfile.TilePostProcessor() {
@Override
public Map<String, List<VectorTile.Feature>> postProcessTile(TileCoord tileCoord,
Map<String, List<VectorTile.Feature>> layers) {
Map<String, List<VectorTile.Feature>> result = new HashMap<>();
for (var key : layers.keySet()) {
result.put(key, layers.get(key).stream().skip(1).toList());
}
return result;
}
};
profile.registerHandler(skip1);
profile.registerHandler(skip1);
profile.registerHandler((ForwardingProfile.TilePostProcessor) (tileCoord, layers) -> {
// ensure that returning null after initial post-processors run keeps the postprocessed result
return null;
});
assertEquals(Map.of("b", List.of(feature, feature)),
profile.postProcessTileFeatures(TileCoord.ofXYZ(0, 0, 0),
Map.of("b", List.of(feature, feature, feature, feature))));
assertEquals(Map.of("c", List.of(feature, feature)),
profile.postProcessTileFeatures(TileCoord.ofXYZ(0, 0, 0),
Map.of("c", List.of(feature, feature, feature, feature))));
}
}

0 comments on commit 7c03592

Please sign in to comment.