Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore url parameters when checking if file ends with ".pmtiles" #722

Merged
merged 2 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()