diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileWeights.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileWeights.java index 461de915f9..a3d7082f88 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileWeights.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/util/TileWeights.java @@ -17,7 +17,8 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.LinkedHashMap; +import java.util.Comparator; +import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @@ -41,8 +42,8 @@ public class TileWeights { .withLineSeparator("\n"); private static final ObjectWriter WRITER = MAPPER.writer(SCHEMA); private static final ObjectReader READER = MAPPER.readerFor(Row.class).with(SCHEMA); - private final Map byZoom = new LinkedHashMap<>(); - private final Map weights = new LinkedHashMap<>(); + private final Map byZoom = new HashMap<>(); + private final Map weights = new HashMap<>(); public long getWeight(TileCoord coord) { return weights.getOrDefault(coord, 0L); @@ -69,7 +70,11 @@ public void writeToFile(Path path) throws IOException { new BufferedOutputStream(Files.newOutputStream(path, CREATE, TRUNCATE_EXISTING, WRITE))); var writer = WRITER.writeValues(output) ) { - for (var entry : weights.entrySet()) { + var sorted = weights.entrySet().stream() + .sorted(Comparator.comparingInt(e -> e.getKey().encoded())) + .iterator(); + while (sorted.hasNext()) { + var entry = sorted.next(); TileCoord coord = entry.getKey(); writer.write(new Row(coord.z(), coord.x(), coord.y(), entry.getValue())); } 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 760f3dacba..652235e4a5 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 @@ -99,7 +99,6 @@ TileWeights run(int threads, int topN, int maxZoom, List toDownload) counts.entrySet().stream() .sorted(Comparator.comparingLong(e -> -e.getValue())) .limit(topN) - .sorted(Comparator.comparingInt(Map.Entry::getKey)) .forEach(entry -> tileWeights.put(TileCoord.decode(entry.getKey()), entry.getValue())); result.complete(tileWeights); }); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TileWeightsTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TileWeightsTest.java index 9d528f40dd..26c90a0c38 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TileWeightsTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/util/TileWeightsTest.java @@ -52,28 +52,13 @@ void testWriteToFile(@TempDir Path path) throws IOException { var read = TileWeights.readFromFile(file); assertEquals(""" z x y loads - 1 0 0 2 0 0 0 1 + 1 0 0 2 """, new String(new GZIPInputStream(Files.newInputStream(file)).readAllBytes())); assertEquals(1, read.getWeight(TileCoord.ofXYZ(0, 0, 0))); assertEquals(2, read.getWeight(TileCoord.ofXYZ(0, 0, 1))); } - @Test - void testWriteToFileDifferentOrder(@TempDir Path path) throws IOException { - Path file = path.resolve("test.tsv.gz"); - new TileWeights() - .put(TileCoord.ofXYZ(0, 0, 0), 1) - .put(TileCoord.ofXYZ(0, 0, 1), 1) - .put(TileCoord.ofXYZ(0, 0, 1), 1) - .writeToFile(file); - assertEquals(""" - z x y loads - 0 0 0 1 - 1 0 0 2 - """, new String(new GZIPInputStream(Files.newInputStream(file)).readAllBytes())); - } - @Test void testReadCorruptFile(@TempDir Path path) throws IOException { Path file = path.resolve("test.tsv.gz");