Skip to content

Instantly share code, notes, and snippets.

@zabop
Created November 15, 2025 12:57
Show Gist options
  • Select an option

  • Save zabop/bd09ae03bcfbf58d6c07a4ca3b940a4c to your computer and use it in GitHub Desktop.

Select an option

Save zabop/bd09ae03bcfbf58d6c07a4ca3b940a4c to your computer and use it in GitHub Desktop.
import numpy as np
import shapely
from typing import Callable
maxc = 20037508.342789244
sidelength: Callable[[int], float] = lambda z: 2 * maxc / (2**z)
def get_tile_centres(z: int, x: int, y: int) -> tuple[float, float]:
# return coordinates of tile centres in EPSG:3857
x = -maxc + maxc / (2**z) + sidelength(z) * x
y = maxc - maxc / (2**z) - sidelength(z) * y
return x, y
def get_tile_corners(z: int, x: int, y: int):
tc = get_tile_centres(z, x, y)
s = sidelength(z)
corners = []
for each in [[1, 1], [1, -1], [-1, -1], [-1, 1], [1, 1]]:
v = 0.5 * np.array([each]) * np.array([s, s])
corner = tc + v
corners.append(corner)
corners = np.squeeze(corners)
p = shapely.geometry.Polygon(corners)
return p
minx, miny, maxx, maxy = get_tile_corners(z, x, y).bounds
print(f"{minx}, {maxy}, {maxx}, {miny}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment