Exemplo n.º 1
0
def main(argv):
    # Create our RenderingTheWorld tile store that will manage the queue and subdivision.
    # We pass it the function that decides whether a tile should be subdivided, and an initial tile.
    rendering_the_world_tilestore = RenderingTheWorldTileStore(subdivide, seeds=(Tile(TileCoord(0, 0, 0)),))
    # Start the tilestream by getting a list of all tiles to be generated.
    tilestream = rendering_the_world_tilestore.list()
    tilestream = imap(Logger(logger, logging.INFO, 'get %(tilecoord)s'), tilestream)
    # Create the tile store that will generate our tiles, in this case it's a demo WMTS server at OpenGeo.
    # Getting tiles from this store will either return the tile as a PNG file, or set an error on the tile if there are no features in this tile.
    generate_tilestore = WMTSTileStore(
        url='http://v2.suite.opengeo.org/geoserver/gwc/service/wmts/',
        layer='medford:buildings',
        style='_null',
        format='image/png',
        tile_matrix_set='EPSG:900913',
        tile_matrix=lambda z: 'EPSG:900913:%d' % (z,))
    tilestream = generate_tilestore.get(tilestream)
    tilestream = imap(Logger(logger, logging.INFO, 'got %(tilecoord)s, error=%(error)s'), tilestream)
    # Put the tile back into the RenderingTheWorld tile store.  This check whether the tile should be subdivided, and, if so, adds the tile's children to the list of tiles to be generated.
    tilestream = rendering_the_world_tilestore.put(tilestream)
    # Get rid of tiles that returned an error (i.e. where there was no data).
    tilestream = imap(DropErrors(), tilestream)
    # Store the generated tiles in the output tile store, in our case a local MBTiles file.
    output_tilestore = MBTilesTileStore(sqlite3.connect('medford_buildings.mbtiles'))
    tilestream = output_tilestore.put(tilestream)
    tilestream = imap(Logger(logger, logging.INFO, 'saved %(tilecoord)s'), tilestream)
    # Go!
    consume(tilestream, None)
Exemplo n.º 2
0
 def test_metadata(self):
     tilestore = MBTilesTileStore(sqlite3.connect(':memory:'))
     tilestore.put_one(Tile(TileCoord(1, 0, 0)))
     tilestore.put_one(Tile(TileCoord(2, 0, 0)))
     tilestore.set_metadata_zooms()
     self.assertEqual(int(tilestore.metadata['minzoom']), 1)
     self.assertEqual(int(tilestore.metadata['maxzoom']), 2)
     self.assertEqual(sorted(tilestore.metadata.itervalues()), ['1', '2'])
     self.assertEqual(sorted(tilestore.metadata.keys()), ['maxzoom', 'minzoom'])
