Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Oct 27, 2023
1 parent b2f99a3 commit 2bf080d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public static Directories buildRootLeaves(List<Entry> entries, int leafSize) thr
public static Directories optimizeDirectories(List<Entry> 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);
Expand All @@ -376,9 +376,9 @@ public static Directories optimizeDirectories(List<Entry> 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);
Expand All @@ -387,6 +387,5 @@ public static Directories optimizeDirectories(List<Entry> entries, int targetRoo
}
leafSize = leafSize * 1.2;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +34,21 @@ public class PMTilesStore implements TileStore {
public PMTilesStore(Path path) {
try {
this.writer = new PMTilesWriter(path);

var metadata = new HashMap<String, Object>();
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class PMTilesWriter {

private final Path path;

private Map<String, Object> metadata = new HashMap<>();

private final List<Entry> entries;

private final Map<Long, Long> tileHashToOffset;
Expand All @@ -52,7 +54,11 @@ public PMTilesWriter(Path path, List<Entry> entries, Map<Long, Long> 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<String, Object> 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;
Expand Down Expand Up @@ -93,20 +99,7 @@ public void finalize() throws IOException {
entries.sort(Comparator.comparingLong(Entry::getTileId));
}

var metadataMap = new HashMap<String, Object>();
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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
package org.apache.baremaps.tilestore.pmtiles;

enum TileType {
Unknown,
Mvt,
Png,
Jpeg,
Webp,
Avif,
unknown,
mvt,
png,
jpeg,
webp,
avif,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -178,7 +178,7 @@ void encodeHeader() throws IOException {
false,
Compression.Gzip,
Compression.Gzip,
TileType.Mvt,
TileType.mvt,
0,
0,
0,
Expand Down

0 comments on commit 2bf080d

Please sign in to comment.