Skip to content

Commit

Permalink
Add vector_to_pmtiles function
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Nov 5, 2023
1 parent 20c33e8 commit d405e5b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
29 changes: 28 additions & 1 deletion leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10998,7 +10998,7 @@ def geojson_to_mbtiles(
input_file (str): Path to the input vector data file (e.g., .geojson).
output_file (str): Path to the output .mbtiles file.
layer_name (Optional[str]): Optional name for the layer. Defaults to None.
options (Optional[List[str]]): List of additional arguments for tippecanoe. Defaults to None.
options (Optional[List[str]]): List of additional arguments for tippecanoe. For example '-zg' for auto maxzoom. Defaults to None.
quiet (bool): If True, suppress the log output. Defaults to False.
Returns:
Expand Down Expand Up @@ -11079,6 +11079,33 @@ def mbtiles_to_pmtiles(
convert.mbtiles_to_pmtiles(input_file, output_file, maxzoom=max_zoom)


def vector_to_pmtiles(
source_path: str, target_path: str, max_zoom: int = 5, name: str = None, **kwargs
):
"""
Converts a vector file to PMTiles format.
Args:
source_path (str): Path to the source vector file.
target_path (str): Path to the target PMTiles file.
max_zoom (int, optional): Maximum zoom level for the PMTiles. Defaults to 5.
name (str, optional): Name of the PMTiles dataset. Defaults to None.
**kwargs: Additional keyword arguments to be passed to the underlying conversion functions.
Raises:
ValueError: If the target file does not have a .pmtiles extension.
Returns:
None
"""
if not target_path.endswith(".pmtiles"):
raise ValueError("Error: target file must be a .pmtiles file.")
mbtiles = target_path.replace(".pmtiles", ".mbtiles")
vector_to_mbtiles(source_path, mbtiles, max_zoom=max_zoom, name=name, **kwargs)
mbtiles_to_pmtiles(mbtiles, target_path)
os.remove(mbtiles)


def geojson_to_pmtiles(
input_file: str,
output_file: Optional[str] = None,
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
],
"sql": ["psycopg2", "sqlalchemy"],
"apps": ["streamlit-folium", "voila", "solara"],
"vector": ["geopandas", "osmnx", "pmtiles", "flask", "flask-cors"],
"vector": ["geopandas", "osmnx", "pmtiles", "flask", "flask-cors", "lonboard"],
"pmtiles": ["pmtiles", "flask", "flask-cors"],
"ai": [
"geopandas",
Expand Down

0 comments on commit d405e5b

Please sign in to comment.