planetiler/src/main/java/com/onthegomap/flatmap/OpenMapTilesMain.java

105 wiersze
4.9 KiB
Java
Czysty Zwykły widok Historia

2021-04-10 09:25:42 +00:00
package com.onthegomap.flatmap;
2021-04-30 10:31:56 +00:00
import com.onthegomap.flatmap.collections.FeatureGroup;
import com.onthegomap.flatmap.collections.FeatureSort;
2021-04-12 10:05:32 +00:00
import com.onthegomap.flatmap.collections.LongLongMap;
2021-04-10 09:25:42 +00:00
import com.onthegomap.flatmap.profiles.OpenMapTilesProfile;
2021-05-01 20:40:44 +00:00
import com.onthegomap.flatmap.read.NaturalEarthReader;
import com.onthegomap.flatmap.read.OpenStreetMapReader;
import com.onthegomap.flatmap.read.OsmInputFile;
import com.onthegomap.flatmap.read.ShapefileReader;
import com.onthegomap.flatmap.write.MbtilesWriter;
2021-04-10 09:25:42 +00:00
import java.io.IOException;
2021-05-01 20:08:20 +00:00
import java.nio.file.Files;
2021-04-10 09:25:42 +00:00
import java.nio.file.Path;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OpenMapTilesMain {
private static final Logger LOGGER = LoggerFactory.getLogger(OpenMapTilesMain.class);
public static void main(String[] args) throws IOException {
Arguments arguments = new Arguments(args);
var stats = arguments.getStats();
stats.startTimer("import");
LOGGER.info("Arguments:");
2021-05-01 20:08:20 +00:00
Path sourcesDir = Path.of("data", "sources");
2021-04-10 09:25:42 +00:00
OsmInputFile osmInputFile = new OsmInputFile(
2021-05-01 20:08:20 +00:00
arguments.inputFile("input", "OSM input file", sourcesDir.resolve("north-america_us_massachusetts.pbf")));
Path centerlines = arguments
.inputFile("centerline", "lake centerlines input", sourcesDir.resolve("lake_centerline.shp.zip"));
Path naturalEarth = arguments
.inputFile("natural_earth", "natural earth input", sourcesDir.resolve("natural_earth_vector.sqlite.zip"));
Path waterPolygons = arguments
.inputFile("water_polygons", "water polygons input", sourcesDir.resolve("water-polygons-split-3857.zip"));
Path tmpDir = arguments.file("tmpdir", "temp directory", Path.of("data", "tmp"));
2021-04-10 09:25:42 +00:00
boolean fetchWikidata = arguments.get("fetch_wikidata", "fetch wikidata translations", false);
boolean useWikidata = arguments.get("use_wikidata", "use wikidata translations", true);
2021-05-01 20:08:20 +00:00
Path wikidataNamesFile = arguments.file("wikidata_cache", "wikidata cache file",
Path.of("data", "sources", "wikidata_names.json"));
2021-05-05 10:29:12 +00:00
Path output = arguments.file("output", "mbtiles output file", Path.of("data", "massachusetts.mbtiles"));
2021-04-10 09:25:42 +00:00
List<String> languages = arguments.get("name_languages", "languages to use",
"en,ru,ar,zh,ja,ko,fr,de,fi,pl,es,be,br,he".split(","));
2021-05-01 20:08:20 +00:00
CommonParams config = CommonParams.from(arguments, osmInputFile);
2021-04-10 09:25:42 +00:00
LOGGER.info("Building OpenMapTiles profile into " + output + " in these phases:");
if (fetchWikidata) {
2021-04-22 21:19:58 +00:00
LOGGER.info(" [wikidata] Fetch OpenStreetMap element name translations from wikidata");
2021-04-10 09:25:42 +00:00
}
2021-04-11 20:10:28 +00:00
LOGGER.info(" [lake_centerlines] Extract lake centerlines");
LOGGER.info(" [water_polygons] Process ocean polygons");
LOGGER.info(" [natural_earth] Process natural earth features");
LOGGER.info(" [osm_pass1] Pre-process OpenStreetMap input (store node locations then relation members)");
LOGGER.info(" [osm_pass2] Process OpenStreetMap nodes, ways, then relations");
LOGGER.info(" [sort] Sort rendered features by tile ID");
LOGGER.info(" [mbtiles] Encode each tile and write to " + output);
2021-04-10 09:25:42 +00:00
var translations = Translations.defaultProvider(languages);
var profile = new OpenMapTilesProfile();
2021-05-04 10:17:10 +00:00
Files.createDirectories(tmpDir);
2021-05-01 20:08:20 +00:00
Path nodeDb = tmpDir.resolve("node.db");
2021-05-04 10:17:10 +00:00
LongLongMap nodeLocations = LongLongMap.newFileBackedSortedTable(nodeDb);
2021-05-01 20:08:20 +00:00
FeatureSort featureDb = FeatureSort.newExternalMergeSort(tmpDir.resolve("feature.db"), config.threads(), stats);
2021-04-30 10:31:56 +00:00
FeatureGroup featureMap = new FeatureGroup(featureDb, profile);
2021-04-12 12:53:53 +00:00
FeatureRenderer renderer = new FeatureRenderer(config);
2021-04-11 20:10:28 +00:00
2021-04-10 09:25:42 +00:00
if (fetchWikidata) {
2021-05-01 20:08:20 +00:00
stats.time("wikidata", () -> Wikidata.fetch(osmInputFile, wikidataNamesFile, config, profile, stats));
2021-04-10 09:25:42 +00:00
}
if (useWikidata) {
translations.addTranslationProvider(Wikidata.load(wikidataNamesFile));
}
stats.time("lake_centerlines", () ->
2021-05-01 20:08:20 +00:00
ShapefileReader
.process("EPSG:3857", "lake_centerlines", centerlines, renderer, featureMap, config, profile, stats));
2021-04-10 09:25:42 +00:00
stats.time("water_polygons", () ->
2021-05-01 20:08:20 +00:00
ShapefileReader.process("water_polygons", waterPolygons, renderer, featureMap, config, profile, stats));
2021-04-10 09:25:42 +00:00
stats.time("natural_earth", () ->
2021-05-01 20:08:20 +00:00
new NaturalEarthReader(naturalEarth, tmpDir.resolve("natearth.sqlite"), profile, stats)
2021-04-11 20:10:28 +00:00
.process("natural_earth", renderer, featureMap, config)
);
2021-04-10 09:25:42 +00:00
2021-05-01 20:08:20 +00:00
try (var osmReader = new OpenStreetMapReader(osmInputFile, nodeLocations, profile, stats)) {
2021-04-12 10:05:32 +00:00
stats.time("osm_pass1", () -> osmReader.pass1(config));
stats.time("osm_pass2", () -> osmReader.pass2(renderer, featureMap, config));
2021-04-10 09:25:42 +00:00
}
LOGGER.info("Deleting node.db to make room for mbtiles");
profile.release();
2021-05-01 20:08:20 +00:00
Files.delete(nodeDb);
2021-04-10 09:25:42 +00:00
stats.time("sort", featureDb::sort);
stats.time("mbtiles", () -> MbtilesWriter.writeOutput(featureMap, output, profile, config, stats));
2021-04-10 09:25:42 +00:00
stats.stopTimer("import");
LOGGER.info("FINISHED!");
stats.printSummary();
}
}