Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
  • 9 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
4 Makefile
@@ -7,12 +7,12 @@ clean:
.PHONY: pep8
pep8:
- find tilecloud tiles -name \*.py | xargs pep8 --ignore=E501
+ find examples tilecloud tiles -name \*.py | xargs pep8 --ignore=E501
pep8 --ignore=E501 tc-*
.PHONY: pyflakes
pyflakes:
- find tilecloud tiles -name \*.py | xargs pyflakes
+ find examples tilecloud tiles -name \*.py | xargs pyflakes
pyflakes tc-*
.PHONY: test
View
1  examples/download.py
@@ -14,7 +14,6 @@
logger = logging.getLogger(os.path.basename(sys.argv[0]))
-
def main(argv):
# Create our input and output TileStores
input_tile_store = TileStore.load('tiles.openstreetmap_org')
View
38 examples/renderingtheworld.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+
+from itertools import imap
+import logging
+import os.path
+import sys
+
+from tilecloud import Tile, TileCoord, TileStore, consume
+from tilecloud.filter.error import DropErrors
+from tilecloud.filter.logger import Logger
+from tilecloud.store.renderingtheworld import RenderingTheWorldTileStore
+
+
+logging.basicConfig(level=logging.DEBUG)
+logger = logging.getLogger(os.path.basename(sys.argv[0]))
+
+
+def subdivide(tile):
+ return tile.error is None and tile.tilecoord.z < 15
+
+
+def main(argv):
+ input_tile_store = TileStore.load('tiles.medford_buildings')
+ output_tile_store = TileStore.load('medford_buildings.mbtiles')
+ rendering_the_world_tile_store = RenderingTheWorldTileStore(subdivide, seed=Tile(TileCoord(0, 0, 0)))
+ tilestream = rendering_the_world_tile_store.list()
+ tilestream = imap(Logger(logger, logging.INFO, 'get %(tilecoord)s'), tilestream)
+ tilestream = input_tile_store.get(tilestream)
+ tilestream = imap(Logger(logger, logging.INFO, 'got %(tilecoord)s, error=%(error)s'), tilestream)
+ tilestream = rendering_the_world_tile_store.put(tilestream)
+ tilestream = imap(DropErrors(), tilestream)
+ tilestream = output_tile_store.put(tilestream)
+ tilestream = imap(Logger(logger, logging.INFO, 'saved %(tilecoord)s'), tilestream)
+ consume(tilestream, None)
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
View
12 tilecloud/__init__.py
@@ -238,7 +238,7 @@ def __init__(self, z, x, y):
self.y = y
def __cmp__(self, other):
- return cmp(self.x, other.x) or cmp(self.y, other.y) or cmp(self.z, other.z)
+ return cmp(self.z, other.z) or cmp(self.x, other.x) or cmp(self.y, other.y)
def __hash__(self):
return (self.x << self.z) ^ self.y
@@ -253,6 +253,12 @@ def __str__(self):
def normalize(self):
return (float(self.x) / (1 << self.z), float(self.y) / (1 << self.z))
+ def subdivide(self):
+ yield TileCoord(self.z + 1, 2 * self.x, 2 * self.y)
+ yield TileCoord(self.z + 1, 2 * self.x + 1, 2 * self.y)
+ yield TileCoord(self.z + 1, 2 * self.x, 2 * self.y + 1)
+ yield TileCoord(self.z + 1, 2 * self.x + 1, 2 * self.y + 1)
+
def tuple(self):
return (self.z, self.x, self.y)
@@ -304,8 +310,8 @@ def __init__(self, tilecoord, content_encoding=None, content_type=None,
for key, value in kwargs.iteritems():
setattr(self, key, value)
- def __nonzero__(self):
- return self.error is None
+ def __cmp__(self, other):
+ return cmp(self.tilecoord, other.tilecoord)
class TileStore(object):
View
4 tilecloud/store/dict.py
@@ -19,7 +19,7 @@ def delete_one(self, tile):
def get_one(self, tile):
if tile and tile.tilecoord in self.tiles:
- tile.data = self.tiles[tile.tilecoord]
+ tile.__dict__.update(self.tiles[tile.tilecoord])
return tile
else:
return None
@@ -30,5 +30,5 @@ def list(self):
def put_one(self, tile):
if tile:
- self.tiles[tile.tilecoord] = tile.data
+ self.tiles[tile.tilecoord] = tile.__dict__
return tile
View
29 tilecloud/store/renderingtheworld.py
@@ -0,0 +1,29 @@
+from collections import deque
+import logging
+
+from tilecloud import Tile, TileStore
+
+
+class RenderingTheWorldTileStore(TileStore):
+ """http://mapbox.com/blog/rendering-the-world/"""
+
+ def __init__(self, subdivide, queue=None, seed=None):
+ self.subdivide = subdivide
+ self.queue = queue
+ if self.queue is None:
+ self.queue = deque()
+ if seed is not None:
+ self.queue.append(seed)
+
+ def list(self):
+ try:
+ while True:
+ yield self.queue.popleft()
+ except IndexError:
+ pass
+
+ def put_one(self, tile):
+ if self.subdivide(tile):
+ for tilecoord in tile.tilecoord.subdivide():
+ self.queue.append(Tile(tilecoord))
+ return tile
View
5 tilecloud/store/url.py
@@ -1,8 +1,12 @@
+import logging
from urllib2 import HTTPError, Request, urlopen
from tilecloud import TileStore
+logger = logging.getLogger(__name__)
+
+
class URLTileStore(TileStore):
def __init__(self, tile_layouts, headers=None, **kwargs):
@@ -19,6 +23,7 @@ def get_one(self, tile):
url = tile_layout.filename(tile.tilecoord)
request = Request(url, headers=self.headers)
try:
+ logger.debug('GET %s' % (url,))
response = urlopen(request)
info = response.info()
if 'Content-Encoding' in info:
View
6 tilecloud/tests/test_tile.py
@@ -15,9 +15,3 @@ def test_empty(self):
def test_init_kwargs(self):
tile = Tile(TileCoord(0, 0, 0), kwarg=None)
self.assertEqual(tile.kwarg, None)
-
- def test_nonzero(self):
- tile = Tile(TileCoord(0, 0, 0))
- self.assertTrue(tile)
- tile.error = True
- self.assertFalse(tile)
View
4 tilecloud/tests/test_tilecoord.py
@@ -27,6 +27,10 @@ def test_str(self):
def test_normalize(self):
self.assertEqual(TileCoord(2, 2, 2).normalize(), (0.5, 0.5))
+ def test_subdivide(self):
+ tc = TileCoord(1, 2, 3)
+ self.assertEqual(sorted(tc.subdivide()), [TileCoord(2, 4, 6), TileCoord(2, 4, 7), TileCoord(2, 5, 6), TileCoord(2, 5, 7)])
+
def test_tuple(self):
self.assertEqual(TileCoord(1, 2, 3).tuple(), (1, 2, 3))
View
37 tilecloud/tests/test_tilestore.py
@@ -66,23 +66,29 @@ def test_one(self):
tilestream = [Tile(TileCoord(1, 0, 0), data='data'), None, Tile(TileCoord(1, 0, 1), error=True)]
tilestream = tile_store.put(tilestream)
tiles = list(tilestream)
- self.assertEqual(len(tiles), 1)
+ self.assertEqual(len(tiles), 2)
self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0))
self.assertEqual(tiles[0].data, 'data')
+ self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1))
+ self.assertEqual(tiles[1].error, True)
self.assertTrue(Tile(TileCoord(1, 0, 0)) in tile_store)
+ self.assertTrue(Tile(TileCoord(1, 0, 1)) in tile_store)
tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))]
tilestream = tile_store.get(tilestream)
consume(tilestream, None)
tiles = list(tile_store.get_all())
- self.assertEqual(len(tiles), 1)
+ self.assertEqual(len(tiles), 2)
self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0))
self.assertEqual(tiles[0].data, 'data')
+ self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1))
+ self.assertEqual(tiles[1].error, True)
tilestream = [Tile(TileCoord(1, 0, 0))]
tilestream = tile_store.delete(tilestream)
consume(tilestream, None)
tiles = list(tile_store.get_all())
- self.assertEqual(len(tiles), 0)
+ self.assertEqual(len(tiles), 1)
self.assertFalse(Tile(TileCoord(1, 0, 0)) in tile_store)
+ self.assertTrue(Tile(TileCoord(1, 0, 1)) in tile_store)
class TestMBTilesTileStore(unittest.TestCase):
@@ -93,30 +99,35 @@ def test_one(self):
tilestream = [Tile(TileCoord(1, 0, 0), data='data'), None, Tile(TileCoord(1, 0, 1), error=True)]
tilestream = tile_store.put(tilestream)
tiles = list(tilestream)
- self.assertEqual(tile_store.count(), 1)
- self.assertEqual(len(tiles), 1)
+ self.assertEqual(tile_store.count(), 2)
+ self.assertEqual(len(tiles), 2)
self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0))
self.assertEqual(tiles[0].data, 'data')
+ self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1))
+ self.assertEqual(tiles[1].error, True)
self.assertTrue(Tile(TileCoord(1, 0, 0)) in tile_store)
+ self.assertTrue(Tile(TileCoord(1, 0, 1)) in tile_store)
tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))]
tilestream = tile_store.get(tilestream)
consume(tilestream, None)
- self.assertEqual(tile_store.get_cheap_bounding_pyramid(), BoundingPyramid({1: (Bounds(0, 1), Bounds(0, 1))}))
- self.assertEqual(tile_store.count(), 1)
+ self.assertEqual(tile_store.get_cheap_bounding_pyramid(), BoundingPyramid({1: (Bounds(0, 1), Bounds(0, 2))}))
+ self.assertEqual(tile_store.count(), 2)
tiles = list(tile_store.list())
- self.assertEqual(len(tiles), 1)
- tiles = list(tile_store.get_all())
- self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0))
- self.assertEqual(len(tiles), 1)
+ self.assertEqual(len(tiles), 2)
+ tiles = sorted(tile_store.get_all())
+ self.assertEqual(len(tiles), 2)
self.assertEqual(tiles[0].tilecoord, TileCoord(1, 0, 0))
self.assertEqual(str(tiles[0].data), 'data')
+ self.assertEqual(tiles[1].tilecoord, TileCoord(1, 0, 1))
+ self.assertEqual(tiles[1].data, None)
tilestream = [Tile(TileCoord(1, 0, 0))]
tilestream = tile_store.delete(tilestream)
consume(tilestream, None)
- self.assertEqual(tile_store.count(), 0)
+ self.assertEqual(tile_store.count(), 1)
tiles = list(tile_store.get_all())
- self.assertEqual(len(tiles), 0)
+ self.assertEqual(len(tiles), 1)
self.assertFalse(Tile(TileCoord(1, 0, 0)) in tile_store)
+ self.assertTrue(Tile(TileCoord(1, 0, 1)) in tile_store)
def test_metadata(self):
tile_store = MBTilesTileStore(sqlite3.connect(':memory:'))

No commit comments for this range

Something went wrong with that request. Please try again.