### Install pymbtiles using pip Source: https://github.com/consbio/pymbtiles/blob/main/README.md Install the library using pip for standard installation. ```bash pip install pymbtiles ``` -------------------------------- ### Install pymbtiles from GitHub Source: https://github.com/consbio/pymbtiles/blob/main/README.md Install the latest version from the master branch on GitHub using pip. ```bash pip install git+https://github.com/consbio/pymbtiles.git#egg=pymbtiles --upgrade ``` -------------------------------- ### zoom_range Source: https://context7.com/consbio/pymbtiles/llms.txt Gets the available zoom level range. ```APIDOC ## zoom_range ### Description Returns a tuple of (min_zoom, max_zoom) representing the range of zoom levels available in the tileset. ``` -------------------------------- ### Get Available Zoom Range with zoom_range Source: https://context7.com/consbio/pymbtiles/llms.txt Returns a tuple of (min_zoom, max_zoom) for available zoom levels. Useful for understanding resolution coverage and iterating through zoom levels. ```python from pymbtiles import MBtiles with MBtiles('map.mbtiles') as src: min_zoom, max_zoom = src.zoom_range() print(f"Zoom range: {min_zoom} to {max_zoom}") # Iterate through all available zoom levels for z in range(min_zoom, max_zoom + 1): row_min, row_max = src.row_range(z) col_min, col_max = src.col_range(z) print(f"Zoom {z}: rows [{row_min}-{row_max}], cols [{col_min}-{col_max}]") ``` -------------------------------- ### row_range / col_range Source: https://context7.com/consbio/pymbtiles/llms.txt Gets the tile extent at a specific zoom level. ```APIDOC ## row_range / col_range ### Description Returns the minimum and maximum row or column indices for tiles at a specific zoom level. ### Parameters #### Query Parameters - **zoom** (int) - Required - The zoom level to query ``` -------------------------------- ### Get Tile Extent with row_range / col_range Source: https://context7.com/consbio/pymbtiles/llms.txt Returns the min/max row or column indices for tiles at a specific zoom level. Returns (None, None) if no tiles exist at that zoom. Useful for determining geographic extent. ```python from pymbtiles import MBtiles with MBtiles('regional_map.mbtiles') as src: zoom = 10 row_min, row_max = src.row_range(zoom) col_min, col_max = src.col_range(zoom) if row_min is not None: tile_count = (row_max - row_min + 1) * (col_max - col_min + 1) print(f"Zoom {zoom} extent:") print(f" Rows: {row_min} to {row_max}") print(f" Columns: {col_min} to {col_max}") print(f" Max possible tiles: {tile_count}") else: print(f"No tiles at zoom {zoom}") ``` -------------------------------- ### Open and Create MBtiles Files Source: https://context7.com/consbio/pymbtiles/llms.txt Use the MBtiles class to manage file access modes ('r', 'w', 'r+'). The class supports the context manager protocol for automatic resource cleanup. ```python from pymbtiles import MBtiles # Open existing mbtiles file for reading with MBtiles('map_tiles.mbtiles', mode='r') as src: # Access tile data and metadata print(f"Tileset name: {src.meta.get('name')}") print(f"Format: {src.meta.get('format')}") # Create new mbtiles file (overwrites if exists) with MBtiles('new_tiles.mbtiles', mode='w') as out: out.meta = { 'name': 'My Tileset', 'format': 'png', 'version': '1.0.0', 'description': 'Custom map tiles' } # Open existing file for read-write access with MBtiles('existing.mbtiles', mode='r+') as db: # Can both read and write tiles tile_data = db.read_tile(z=0, x=0, y=0) db.write_tile(z=1, x=0, y=0, data=b'new_tile_data') ``` -------------------------------- ### List tiles in batches for large tilesets Source: https://github.com/consbio/pymbtiles/blob/main/README.md Iterate through tiles in batches to manage memory usage for large mbtiles files. Each batch contains a list of tile coordinates. ```python with MBtiles('my.mbtiles') as src: for batch in src.list_tiles_batched(): for tile_coords in batch: # [TileCoordinate(z, x, y)...] ... ``` -------------------------------- ### List all available tiles Source: https://github.com/consbio/pymbtiles/blob/main/README.md Iterate through all tile coordinates (z, x, y) present in an mbtiles file. Warning: This can consume significant memory for large tilesets. ```python with MBtiles('my.mbtiles') as src: for tile_coords in src.list_tiles(): # [TileCoordinate(z, x, y)...] ... ``` -------------------------------- ### Write multiple tiles to an MBtiles file Source: https://github.com/consbio/pymbtiles/blob/main/README.md Open an mbtiles file in write mode ('w') and write a collection of tiles at once. Ensure tile data is correctly formatted. ```python from pymbtiles import MBtiles, Tile tiles = ( Tile(z=1, x=0, y=0, tile_data=first_tile), ... ) with MBtiles('my.mbtiles', mode='w') as out: out.write_tiles(tiles) ``` -------------------------------- ### MBtiles Class - Open and Create MBtiles Files Source: https://context7.com/consbio/pymbtiles/llms.txt The MBtiles class is the primary interface for working with mbtiles files. It supports read-only ('r'), write ('w'), and read-write ('r+') modes. It also implements the context manager protocol for automatic resource cleanup. ```APIDOC ## MBtiles Class - Open and Create MBtiles Files ### Description The `MBtiles` class is the primary interface for working with mbtiles files. It supports three modes: `'r'` for read-only access, `'w'` for creating a new file (overwrites existing), and `'r+'` for read-write access to existing files. The class implements context manager protocol for automatic resource cleanup. ### Request Example ```python from pymbtiles import MBtiles # Open existing mbtiles file for reading with MBtiles('map_tiles.mbtiles', mode='r') as src: # Access tile data and metadata print(f"Tileset name: {src.meta.get('name')}") print(f"Format: {src.meta.get('format')}") # Create new mbtiles file (overwrites if exists) with MBtiles('new_tiles.mbtiles', mode='w') as out: out.meta = { 'name': 'My Tileset', 'format': 'png', 'version': '1.0.0', 'description': 'Custom map tiles' } # Open existing file for read-write access with MBtiles('existing.mbtiles', mode='r+') as db: # Can both read and write tiles tile_data = db.read_tile(z=0, x=0, y=0) db.write_tile(z=1, x=0, y=0, data=b'new_tile_data') ``` ``` -------------------------------- ### Access and Update Metadata with meta Source: https://context7.com/consbio/pymbtiles/llms.txt Provides dictionary-like access to the mbtiles metadata table for reading, setting keys, and bulk updates. Changes are committed immediately. ```python from pymbtiles import MBtiles # Read metadata from existing file with MBtiles('existing.mbtiles') as src: print(f"Name: {src.meta.get('name')}") print(f"Format: {src.meta.get('format')}") print(f"All metadata: {dict(src.meta)}") ``` -------------------------------- ### Write a tile to an MBtiles file Source: https://github.com/consbio/pymbtiles/blob/main/README.md Open an mbtiles file in write mode ('w') and write a single tile using its zoom, x, and y coordinates along with the tile data. Existing files will be overwritten. ```python with MBtiles('my.mbtiles', mode='w') as out: out.write_tile(z=0, x=0, y=0, tile_data) ``` -------------------------------- ### difference - Create Tileset with Tiles Only in Left Source: https://context7.com/consbio/pymbtiles/llms.txt Creates a new tileset containing tiles that exist in the left tileset but not in the right tileset. ```APIDOC ## difference ### Description Creates a new tileset containing tiles that exist in the left tileset but not in the right tileset. ### Parameters - **left** (str) - Required - Path to the primary tileset. - **right** (str) - Required - Path to the tileset to compare against. - **output** (str) - Required - Path to the resulting MBtiles file. ``` -------------------------------- ### Read and write to an MBtiles file Source: https://github.com/consbio/pymbtiles/blob/main/README.md Use 'r+' mode to open an mbtiles file for both reading and writing operations. ```python with MBtiles('my.mbtiles', 'r+') as out: out.write_tile(z=0, x=0, y=0, tile_data) ``` -------------------------------- ### Read a tile from an MBtiles file Source: https://github.com/consbio/pymbtiles/blob/main/README.md Open an mbtiles file for reading and retrieve tile data using its zoom, x, and y coordinates. Returns tile data in bytes. ```python from pymbtiles import MBtiles with MBtiles('my.mbtiles') as src: tile_data = src.read_tile(z=0, x=0, y=0) ``` -------------------------------- ### meta Source: https://context7.com/consbio/pymbtiles/llms.txt Access and update metadata. ```APIDOC ## meta ### Description Provides dictionary-like access to the mbtiles metadata table. Supports reading, setting individual keys, and bulk updates. ``` -------------------------------- ### Union MBTiles - Combine Two Tilesets Source: https://context7.com/consbio/pymbtiles/llms.txt Create a new tileset containing all unique tiles from two input tilesets. If tiles exist at the same coordinates, the tile from the larger file is preserved. Metadata is copied from the larger source file. ```python from pymbtiles.ops import union from pymbtiles import MBtiles # Combine two regional tilesets into one union('region_north.mbtiles', 'region_south.mbtiles', 'combined.mbtiles') # Merge with custom batch size union('tileset_a.mbtiles', 'tileset_b.mbtiles', 'merged.mbtiles', batch_size=2000) # Verify the union with MBtiles('combined.mbtiles') as src: tiles = src.list_tiles() zoom_min, zoom_max = src.zoom_range() print(f"Combined tileset: {len(tiles)} tiles, zoom {zoom_min}-{zoom_max}") ``` -------------------------------- ### write_tile - Write a Single Tile Source: https://context7.com/consbio/pymbtiles/llms.txt Writes a single tile to the mbtiles file at the specified coordinates. If a tile already exists at the coordinates, it will be overwritten. This method commits the transaction immediately, making it suitable for adding individual tiles. ```APIDOC ## write_tile - Write a Single Tile ### Description Writes a single tile to the mbtiles file at the specified coordinates. If a tile already exists at the coordinates, it will be overwritten. This method commits the transaction immediately, making it suitable for adding individual tiles. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **z** (int) - Zoom level. - **x** (int) - Column coordinate. - **y** (int) - Row coordinate (TMS convention). - **data** (bytes) - The binary data of the tile. ### Request Example ```python from pymbtiles import MBtiles # Read a PNG image file as tile data with open('my_tile.png', 'rb') as f: tile_data = f.read() # Write tile to new mbtiles file with MBtiles('output.mbtiles', mode='w') as out: # Set required metadata out.meta = { 'name': 'Custom Tiles', 'format': 'png', 'minzoom': '0', 'maxzoom': '5' } # Write single tile at zoom 0 out.write_tile(z=0, x=0, y=0, data=tile_data) # Overwrite existing tile with new data out.write_tile(z=0, x=0, y=0, data=b'updated_data') ``` ``` -------------------------------- ### write_tiles - Write Multiple Tiles in Batch Source: https://context7.com/consbio/pymbtiles/llms.txt Writes multiple tiles in a single transaction for improved performance. Accepts an iterable of `Tile` namedtuples containing z, x, y coordinates and data. This is significantly more efficient than calling `write_tile` repeatedly. ```APIDOC ## write_tiles - Write Multiple Tiles in Batch ### Description Writes multiple tiles in a single transaction for improved performance. Accepts an iterable of `Tile` namedtuples containing z, x, y coordinates and data. This is significantly more efficient than calling `write_tile` repeatedly. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **tiles** (iterable of Tile objects) - An iterable where each element is a `Tile` namedtuple with `z`, `x`, `y` coordinates and `data`. ### Request Example ```python from pymbtiles import MBtiles, Tile # Create tiles for a full zoom level def generate_tiles(zoom_level): num_tiles = 2 ** zoom_level for x in range(num_tiles): for y in range(num_tiles): # Generate or load tile data tile_data = b'tile_content_%d_%d_%d' % (zoom_level, x, y) yield Tile(z=zoom_level, x=x, y=y, data=tile_data) # Write all tiles efficiently with MBtiles('batch_output.mbtiles', mode='w') as out: out.meta = { 'name': 'Batch Generated Tiles', 'format': 'pbf', 'type': 'baselayer' } # Write tiles for zoom levels 0-3 for zoom in range(4): tiles = list(generate_tiles(zoom)) out.write_tiles(tiles) print(f"Wrote {len(tiles)} tiles at zoom {zoom}") # Using generator directly (memory efficient) with MBtiles('large_tileset.mbtiles', mode='w') as out: out.meta = {'name': 'Large Tileset', 'format': 'png'} out.write_tiles(generate_tiles(zoom_level=5)) ``` ``` -------------------------------- ### Metadata Management Source: https://context7.com/consbio/pymbtiles/llms.txt Methods for setting and updating metadata within an MBtiles file. ```APIDOC ## Metadata Management ### Description Set or update metadata fields for an MBtiles file. ### Request Body - **meta** (dict) - Required - Dictionary containing metadata keys such as name, format, bounds, center, minzoom, maxzoom, attribution, description, type, and version. ``` -------------------------------- ### Create a union of two tilesets Source: https://github.com/consbio/pymbtiles/blob/main/README.md Perform a set operation to create a new mbtiles file containing unique tiles combined from two specified source files. ```python union(left_filename, right_filename, out_filename) ``` -------------------------------- ### List All Tile Coordinates with list_tiles Source: https://context7.com/consbio/pymbtiles/llms.txt Returns all tile coordinates as TileCoordinate namedtuples. Warning: loads all coordinates into memory, use list_tiles_batched for large tilesets. ```python from pymbtiles import MBtiles with MBtiles('map.mbtiles') as src: # Get all tile coordinates tiles = src.list_tiles() print(f"Total tiles: {len(tiles)}") # Group tiles by zoom level zoom_counts = {} for tile in tiles: zoom_counts[tile.z] = zoom_counts.get(tile.z, 0) + 1 for zoom, count in sorted(zoom_counts.items()): print(f"Zoom {zoom}: {count} tiles") # Access individual tile coordinates if tiles: first = tiles[0] print(f"First tile: z={first.z}, x={first.x}, y={first.y}") ``` -------------------------------- ### Set MBTiles Metadata Source: https://context7.com/consbio/pymbtiles/llms.txt Use the MBtiles context manager to set metadata for a new or existing MBTiles file. Metadata can be set all at once or updated individually. ```python from pymbtiles import MBtiles # Set all metadata at once with MBtiles('new.mbtiles', mode='w') as out: out.meta = { 'name': 'World Basemap', 'format': 'png', 'bounds': '-180,-85,180,85', 'center': '0,0,2', 'minzoom': '0', 'maxzoom': '14', 'attribution': '© OpenStreetMap contributors', 'description': 'A beautiful world map', 'type': 'baselayer', 'version': '1.0.0' } # Update individual metadata values with MBtiles('existing.mbtiles', mode='r+') as out: out.meta['version'] = '1.1.0' out.meta['description'] = 'Updated description' ``` -------------------------------- ### Create a difference of two tilesets Source: https://github.com/consbio/pymbtiles/blob/main/README.md Perform a set operation to create a new mbtiles file containing tiles present in the left file but not in the right file. ```python difference(left_filename, right_filename, out_filename) ``` -------------------------------- ### Write a Single Tile Source: https://context7.com/consbio/pymbtiles/llms.txt Use write_tile to commit individual tiles to the database. This method performs an immediate transaction commit. ```python from pymbtiles import MBtiles # Read a PNG image file as tile data with open('my_tile.png', 'rb') as f: tile_data = f.read() # Write tile to new mbtiles file with MBtiles('output.mbtiles', mode='w') as out: # Set required metadata out.meta = { 'name': 'Custom Tiles', 'format': 'png', 'minzoom': '0', 'maxzoom': '5' } # Write single tile at zoom 0 out.write_tile(z=0, x=0, y=0, data=tile_data) # Overwrite existing tile with new data out.write_tile(z=0, x=0, y=0, data=b'updated_data') ``` -------------------------------- ### extend - Add Tiles from Source to Target Source: https://context7.com/consbio/pymbtiles/llms.txt Adds tiles from a source tileset to a target tileset without overwriting existing tiles. ```APIDOC ## extend ### Description Adds tiles from a source tileset to a target tileset without overwriting existing tiles in the target. The operation modifies the target file in place. ### Parameters - **source** (str) - Required - Path to the source MBtiles file. - **target** (str) - Required - Path to the target MBtiles file. - **batch_size** (int) - Optional - Number of tiles to process in a single batch. ``` -------------------------------- ### List Tiles in Batches with list_tiles_batched Source: https://context7.com/consbio/pymbtiles/llms.txt Generator yielding batches of tile coordinates for memory-efficient iteration. Default batch size is 1000. Useful for processing large tilesets or filtering tiles. ```python from pymbtiles import MBtiles with MBtiles('large_tileset.mbtiles') as src: total_count = 0 batch_num = 0 # Process tiles in batches of 500 for batch in src.list_tiles_batched(batch_size=500): batch_num += 1 total_count += len(batch) # Process each tile in the batch for tile_coord in batch: # Do something with tile_coord.z, tile_coord.x, tile_coord.y pass print(f"Processed batch {batch_num}: {len(batch)} tiles") print(f"Total tiles processed: {total_count}") # Copy specific tiles to new tileset with MBtiles('source.mbtiles') as src, MBtiles('filtered.mbtiles', 'w') as out: out.meta = src.meta from pymbtiles import Tile for batch in src.list_tiles_batched(batch_size=1000): # Filter to only zoom levels 0-5 filtered = [t for t in batch if t.z <= 5] if filtered: tiles = [Tile(*t, data=src.read_tile(*t)) for t in filtered] out.write_tiles(tiles) ``` -------------------------------- ### Extend a tileset with new tiles Source: https://github.com/consbio/pymbtiles/blob/main/README.md Perform a set operation to add new tiles from a source mbtiles file into a target mbtiles file. ```python extend(source_filename, target_filename) ``` -------------------------------- ### Difference MBTiles - Tiles Only in Left Source: https://context7.com/consbio/pymbtiles/llms.txt Create a new tileset with tiles present in the left tileset but not in the right. This is useful for identifying new additions or tiles to be deleted. Metadata is copied from the left tileset. ```python from pymbtiles.ops import difference from pymbtiles import MBtiles # Find tiles in new version not in old version (new additions) difference('new_version.mbtiles', 'old_version.mbtiles', 'new_tiles_only.mbtiles') # Find tiles that need to be deleted (exist in old but not new) difference('old_version.mbtiles', 'new_version.mbtiles', 'deleted_tiles.mbtiles') # Analyze differences with MBtiles('new_tiles_only.mbtiles') as src: tiles = src.list_tiles() print(f"New tiles added: {len(tiles)}") # Group by zoom level by_zoom = {} for t in tiles: by_zoom[t.z] = by_zoom.get(t.z, 0) + 1 for z, count in sorted(by_zoom.items()): print(f" Zoom {z}: {count} new tiles") ``` -------------------------------- ### PyMBTiles Tile and TileCoordinate Data Structures Source: https://context7.com/consbio/pymbtiles/llms.txt Utilize Tile and TileCoordinate namedtuples for clear and type-safe handling of tile data and positions. Tile includes data, while TileCoordinate only has z, x, y. ```python from pymbtiles import MBtiles, Tile, TileCoordinate # Create Tile objects for writing tiles = [ Tile(z=0, x=0, y=0, data=b'tile_0'), Tile(z=1, x=0, y=0, data=b'tile_1'), Tile(z=1, x=1, y=0, data=b'tile_2'), Tile(z=1, x=0, y=1, data=b'tile_3'), Tile(z=1, x=1, y=1, data=b'tile_4'), ] with MBtiles('output.mbtiles', mode='w') as out: out.meta = {'name': 'Test', 'format': 'pbf'} out.write_tiles(tiles) # TileCoordinate is returned by list_tiles with MBtiles('output.mbtiles') as src: coords = src.list_tiles() for coord in coords: # TileCoordinate has z, x, y attributes print(f"Tile at z={coord.z}, x={coord.x}, y={coord.y}") # Can unpack directly into read_tile data = src.read_tile(*coord) print(f" Data: {data}") ``` -------------------------------- ### union - Combine Two Tilesets Source: https://context7.com/consbio/pymbtiles/llms.txt Creates a new tileset containing all unique tiles from both input tilesets. ```APIDOC ## union ### Description Creates a new tileset containing all unique tiles from both input tilesets. When tiles exist at the same coordinates, the tile from the larger file is preserved. ### Parameters - **tileset_a** (str) - Required - Path to the first input tileset. - **tileset_b** (str) - Required - Path to the second input tileset. - **output** (str) - Required - Path to the resulting combined MBtiles file. - **batch_size** (int) - Optional - Number of tiles to process in a single batch. ``` -------------------------------- ### list_tiles_batched Source: https://context7.com/consbio/pymbtiles/llms.txt Retrieves tile coordinates in memory-efficient batches. ```APIDOC ## list_tiles_batched ### Description Generator that yields batches of tile coordinates for memory-efficient iteration. ### Parameters #### Query Parameters - **batch_size** (int) - Optional - Number of tiles per batch (default 1000) ``` -------------------------------- ### list_tiles Source: https://context7.com/consbio/pymbtiles/llms.txt Retrieves all tile coordinates in the tileset. ```APIDOC ## list_tiles ### Description Returns a list of all tile coordinates in the tileset as TileCoordinate namedtuples (z, x, y). Note: Loads all coordinates into memory. ``` -------------------------------- ### Write Multiple Tiles in Batch Source: https://context7.com/consbio/pymbtiles/llms.txt Use write_tiles to process multiple tiles in a single transaction for improved performance. This method accepts an iterable of Tile namedtuples. ```python from pymbtiles import MBtiles, Tile # Create tiles for a full zoom level def generate_tiles(zoom_level): num_tiles = 2 ** zoom_level for x in range(num_tiles): for y in range(num_tiles): # Generate or load tile data tile_data = b'tile_content_%d_%d_%d' % (zoom_level, x, y) yield Tile(z=zoom_level, x=x, y=y, data=tile_data) # Write all tiles efficiently with MBtiles('batch_output.mbtiles', mode='w') as out: out.meta = { 'name': 'Batch Generated Tiles', 'format': 'pbf', 'type': 'baselayer' } # Write tiles for zoom levels 0-3 for zoom in range(4): tiles = list(generate_tiles(zoom)) out.write_tiles(tiles) print(f"Wrote {len(tiles)} tiles at zoom {zoom}") # Using generator directly (memory efficient) with MBtiles('large_tileset.mbtiles', mode='w') as out: out.meta = {'name': 'Large Tileset', 'format': 'png'} out.write_tiles(generate_tiles(zoom_level=5)) ``` -------------------------------- ### Extend MBTiles - Add Unique Tiles Source: https://context7.com/consbio/pymbtiles/llms.txt Add tiles from a source to a target MBTiles file without overwriting existing tiles. The target file is modified in place. Caller is responsible for updating metadata. ```python from pymbtiles.ops import extend # Add unique tiles from updates.mbtiles to base.mbtiles # Tiles already in base.mbtiles are not overwritten extend('updates.mbtiles', 'base.mbtiles') # With custom batch size for large tilesets extend('new_region.mbtiles', 'world.mbtiles', batch_size=5000) # Verify results from pymbtiles import MBtiles with MBtiles('base.mbtiles') as src: tiles = src.list_tiles() print(f"Total tiles after extend: {len(tiles)}") ``` -------------------------------- ### Set multiple MBtiles metadata values Source: https://github.com/consbio/pymbtiles/blob/main/README.md Replace all metadata in an mbtiles file by assigning a new dictionary to the 'meta' attribute. This operation is performed when opening the file in write mode ('w'). ```python with MBtiles('my.mbtiles', 'w') as out: out.meta = my_metadata_dict ``` -------------------------------- ### Update MBtiles metadata Source: https://github.com/consbio/pymbtiles/blob/main/README.md Update individual metadata key-value pairs in an mbtiles file opened in read-write mode ('r+'). ```python with MBtiles('my.mbtiles', 'r+') as out: out.meta['some_key'] = 'some_value' ``` -------------------------------- ### Access MBtiles metadata Source: https://github.com/consbio/pymbtiles/blob/main/README.md Access the metadata associated with an mbtiles file through the 'meta' attribute. This metadata is stored in the 'metadata' table. ```python with MBtiles('my.mbtiles') as src: metadata = src.meta ``` -------------------------------- ### Check Tile Existence with has_tile Source: https://context7.com/consbio/pymbtiles/llms.txt Efficiently checks if a tile exists at given coordinates without reading tile data. Useful for avoiding duplicate writes or verifying coverage. ```python from pymbtiles import MBtiles with MBtiles('map.mbtiles') as src: # Check specific tile existence if src.has_tile(z=5, x=10, y=15): print("Tile exists at z=5, x=10, y=15") else: print("Tile not found") # Check coverage at a zoom level zoom = 3 num_tiles = 2 ** zoom coverage_count = 0 for x in range(num_tiles): for y in range(num_tiles): if src.has_tile(zoom, x, y): coverage_count += 1 total_possible = num_tiles ** 2 print(f"Coverage at zoom {zoom}: {coverage_count}/{total_possible} tiles") ``` -------------------------------- ### Read a Single Tile Source: https://context7.com/consbio/pymbtiles/llms.txt Retrieve tile data as bytes using the read_tile method. The y coordinate follows the TMS convention. ```python from pymbtiles import MBtiles with MBtiles('world_tiles.mbtiles') as src: # Read tile at zoom level 0, column 0, row 0 tile_data = src.read_tile(z=0, x=0, y=0) if tile_data is not None: # Save tile to file (assuming PNG format) with open('tile_0_0_0.png', 'wb') as f: f.write(tile_data) print(f"Tile size: {len(tile_data)} bytes") else: print("Tile not found") # Read multiple tiles at different zoom levels for z in range(3): tile = src.read_tile(z=z, x=0, y=0) if tile: print(f"Zoom {z}: {len(tile)} bytes") ``` -------------------------------- ### has_tile Source: https://context7.com/consbio/pymbtiles/llms.txt Checks if a tile exists at specific coordinates. ```APIDOC ## has_tile ### Description Checks whether a tile exists at the specified coordinates without reading the tile data. Returns True if the tile exists, False otherwise. ### Parameters #### Query Parameters - **z** (int) - Required - Zoom level - **x** (int) - Required - Column index - **y** (int) - Required - Row index ``` -------------------------------- ### read_tile - Read a Single Tile Source: https://context7.com/consbio/pymbtiles/llms.txt Retrieves tile data for a specific zoom level, column, and row. Returns the tile data as bytes if the tile exists, or `None` if no tile is found at the specified coordinates. The y coordinate follows the TMS (Tile Map Service) convention. ```APIDOC ## read_tile - Read a Single Tile ### Description Retrieves tile data for a specific zoom level, column, and row. Returns the tile data as bytes if the tile exists, or `None` if no tile is found at the specified coordinates. The y coordinate follows the TMS (Tile Map Service) convention. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from pymbtiles import MBtiles with MBtiles('world_tiles.mbtiles') as src: # Read tile at zoom level 0, column 0, row 0 tile_data = src.read_tile(z=0, x=0, y=0) if tile_data is not None: # Save tile to file (assuming PNG format) with open('tile_0_0_0.png', 'wb') as f: f.write(tile_data) print(f"Tile size: {len(tile_data)} bytes") else: print("Tile not found") # Read multiple tiles at different zoom levels for z in range(3): tile = src.read_tile(z=z, x=0, y=0) if tile: print(f"Zoom {z}: {len(tile)} bytes") ``` ### Response #### Success Response (200) - **tile_data** (bytes) - The binary data of the tile, or None if not found. #### Response Example ```json { "example": "tile_data_bytes" } ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.