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

Improve geofabrik area search #317

Merged
merged 2 commits into from
Aug 9, 2022
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onthegomap.planetiler.util;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.onthegomap.planetiler.config.PlanetilerConfig;
Expand Down Expand Up @@ -71,39 +72,61 @@ static IndexJson parseIndexJson(InputStream indexJsonContent) throws IOException

static String searchIndexForDownloadUrl(String searchQuery, IndexJson index) {
Set<String> searchTokens = tokenize(searchQuery);
List<PropertiesJson> approx = new ArrayList<>();
List<PropertiesJson> exact = new ArrayList<>();
List<PropertiesJson> approxName = new ArrayList<>();
List<PropertiesJson> id = new ArrayList<>();
List<PropertiesJson> exactName = new ArrayList<>();
for (var feature : index.features) {
PropertiesJson properties = feature.properties;
if (properties.urls.containsKey("pbf")) {
if (tokenize(properties.id).equals(searchTokens) ||
tokenize(properties.name).equals(searchTokens)) {
exact.add(properties);
if (properties.ids().stream().map(Geofabrik::tokenize).anyMatch(searchTokens::equals)) {
id.add(properties);
} else if (tokenize(properties.name).equals(searchTokens)) {
exactName.add(properties);
} else if (tokenize(properties.name).containsAll(searchTokens)) {
approx.add(properties);
approxName.add(properties);
}
}
}
if (exact.size() > 1) {
String result = getIfOnly(searchQuery, "exact ID matches", id);
if (result == null) {
result = getIfOnly(searchQuery, "exact name matches", exactName);
}
if (result == null) {
result = getIfOnly(searchQuery, "approximate name matches", approxName);
}
if (result == null) {
throw new IllegalArgumentException("No matches for '" + searchQuery + "'");
}
return result;
}

private static String getIfOnly(String name, String searchQuery, List<PropertiesJson> values) {
if (values.size() > 1) {
throw new IllegalArgumentException(
"Multiple exact matches for '" + searchQuery + "': " + exact.stream().map(d -> d.id).collect(
"Multiple " + name + " for '" + searchQuery + "': " + values.stream().map(d -> d.id).collect(
Collectors.joining(", ")));
} else if (exact.size() == 1) {
return exact.get(0).urls.get("pbf");
} else if (values.size() == 1) {
return values.get(0).urls.get("pbf");
} else {
if (approx.size() > 1) {
throw new IllegalArgumentException(
"Multiple approximate matches for '" + searchQuery + "': " + approx.stream().map(d -> d.id).collect(
Collectors.joining(", ")));
} else if (approx.size() == 1) {
return approx.get(0).urls.get("pbf");
} else {
throw new IllegalArgumentException("No matches for '" + searchQuery + "'");
}
return null;
}
}

record PropertiesJson(String id, String parent, String name, Map<String, String> urls) {}
record PropertiesJson(String id, String parent, String name, Map<String, String> urls,
@JsonProperty("iso3166-1:alpha2") List<String> iso3166_1,
@JsonProperty("iso3166-2") List<String> iso3166_2
) {
List<String> ids() {
List<String> result = new ArrayList<>(List.of(id, name));
if (iso3166_1 != null) {
result.addAll(iso3166_1);
}
if (iso3166_2 != null) {
result.addAll(iso3166_2);
}
return result;
}
}

record FeatureJson(PropertiesJson properties) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,156 @@
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class GeofabrikTest {

private static final byte[] response =
"""
{ "type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id" : "afghanistan",
"parent" : "asia",
"iso3166-1:alpha2" : [ "AF" ],
"name" : "Afghanistan",
"urls" : {
"pbf" : "https://download.geofabrik.de/asia/afghanistan-latest.osm.pbf",
"bz2" : "https://download.geofabrik.de/asia/afghanistan-latest.osm.bz2",
"shp" : "https://download.geofabrik.de/asia/afghanistan-latest-free.shp.zip",
"pbf-internal" : "https://osm-internal.download.geofabrik.de/asia/afghanistan-latest-internal.osm.pbf",
"history" : "https://osm-internal.download.geofabrik.de/asia/afghanistan-internal.osh.pbf",
"taginfo" : "https://taginfo.geofabrik.de/asia/afghanistan/",
"updates" : "https://download.geofabrik.de/asia/afghanistan-updates"
}
}
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"id": "afghanistan",
"parent": "asia",
"iso3166-1:alpha2": [
"AF"
],
"name": "Afghanistan",
"urls": {
"pbf": "https://download.geofabrik.de/asia/afghanistan-latest.osm.pbf",
"bz2": "https://download.geofabrik.de/asia/afghanistan-latest.osm.bz2",
"shp": "https://download.geofabrik.de/asia/afghanistan-latest-free.shp.zip",
"pbf-internal": "https://osm-internal.download.geofabrik.de/asia/afghanistan-latest-internal.osm.pbf",
"history": "https://osm-internal.download.geofabrik.de/asia/afghanistan-internal.osh.pbf",
"taginfo": "https://taginfo.geofabrik.de/asia/afghanistan/",
"updates": "https://download.geofabrik.de/asia/afghanistan-updates"
}
]
}
},
{
"type": "Feature",
"properties": {
"id": "georgia",
"parent": "europe",
"iso3166-1:alpha2": [
"GE"
],
"name": "Georgia",
"urls": {
"pbf": "https://download.geofabrik.de/europe/georgia-latest.osm.pbf",
"bz2": "https://download.geofabrik.de/europe/georgia-latest.osm.bz2",
"shp": "https://download.geofabrik.de/europe/georgia-latest-free.shp.zip",
"pbf-internal": "https://osm-internal.download.geofabrik.de/europe/georgia-latest-internal.osm.pbf",
"history": "https://osm-internal.download.geofabrik.de/europe/georgia-internal.osh.pbf",
"taginfo": "https://taginfo.geofabrik.de/europe/georgia/",
"updates": "https://download.geofabrik.de/europe/georgia-updates"
}
}
},
{
"type": "Feature",
"properties": {
"id": "us/georgia",
"parent": "north-america",
"iso3166-2": [
"US-GA"
],
"name": "Georgia",
"urls": {
"pbf": "https://download.geofabrik.de/north-america/us/georgia-latest.osm.pbf",
"bz2": "https://download.geofabrik.de/north-america/us/georgia-latest.osm.bz2",
"shp": "https://download.geofabrik.de/north-america/us/georgia-latest-free.shp.zip",
"pbf-internal": "https://osm-internal.download.geofabrik.de/north-america/us/georgia-latest-internal.osm.pbf",
"history": "https://osm-internal.download.geofabrik.de/north-america/us/georgia-internal.osh.pbf",
"taginfo": "https://taginfo.geofabrik.de/north-america/us/georgia/",
"updates": "https://download.geofabrik.de/north-america/us/georgia-updates"
}
}
},
{
"type": "Feature",
"properties": {
"id": "us/massachusetts",
"parent": "north-america",
"iso3166-2": [
"US-MA"
],
"name": "us/massachusetts",
"urls": {
"pbf": "https://download.geofabrik.de/north-america/us/massachusetts-latest.osm.pbf",
"bz2": "https://download.geofabrik.de/north-america/us/massachusetts-latest.osm.bz2",
"shp": "https://download.geofabrik.de/north-america/us/massachusetts-latest-free.shp.zip",
"pbf-internal": "https://osm-internal.download.geofabrik.de/north-america/us/massachusetts-latest-internal.osm.pbf",
"history": "https://osm-internal.download.geofabrik.de/north-america/us/massachusetts-internal.osh.pbf",
"taginfo": "https://taginfo.geofabrik.de/north-america/us/massachusetts/",
"updates": "https://download.geofabrik.de/north-america/us/massachusetts-updates"
}
}
},
{
"type": "Feature",
"properties": {
"id": "us/west-virginia",
"parent": "north-america",
"iso3166-2": [
"US-WV"
],
"name": "us/west-virginia",
"urls": {
"pbf": "https://download.geofabrik.de/north-america/us/west-virginia-latest.osm.pbf",
"bz2": "https://download.geofabrik.de/north-america/us/west-virginia-latest.osm.bz2",
"shp": "https://download.geofabrik.de/north-america/us/west-virginia-latest-free.shp.zip",
"pbf-internal": "https://osm-internal.download.geofabrik.de/north-america/us/west-virginia-latest-internal.osm.pbf",
"history": "https://osm-internal.download.geofabrik.de/north-america/us/west-virginia-internal.osh.pbf",
"taginfo": "https://taginfo.geofabrik.de/north-america/us/west-virginia/",
"updates": "https://download.geofabrik.de/north-america/us/west-virginia-updates"
}
}
},
{
"type": "Feature",
"properties": {
"id": "west-sussex",
"parent": "england",
"name": "West Sussex",
"urls": {
"pbf": "https://download.geofabrik.de/europe/great-britain/england/west-sussex-latest.osm.pbf",
"bz2": "https://download.geofabrik.de/europe/great-britain/england/west-sussex-latest.osm.bz2",
"shp": "https://download.geofabrik.de/europe/great-britain/england/west-sussex-latest-free.shp.zip",
"pbf-internal": "https://osm-internal.download.geofabrik.de/europe/great-britain/england/west-sussex-latest-internal.osm.pbf",
"history": "https://osm-internal.download.geofabrik.de/europe/great-britain/england/west-sussex-internal.osh.pbf",
"taginfo": "https://taginfo.geofabrik.de/europe/great-britain/england/west-sussex/",
"updates": "https://download.geofabrik.de/europe/great-britain/england/west-sussex-updates"
}
}
}
]
}
"""
.getBytes(StandardCharsets.UTF_8);

@Test
void testFound() throws IOException {
@ParameterizedTest
@CsvSource({
"afghanistan,https://download.geofabrik.de/asia/afghanistan-latest.osm.pbf",
"af,https://download.geofabrik.de/asia/afghanistan-latest.osm.pbf",
"ge,https://download.geofabrik.de/europe/georgia-latest.osm.pbf",
"us-ga,https://download.geofabrik.de/north-america/us/georgia-latest.osm.pbf",
"us ga,https://download.geofabrik.de/north-america/us/georgia-latest.osm.pbf",
"us/ga,https://download.geofabrik.de/north-america/us/georgia-latest.osm.pbf",
"us_ga,https://download.geofabrik.de/north-america/us/georgia-latest.osm.pbf",
"us/georgia,https://download.geofabrik.de/north-america/us/georgia-latest.osm.pbf",
"west virginia,https://download.geofabrik.de/north-america/us/west-virginia-latest.osm.pbf",
"us/west-virginia,https://download.geofabrik.de/north-america/us/west-virginia-latest.osm.pbf",
"west sussex,https://download.geofabrik.de/europe/great-britain/england/west-sussex-latest.osm.pbf"
})
void testFound(String search, String expectedUrl) throws IOException {
var index = Geofabrik.parseIndexJson(new ByteArrayInputStream(response));
String url = Geofabrik.searchIndexForDownloadUrl("afghanistan", index);
assertEquals("https://download.geofabrik.de/asia/afghanistan-latest.osm.pbf", url);
String url = Geofabrik.searchIndexForDownloadUrl(search, index);
assertEquals(expectedUrl, url);
}

@Test
Expand All @@ -50,4 +165,11 @@ void testNotFound() throws IOException {
assertThrows(IllegalArgumentException.class,
() -> Geofabrik.searchIndexForDownloadUrl("monaco", index));
}

@Test
void testAmbiguous() throws IOException {
var index = Geofabrik.parseIndexJson(new ByteArrayInputStream(response));
assertThrows(IllegalArgumentException.class,
() -> Geofabrik.searchIndexForDownloadUrl("georgia", index));
}
}