Skip to content

Commit

Permalink
Make VectorTile.Feature implement WithTags (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry authored May 26, 2024
1 parent eca7e53 commit f8e64a4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static List<VectorTile.Feature> mergeLineStrings(List<VectorTile.Feature>
var groupedByAttrs = groupByAttrs(features, result, GeometryType.LINE);
for (List<VectorTile.Feature> groupedFeatures : groupedByAttrs) {
VectorTile.Feature feature1 = groupedFeatures.getFirst();
double lengthLimit = lengthLimitCalculator.apply(feature1.attrs());
double lengthLimit = lengthLimitCalculator.apply(feature1.tags());

// as a shortcut, can skip line merging only if:
// - only 1 element in the group
Expand Down Expand Up @@ -401,7 +401,7 @@ public static Collection<List<VectorTile.Feature>> groupByAttrs(
others.add(feature);
} else {
groupedByAttrs
.computeIfAbsent(feature.attrs(), k -> new ArrayList<>())
.computeIfAbsent(feature.tags(), k -> new ArrayList<>())
.add(feature);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.geo.GeometryType;
import com.onthegomap.planetiler.geo.MutableCoordinateSequence;
import com.onthegomap.planetiler.reader.WithTags;
import com.onthegomap.planetiler.util.Hilbert;
import com.onthegomap.planetiler.util.LayerAttrStats;
import java.util.ArrayList;
Expand Down Expand Up @@ -484,7 +485,7 @@ public VectorTile addLayerFeatures(String layerName, List<Feature> features) {
if (inFeature != null && inFeature.geometry().commands().length > 0) {
EncodedFeature outFeature = new EncodedFeature(inFeature);

for (Map.Entry<String, ?> e : inFeature.attrs().entrySet()) {
for (Map.Entry<String, ?> e : inFeature.tags().entrySet()) {
// skip attribute without value
if (e.getValue() != null) {
String key = e.getKey();
Expand Down Expand Up @@ -1010,7 +1011,7 @@ public CoordinateXY firstCoordinate() {
* @param layer the layer the feature was in
* @param id the feature ID
* @param geometry the encoded feature geometry (decode using {@link VectorGeometry#decode()})
* @param attrs tags for the feature to output
* @param tags tags for the feature to output
* @param group grouping key used to limit point density or {@link #NO_GROUP} if not in a group. NOTE: this is only
* populated when this feature was deserialized from {@link FeatureGroup}, not when parsed from a tile
* since vector tile schema does not encode group.
Expand All @@ -1019,9 +1020,9 @@ public record Feature(
String layer,
long id,
VectorGeometry geometry,
Map<String, Object> attrs,
Map<String, Object> tags,
long group
) {
) implements WithTags {

public static final long NO_GROUP = Long.MIN_VALUE;

Expand Down Expand Up @@ -1054,7 +1055,7 @@ public Feature copyWithNewGeometry(VectorGeometry newGeometry) {
layer,
id,
newGeometry,
attrs,
tags,
group
);
}
Expand All @@ -1065,11 +1066,17 @@ public Feature copyWithExtraAttrs(Map<String, Object> extraAttrs) {
layer,
id,
geometry,
Stream.concat(attrs.entrySet().stream(), extraAttrs.entrySet().stream())
Stream.concat(tags.entrySet().stream(), extraAttrs.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)),
group
);
}

/** @deprecated use {@link #tags()} instead. */
@Deprecated(forRemoval = true)
public Map<String, Object> attrs() {
return tags;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private byte[] encodeValue(VectorTile.Feature vectorTileFeature, RenderedFeature
}
packer.packLong(vectorTileFeature.id());
packer.packByte(encodeGeomTypeAndScale(vectorTileFeature.geometry()));
var attrs = vectorTileFeature.attrs();
var attrs = vectorTileFeature.tags();
packer.packMapHeader((int) attrs.values().stream().filter(Objects::nonNull).count());
for (Map.Entry<String, Object> entry : attrs.entrySet()) {
Object value = entry.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private static long getTilesCount(Mbtiles db) throws SQLException {
* <li>{@link VectorTile.Feature#id()} won't be compared
* <li>{@link VectorTile.Feature#layer()} will be compared
* <li>{@link VectorTile.Feature#geometry()} gets normalized for comparing
* <li>{@link VectorTile.Feature#attrs()} will be compared except for the rank attribute since the value produced by
* <li>{@link VectorTile.Feature#tags()} will be compared except for the rank attribute since the value produced by
* planetiler is not stable and differs on every run (at least for parks)
* <li>{@link VectorTile.Feature#group()} will be compared
* </ul>
Expand All @@ -129,7 +129,7 @@ private record VectorTileFeatureForCmp(
) {
static VectorTileFeatureForCmp fromActualFeature(VectorTile.Feature f) {
try {
var attrs = new HashMap<>(f.attrs());
var attrs = new HashMap<>(f.tags());
attrs.remove("rank");
return new VectorTileFeatureForCmp(f.layer(), f.geometry().decode().norm(), attrs, f.group());
} catch (GeometryException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static int getNumFeatures(Mbtiles db, String layer, int zoom, Map<String,
if (tileCoord.z() == zoom) {
byte[] data = db.getTile(tileCoord);
for (var feature : decode(data)) {
if (layer.equals(feature.layer()) && feature.attrs().entrySet().containsAll(attrs.entrySet())) {
if (layer.equals(feature.layer()) && feature.tags().entrySet().containsAll(attrs.entrySet())) {
Geometry geometry = feature.geometry().decode();
num += getGeometryCounts(geometry, clazz);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public static Map<TileCoord, List<ComparableFeature>> getTileMap(ReadableTileArc
case UNKNOWN -> throw new IllegalArgumentException("cannot decompress \"UNKNOWN\"");
};
var decoded = VectorTile.decode(bytes).stream()
.map(feature -> feature(decodeSilently(feature.geometry()), feature.layer(), feature.attrs())).toList();
.map(feature -> feature(decodeSilently(feature.geometry()), feature.layer(), feature.tags())).toList();
tiles.put(tile.coord(), decoded);
}
return tiles;
Expand Down Expand Up @@ -738,7 +738,7 @@ public static void assertFeatureNear(Mbtiles db, String layer, Map<String, Objec
if (feature.geometry().decode().isWithinDistance(tilePoint, 2)) {
containedInLayers.add(feature.layer());
if (layer.equals(feature.layer())) {
Map<String, Object> tags = feature.attrs();
Map<String, Object> tags = feature.tags();
containedInLayerFeatures.add(tags.toString());
if (tags.entrySet().containsAll(attrs.entrySet())) {
// found a match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void testAttributeTypes() {

List<VectorTile.Feature> decoded = VectorTile.decode(encoded);
assertEquals(1, decoded.size());
Map<String, Object> decodedAttributes = decoded.getFirst().attrs();
Map<String, Object> decodedAttributes = decoded.getFirst().tags();
assertEquals("value1", decodedAttributes.get("key1"));
assertEquals(123L, decodedAttributes.get("key2"));
assertEquals(234.1f, decodedAttributes.get("key3"));
Expand Down Expand Up @@ -330,13 +330,13 @@ void testMultipleFeaturesMultipleLayer() {
)).encode();

List<VectorTile.Feature> decoded = VectorTile.decode(encoded);
assertEquals(attrs1, decoded.get(0).attrs());
assertEquals(attrs1, decoded.get(0).tags());
assertEquals("layer1", decoded.get(0).layer());

assertEquals(attrs2, decoded.get(1).attrs());
assertEquals(attrs2, decoded.get(1).tags());
assertEquals("layer1", decoded.get(1).layer());

assertEquals(attrs1, decoded.get(2).attrs());
assertEquals(attrs1, decoded.get(2).tags());
assertEquals("layer2", decoded.get(2).layer());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private Map<Integer, Map<String, List<Feature>>> getFeatures() {
for (var feature : VectorTile.decode(tile.getVectorTile().encode())) {
map.computeIfAbsent(tile.tileCoord().encoded(), (i) -> new TreeMap<>())
.computeIfAbsent(feature.layer(), l -> new ArrayList<>())
.add(new Feature(feature.attrs(), decodeSilently(feature.geometry())));
.add(new Feature(feature.tags(), decodeSilently(feature.geometry())));
}
}
return map;
Expand All @@ -109,7 +109,7 @@ private Map<Integer, Map<String, List<Feature>>> getFeaturesParallel() {
for (var feature : VectorTile.decode(tile.getVectorTile().encode())) {
map.computeIfAbsent(tile.tileCoord().encoded(), (i) -> new TreeMap<>())
.computeIfAbsent(feature.layer(), l -> new ArrayList<>())
.add(new Feature(feature.attrs(), decodeSilently(feature.geometry())));
.add(new Feature(feature.tags(), decodeSilently(feature.geometry())));
}
}
return map;
Expand Down

0 comments on commit f8e64a4

Please sign in to comment.