Skip to content

Commit

Permalink
Encode a constant layer ID as part of feature IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Feb 25, 2025
1 parent 8f7f53f commit 3c8cde6
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 18 deletions.
14 changes: 11 additions & 3 deletions tiles/src/main/java/com/protomaps/basemap/feature/FeatureId.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ public class FeatureId {

private FeatureId() {}

public static long create(SourceFeature sf) {
// uses 44 bits for the Feature ID,
// 2 bits for class (OSM node/way/relation or Not OSM)
// 6 bits for the layer id

public static long encode(int layerId) {
return (layerId << 46);
}

public static long encode(int layerId, SourceFeature sf) {
if (sf instanceof OsmSourceFeature osmFeature) {
long elemType;
var element = osmFeature.originalElement();
Expand All @@ -19,8 +27,8 @@ public static long create(SourceFeature sf) {
} else {
elemType = 0x1;
}
return (elemType << 44) | element.id();
return (layerId << 46) | (elemType << 44) | element.id();
}
return sf.id();
return (layerId << 46) | sf.id();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
public class Boundaries implements ForwardingProfile.OsmRelationPreprocessor,
ForwardingProfile.LayerPostProcesser {

final int LAYER_ID = 0;

@Override
public String name() {
return "boundaries";
Expand Down Expand Up @@ -214,7 +216,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {

if (!kind.isEmpty()) {
var line = features.line(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
// Core Tilezen schema properties
.setAttr("kind", kind)
.setAttr("kind_detail", minAdminLevel.getAsInt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class Buildings implements ForwardingProfile.LayerPostProcesser {
static final String HEIGHT_KEY = "height";
static final String MIN_HEIGHT_KEY = "min_height";

final int LAYER_ID = 1;

@Override
public String name() {
return "buildings";
Expand Down Expand Up @@ -78,7 +80,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

var feature = features.polygon(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
// Core Tilezen schema properties
.setAttr("kind", kind)
// Core OSM tags for different kinds of places
Expand Down Expand Up @@ -108,7 +110,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}
if (housenumberFeature != null) {
housenumberFeature
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("addr_housenumber", sf.getString("addr:housenumber"))
.setAttr("addr_street", sf.getString("addr:street"))
.setAttr("kind", "address")
Expand Down
6 changes: 4 additions & 2 deletions tiles/src/main/java/com/protomaps/basemap/layers/Earth.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.locationtech.jts.geom.Point;

public class Earth implements ForwardingProfile.LayerPostProcesser {

final int LAYER_ID = 2;
@Override
public String name() {
return "earth";
Expand All @@ -20,7 +22,7 @@ public String name() {
public void processPreparedOsm(SourceFeature ignoredSf, FeatureCollector features) {
features.polygon(this.name())
.setAttr("kind", "earth")
.setId(0)
.setId(FeatureId.encode(LAYER_ID))
.setZoomRange(6, 15).setBufferPixels(8);
}

Expand Down Expand Up @@ -54,7 +56,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
if (sf.canBeLine() && !sf.canBePolygon() && sf.hasTag("natural", "cliff")) {
int minZoom = 12;
var feat = features.line(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("min_zoom", minZoom + 1)
.setAttr("kind", "cliff")
.setZoomRange(minZoom, 15);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Map;

public class Landcover implements ForwardingProfile.LayerPostProcesser {
final int LAYER_ID = 3;

static final Map<String, String> kindMapping = Map.of("urban", "urban_area", "crop", "farmland", "grass", "grassland",
"trees", "forest", "snow", "glacier", "shrub", "scrub");
Expand All @@ -25,7 +26,7 @@ public void processLandcover(SourceFeature sf, FeatureCollector features) {
Integer sortKey = sortKeyMapping.getOrDefault(daylightClass, 6);

features.polygon(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("kind", kind)
.setZoomRange(0, 7)
.setSortKey(sortKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

public class Landuse implements ForwardingProfile.LayerPostProcesser {
final int LAYER_ID = 4;

public void processOsm(SourceFeature sf, FeatureCollector features) {
if (sf.canBePolygon() && (sf.hasTag("aeroway", "aerodrome", "runway") ||
Expand Down Expand Up @@ -151,7 +152,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

features.polygon(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
// Core Tilezen schema properties
.setAttr("kind", kind)
.setAttr("sort_rank", 189)
Expand Down
3 changes: 2 additions & 1 deletion tiles/src/main/java/com/protomaps/basemap/layers/Places.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.atomic.AtomicInteger;

public class Places implements ForwardingProfile.LayerPostProcesser {
final int LAYER_ID = 5;

private NaturalEarthDb naturalEarthDb;

Expand Down Expand Up @@ -266,7 +267,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

var feat = features.point(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
// Core Tilezen schema properties
.setAttr("kind", kind)
.setAttr("kind_detail", place)
Expand Down
5 changes: 3 additions & 2 deletions tiles/src/main/java/com/protomaps/basemap/layers/Pois.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;

public class Pois implements ForwardingProfile.LayerPostProcesser {
final int LAYER_ID = 6;

private QrankDb qrankDb;

Expand Down Expand Up @@ -443,7 +444,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
var polyLabelPosition = features.pointOnSurface(this.name())
// all POIs should receive their IDs at all zooms
// (there is no merging of POIs like with lines and polygons in other layers)
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
// Core Tilezen schema properties
.setAttr("kind", kind)
// While other layers don't need min_zoom, POIs do for more predictable client-side label collisions
Expand Down Expand Up @@ -485,7 +486,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
var pointFeature = features.point(this.name())
// all POIs should receive their IDs at all zooms
// (there is no merging of POIs like with lines and polygons in other layers)
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
// Core Tilezen schema properties
.setAttr("kind", kind)
// While other layers don't need min_zoom, POIs do for more predictable client-side label collisions
Expand Down
5 changes: 3 additions & 2 deletions tiles/src/main/java/com/protomaps/basemap/layers/Roads.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.*;

public class Roads implements ForwardingProfile.LayerPostProcessor, ForwardingProfile.OsmRelationPreprocessor {
final int LAYER_ID = 7;

private CountryCoder countryCoder;

Expand Down Expand Up @@ -190,7 +191,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

var feat = features.line("roads")
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("kind", kind)
// To power better client label collisions
.setAttr("min_zoom", minZoom + 1)
Expand Down Expand Up @@ -294,7 +295,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

var feature = features.line(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("kind", kind)
// Used for client-side label collisions
.setAttr("min_zoom", minZoom + 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;

public class Transit implements ForwardingProfile.LayerPostProcesser {
final int LAYER_ID = 8;

@Override
public String name() {
Expand Down
7 changes: 4 additions & 3 deletions tiles/src/main/java/com/protomaps/basemap/layers/Water.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;

public class Water implements ForwardingProfile.LayerPostProcesser {
final int LAYER_ID = 9;

private static final double WORLD_AREA_FOR_70K_SQUARE_METERS =
Math.pow(GeoUtils.metersToPixelAtEquator(0, Math.sqrt(70_000)) / 256d, 2);
Expand All @@ -25,7 +26,7 @@ public String name() {

public void processPreparedOsm(SourceFeature ignoredSf, FeatureCollector features) {
features.polygon(this.name())
.setId(0)
.setId(FeatureId.encode(LAYER_ID))
.setAttr("kind", "ocean")
.setAttr("sort_rank", 200)
.setZoomRange(6, 15).setBufferPixels(8);
Expand Down Expand Up @@ -200,7 +201,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

var feat = features.line(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("kind", kind)
// Used for client-side label collisions
.setAttr("min_zoom", minZoom + 1)
Expand Down Expand Up @@ -249,7 +250,7 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
}

var feat = features.point(this.name())
.setId(FeatureId.create(sf))
.setId(FeatureId.encode(LAYER_ID, sf))
.setAttr("kind", kind)
// Used for client-side label collisions
.setAttr("min_zoom", minZoom + 1)
Expand Down

0 comments on commit 3c8cde6

Please # to comment.