From 0a241e17e1ddf6efd1fd442dc10a71c8cdbed507 Mon Sep 17 00:00:00 2001 From: Michael Barry Date: Fri, 22 Sep 2023 06:30:17 -0400 Subject: [PATCH] Fix downloading tile weights in CI (#668) --- .../com/onthegomap/planetiler/Planetiler.java | 2 +- .../planetiler/util/TileSizeStats.java | 4 +-- .../planetiler/util/TopOsmTiles.java | 30 +++++++++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java index b36d417d6b..5fe00af280 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/Planetiler.java @@ -725,7 +725,7 @@ public void run() throws Exception { download(); } if (fetchOsmTileStats) { - TopOsmTiles.downloadPrecomputed(config, stats); + TopOsmTiles.downloadPrecomputed(config); } ensureInputFilesExist(); diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileSizeStats.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileSizeStats.java index 2b07a5bd07..9276ef7e4d 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileSizeStats.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileSizeStats.java @@ -68,9 +68,9 @@ public static void main(String... args) { var arguments = Arguments.fromArgsOrConfigFile(args); var config = PlanetilerConfig.from(arguments); var stats = Stats.inMemory(); - var download = arguments.getBoolean("download_osm_tile_weights", "download OSM tile weights file", true); + var download = arguments.getBoolean("download_osm_tile_weights|download", "download OSM tile weights file", false); if (download && !Files.exists(config.tileWeights())) { - TopOsmTiles.downloadPrecomputed(config, stats); + TopOsmTiles.downloadPrecomputed(config); } var tileStats = new TilesetSummaryStatistics(TileWeights.readFromFile(config.tileWeights())); var inputString = arguments.getString("input", "input file"); diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TopOsmTiles.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TopOsmTiles.java index 652235e4a5..e66e8cbcb5 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TopOsmTiles.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TopOsmTiles.java @@ -1,6 +1,9 @@ package com.onthegomap.planetiler.util; import static com.onthegomap.planetiler.util.Exceptions.throwFatalException; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; import com.google.common.io.LineReader; import com.onthegomap.planetiler.config.Arguments; @@ -45,7 +48,7 @@ * *

* You can also fetch precomputed top-1m tile stats from summer 2023 using - * {@link #downloadPrecomputed(PlanetilerConfig, Stats)} + * {@link #downloadPrecomputed(PlanetilerConfig)} */ public class TopOsmTiles { @@ -180,10 +183,27 @@ public static void main(String[] args) throws IOException { * Download precomputed top-1m tile stats from 90 days of openstreetmap.org tile logs to * {@link PlanetilerConfig#tileWeights()} path if they don't already exist. */ - public static void downloadPrecomputed(PlanetilerConfig config, Stats stats) { - if (!Files.exists(config.tileWeights())) { - Downloader.create(config, stats) - .downloadIfNecessary(new Downloader.ResourceToDownload("osm-tile-weights", DOWLOAD_URL, config.tileWeights())); + @SuppressWarnings("java:S2142") + public static void downloadPrecomputed(PlanetilerConfig config) { + var dest = config.tileWeights(); + if (!Files.exists(dest)) { + var tmp = dest.resolveSibling(dest.getFileName() + ".tmp"); + FileUtils.deleteOnExit(tmp); + try { + try ( + var in = Downloader.openStream(DOWLOAD_URL, config); + var out = Files.newOutputStream(tmp, CREATE, TRUNCATE_EXISTING, WRITE) + ) { + LOGGER.info("Downloading pre-computed tile weights from {} to {}", DOWLOAD_URL, dest); + in.transferTo(out); + } + Files.move(tmp, dest); + } catch (IOException e) { + LOGGER.warn("Failed downloading pre-computed tile weights: {}", e.toString()); + FileUtils.delete(dest); + } finally { + FileUtils.delete(tmp); + } } } }