Skip to content

Commit 3b7e5c2

Browse files
committed
Add mbtiles and pmtiles functions
1 parent 099ca2f commit 3b7e5c2

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

leafmap/common.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10934,6 +10934,57 @@ def index():
1093410934

1093510935

1093610936
def vector_to_mbtiles(
10937+
source_path: str, target_path: str, max_zoom: int = 5, name: str = None, **kwargs
10938+
) -> None:
10939+
"""
10940+
Convert a vector dataset to MBTiles format using the ogr2ogr command-line tool.
10941+
10942+
Args:
10943+
source_path (str): The path to the source vector dataset (GeoPackage, Shapefile, etc.).
10944+
target_path (str): The path to the target MBTiles file to be created.
10945+
max_zoom (int, optional): The maximum zoom level for the MBTiles dataset. Defaults to 5.
10946+
name (str, optional): The name of the MBTiles dataset. Defaults to None.
10947+
**kwargs: Additional options to be passed as keyword arguments. These options will be used as -dsco options
10948+
when calling ogr2ogr. See https://gdal.org/drivers/raster/mbtiles.html for a list of options.
10949+
10950+
Returns:
10951+
None
10952+
10953+
Raises:
10954+
subprocess.CalledProcessError: If the ogr2ogr command fails to execute.
10955+
10956+
Example:
10957+
source_path = "countries.gpkg"
10958+
target_path = "target.mbtiles"
10959+
name = "My MBTiles"
10960+
max_zoom = 5
10961+
vector_to_mbtiles(source_path, target_path, name=name, max_zoom=max_zoom)
10962+
"""
10963+
import subprocess
10964+
10965+
command = [
10966+
"ogr2ogr",
10967+
"-f",
10968+
"MBTILES",
10969+
target_path,
10970+
source_path,
10971+
"-dsco",
10972+
f"MAXZOOM={max_zoom}",
10973+
]
10974+
10975+
if name:
10976+
command.extend(["-dsco", f"NAME={name}"])
10977+
10978+
for key, value in kwargs.items():
10979+
command.extend(["-dsco", f"{key.upper()}={value}"])
10980+
10981+
try:
10982+
subprocess.run(command, check=True)
10983+
except subprocess.CalledProcessError as e:
10984+
raise e
10985+
10986+
10987+
def geojson_to_mbtiles(
1093710988
input_file: str,
1093810989
output_file: str,
1093910990
layer_name: Optional[str] = None,
@@ -11028,7 +11079,7 @@ def mbtiles_to_pmtiles(
1102811079
convert.mbtiles_to_pmtiles(input_file, output_file, maxzoom=max_zoom)
1102911080

1103011081

11031-
def vector_to_pmtiles(
11082+
def geojson_to_pmtiles(
1103211083
input_file: str,
1103311084
output_file: Optional[str] = None,
1103411085
layer_name: Optional[str] = None,

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
],
4444
"sql": ["psycopg2", "sqlalchemy"],
4545
"apps": ["streamlit-folium", "voila", "solara"],
46-
"vector": ["geopandas", "osmnx"],
46+
"vector": ["geopandas", "osmnx", "pmtiles", "flask", "flask-cors"],
4747
"pmtiles": ["pmtiles", "flask", "flask-cors"],
4848
"ai": [
4949
"geopandas",

0 commit comments

Comments
 (0)