Beispiel #1
0
    def get_store(self, cache, layer, read_only=False):
        grid = layer['grid_ref'] if 'grid_ref' in layer else None
        layout = WMTSTileLayout(
            layer=layer['name'],
            url=cache['folder'],
            style=layer['wmts_style'],
            format='.' + layer['extension'],
            dimensions_name=[
                dimension['name'] for dimension in layer['dimensions']
            ],
            tile_matrix_set=layer['grid'],
            tile_matrix=lambda z: get_tile_matrix_identifier(grid, zoom=z),
            request_encoding='REST',
        )
        # store
        if cache['type'] == 's3':
            # on s3
            cache_tilestore = S3TileStore(
                cache['bucket'],
                layout,
                s3_host=cache.get('host'),
                cache_control=cache.get('cache_control'))  # pragma: no cover
        elif cache['type'] == 'mbtiles':
            metadata = {}
            for dimension in layer['dimensions']:
                metadata['dimension_' +
                         dimension['name']] = dimension['default']
            # on mbtiles file
            filename = layout.filename(TileCoord(
                0, 0, 0), metadata=metadata).replace('/0/0/0', '') + '.mbtiles'
            if not os.path.exists(os.path.dirname(filename)):
                os.makedirs(os.path.dirname(filename))
            cache_tilestore = MBTilesTileStore(
                sqlite3.connect(filename),
                content_type=layer['mime_type'],
                tilecoord_in_topleft=True,
            )
        elif cache['type'] == 'bsddb':
            metadata = {}
            for dimension in layer['dimensions']:
                metadata['dimension_' +
                         dimension['name']] = dimension['default']
            import bsddb3 as bsddb
            from tilecloud.store.bsddb import BSDDBTileStore

            # on bsddb file
            filename = layout.filename(TileCoord(
                0, 0, 0), metadata=metadata).replace('/0/0/0', '') + '.bsddb'
            if not os.path.exists(os.path.dirname(filename)):
                os.makedirs(os.path.dirname(filename))
            db = bsddb.hashopen(
                filename,
                # and os.path.exists(filename) to avoid error on non existing file
                'r' if read_only and os.path.exists(filename) else 'c')

            class Close:
                def __init__(self, db):
                    self.db = db

                def __call__(self):
                    self.db.close()

            self._close_actions.append(Close(db))

            cache_tilestore = BSDDBTileStore(
                db,
                content_type=layer['mime_type'],
            )
        elif cache['type'] == 'filesystem':
            # on filesystem
            cache_tilestore = FilesystemTileStore(
                layout,
                content_type=layer['mime_type'],
            )
        else:
            exit('unknown cache type: ' + cache['type'])  # pragma: no cover

        return cache_tilestore
Beispiel #2
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>

        <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')
Beispiel #3
0
    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")