diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java index 53ac3db7c..0e83c018a 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTiles.java @@ -365,7 +365,7 @@ public static Directories buildRootLeaves(List entries, int leafSize) thr public static Directories optimizeDirectories(List entries, int targetRootLenght) throws IOException { if (entries.size() < 16384) { - byte[] rootBytes = null; + byte[] rootBytes; try (var rootOutput = new ByteArrayOutputStream(); var rootDataOutput = new LittleEndianDataOutputStream(rootOutput)) { serializeEntries(rootDataOutput, entries); @@ -376,9 +376,9 @@ public static Directories optimizeDirectories(List entries, int targetRoo } } - double leafSize = (double) entries.size() / 3500d; - if (leafSize < 4096d) { - leafSize = 4096d; + double leafSize = (double) entries.size() / 3500; + if (leafSize < 4096) { + leafSize = 4096; } for (;;) { var directories = buildRootLeaves(entries, (int) leafSize); @@ -387,6 +387,5 @@ public static Directories optimizeDirectories(List entries, int targetRoo } leafSize = leafSize * 1.2; } - } } diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesStore.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesStore.java index 272b4c95a..5c06d2197 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesStore.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesStore.java @@ -20,6 +20,9 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.file.Path; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.apache.baremaps.tilestore.TileCoord; import org.apache.baremaps.tilestore.TileStore; import org.apache.baremaps.tilestore.TileStoreException; @@ -31,6 +34,21 @@ public class PMTilesStore implements TileStore { public PMTilesStore(Path path) { try { this.writer = new PMTilesWriter(path); + + var metadata = new HashMap(); + metadata.put("name", "baremaps"); + metadata.put("type", "baselayer"); + metadata.put("version", "0.0.1"); + metadata.put("description", "PMTiles generated by Baremaps"); + metadata.put("attribution", "OpenStreetMap contributors"); + metadata.put("vector_layers", List.of( + "aerialway", "aeroway", "amenity", "attraction", + "barrier", "boundary", "building", "highway", "landuse", + "leisure", "man_made", "natural", "ocean", "point", + "power", "railway", "route", "waterway") + .stream().map(s -> Map.of("id", s)).toList()); + this.writer.writeMetadata(metadata); + } catch (IOException e) { throw new RuntimeException(e); } diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesWriter.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesWriter.java index 66566dffa..eeb65c4e7 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesWriter.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/PMTilesWriter.java @@ -30,6 +30,8 @@ public class PMTilesWriter { private final Path path; + private Map metadata = new HashMap<>(); + private final List entries; private final Map tileHashToOffset; @@ -52,7 +54,11 @@ public PMTilesWriter(Path path, List entries, Map tileHashToO this.tilePath = Files.createTempFile(path.getParent(), "tiles", ".tmp"); } - public synchronized void writeTile(int z, int x, int y, byte[] bytes) throws IOException { + public void writeMetadata(Map metadata) { + this.metadata = metadata; + } + + public void writeTile(int z, int x, int y, byte[] bytes) throws IOException { // Write the tile var tileId = PMTiles.zxyToTileId(z, x, y); var tileLength = bytes.length; @@ -93,20 +99,7 @@ public void finalize() throws IOException { entries.sort(Comparator.comparingLong(Entry::getTileId)); } - var metadataMap = new HashMap(); - metadataMap.put("name", "baremaps"); - metadataMap.put("type", "baselayer"); - metadataMap.put("version", "0.0.1"); - metadataMap.put("description", "PMTiles generated by Baremaps"); - metadataMap.put("attribution", "OpenStreetMap contributors"); - metadataMap.put("vector_layers", List.of("aerialway", "aeroway", "amenity", "attraction", - "barrier", "boundary", "building", "highway", "landuse", - "leisure", "man_made", "natural", "ocean", "point", - "power", "railway", "route", "waterway") - .stream().map(s -> Map.of("id", s)).toList()); - - - var metadataBytes = new ObjectMapper().writeValueAsBytes(metadataMap); + var metadataBytes = new ObjectMapper().writeValueAsBytes(metadata); var directories = PMTiles.optimizeDirectories(entries, 16247); var rootOffset = 127; @@ -127,7 +120,7 @@ public void finalize() throws IOException { header.setInternalCompression(Compression.None); header.setTileCompression(Compression.Gzip); - header.setTileType(TileType.Mvt); + header.setTileType(TileType.mvt); header.setRootOffset(rootOffset); header.setRootLength(rootLength); header.setMetadataOffset(metadataOffset); diff --git a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/TileType.java b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/TileType.java index 2231fd67e..335bc4884 100644 --- a/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/TileType.java +++ b/baremaps-core/src/main/java/org/apache/baremaps/tilestore/pmtiles/TileType.java @@ -18,10 +18,10 @@ package org.apache.baremaps.tilestore.pmtiles; enum TileType { - Unknown, - Mvt, - Png, - Jpeg, - Webp, - Avif, + unknown, + mvt, + png, + jpeg, + webp, + avif, } diff --git a/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java b/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java index 7de357cb2..fd60b1979 100644 --- a/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java +++ b/baremaps-core/src/test/java/org/apache/baremaps/tilestore/pmtiles/PMTilesTest.java @@ -149,7 +149,7 @@ void decodeHeader() throws IOException { assertFalse(header.isClustered()); assertEquals(header.getInternalCompression(), Compression.Gzip); assertEquals(header.getTileCompression(), Compression.Gzip); - assertEquals(header.getTileType(), TileType.Mvt); + assertEquals(header.getTileType(), TileType.mvt); assertEquals(header.getMinZoom(), 0); assertEquals(header.getMaxZoom(), 0); assertEquals(header.getMinLon(), 0); @@ -178,7 +178,7 @@ void encodeHeader() throws IOException { false, Compression.Gzip, Compression.Gzip, - TileType.Mvt, + TileType.mvt, 0, 0, 0,