-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i.e. write individual pbf-files to disk in the format <base>/z/x/y.pbf in order to use that format it must be passed as "--ouput=/path/to/tiles?format=files" Fixes #536
- Loading branch information
Showing
10 changed files
with
532 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
planetiler-core/src/main/java/com/onthegomap/planetiler/files/FilesArchiveUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.onthegomap.planetiler.files; | ||
|
||
import com.onthegomap.planetiler.geo.TileCoord; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
final class FilesArchiveUtils { | ||
|
||
static final String PBF_FILE_ENDING = ".pbf"; | ||
|
||
private FilesArchiveUtils() {} | ||
|
||
static Path relativePathFromTileCoord(TileCoord tc) { | ||
return Paths.get(Integer.toString(tc.z()), Integer.toString(tc.x()), | ||
tc.y() + PBF_FILE_ENDING); | ||
} | ||
|
||
static Path absolutePathFromTileCoord(Path basePath, TileCoord tc) { | ||
return basePath.resolve(relativePathFromTileCoord(tc)); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
planetiler-core/src/main/java/com/onthegomap/planetiler/files/ReadableFilesArchive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.onthegomap.planetiler.files; | ||
|
||
import static com.onthegomap.planetiler.files.FilesArchiveUtils.PBF_FILE_ENDING; | ||
import static com.onthegomap.planetiler.files.FilesArchiveUtils.absolutePathFromTileCoord; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.onthegomap.planetiler.archive.ReadableTileArchive; | ||
import com.onthegomap.planetiler.archive.TileArchiveMetadata; | ||
import com.onthegomap.planetiler.geo.TileCoord; | ||
import com.onthegomap.planetiler.util.CloseableIterator; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import org.apache.commons.lang3.math.NumberUtils; | ||
|
||
public class ReadableFilesArchive implements ReadableTileArchive { | ||
|
||
private final Path basePath; | ||
|
||
private ReadableFilesArchive(Path basePath) { | ||
this.basePath = basePath; | ||
Preconditions.checkArgument( | ||
Files.isDirectory(basePath), | ||
"require \"" + basePath + "\" to be an existing directory" | ||
); | ||
} | ||
|
||
public static ReadableFilesArchive newReader(Path basePath) { | ||
return new ReadableFilesArchive(basePath); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("java:S1168") // returning null is in sync with other mbtiles/pmtiles implementation | ||
public byte[] getTile(int x, int y, int z) { | ||
final Path absolute = absolutePathFromTileCoord(basePath, TileCoord.ofXYZ(x, y, z)); | ||
if (!Files.exists(absolute)) { | ||
return null; | ||
} | ||
try { | ||
return Files.readAllBytes(absolute); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public CloseableIterator<TileCoord> getAllTileCoords() { | ||
|
||
try { | ||
final Stream<TileCoord> it = Files.find(basePath, 3, (p, a) -> a.isRegularFile()) | ||
.map(this::mapFileToTileCoord) | ||
.flatMap(Optional::stream); | ||
return CloseableIterator.of(it); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public TileArchiveMetadata metadata() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nothing to do here | ||
} | ||
|
||
private Optional<TileCoord> mapFileToTileCoord(Path path) { | ||
final Path relative = basePath.relativize(path); | ||
if (relative.getNameCount() != 3) { | ||
return Optional.empty(); | ||
} | ||
final int z = NumberUtils.toInt(relative.getName(0).toString(), -1); | ||
if (z < 0) { | ||
return Optional.empty(); | ||
} | ||
final int x = NumberUtils.toInt(relative.getName(1).toString(), -1); | ||
if (x < 0) { | ||
return Optional.empty(); | ||
} | ||
final String yPbf = relative.getName(2).toString(); | ||
int dotIdx = yPbf.indexOf('.'); | ||
if (dotIdx < 1) { | ||
return Optional.empty(); | ||
} | ||
final int y = NumberUtils.toInt(yPbf.substring(0, dotIdx), -1); | ||
if (y < 0) { | ||
return Optional.empty(); | ||
} | ||
if (!PBF_FILE_ENDING.equals(yPbf.substring(dotIdx))) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(TileCoord.ofXYZ(x, y, z)); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
planetiler-core/src/main/java/com/onthegomap/planetiler/files/WriteableFilesArchive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package com.onthegomap.planetiler.files; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.onthegomap.planetiler.archive.TileEncodingResult; | ||
import com.onthegomap.planetiler.archive.WriteableTileArchive; | ||
import com.onthegomap.planetiler.geo.TileOrder; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class WriteableFilesArchive implements WriteableTileArchive { | ||
|
||
private final Path basePath; | ||
|
||
private WriteableFilesArchive(Path basePath) { | ||
this.basePath = basePath; | ||
if (!Files.exists(basePath)) { | ||
mkdirs(basePath); | ||
} | ||
Preconditions.checkArgument( | ||
Files.isDirectory(basePath), | ||
"require \"" + basePath + "\" to be a directory" | ||
); | ||
} | ||
|
||
public static WriteableFilesArchive newWriter(Path basePath) { | ||
return new WriteableFilesArchive(basePath); | ||
} | ||
|
||
@Override | ||
public boolean deduplicates() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public TileOrder tileOrder() { | ||
return TileOrder.TMS; | ||
} | ||
|
||
@Override | ||
public TileWriter newTileWriter() { | ||
return new FilesWriter(basePath); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
// nothing to do here | ||
} | ||
|
||
private static void mkdirs(Path p) { | ||
try { | ||
Files.createDirectories(p); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
private static class FilesWriter implements TileWriter { | ||
|
||
private final Path basePath; | ||
private Path lastCheckedFolder; | ||
|
||
FilesWriter(Path basePath) { | ||
this.basePath = basePath; | ||
this.lastCheckedFolder = basePath; | ||
} | ||
|
||
@Override | ||
public void write(TileEncodingResult encodingResult) { | ||
|
||
final Path file = FilesArchiveUtils.absolutePathFromTileCoord(basePath, encodingResult.coord()); | ||
final Path folder = file.getParent(); | ||
|
||
// tiny optimization in order to avoid too many unnecessary "folder-exists-checks" (I/O) | ||
if (!lastCheckedFolder.equals(folder) && !Files.exists(folder)) { | ||
mkdirs(folder); | ||
} | ||
lastCheckedFolder = folder; | ||
try { | ||
Files.write(file, encodingResult.tileData()); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nothing to do here | ||
} | ||
} | ||
} |
Oops, something went wrong.