-
-
Notifications
You must be signed in to change notification settings - Fork 59
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
58fe528
add country-coder json [#44]
bdon 6b31da3
handle edge cases in country-coder JSON
bdon 18b8d8b
linting
bdon 011bf25
precise filtering of country coder
bdon e78dbae
Fix tests
bdon c7aa2ff
formatting
bdon 4494c24
use planetiler GeoJSON module [#44]
bdon 313df6d
linting
bdon 1fcd952
linting
bdon e5316fb
linting
bdon b57042d
refactor
bdon e6b17e7
don't set any attributes for now [#44]
bdon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
tiles/src/main/java/com/protomaps/basemap/feature/CountryCoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might want to use Planetiler's
PolygonIndex
, see https://github.com/onthegomap/planetiler/blob/f5fe38a4d4400f3aa30998a1eb3ac551ce3e47d9/planetiler-core/src/main/java/com/onthegomap/planetiler/geo/PolygonIndex.java#L20, cc @msbarryThere was a problem hiding this comment.
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
There was a problem hiding this comment.
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