def main(): # Create our input and output TileStores input_tilestore = TileStore.load('tiles.openstreetmap_org') output_tilestore = TileStore.load('local.mbtiles') # 1. Generate a list of tiles to download from a BoundingPyramid # 4/8/5 is the root tile, corresponding to Central Europe # +3/+1/+1 specifies up to zoom level 4 + 3 = 7 and an extent of one tile in the X and Y directions bounding_pyramid = BoundingPyramid.from_string('4/8/5:+3/+1/+1') bounding_pyramid_tilestore = BoundingPyramidTileStore(bounding_pyramid) tilestream = bounding_pyramid_tilestore.list() # 2. Filter out tiles that already downloaded tilestream = (tile for tile in tilestream if tile not in output_tilestore) # 3. Get the tile from openstreetmap.org tilestream = input_tilestore.get(tilestream) # 4. Save the tile to local.mbtiles tilestream = output_tilestore.put(tilestream) # 5. Log the fact that the tile was downloaded tilestream = imap(Logger(logger, logging.INFO, 'downloaded %(tilecoord)s'), tilestream) # Go! consume(tilestream, None)
def main(argv): # Create our input and output TileStores input_tilestore = TileStore.load('tiles.openstreetmap_org') output_tilestore = TileStore.load('local.mbtiles') # 1. Generate a list of tiles to download from a BoundingPyramid # 4/8/5 is the root tile, corresponding to Central Europe # +3/+1/+1 specifies up to zoom level 4 + 3 = 7 and an extent of one tile in the X and Y directions bounding_pyramid = BoundingPyramid.from_string('4/8/5:+3/+1/+1') bounding_pyramid_tilestore = BoundingPyramidTileStore(bounding_pyramid) tilestream = bounding_pyramid_tilestore.list() # 2. Filter out tiles that already downloaded tilestream = (tile for tile in tilestream if not tile in output_tilestore) # 3. Get the tile from openstreetmap.org tilestream = input_tilestore.get(tilestream) # 4. Save the tile to local.mbtiles tilestream = output_tilestore.put(tilestream) # 5. Log the fact that the tile was downloaded tilestream = imap(Logger(logger, logging.INFO, 'downloaded %(tilecoord)s'), tilestream) # Go! consume(tilestream, None)
def load(cls, name): # pragma: no cover """ Construct a :class:`TileStore` from a name. :param name: Name :type name: string :rtype: :class:`TileStore` The following shortcuts are available: bounds://<bounding-pyramid> file://<template> http://<template> and https://<template> memcached://<server>:<port>/<template> s3://<bucket>/<template> sqs://<region>/<queue> <filename>.bsddb <filename>.mbtiles <filename>.zip <module> """ if name == 'null://': from tilecloud.store.null import NullTileStore return NullTileStore() if name.startswith('bounds://'): from tilecloud.store.boundingpyramid import BoundingPyramidTileStore return BoundingPyramidTileStore( BoundingPyramid.from_string(name[9:])) if name.startswith('file://'): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.filesystem import FilesystemTileStore return FilesystemTileStore(TemplateTileLayout(name[7:]), ) if name.startswith('http://') or name.startswith('https://'): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.url import URLTileStore return URLTileStore((TemplateTileLayout(name), )) if name.startswith('memcached://'): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.memcached import MemcachedTileStore from tilecloud.lib.memcached import MemcachedClient server, template = name[12:].split('/', 1) host, port = server.split(':', 1) client = MemcachedClient(host, int(port)) return MemcachedTileStore(client, TemplateTileLayout(template)) if name.startswith('s3://'): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.s3 import S3TileStore bucket, template = name[5:].split('/', 1) return S3TileStore(bucket, TemplateTileLayout(template)) if name.startswith('sqs://'): from tilecloud.store.sqs import SQSTileStore import boto.sqs from boto.sqs.jsonmessage import JSONMessage region_name, queue_name = name[6:].split('/', 1) connection = boto.sqs.connect_to_region(region_name) queue = connection.create_queue(queue_name) queue.set_message_class(JSONMessage) return SQSTileStore(queue) root, ext = os.path.splitext(name) if ext == '.bsddb': import bsddb from tilecloud.store.bsddb import BSDDBTileStore return BSDDBTileStore(bsddb.hashopen(name)) if ext == '.mbtiles': import sqlite3 from tilecloud.store.mbtiles import MBTilesTileStore return MBTilesTileStore(sqlite3.connect(name)) if ext == '.zip': import zipfile from tilecloud.store.zip import ZipTileStore return ZipTileStore(zipfile.ZipFile(name, 'a')) module = __import__(name) components = name.split('.') module = reduce(lambda module, attr: getattr(module, attr), components[1:], module) return getattr(module, 'tilestore')
def load(name): # pragma: no cover """ Construct a :class:`TileStore` from a name. :param name: Name :type name: string :rtype: :class:`TileStore` The following shortcuts are available: bounds://<bounding-pyramid> file://<template> http://<template> and https://<template> memcached://<server>:<port>/<template> s3://<bucket>/<template> sqs://<region>/<queue> <filename>.bsddb <filename>.mbtiles <filename>.zip <module> """ if name == "null://": from tilecloud.store.null import NullTileStore return NullTileStore() if name.startswith("bounds://"): from tilecloud.store.boundingpyramid import BoundingPyramidTileStore return BoundingPyramidTileStore( BoundingPyramid.from_string(name[9:])) if name.startswith("file://"): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.filesystem import FilesystemTileStore return FilesystemTileStore(TemplateTileLayout(name[7:]), ) if name.startswith("http://") or name.startswith("https://"): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.url import URLTileStore return URLTileStore((TemplateTileLayout(name), )) if name.startswith("memcached://"): from tilecloud.layout.template import TemplateTileLayout from tilecloud.lib.memcached import MemcachedClient from tilecloud.store.memcached import MemcachedTileStore server, template = name[12:].split("/", 1) host, port = server.split(":", 1) client = MemcachedClient(host, int(port)) return MemcachedTileStore(client, TemplateTileLayout(template)) if name.startswith("s3://"): from tilecloud.layout.template import TemplateTileLayout from tilecloud.store.s3 import S3TileStore bucket, template = name[5:].split("/", 1) return S3TileStore(bucket, TemplateTileLayout(template)) if name.startswith("sqs://"): import boto.sqs # pylint: disable=import-error from boto.sqs.jsonmessage import JSONMessage # pylint: disable=import-error from tilecloud.store.sqs import SQSTileStore region_name, queue_name = name[6:].split("/", 1) connection = boto.sqs.connect_to_region(region_name) queue = connection.create_queue(queue_name) queue.set_message_class(JSONMessage) return SQSTileStore(queue) if name.startswith("redis://"): from tilecloud.store.redis import RedisTileStore return RedisTileStore(name) _, ext = os.path.splitext(name) if ext == ".bsddb": import bsddb # pylint: disable=import-error from tilecloud.store.bsddb import BSDDBTileStore return BSDDBTileStore(bsddb.hashopen(name)) if ext == ".mbtiles": import sqlite3 from tilecloud.store.mbtiles import MBTilesTileStore return MBTilesTileStore(sqlite3.connect(name)) if ext == ".zip": import zipfile from tilecloud.store.zip import ZipTileStore return ZipTileStore(zipfile.ZipFile(name, "a")) module = __import__(name) components = name.split(".") module = reduce(getattr, components[1:], module) return getattr(module, "tilestore")