Skip to content

Commit

Permalink
Make sure feature collector can handle structs (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored May 30, 2024
1 parent 21f061e commit 2cc1e2b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.geo.GeometryType;
import com.onthegomap.planetiler.reader.SourceFeature;
import com.onthegomap.planetiler.reader.Struct;
import com.onthegomap.planetiler.render.FeatureRenderer;
import com.onthegomap.planetiler.stats.Stats;
import com.onthegomap.planetiler.util.CacheByZoom;
Expand Down Expand Up @@ -807,17 +808,24 @@ public Feature setPointLabelGridSizeAndLimit(int maxzoom, double size, int limit
private Map<String, Object> computeAttrsAtZoom(int zoom) {
Map<String, Object> result = new TreeMap<>();
for (var entry : attrs.entrySet()) {
Object value = entry.getValue();
if (value instanceof ZoomFunction<?> fn) {
value = fn.apply(zoom);
}
Object value = unwrap(entry.getValue(), zoom);
if (value != null && !"".equals(value)) {
result.put(entry.getKey(), value);
}
}
return result;
}

private static Object unwrap(Object object, int zoom) {
if (object instanceof ZoomFunction<?> fn) {
object = fn.apply(zoom);
}
if (object instanceof Struct struct) {
object = struct.rawValue();
}
return object;
}

/** Returns the attribute to put on all output vector tile features at a zoom level. */
public Map<String, Object> getAttrsAtZoom(int zoom) {
if (!attrsChangeByZoom) {
Expand Down Expand Up @@ -952,8 +960,7 @@ Partial withAttr(String key, Object value) {
MergingRangeMap<Partial> result = MergingRangeMap.unit(new Partial(false, attrs), Partial::merge);
for (var override : partialOverrides) {
result.update(override.range(), m -> switch (override) {
case Attr attr ->
m.withAttr(attr.key, attr.value instanceof ZoomFunction<?> fn ? fn.apply(zoom) : attr.value);
case Attr attr -> m.withAttr(attr.key, unwrap(attr.value, zoom));
case Maxzoom mz -> m.withOmit(mz.maxzoom < zoom);
case Minzoom mz -> m.withOmit(mz.minzoom > zoom);
case Omit ignored -> m.withOmit(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.geo.GeometryType;
import com.onthegomap.planetiler.reader.SimpleFeature;
import com.onthegomap.planetiler.reader.Struct;
import com.onthegomap.planetiler.stats.Stats;
import com.onthegomap.planetiler.util.ZoomFunction;
import java.util.Arrays;
Expand Down Expand Up @@ -748,4 +749,38 @@ void testPartialOmit() {
new FeatureCollector.RangeWithTags(0.75, 1, roundTrip(newLineString(0.75, 0, 1, 0)), Map.of())
), feature.getLinearRangesAtZoom(14));
}

@Test
void testSetAttrStruct() {
var collector = factory.get(newReaderFeature(newPoint(0, 0), Map.of()));
collector.point("layername")
.setAttr("a", Struct.of(1))
.setAttrWithMinzoom("b", Struct.of(2d), 9)
.putAttrs(Map.of("c", Struct.of("3"), "d", ZoomFunction.minZoom(9, Struct.of(true))));
var feature = collector.iterator().next();
assertEquals(Map.of(
"a", 1,
"b", 2d,
"c", "3",
"d", true
), feature.getAttrsAtZoom(14));
}

@Test
void testSetAttrPartial() {
var collector = factory.get(newReaderFeature(newLineString(0, 0, 1, 1), Map.of()));
collector.line("layername")
.linearRange(0, 0.5)
.setAttr("a", Struct.of(1))
.setAttrWithMinzoom("b", Struct.of(2d), 9)
.putAttrs(Map.of("c", Struct.of("3"), "d", ZoomFunction.minZoom(9, Struct.of(true))));
var feature = collector.iterator().next();
var subFeature = feature.getLinearRangesAtZoom(14).getFirst();
assertEquals(Map.of(
"a", 1,
"b", 2d,
"c", "3",
"d", true
), subFeature.attrs());
}
}

0 comments on commit 2cc1e2b

Please sign in to comment.