def maps(): flatmap_list = [] for tile_dir in pathlib.Path(root_paths['flatmaps']).iterdir(): mbtiles = os.path.join(root_paths['flatmaps'], tile_dir, 'index.mbtiles') if os.path.isdir(tile_dir) and os.path.exists(mbtiles): reader = MBTilesReader(mbtiles) try: source_row = reader._query( "SELECT value FROM metadata WHERE name='source';" ).fetchone() except (InvalidFormatError, sqlite3.OperationalError): flask.abort(404, 'Cannot read tile database: {}'.format(mbtiles)) if source_row is not None: flatmap = {'id': tile_dir.name, 'source': source_row[0]} created = reader._query( "SELECT value FROM metadata WHERE name='created';" ).fetchone() if created is not None: flatmap['created'] = created[0] describes = reader._query( "SELECT value FROM metadata WHERE name='describes';" ).fetchone() if describes is not None and describes[0]: flatmap['describes'] = normalise_identifier(describes[0]) flatmap_list.append(flatmap) return flask.jsonify(flatmap_list)
class TileDatabase(object): def __init__(self, db_file): self._db = MBTilesReader(db_file) def execute(self, sql): return self._db._query(sql) def metadata(self): return self._db.metadata()
def get_metadata(map_path, name): mbtiles = os.path.join(root_paths['flatmaps'], map_path, 'index.mbtiles') reader = MBTilesReader(mbtiles) try: row = reader._query( "SELECT value FROM metadata WHERE name='{}';".format( name)).fetchone() except (InvalidFormatError, sqlite3.OperationalError): flask.abort(404, 'Cannot read tile database') return {} if row is None else json.loads(row[0])
def maps(): """ Get a list of available flatmaps. :>jsonarr string id: the flatmap's unique identifier on the server :>jsonarr string source: the map's source URL :>jsonarr string created: when the map was generated :>jsonarr string describes: the map's description """ flatmap_list = [] root_path = pathlib.Path(settings['FLATMAP_ROOT']) if root_path.is_dir(): for tile_dir in root_path.iterdir(): mbtiles = os.path.join(settings['FLATMAP_ROOT'], tile_dir, 'index.mbtiles') if os.path.isdir(tile_dir) and os.path.exists(mbtiles): reader = MBTilesReader(mbtiles) try: source_row = reader._query( "SELECT value FROM metadata WHERE name='source';" ).fetchone() except (InvalidFormatError, sqlite3.OperationalError): flask.abort( 404, 'Cannot read tile database: {}'.format(mbtiles)) if source_row is not None: flatmap = {'id': tile_dir.name, 'source': source_row[0]} created = reader._query( "SELECT value FROM metadata WHERE name='created';" ).fetchone() if created is not None: flatmap['created'] = created[0] describes = reader._query( "SELECT value FROM metadata WHERE name='describes';" ).fetchone() if describes is not None and describes[0]: flatmap['describes'] = normalise_identifier( describes[0]) flatmap_list.append(flatmap) return flask.jsonify(flatmap_list)