From d405e5be21e9a851d7f33c40ddec8dd7471c7e43 Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Sun, 5 Nov 2023 15:20:56 -0500 Subject: [PATCH] Add vector_to_pmtiles function --- leafmap/common.py | 29 ++++++++++++++++++++++++++++- setup.py | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/leafmap/common.py b/leafmap/common.py index 7d844802a8..5fda2de271 100644 --- a/leafmap/common.py +++ b/leafmap/common.py @@ -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: @@ -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, diff --git a/setup.py b/setup.py index 8a695934f6..fcd486a699 100644 --- a/setup.py +++ b/setup.py @@ -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",