Example #1
0
    def test_fill_borders(self):
        with NamedTemporaryFile(suffix='.mbtiles') as output:
            # fill-borders
            command = [sys.executable, self.script,
                       '--fill-borders',
                       self.spanningfile, output.name]
            check_call(command, env=self.environ)

            # Dataset (bluemarble-spanning-ll.tif) bounds in EPSG:4326
            dataset_bounds = '-180.0,-90.0,0.0,0.0'

            with MBTiles(output.name) as mbtiles:
                # Default metadata
                cursor = mbtiles._conn.execute('SELECT * FROM metadata')
                self.assertTrue(dict(cursor.fetchall()),
                                dict(name=os.path.basename(self.inputfile),
                                     description='',
                                     format='png',
                                     type='overlay',
                                     version='1.0.0',
                                     bounds=dataset_bounds))
                # 16 tiles
                cursor = cursor.execute('SELECT COUNT(*) FROM tiles')
                self.assertTrue(cursor.fetchone(), [16])

            # --no-fill-borders
            command = [sys.executable, self.script,
                       '--no-fill-borders',
                       self.spanningfile, output.name]
            check_call(command, env=self.environ)
            with MBTiles(output.name) as mbtiles:
                # 4 tiles, since the borders were not created
                cursor = mbtiles._conn.execute('SELECT COUNT(*) FROM tiles')
                self.assertTrue(cursor.fetchone(), [4])
Example #2
0
 def test_simple(self):
     with NamedTemporaryFile(suffix='.mbtiles') as output:
         command = [sys.executable, self.script, self.inputfile, output.name]
         check_call(command, env=self.environ)
         with MBTiles(output.name) as mbtiles:
             # 4×4 at resolution 2
             cursor = mbtiles._conn.execute('SELECT COUNT(*) FROM tiles')
             self.assertEqual(cursor.fetchone(), (1,))
Example #3
0
    def test_metadata(self):
        with NamedTemporaryFile(suffix='.mbtiles') as output:
            command = [sys.executable, self.script, self.inputfile, output.name]
            check_call(command, env=self.environ)

            # Dataset (upsampling.tif) bounds in EPSG:4326
            dataset_bounds = '-180.0,-90.0,180.0,90.0'

            with MBTiles(output.name) as mbtiles:
                # Default metadata
                cursor = mbtiles._conn.execute('SELECT * FROM metadata')
                self.assertEqual(dict(cursor.fetchall()),
                                 {
                                     'name': os.path.basename(self.inputfile),
                                     'description': '',
                                     'format': 'png',
                                     'type': 'overlay',
                                     'version': '1.0.0',
                                     'bounds': dataset_bounds,
                                     'x-minzoom': '0',
                                     'x-maxzoom': '0',
                                 })

            command = [sys.executable, self.script,
                       '--name', 'test',
                       '--description', 'Unit test',
                       '--format', 'jpg',
                       '--layer-type', 'baselayer',
                       '--version', '2.0.1',
                       self.inputfile, output.name]
            check_call(command, env=self.environ)
            with MBTiles(output.name) as mbtiles:
                # Default metadata
                cursor = mbtiles._conn.execute('SELECT * FROM metadata')
                self.assertEqual(dict(cursor.fetchall()),
                                 {
                                     'name': 'test',
                                     'description': 'Unit test',
                                     'format': 'jpg',
                                     'type': 'baselayer',
                                     'version': '2.0.1',
                                     'bounds': dataset_bounds,
                                     'x-minzoom': '0',
                                     'x-maxzoom': '0',
                                 })
Example #4
0
    def test_render(self):
        null = open('/dev/null', 'rw')

        with NamedTemporaryFile(suffix='.mbtiles') as output:
            # Valid
            command = [
                sys.executable, self.script, '--min-resolution', '1',
                '--max-resolution', '3', self.rgbfile, output.name
            ]
            check_call(command, env=self.environ)
            with MBTiles(output.name) as mbtiles:
                cursor = mbtiles._conn.execute("""
                    SELECT zoom_level, COUNT(*) FROM tiles
                    GROUP BY zoom_level
                    """)
                self.assertEqual(
                    dict(cursor.fetchall()),
                    {
                        1: 4,  # 2×2 at resolution 1
                        2: 16,  # 4×4 at resolution 2
                        3: 64
                    }  # 8×8 at resolution 3
                )

            # Min resolution greater than input resolution with no max
            command = [
                sys.executable, self.script, '--min-resolution', '3',
                self.inputfile, output.name
            ]
            self.assertRaises(CalledProcessError,
                              check_call,
                              command,
                              env=self.environ,
                              stderr=null)

            # Min resolution greater than max resolution
            command = [
                sys.executable, self.script, '--min-resolution', '2',
                '--max-resolution', '1', self.inputfile, output.name
            ]
            self.assertRaises(CalledProcessError,
                              check_call,
                              command,
                              env=self.environ,
                              stderr=null)

            # Max resolution less than input resolution with no min
            command = [
                sys.executable, self.script, '--max-resolution', '0',
                self.rgbfile, output.name
            ]
            self.assertRaises(CalledProcessError,
                              check_call,
                              command,
                              env=self.environ,
                              stderr=null)
Example #5
0
def get_mbtiles(name, folder=''):
    """
    Returns the MBTiles associated with `folder` and `name`.

    Searches through config['PATHS'] and finds the earliest match.
    """
    mbtiles_path = os.path.join(folder, '{0}.mbtiles'.format(name))
    for path in app.config['PATHS']:
        filename = os.path.join(path, mbtiles_path)
        if os.path.exists(filename):
            return MBTiles(filename)
    raise IOError(errno.ENOENT, os.strerror(errno.ENOENT))
Example #6
0
    def test_open(self):
        with MBTiles.create(filename=self.filename,
                            metadata=self.metadata,
                            version=self.version):
            pass

        mbtiles = MBTiles(filename=self.filename)

        # Version detection
        self.assertEqual(mbtiles.version, self.version)

        # File are auto-opened
        self.assertFalse(mbtiles.closed)
        conn = mbtiles._conn

        # Open again
        self.assertNotEqual(mbtiles.open(), conn)

        # Close
        mbtiles.close()
        self.assertTrue(mbtiles.closed)