Exemplo n.º 3
0
 def test_one(self):
     tilestore = MBTilesTileStore(sqlite3.connect(':memory:'), content_type='image/png')
     self.assertEqual(len(tilestore), 0)
     tilestream = [Tile(TileCoord(1, 0, 0), data='data'), None, Tile(TileCoord(1, 0, 1), error=True)]
     tilestream = tilestore.put(tilestream)
     tiles = list(tilestream)
     self.assertEqual(len(tilestore), 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 tilestore)
     self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
     tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))]
     tilestream = tilestore.get(tilestream)
     consume(tilestream, None)
     self.assertEqual(tilestore.get_cheap_bounding_pyramid(), BoundingPyramid({1: (Bounds(0, 1), Bounds(0, 2))}))
     self.assertEqual(len(tilestore), 2)
     tiles = list(tilestore.list())
     self.assertEqual(len(tiles), 2)
     tiles = sorted(tilestore.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 = tilestore.delete(tilestream)
     consume(tilestream, None)
     self.assertEqual(len(tilestore), 1)
     tiles = list(tilestore.get_all())
     self.assertEqual(len(tiles), 1)
     self.assertFalse(Tile(TileCoord(1, 0, 0)) in tilestore)
     self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
Exemplo n.º 4
0
 def test_metadata(self):
     tilestore = MBTilesTileStore(sqlite3.connect(':memory:'))
     tilestore.put_one(Tile(TileCoord(1, 0, 0)))
     tilestore.put_one(Tile(TileCoord(2, 0, 0)))
     tilestore.set_metadata_zooms()
     self.assertEqual(int(tilestore.metadata['minzoom']), 1)
     self.assertEqual(int(tilestore.metadata['maxzoom']), 2)
     self.assertEqual(sorted(tilestore.metadata.itervalues()), ['1', '2'])
     self.assertEqual(sorted(tilestore.metadata.keys()),
                      ['maxzoom', 'minzoom'])
Exemplo n.º 5
0
 def test_one(self):
     tilestore = MBTilesTileStore(sqlite3.connect(':memory:'),
                                  content_type='image/png')
     self.assertEqual(len(tilestore), 0)
     tilestream = [
         Tile(TileCoord(1, 0, 0), data='data'), None,
         Tile(TileCoord(1, 0, 1), error=True)
     ]
     tilestream = tilestore.put(tilestream)
     tiles = list(tilestream)
     self.assertEqual(len(tilestore), 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 tilestore)
     self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
     tilestream = [Tile(TileCoord(1, 0, 0)), Tile(TileCoord(1, 0, 1))]
     tilestream = tilestore.get(tilestream)
     consume(tilestream, None)
     self.assertEqual(tilestore.get_cheap_bounding_pyramid(),
                      BoundingPyramid({1: (Bounds(0, 1), Bounds(0, 2))}))
     self.assertEqual(len(tilestore), 2)
     tiles = list(tilestore.list())
     self.assertEqual(len(tiles), 2)
     tiles = sorted(tilestore.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 = tilestore.delete(tilestream)
     consume(tilestream, None)
     self.assertEqual(len(tilestore), 1)
     tiles = list(tilestore.get_all())
     self.assertEqual(len(tiles), 1)
     self.assertFalse(Tile(TileCoord(1, 0, 0)) in tilestore)
     self.assertTrue(Tile(TileCoord(1, 0, 1)) in tilestore)
Exemplo n.º 6
0
    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>

        mongodb://<connection>/<collection>

        <filename>.bsddb

        <filename>.mbtiles

        <filename>.zip

        <module>

        """
        if name == 'null://':
            from tilebox.store.null import NullTileStore
            return NullTileStore()
        if name.startswith('bounds://'):
            from tilebox.store.boundingpyramid import BoundingPyramidTileStore
            return BoundingPyramidTileStore(
                BoundingPyramid.from_string(name[9:]))
        if name.startswith('file://'):
            from tilebox.layout.template import TemplateTileLayout
            from tilebox.store.filesystem import FilesystemTileStore
            return FilesystemTileStore(TemplateTileLayout(name[7:]), )
        if name.startswith('http://') or name.startswith('https://'):
            from tilebox.layout.template import TemplateTileLayout
            from tilebox.store.url import URLTileStore
            return URLTileStore((TemplateTileLayout(name), ))
        if name.startswith('memcached://'):
            from tilebox.layout.template import TemplateTileLayout
            from tilebox.store.memcached import MemcachedTileStore
            from tilebox.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 tilebox.layout.template import TemplateTileLayout
            from tilebox.store.s3 import S3TileStore
            bucket, template = name[5:].split('/', 1)
            return S3TileStore(bucket, TemplateTileLayout(template))
        if name.startswith('mongodb://'):
            from tilebox.store.mongodb import MongoDbTileStore
            connection, database, collection = name[10:].split('/', 2)
            return MongoDbTileStore(connection, database, collection)
        if name.startswith('sqs://'):
            from tilebox.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 tilebox.store.bsddb import BSDDBTileStore
            return BSDDBTileStore(bsddb.hashopen(name))
        if ext == '.mbtiles':
            import sqlite3
            from tilebox.store.mbtiles import MBTilesTileStore
            return MBTilesTileStore(sqlite3.connect(name))
        if ext == '.tilelite':
            import sqlite3
            from tilebox.store.tilelite import TileliteTileStore
            return TileliteTileStore(sqlite3.connect(name))
        if ext == '.zip':
            import zipfile
            from tilebox.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')
Exemplo n.º 7
0
 def test_empty(self):
     connection = sqlite3.connect(':memory:')
     tilestore = MBTilesTileStore(connection)
     self.assertEqual(len(tilestore), 0)
     self.assertEqual(tilestore.get_one(Tile(TileCoord(0, 0, 0))), None)
Exemplo n.º 8
0
 def test_content_type(self):
     connection = sqlite3.connect(':memory:')
     tilestore1 = MBTilesTileStore(connection)
     tilestore1.metadata['format'] = 'png'
     tilestore2 = MBTilesTileStore(connection)
     self.assertEqual(tilestore2.content_type, 'image/png')
Exemplo n.º 9
0
 def test_empty(self):
     connection = sqlite3.connect(':memory:')
     tilestore = MBTilesTileStore(connection)
     self.assertEqual(len(tilestore), 0)
     self.assertEqual(tilestore.get_one(Tile(TileCoord(0, 0, 0))), None)
Exemplo n.º 10
0
 def test_content_type(self):
     connection = sqlite3.connect(':memory:')
     tilestore1 = MBTilesTileStore(connection)
     tilestore1.metadata['format'] = 'png'
     tilestore2 = MBTilesTileStore(connection)
     self.assertEqual(tilestore2.content_type, 'image/png')