Skip to content

Commit

Permalink
Convert Tile to use tileid
Browse files Browse the repository at this point in the history
  • Loading branch information
pnorman committed Apr 24, 2024
1 parent d71a4d3 commit 0785969
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies = [
"fastapi",
"fs",
"Jinja2",
"pmtiles",
"prometheus_client",
"psycopg",
"pyyaml",
Expand Down
18 changes: 18 additions & 0 deletions tests/test_tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,21 @@ def test_bounds(self):
t = Tile(3, 2, 1)
self.assertEqual(t.bbox(0), 'ST_TileEnvelope(3, 2, 1, margin=>0)')
self.assertEqual(t.bbox(8/4096), 'ST_TileEnvelope(3, 2, 1, margin=>0.001953125)')

def test_eq(self):
t1 = Tile(3, 2, 1)
t2 = Tile(3, 2, 1)
t3 = Tile(3, 1, 1)

self.assertEqual(t1, t2)
self.assertNotEqual(t1, t3)

def test_tileid(self):
self.assertEqual(Tile(0, 0, 0).tileid, 0)
self.assertEqual(Tile(0, 0, 0), Tile.from_tileid(0))
self.assertEqual(Tile(1, 0, 0).tileid, 1)
self.assertEqual(Tile(1, 0, 0), Tile.from_tileid(1))
self.assertEqual(Tile(2, 0, 0).tileid, 5)
self.assertEqual(Tile(2, 0, 0), Tile.from_tileid(5))
self.assertEqual(Tile(2, 1, 0).tileid, 6)
self.assertEqual(Tile(2, 1, 0), Tile.from_tileid(6))
39 changes: 30 additions & 9 deletions tilekiln/tile.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
# TODO: Add dataclass
import pmtiles.tile # type: ignore
# TODO: __slots__?


class Tile:
__slots__ = ("tileid")

def __init__(self, zoom: int, x: int, y: int):
'''Creates a tile object, with x, y, and zoom
'''
assert zoom >= 0
assert x >= 0
assert x < 2**zoom
assert y >= 0
assert y < 2**zoom
self.tileid = pmtiles.tile.zxy_to_tileid(zoom, x, y)

def __eq__(self, other):
return isinstance(other, self.__class__) and self.tileid == other.tileid

@property
def zxy(self):
return pmtiles.tile.tileid_to_zxy(self.tileid)

self.zoom = zoom
self.x = x
self.y = y
@property
def zoom(self):
return self.zxy[0]

@property
def x(self):
return self.zxy[1]

@property
def y(self):
return self.zxy[2]

def __repr__(self) -> str:
return f"Tile({self.zoom},{self.x},{self.y})"
Expand All @@ -22,6 +37,12 @@ def from_string(cls, tile: str):
fragments = tile.split("/")
return cls(int(fragments[0]), int(fragments[1]), int(fragments[2]))

@classmethod
def from_tileid(cls, tileid: int):
# TODO: This converts id to xyz to id, there should be a way with less calls
(zoom, x, y) = pmtiles.tile.tileid_to_zxy(tileid)
return cls(zoom, x, y)

def bbox(self, buffer) -> str:
'''Returns the bounding box for a tile
'''
Expand Down

0 comments on commit 0785969

Please sign in to comment.