Skip to content

Instantly share code, notes, and snippets.

@zabop
Created November 15, 2025 13:11
Show Gist options
  • Select an option

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

Select an option

Save zabop/7404d3ba218ea97e3346896fa1b76aea to your computer and use it in GitHub Desktop.
import numpy as np
import shapely.ops
import shapely
import pyproj
from typing import Callable
z,x,y = 6, 36, 16
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
trans = pyproj.Transformer.from_crs(
crs_from="EPSG:3857", crs_to="EPSG:25835", always_xy=True
).transform
transformed_tile = shapely.ops.transform(trans, get_tile_corners(z, x, y))
xys = [p for p in transformed_tile.exterior.coords]
xs = sorted([x for x, _ in xys])
ys = sorted([y for _, y in xys])
inner_rectangle = shapely.geometry.box(xs[1],ys[1],xs[2],ys[2])
print(inner_rectangle.wkt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment