Skip to content

Commit

Permalink
debug tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
dsparber committed Dec 5, 2024
1 parent c1887ec commit 53fce17
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
58 changes: 58 additions & 0 deletions main/debug/debug_tiles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
from datetime import timedelta
from random import randint

from PIL import Image, ImageDraw, ImageFont

from commons import R2Client, BucketTileOutput, CompositeTileOutput
from elevation import ElevationTools
from tiles import TileInfo, WebmercatorTileInfo


def create_png(tile: TileInfo) -> Image:
r, g, b = (randint(0, 128), randint(0, 128), randint(0, 128))
image = Image.new("RGBA", (256, 256), (r, g, b, 50))
draw = ImageDraw.Draw(image)

draw.rectangle([(0, 0), (255, 255)], outline=(r, g, b, 255), width=5)

font = ImageFont.truetype("Arial.ttf", size=36)
text = f"z: {tile.zoom}\nx: {tile.x}\ny: {tile.y}"
draw.text((10, 10), text, fill=(r, g, b, 255), font=font)

return image


def main() -> None:
r2 = R2Client()

max_age = int(timedelta(days=1).total_seconds())
cache_control = f"max-age={max_age}"
output = CompositeTileOutput(
[
BucketTileOutput(
bucket=r2.maps_dev,
base_path=f"v1/map/4326/debug",
cache_control=cache_control,
),
BucketTileOutput(
bucket=r2.maps_dev,
base_path=f"v1/map/3857/debug",
cache_control=cache_control,
),
]
)

def create_debug_tile(tile: TileInfo) -> None:
path = f"{tile.path}.png"
os.makedirs(os.path.dirname(path), exist_ok=True)
create_png(tile).save(path)
output.upload(path, tile)

tiles = WebmercatorTileInfo.all_tiles(max_zoom=8)
ElevationTools.apply_for_all_tile_infos(tiles, create_debug_tile)


# Example usage
if __name__ == "__main__":
main()
16 changes: 15 additions & 1 deletion tiles/webmercator_tile_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ def children(self) -> set[WebmercatorTileInfo]:
def parent(self) -> WebmercatorTileInfo:
return WebmercatorTileInfo(zoom=self.zoom - 1, x=self.x // 2, y=self.y // 2)

def descendants(self, max_zoom: int, min_zoom: int = 0) -> Iterable[TileInfo]:
def descendants(
self, max_zoom: int, min_zoom: int = 0
) -> Iterable[WebmercatorTileInfo]:
if self.zoom > max_zoom:
return []

Expand Down Expand Up @@ -137,3 +139,15 @@ def is_within_bounds(tile: TileInfo) -> bool:
min_zoom=min_zoom, max_zoom=max_zoom
),
)

@staticmethod
def root() -> WebmercatorTileInfo:
return WebmercatorTileInfo(zoom=0, x=0, y=0)

@staticmethod
def all_tiles(
min_zoom: int = 0, max_zoom: int = 10
) -> Iterable[WebmercatorTileInfo]:
return WebmercatorTileInfo.root().descendants(
min_zoom=min_zoom, max_zoom=max_zoom
)

0 comments on commit 53fce17

Please sign in to comment.