Skip to content

Commit 0785969

Browse files
committed
Convert Tile to use tileid
1 parent d71a4d3 commit 0785969

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ dependencies = [
3838
"fastapi",
3939
"fs",
4040
"Jinja2",
41+
"pmtiles",
4142
"prometheus_client",
4243
"psycopg",
4344
"pyyaml",

tests/test_tile.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,21 @@ def test_bounds(self):
1414
t = Tile(3, 2, 1)
1515
self.assertEqual(t.bbox(0), 'ST_TileEnvelope(3, 2, 1, margin=>0)')
1616
self.assertEqual(t.bbox(8/4096), 'ST_TileEnvelope(3, 2, 1, margin=>0.001953125)')
17+
18+
def test_eq(self):
19+
t1 = Tile(3, 2, 1)
20+
t2 = Tile(3, 2, 1)
21+
t3 = Tile(3, 1, 1)
22+
23+
self.assertEqual(t1, t2)
24+
self.assertNotEqual(t1, t3)
25+
26+
def test_tileid(self):
27+
self.assertEqual(Tile(0, 0, 0).tileid, 0)
28+
self.assertEqual(Tile(0, 0, 0), Tile.from_tileid(0))
29+
self.assertEqual(Tile(1, 0, 0).tileid, 1)
30+
self.assertEqual(Tile(1, 0, 0), Tile.from_tileid(1))
31+
self.assertEqual(Tile(2, 0, 0).tileid, 5)
32+
self.assertEqual(Tile(2, 0, 0), Tile.from_tileid(5))
33+
self.assertEqual(Tile(2, 1, 0).tileid, 6)
34+
self.assertEqual(Tile(2, 1, 0), Tile.from_tileid(6))

tilekiln/tile.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
# TODO: Add dataclass
1+
import pmtiles.tile # type: ignore
22
# TODO: __slots__?
3+
4+
35
class Tile:
6+
__slots__ = ("tileid")
7+
48
def __init__(self, zoom: int, x: int, y: int):
59
'''Creates a tile object, with x, y, and zoom
610
'''
7-
assert zoom >= 0
8-
assert x >= 0
9-
assert x < 2**zoom
10-
assert y >= 0
11-
assert y < 2**zoom
11+
self.tileid = pmtiles.tile.zxy_to_tileid(zoom, x, y)
12+
13+
def __eq__(self, other):
14+
return isinstance(other, self.__class__) and self.tileid == other.tileid
15+
16+
@property
17+
def zxy(self):
18+
return pmtiles.tile.tileid_to_zxy(self.tileid)
1219

13-
self.zoom = zoom
14-
self.x = x
15-
self.y = y
20+
@property
21+
def zoom(self):
22+
return self.zxy[0]
23+
24+
@property
25+
def x(self):
26+
return self.zxy[1]
27+
28+
@property
29+
def y(self):
30+
return self.zxy[2]
1631

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

40+
@classmethod
41+
def from_tileid(cls, tileid: int):
42+
# TODO: This converts id to xyz to id, there should be a way with less calls
43+
(zoom, x, y) = pmtiles.tile.tileid_to_zxy(tileid)
44+
return cls(zoom, x, y)
45+
2546
def bbox(self, buffer) -> str:
2647
'''Returns the bounding box for a tile
2748
'''

0 commit comments

Comments
 (0)