planetiler/planetiler-custommap/src/main/java/com/onthegomap/planetiler/custommap/ConfiguredMapMain.java

92 wiersze
3.0 KiB
Java

package com.onthegomap.planetiler.custommap;
import com.onthegomap.planetiler.Planetiler;
import com.onthegomap.planetiler.config.Arguments;
import com.onthegomap.planetiler.custommap.configschema.DataSource;
import com.onthegomap.planetiler.custommap.configschema.DataSourceType;
import com.onthegomap.planetiler.custommap.configschema.SchemaConfig;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Main driver to create maps configured by a YAML file.
*
* Parses the config file into a {@link ConfiguredProfile}, loads sources into {@link Planetiler} runner and kicks off
* the map generation process.
*/
public class ConfiguredMapMain {
/*
* Main entrypoint
*/
public static void main(String... args) throws Exception {
run(Arguments.fromArgsOrConfigFile(args));
}
static void run(Arguments args) throws Exception {
var dataDir = Path.of("data");
var sourcesDir = dataDir.resolve("sources");
var schemaFile = args.getString(
"schema",
"Location of YML-format schema definition file"
);
var path = Path.of(schemaFile);
SchemaConfig config;
if (Files.exists(path)) {
config = SchemaConfig.load(path);
} else {
// if the file doesn't exist, check if it's bundled in the jar
schemaFile = schemaFile.startsWith("/samples/") ? schemaFile : "/samples/" + schemaFile;
if (ConfiguredMapMain.class.getResource(schemaFile) != null) {
config = YAML.loadResource(schemaFile, SchemaConfig.class);
} else {
throw new IllegalArgumentException("Schema file not found: " + schemaFile);
}
}
var planetiler = Planetiler.create(args)
.setProfile(new ConfiguredProfile(config));
var sources = config.sources();
for (var source : sources.entrySet()) {
configureSource(planetiler, sourcesDir, source.getKey(), source.getValue());
}
planetiler.overwriteOutput("mbtiles", Path.of("data", "output.mbtiles"))
.run();
}
private static void configureSource(Planetiler planetiler, Path sourcesDir, String sourceName, DataSource source)
throws URISyntaxException {
DataSourceType sourceType = source.type();
Path localPath = source.localPath();
switch (sourceType) {
case OSM -> {
String url = source.url();
String[] areaParts = url.split("[:/]");
String areaFilename = areaParts[areaParts.length - 1];
String areaName = areaFilename.replaceAll("\\..*$", "");
if (localPath == null) {
localPath = sourcesDir.resolve(areaName + ".osm.pbf");
}
planetiler.addOsmSource(sourceName, localPath, url);
}
case SHAPEFILE -> {
String url = source.url();
if (localPath == null) {
localPath = sourcesDir.resolve(Paths.get(new URI(url).getPath()).getFileName().toString());
}
planetiler.addShapefileSource(sourceName, localPath, url);
}
default -> throw new IllegalArgumentException("Unhandled source " + sourceType);
}
}
}