Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add country-coder json [#44] #328

Merged
merged 12 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions tiles/src/main/java/com/protomaps/basemap/Basemap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.onthegomap.planetiler.Planetiler;
import com.onthegomap.planetiler.config.Arguments;
import com.onthegomap.planetiler.util.Downloader;
import com.protomaps.basemap.feature.CountryCoder;
import com.protomaps.basemap.feature.NaturalEarthDb;
import com.protomaps.basemap.feature.QrankDb;
import com.protomaps.basemap.layers.Boundaries;
Expand All @@ -18,6 +19,7 @@
import com.protomaps.basemap.layers.Water;
import com.protomaps.basemap.postprocess.Clip;
import com.protomaps.basemap.text.FontRegistry;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
Expand All @@ -27,7 +29,7 @@

public class Basemap extends ForwardingProfile {

public Basemap(NaturalEarthDb naturalEarthDb, QrankDb qrankDb, Clip clip) {
public Basemap(NaturalEarthDb naturalEarthDb, QrankDb qrankDb, CountryCoder countryCoder, Clip clip) {

var admin = new Boundaries();
registerHandler(admin);
Expand All @@ -54,7 +56,7 @@ public Basemap(NaturalEarthDb naturalEarthDb, QrankDb qrankDb, Clip clip) {
registerHandler(poi);
registerSourceHandler("osm", poi::processOsm);

var roads = new Roads();
var roads = new Roads(countryCoder);
registerHandler(roads);
registerSourceHandler("osm", roads::processOsm);

Expand Down Expand Up @@ -122,11 +124,11 @@ public Map<String, String> extraArchiveMetadata() {
return result;
}

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
run(Arguments.fromArgsOrConfigFile(args));
}

static void run(Arguments args) {
static void run(Arguments args) throws IOException {
args = args.orElse(Arguments.of("maxzoom", 15));

Path dataDir = Path.of("data");
Expand All @@ -135,6 +137,8 @@ static void run(Arguments args) {
Path nePath = sourcesDir.resolve("natural_earth_vector.sqlite.zip");
String neUrl = "https://naciscdn.org/naturalearth/packages/natural_earth_vector.sqlite.zip";

var countryCoder = CountryCoder.fromJarResource();

String area = args.getString("area", "geofabrik area to download", "monaco");

var planetiler = Planetiler.create(args)
Expand Down Expand Up @@ -171,7 +175,8 @@ static void run(Arguments args) {

fontRegistry.loadFontBundle("NotoSansDevanagari-Regular", "1", "Devanagari");

planetiler.setProfile(new Basemap(naturalEarthDb, qrankDb, clip)).setOutput(Path.of(area + ".pmtiles"))
planetiler.setProfile(new Basemap(naturalEarthDb, qrankDb, countryCoder, clip))
.setOutput(Path.of(area + ".pmtiles"))
.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.protomaps.basemap.feature;

import com.onthegomap.planetiler.reader.geojson.GeoJson;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.index.strtree.STRtree;

public class CountryCoder {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need to enhance that to handle (Polygon contains LineString) - looks like Contains only supports Point and Intersects works for all geometries? TBD if it actually matters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, it seems like country coder should return the country that feature is "most within" so we could sample a few points? Here's the api that prepared geometry exposes when testing geometries: https://locationtech.github.io/jts/javadoc/org/locationtech/jts/geom/prep/PreparedGeometry.html


public record Record(String country, String nameEn, MultiPolygon multiPolygon) {}

private STRtree tree;

public CountryCoder(STRtree tree) {
this.tree = tree;
}

public static CountryCoder fromJarResource() throws IOException {
InputStream inputStream = CountryCoder.class.getResourceAsStream("/borders.json");

String jsonContent = new String(inputStream.readAllBytes());
return fromJsonString(jsonContent);
}

public static CountryCoder fromJsonString(String s) {
STRtree tree = new STRtree();

var g = GeoJson.from(s);

for (var feature : g) {
var properties = feature.tags();

String country = "";
if (properties.containsKey("iso1A2")) {
country = properties.get("iso1A2").toString();
} else if (properties.containsKey("country")) {
country = properties.get("country").toString();
}

if (country.isBlank() || feature.geometry().getNumGeometries() == 0) {
continue;
}
MultiPolygon mp = (MultiPolygon) feature.geometry();
tree.insert(mp.getEnvelopeInternal(),
new Record(country, properties.get("nameEn").toString(), mp));
}
return new CountryCoder(tree);
}

public Optional<String> getCountryCode(Geometry geom) {
List<Record> results = tree.query(geom.getEnvelopeInternal());
if (results.isEmpty())
return Optional.empty();
var filtered = results.stream().filter(rec -> rec.multiPolygon.contains(geom)
).toList();
if (filtered.isEmpty()) {
return Optional.empty();
}
return Optional.of(filtered.getFirst().country);
}
}
13 changes: 13 additions & 0 deletions tiles/src/main/java/com/protomaps/basemap/layers/Roads.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.onthegomap.planetiler.VectorTile;
import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.reader.SourceFeature;
import com.protomaps.basemap.feature.CountryCoder;
import com.protomaps.basemap.feature.FeatureId;
import com.protomaps.basemap.locales.CartographicLocale;
import com.protomaps.basemap.locales.US;
Expand All @@ -16,6 +17,12 @@

public class Roads implements ForwardingProfile.LayerPostProcesser {

private CountryCoder countryCoder;

public Roads(CountryCoder countryCoder) {
this.countryCoder = countryCoder;
}

@Override
public String name() {
return "roads";
Expand Down Expand Up @@ -147,6 +154,12 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
.setPixelTolerance(0)
.setZoomRange(minZoom, maxZoom);

try {
var code = countryCoder.getCountryCode(sf.latLonGeometry());
} catch (Exception e) {
// do logic based on country code
}

if (!kindDetail.isEmpty()) {
feat.setAttr("kind_detail", kindDetail);
} else {
Expand Down
Loading
Loading