From b25d819e3f32c9e4f8e0df9141ec3bd9af9f3025 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Fri, 19 Jul 2024 14:53:13 +0200 Subject: [PATCH] Support metadata from local pmtiles --- maplibre/pmtiles_utils.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/maplibre/pmtiles_utils.py b/maplibre/pmtiles_utils.py index f8024545..139ae4ae 100644 --- a/maplibre/pmtiles_utils.py +++ b/maplibre/pmtiles_utils.py @@ -113,9 +113,17 @@ def get_pmtiles_header(path: str) -> dict: def get_pmtiles_metadata(path: str) -> tuple: header = get_pmtiles_header(path) - response = range_request(path, header["metadata_offset"], header["metadata_length"]) - get_bytes = MemorySource(response.content) - metadata = get_bytes(0, header["metadata_length"]) + if path.startswith("http"): + response = range_request( + path, header["metadata_offset"], header["metadata_length"] + ) + get_bytes = MemorySource(response.content) + metadata = get_bytes(0, header["metadata_length"]) + else: + with open(path, "rb") as f: + get_bytes = MmapSource(f) + metadata = get_bytes(header["metadata_offset"], header["metadata_length"]) + if header["internal_compression"] == Compression.GZIP: metadata = gzip.decompress(metadata) @@ -187,6 +195,7 @@ def to_basemap_style(self, layer_styles: list) -> dict: }, ) ) + return construct_basemap_style( sources={source_id: self.to_source()}, layers=layers )