Skip to content

Commit

Permalink
ignore url parameters when checking if file ends with ".pmtiles" (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-willis committed Apr 15, 2024
1 parent 644f65c commit 58c3b99
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
8 changes: 5 additions & 3 deletions leafmap/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11472,6 +11472,7 @@ def pmtiles_header(input_file: str):
"""

import requests
from urllib.parse import urlparse

try:
from pmtiles.reader import Reader, MmapSource
Expand All @@ -11481,8 +11482,7 @@ def pmtiles_header(input_file: str):
"pmtiles is not installed. Please install it using `pip install pmtiles`."
)
return

if not input_file.endswith(".pmtiles"):
if not urlparse(input_file).path.endswith(".pmtiles"):
raise ValueError("Input file must be a .pmtiles file.")

if input_file.startswith("http"):
Expand Down Expand Up @@ -11540,6 +11540,7 @@ def pmtiles_metadata(input_file: str) -> Dict[str, Union[str, int, List[str]]]:

import json
import requests
from urllib.parse import urlparse

try:
from pmtiles.reader import Reader, MmapSource, MemorySource
Expand All @@ -11549,7 +11550,8 @@ def pmtiles_metadata(input_file: str) -> Dict[str, Union[str, int, List[str]]]:
)
return

if not input_file.endswith(".pmtiles"):
# ignore uri parameters when checking file suffix
if not urlparse(input_file).path.endswith(".pmtiles"):
raise ValueError("Input file must be a .pmtiles file.")

header = pmtiles_header(input_file)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import geopandas
import pandas
from leafmap.common import *
from pmtiles.tile import MagicNumberNotFound


class TestCommon(unittest.TestCase):
Expand Down Expand Up @@ -60,6 +61,19 @@ def test_cog_center(self):
self.assertIsInstance(cog_center(self.in_cog), tuple)
self.assertEqual(len(cog_center(self.in_cog)), 2)

def test_pmtile_metadata_validates_pmtiles_suffix(self):
with self.assertRaises(ValueError) as cm:
pmtiles_metadata("/some/path/to/pmtiles.pmtiles")
assert cm.exception.message != "Input file must be a .pmtiles file."
with self.assertRaises(MagicNumberNotFound):
pmtiles_metadata("https://mywebsite.com/some/path/to/pmtiles.pmtiles")
assert cm.exception.message != "Input file must be a .pmtiles file."
with self.assertRaises(MagicNumberNotFound):
pmtiles_metadata(
"https://mywebsite.com/some/path/to/pmtiles.pmtiles?query=param"
)
assert cm.exception.message != "Input file must be a .pmtiles file."


if __name__ == "__main__":
unittest.main()

0 comments on commit 58c3b99

Please sign in to comment.