Skip to content

Commit

Permalink
configurable debug url
Browse files Browse the repository at this point in the history
  • Loading branch information
msbarry committed Sep 16, 2023
1 parent a663fed commit 7212583
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private String getLastTileLogDetails() {
lastTile.z(), lastTile.x(), lastTile.y(),
lastTile.z(),
Format.defaultInstance().percent(archive.tileOrder().progressOnLevel(lastTile, config.bounds().tileExtents())),
lastTile.getDebugUrl()
lastTile.getDebugUrl(config.debugUrlPattern())
);
}
return "last tile: " + blurb;
Expand Down Expand Up @@ -389,7 +389,7 @@ private void tileWriter(Iterable<TileBatch> tileBatches) throws ExecutionExcepti
private void printTileStats() {
if (LOGGER.isDebugEnabled()) {
Format format = Format.defaultInstance();
tileStats.printStats();
tileStats.printStats(config.debugUrlPattern());
LOGGER.debug(" # features: {}", format.integer(featuresProcessed.get()));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public record PlanetilerConfig(
Boolean color,
boolean keepUnzippedSources,
TileCompression tileCompression,
boolean outputLayerStats
boolean outputLayerStats,
String debugUrlPattern
) {

public static final int MIN_MINZOOM = 0;
Expand Down Expand Up @@ -192,7 +193,9 @@ public static PlanetilerConfig from(Arguments arguments) {
"the tile compression, one of " +
TileCompression.availableValues().stream().map(TileCompression::id).toList(),
"gzip")),
arguments.getBoolean("output_layerstats", "output a tsv.gz file for each tile/layer size", false)
arguments.getBoolean("output_layerstats", "output a tsv.gz file for each tile/layer size", false),
arguments.getString("debug_url", "debug url to use for displaying tiles with ${z} ${lat} ${lon} placeholders",
"https://onthegomap.github.io/planetiler-demo/#${z}/${lat}/${lon}")
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static com.onthegomap.planetiler.config.PlanetilerConfig.MAX_MAXZOOM;

import com.onthegomap.planetiler.util.Format;
import com.onthegomap.planetiler.util.Hilbert;
import java.text.DecimalFormat;
import java.util.Map;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.text.StringSubstitutor;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.CoordinateXY;
import org.locationtech.jts.geom.Envelope;
Expand Down Expand Up @@ -126,19 +128,32 @@ public int compareTo(TileCoord o) {
}

/** Returns the latitude/longitude of the northwest corner of this tile. */
public Coordinate getLatLon() {
public Envelope getEnvelope() {
double worldWidthAtZoom = Math.pow(2, z);
return new CoordinateXY(
return new Envelope(
GeoUtils.getWorldLon(x / worldWidthAtZoom),
GeoUtils.getWorldLon((x + 1) / worldWidthAtZoom),
GeoUtils.getWorldLat((y + 1) / worldWidthAtZoom),
GeoUtils.getWorldLat(y / worldWidthAtZoom)
);
}


/** Returns a URL that displays the openstreetmap data for this tile. */
public String getDebugUrl() {
Coordinate coord = getLatLon();
return Format.osmDebugUrl(z, coord);
public String getDebugUrl(String pattern) {
Coordinate center = getEnvelope().centre();
DecimalFormat format = new DecimalFormat("0.#####");
String lat = format.format(center.y);
String lon = format.format(center.x);
String zoom = z + ".5";
return StringSubstitutor.replace(pattern, Map.of(
"lat", lat,
"latitude", lat,
"lon", lon,
"longitude", lon,
"zoom", zoom,
"z", zoom
));
}

/** Returns the pixel coordinate on this tile of a given latitude/longitude (assuming 256x256 px tiles). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
public class TileStats {

private static final int BATCH_SIZE = 1_000;
private static final int WARN_BYTES = 100_000;
private static final int ERROR_BYTES = 500_000;

private static final Logger LOGGER = LoggerFactory.getLogger(TileStats.class);

private static final CsvMapper MAPPER = new CsvMapper();
Expand Down Expand Up @@ -155,7 +158,7 @@ record Batch(List<Tile> tiles, CompletableFuture<List<String>> stats) {}

timer.stop();
if (LOGGER.isDebugEnabled()) {
tileStats.printStats();
tileStats.printStats(config.debugUrlPattern());
}
stats.printSummary();
}
Expand Down Expand Up @@ -225,7 +228,7 @@ public static List<LayerStats> computeTileStats(VectorTileProto.Tile proto) {
return result;
}

public void printStats() {
public void printStats(String debugUrlPattern) {
if (LOGGER.isDebugEnabled()) {
Summary result = summary();
var overallStats = result.get();
Expand All @@ -241,15 +244,15 @@ public void printStats() {
tile.coord.x(),
tile.coord.y(),
formatter.storage(tile.size),
tile.coord.getDebugUrl(),
tile.coord.getDebugUrl(debugUrlPattern),
tileBiggestLayers(formatter, tile)
);
}).collect(Collectors.joining("\n"))
);
var alreadyListed = biggestTiles.stream().map(TileSummary::coord).collect(Collectors.toSet());
var otherTiles = result.layers().stream()
.flatMap(layer -> result.get(layer).biggestTiles().stream().limit(1))
.filter(tile -> !alreadyListed.contains(tile.coord) && tile.size > 100_000)
.filter(tile -> !alreadyListed.contains(tile.coord) && tile.size > WARN_BYTES)
.toList();
if (!otherTiles.isEmpty()) {
LOGGER.info("Other tiles with large layers:\n{}",
Expand All @@ -259,16 +262,16 @@ public void printStats() {
tile.coord.x(),
tile.coord.y(),
formatter.storage(tile.size),
tile.coord.getDebugUrl(),
tile.coord.getDebugUrl(debugUrlPattern),
tileBiggestLayers(formatter, tile)
)).collect(Collectors.joining("\n")));
}

LOGGER.debug("Max tile sizes:\n{}\n{}\n{}",
writeStatsTable(result, n -> {
String string = " " + formatter.storage(n, true);
return n.intValue() > 500_000 ? AnsiColors.red(string) :
n.intValue() > 100_000 ? AnsiColors.yellow(string) :
return n.intValue() > ERROR_BYTES ? AnsiColors.red(string) :
n.intValue() > WARN_BYTES ? AnsiColors.yellow(string) :
string;
}, SummaryCell::maxSize),
writeStatsRow(result, "full tile",
Expand All @@ -291,10 +294,9 @@ public void printStats() {
}

private static String tileBiggestLayers(Format formatter, TileSummary tile) {
int minSize = tile.layers.stream().mapToInt(l -> l.layerBytes).max().orElse(0);
return tile.layers.stream()
.filter(
d -> d.layerBytes > Math.min(100_000,
tile.layers.stream().mapToInt(l -> l.layerBytes).max().orElse(0)))
.filter(d -> d.layerBytes >= minSize)
.sorted(Comparator.comparingInt(d -> -d.layerBytes))
.map(d -> d.layer + ":" + formatter.storage(d.layerBytes))
.collect(Collectors.joining(", "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,14 @@ void testTileProgressOnLevelHilbert(int x, int y, int z, double p) {
TileExtents.computeFromWorldBounds(15, GeoUtils.WORLD_BOUNDS));
assertEquals(p, progress);
}

@ParameterizedTest
@CsvSource({
"0,0,0,0.5/0/0",
"0,0,1,1.5/42.52556/-90",
"123,123,14,14.5/84.81142/-177.28638",
})
void testDebugUrl(int x, int y, int z, String expected) {
assertEquals(expected, TileCoord.ofXYZ(x, y, z).getDebugUrl("${z}/${lat}/${lon}"));
}
}

0 comments on commit 7212583

Please sign in to comment.