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])
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,))
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', })
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)
def test_simple(self): mbtiles = MBTiles.create(filename=self.filename, metadata=self.metadata, version=self.version) metadata = mbtiles.metadata # Set metadata['test'] = '' self.assertEqual(metadata['test'], '') # Set again metadata['test'] = 'Tileset' self.assertEqual(metadata['test'], 'Tileset') # Get missing self.assertRaises(MetadataKeyError, metadata.__getitem__, 'missing') self.assertEqual(metadata.get('missing'), None) self.assertEqual(metadata.get('missing', False), False) # Contains self.assertTrue('test' in metadata) self.assertFalse('missing' in metadata) # Delete del metadata['test'] self.assertFalse('test' in metadata) # Delete mandatory self.assertRaises(MetadataKeyError, metadata.__delitem__, 'name') # Pop metadata['test'] = 'Tileset' self.assertEqual(metadata.pop('test'), 'Tileset') # Pop missing self.assertRaises(MetadataKeyError, metadata.pop, 'test') self.assertEqual(metadata.pop('test', None), None) # Update data = dict(self.metadata.items(), name='Tileset', description='This is a test tileset.') metadata.update(data) # Keys self.assertEqual(set(metadata.keys()), set(data.keys())) # Values self.assertEqual(set(metadata.values()), set(data.values())) # Items self.assertEqual(set(metadata.items()), set(data.items())) # Compare with dictionary self.assertEqual(metadata, data)
def test_simple(self): mbtiles = MBTiles.create(filename=self.filename, metadata=self.metadata, version=self.version) metadata = mbtiles.metadata # Set metadata['test'] = '' self.assertEqual(metadata['test'], '') # Set again metadata['test'] = 'Tileset' self.assertEqual(metadata['test'], 'Tileset') # Get missing self.assertRaises(MetadataKeyError, metadata.__getitem__, 'missing') self.assertEqual(metadata.get('missing'), None) self.assertEqual(metadata.get('missing', False), False) # Contains self.assertTrue('test' in metadata) self.assertFalse('missing' in metadata) # Delete del metadata['test'] self.assertFalse('test' in metadata) # Delete mandatory self.assertRaises(MetadataKeyError, metadata.__delitem__, 'name') # Pop metadata['test'] = 'Tileset' self.assertEqual(metadata.pop('test'), 'Tileset') # Pop missing self.assertRaises(MetadataKeyError, metadata.pop, 'test') self.assertEqual(metadata.pop('test', None), None) # Update data = dict(list(self.metadata.items()), name='Tileset', description='This is a test tileset.') metadata.update(data) # Keys self.assertEqual(set(metadata.keys()), set(data.keys())) # Values self.assertEqual(set(metadata.values()), set(data.values())) # Items self.assertEqual(set(metadata.items()), set(data.items())) # Compare with dictionary self.assertEqual(metadata, data)
def test_create(self): # Create when filename does not exist os.remove(self.filename) mbtiles1 = MBTiles.create(filename=self.filename, metadata=self.metadata, version=self.version) self.assertFalse(mbtiles1.closed) # Create again when it exists mbtiles2 = MBTiles.create(filename=self.filename, metadata=self.metadata, version=self.version) self.assertFalse(mbtiles2.closed) self.assertNotEqual(mbtiles1, mbtiles2) # Create without version mbtiles3 = MBTiles.create(filename=self.filename, metadata=self.metadata) self.assertEqual(mbtiles3.version, self.version)
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))
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)
def test_validate_1_0(self): version = '1.0' metadata = dict( name='transparent', type=Metadata.all()[version].TYPES.BASELAYER, version='1.0.0', ) self.assertRaises(MetadataKeyError, MBTiles.create, filename=self.filename, metadata={}, version=version) metadata.update(dict( description='Transparent World 2012', )) with MBTiles.create(filename=self.filename, metadata=metadata) as mbtiles: self.assertEqual(mbtiles.version, version) with MBTiles.create(filename=self.filename, metadata=metadata, version=version) as mbtiles: metadata = mbtiles.metadata self.assertRaises(MetadataKeyError, metadata.__delitem__, 'name') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'type') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'version') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'description') metadata['type'] = metadata.TYPES.OVERLAY self.assertEqual(metadata['type'], 'overlay') metadata['type'] = metadata.TYPES.BASELAYER self.assertEqual(metadata['type'], 'baselayer') self.assertRaises(MetadataValueError, metadata.__setitem__, 'type', 'invalid')
def test_autocommit(self): mbtiles = MBTiles.create(filename=self.filename, metadata=self.metadata, version=self.version) data = 'PNG image' hashed = hash(data) # Insert tile mbtiles.insert(x=0, y=0, z=0, hashed=hashed, data=data) self.assertEqual(mbtiles.get(x=0, y=0, z=0), data) # Reopen mbtiles.open() self.assertEqual(mbtiles.get(x=0, y=0, z=0), data)
def test_validate_1_0(self): version = '1.0' metadata = dict( name='transparent', type=Metadata.all()[version].TYPES.BASELAYER, version='1.0.0', ) self.assertRaises(MetadataKeyError, MBTiles.create, filename=self.filename, metadata={}, version=version) metadata.update(dict(description='Transparent World 2012', )) with MBTiles.create(filename=self.filename, metadata=metadata) as mbtiles: self.assertEqual(mbtiles.version, version) with MBTiles.create(filename=self.filename, metadata=metadata, version=version) as mbtiles: metadata = mbtiles.metadata self.assertRaises(MetadataKeyError, metadata.__delitem__, 'name') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'type') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'version') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'description') metadata['type'] = metadata.TYPES.OVERLAY self.assertEqual(metadata['type'], 'overlay') metadata['type'] = metadata.TYPES.BASELAYER self.assertEqual(metadata['type'], 'baselayer') self.assertRaises(MetadataValueError, metadata.__setitem__, 'type', 'invalid')
def test_out_of_order_tile(self): mbtiles = MBTiles.create(filename=':memory:', metadata=self.metadata, version=self.version) data = 'PNG image' hashed = hash(data) # Link tile to nonexistent data mbtiles.insert(x=1, y=1, z=1, hashed=hashed) # Get linked tile self.assertEqual(mbtiles.get(x=1, y=1, z=1), None) # Add nonexistent data mbtiles.insert(x=0, y=0, z=0, hashed=hashed, data=data) # Get tile again self.assertEqual(mbtiles.get(x=1, y=1, z=1), data)
def test_tiles(self): mbtiles = MBTiles.create(filename=':memory:', metadata=self.metadata, version=self.version) data = 'PNG image' hashed = hash(data) # Get missing tile self.assertEqual(mbtiles.get(x=0, y=0, z=0), None) # Insert tile mbtiles.insert(x=0, y=0, z=0, hashed=hashed, data=data) # Get inserted tile self.assertEqual(mbtiles.get(x=0, y=0, z=0), data) # Link tile mbtiles.insert(x=1, y=1, z=1, hashed=hashed) # Get linked tile self.assertEqual(mbtiles.get(x=1, y=1, z=1), data)
def test_autocommit(self): with NamedTemporaryFile(suffix='.mbtiles') as tempfile: mbtiles = MBTiles.create(filename=tempfile.name, metadata=self.metadata, version=self.version) # Insert metadata mbtiles.metadata['test'] = 'Tileset' self.assertEqual(mbtiles.metadata['test'], 'Tileset') # Reopen mbtiles.open() self.assertEqual(mbtiles.metadata['test'], 'Tileset') # Delete metadata del mbtiles.metadata['test'] self.assertRaises(KeyError, mbtiles.metadata.__getitem__, 'test') # Reopen mbtiles.open() self.assertRaises(KeyError, mbtiles.metadata.__getitem__, 'test')
def test_validate_1_2(self): version = '1.2' metadata = dict( name='transparent', type=Metadata.all()[version].TYPES.BASELAYER, version='1.0.0', description='Transparent World 2012', ) self.assertRaises(MetadataKeyError, MBTiles.create, filename=self.filename, metadata=self.metadata, version=version) metadata.update( dict( format=Metadata.all()[version].FORMATS.PNG, bounds='-180.0,-85,180,85', attribution='Brought to you by the letter A and the number 1.', )) with MBTiles.create(filename=self.filename, metadata=metadata) as mbtiles: self.assertEqual(mbtiles.version, version) with MBTiles.create(filename=self.filename, metadata=metadata, version=version) as mbtiles: metadata = mbtiles.metadata self.assertRaises(MetadataKeyError, metadata.__delitem__, 'name') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'type') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'version') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'description') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'format') metadata['type'] = metadata.TYPES.OVERLAY self.assertEqual(metadata['type'], 'overlay') metadata['type'] = metadata.TYPES.BASELAYER self.assertEqual(metadata['type'], 'baselayer') self.assertRaises(MetadataValueError, metadata.__setitem__, 'type', 'invalid') metadata['format'] = metadata.FORMATS.PNG self.assertEqual(metadata['format'], 'png') metadata['format'] = metadata.FORMATS.JPG self.assertEqual(metadata['format'], 'jpg') self.assertRaises(MetadataValueError, metadata.__setitem__, 'format', 'invalid') metadata['bounds'] = '-1,-1,1,1' metadata['bounds'] = '-1.0,-1.0,1.0,1.0' metadata['bounds'] = '-1.0,-1.0,1.0,1.0' # left < -180 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-180.1,-1,1,1') # bottom < -90 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,-90.1,1,1') # right > 180 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,-1,180.1,1') # top > 90 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,-1,1,90.1') # left == right self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '1,-1,1,1') # left > right self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '1.1,-1,1,1') # bottom == top self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,1,1,1') # bottom > top self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,1.1,1,1')
def test_validate_1_2(self): version = '1.2' metadata = dict( name='transparent', type=Metadata.all()[version].TYPES.BASELAYER, version='1.0.0', description='Transparent World 2012', ) self.assertRaises(MetadataKeyError, MBTiles.create, filename=self.filename, metadata=self.metadata, version=version) metadata.update(dict( format=Metadata.all()[version].FORMATS.PNG, bounds='-180.0,-85,180,85', attribution='Brought to you by the letter A and the number 1.', )) with MBTiles.create(filename=self.filename, metadata=metadata) as mbtiles: self.assertEqual(mbtiles.version, version) with MBTiles.create(filename=self.filename, metadata=metadata, version=version) as mbtiles: metadata = mbtiles.metadata self.assertRaises(MetadataKeyError, metadata.__delitem__, 'name') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'type') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'version') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'description') self.assertRaises(MetadataKeyError, metadata.__delitem__, 'format') metadata['type'] = metadata.TYPES.OVERLAY self.assertEqual(metadata['type'], 'overlay') metadata['type'] = metadata.TYPES.BASELAYER self.assertEqual(metadata['type'], 'baselayer') self.assertRaises(MetadataValueError, metadata.__setitem__, 'type', 'invalid') metadata['format'] = metadata.FORMATS.PNG self.assertEqual(metadata['format'], 'png') metadata['format'] = metadata.FORMATS.JPG self.assertEqual(metadata['format'], 'jpg') self.assertRaises(MetadataValueError, metadata.__setitem__, 'format', 'invalid') metadata['bounds'] = '-1,-1,1,1' metadata['bounds'] = '-1.0,-1.0,1.0,1.0' metadata['bounds'] = '-1.0,-1.0,1.0,1.0' # left < -180 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-180.1,-1,1,1') # bottom < -90 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,-90.1,1,1') # right > 180 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,-1,180.1,1') # top > 90 self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,-1,1,90.1') # left == right self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '1,-1,1,1') # left > right self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '1.1,-1,1,1') # bottom == top self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,1,1,1') # bottom > top self.assertRaises(MetadataValueError, metadata.__setitem__, 'bounds', '-1,1.1,1,1')