package com.onthegomap.planetiler.util; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.onthegomap.planetiler.config.PlanetilerConfig; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.concurrent.ThreadSafe; /** * A utility to search Geofabrik Download Server for a {@code .osm.pbf} * download URL by name. * * @see Geofabrik JSON index technical details */ @ThreadSafe public class Geofabrik { private static volatile IndexJson index = null; private static final ObjectMapper objectMapper = new ObjectMapper() .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); /** * Fetches the Geofabrik index and searches for a {@code .osm.pbf} resource to download where ID or name field * contains all the tokens in {@code searchQuery}. *
* If an exact match is found, returns that. Otherwise, looks for a resource that contains {@code searchQuery} as a * substring. *
* The index is only fetched once and cached after that.
*
* @param searchQuery the tokens to search for
* @param config planetiler config with user-agent and timeout to use when downloading files
* @return the URL of a {@code .osm.pbf} file with name or ID matching {@code searchQuery}
* @throws IllegalArgumentException if no matches, or more than one match is found.
*/
public static String getDownloadUrl(String searchQuery, PlanetilerConfig config) {
IndexJson index = getAndCacheIndex(config);
return searchIndexForDownloadUrl(searchQuery, index);
}
private synchronized static IndexJson getAndCacheIndex(PlanetilerConfig config) {
if (index == null) {
try (
InputStream inputStream = Downloader.openStream("https://download.geofabrik.de/index-v1-nogeom.json",
config)
) {
index = parseIndexJson(inputStream);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
return index;
}
private static Set