Skip to content

Commit

Permalink
precise filtering of country coder
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Feb 12, 2025
1 parent 18b8d8b commit 011bf25
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ public static CountryCoder fromJsonString(String s) throws IOException {
for (var feature : features) {
JsonNode properties = feature.get("properties");

if (!feature.has("geometry") || feature.get("geometry").isNull() || !properties.has("country")) {
if (!feature.has("geometry") || feature.get("geometry").isNull()) {
continue;
}

String country;
if (properties.has("iso1A2")) {
country = properties.get("iso1A2").asText();
} else if (properties.has("country")) {
country = properties.get("country").asText();
} else {
continue;
}

List<Polygon> polygons = new ArrayList<>();
for (var polygon : feature.get("geometry").get("coordinates")) {
ArrayNode outerRingNode = (ArrayNode) polygon.get(0);
Expand All @@ -59,7 +69,7 @@ public static CountryCoder fromJsonString(String s) throws IOException {
MultiPolygon multiPolygon = GeoUtils.createMultiPolygon(polygons);
multiPolygon.getEnvelopeInternal();
tree.insert(multiPolygon.getEnvelopeInternal(),
new Record(properties.get("country").asText(), properties.get("nameEn").asText(), multiPolygon));
new Record(country, properties.get("nameEn").asText(), multiPolygon));
}
return new CountryCoder(tree);
}
Expand All @@ -75,10 +85,15 @@ private static Coordinate[] parseCoordinates(ArrayNode coordinateArray) {
return coordinates;
}

public Optional<String> getCountryCode(Point point) {
List<Record> results = tree.query(point.getEnvelopeInternal());
public Optional<String> getCountryCode(Geometry geom) {
List<Record> results = tree.query(geom.getEnvelopeInternal());
if (results.isEmpty())
return Optional.empty();
return Optional.of(results.getFirst().country);
var filtered = results.stream().filter(record -> record.multiPolygon.contains(geom)
).toList();
if (filtered.isEmpty()) {
return Optional.empty();
}
return Optional.of(filtered.getFirst().country);
}
}
14 changes: 7 additions & 7 deletions tiles/src/main/java/com/protomaps/basemap/layers/Roads.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.onthegomap.planetiler.FeatureMerge;
import com.onthegomap.planetiler.ForwardingProfile;
import com.onthegomap.planetiler.VectorTile;
import com.onthegomap.planetiler.geo.GeoUtils;
import com.onthegomap.planetiler.geo.GeometryException;
import com.onthegomap.planetiler.reader.SourceFeature;
import com.protomaps.basemap.feature.CountryCoder;
Expand Down Expand Up @@ -44,12 +43,6 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
int minZoomShieldText = 10;
int minZoomNames = 14;

try {
var code = countryCoder.getCountryCode(GeoUtils.JTS_FACTORY.createPoint(sf.latLonGeometry().getCoordinate()));
} catch (Exception e) {

}

String highway = sf.getString("highway");
String service = "";

Expand Down Expand Up @@ -161,6 +154,13 @@ public void processOsm(SourceFeature sf, FeatureCollector features) {
.setPixelTolerance(0)
.setZoomRange(minZoom, maxZoom);

try {
var code = countryCoder.getCountryCode(sf.latLonGeometry());
feat.setAttr("country_code", code);
} catch (Exception e) {

}

if (!kindDetail.isEmpty()) {
feat.setAttr("kind_detail", kindDetail);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,42 @@ class CountryCoderTest {
@Test
void testLookupByPoint() throws IOException {
CountryCoder c = CountryCoder.fromJsonString(
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"country\":\"GB\",\"nameEn\":\"Great Britain\"},\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[0,1],[1,1],[0,0]]]]}}]}");
assertEquals(Optional.of("GB"), c.getCountryCode(newPoint(0.1, 0.1)));
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"iso1A2\":\"GB\",\"nameEn\":\"Great Britain\"},\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[0,1],[1,1],[0,0]]]]}}]}");
assertEquals(Optional.of("GB"), c.getCountryCode(newPoint(0.1, 0.9)));
}

@Test
void testLookupByPointActuallyContains() throws IOException {
CountryCoder c = CountryCoder.fromJsonString(
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"iso1A2\":\"GB\",\"nameEn\":\"Great Britain\"},\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[0,1],[1,1],[0,0]]]]}}]}");
assertEquals(Optional.empty(), c.getCountryCode(newPoint(0.9, 0.1)));
}

@Test
void testNullGeometry() throws IOException {
CountryCoder c = CountryCoder.fromJsonString(
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"country\":\"xx\",\"nameEn\":\"Iberia\"},\"geometry\":null}]}");
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"iso1A2\":\"xx\",\"nameEn\":\"Iberia\"},\"geometry\":null}]}");
assertEquals(Optional.empty(), c.getCountryCode(newPoint(0.1, 0.1)));
}

@Test
void testNoGeometry() throws IOException {
CountryCoder c = CountryCoder.fromJsonString(
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"country\":\"xx\",\"nameEn\":\"Iberia\"}}]}");
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"iso1A2\":\"xx\",\"nameEn\":\"Iberia\"}}]}");
assertEquals(Optional.empty(), c.getCountryCode(newPoint(0.1, 0.1)));
}

@Test
void testNoCountry() throws IOException {
CountryCoder c = CountryCoder.fromJsonString(
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"nameEn\":\"Iberia\"},\"geometry\":null}]}");
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"nameEn\":\"Iberia\"},\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[0,1],[1,1],[0,0]]]]}}]}");
assertEquals(Optional.empty(), c.getCountryCode(newPoint(0.1, 0.1)));
}

@Test
void testDependency() throws IOException {
CountryCoder c = CountryCoder.fromJsonString(
"{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"country\":\"GB\",\"nameEn\":\"Great Britain\"},\"geometry\":{\"type\":\"MultiPolygon\",\"coordinates\":[[[[0,0],[0,1],[1,1],[0,0]]]]}}]}");
assertEquals(Optional.of("GB"), c.getCountryCode(newPoint(0.1, 0.9)));
}
}

0 comments on commit 011bf25

Please sign in to comment.