opengeos / leafmap

A Python package for interactive mapping and geospatial analysis with minimal coding in a Jupyter environment
https://leafmap.org
MIT License
3.22k stars 386 forks source link

`add_stac_layer` & `add_mosaic_layer` are broken for STAC mosaicjson files #539

Closed prncevince closed 1 year ago

prncevince commented 1 year ago

Environment Information

Description

Trying to add a STAC mosaicjson layer to Leafmap. We get a similar error compared to #448. A couple examples that have been provided in Leafmap documentation do not work and receive the same error output.

Reference examples:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[], line 3
      1 m = leafmap.Map()
      2 url = 'https://opengeos.github.io/maxar-open-data/datasets/Kahramanmaras-turkey-earthquake-23/1050050044DE7E00.json'
----> 3 m.add_stac_layer(url, name="Mosaic")
      4 m

File /opt/homebrew/Caskroom/miniforge/base/envs/pygis/lib/python3.11/site-packages/leafmap/foliumap.py:1026, in Map.add_stac_layer(self, url, collection, item, assets, bands, titiler_endpoint, name, attribution, opacity, shown, fit_bounds, **kwargs)
    996 def add_stac_layer(
    997     self,
    998     url: str = None,
   (...)
   1009     **kwargs,
   1010 ):
   1011     """Adds a STAC TileLayer to the map.
   1012 
   1013     Args:
   (...)
   1024         fit_bounds (bool, optional): A flag indicating whether the map should be zoomed to the layer extent. Defaults to True.
   1025     """
-> 1026     tile_url = stac_tile(
   1027         url, collection, item, assets, bands, titiler_endpoint, **kwargs
   1028     )
   1029     bounds = stac_bounds(url, collection, item, titiler_endpoint)
   1030     self.add_tile_layer(
   1031         url=tile_url,
   1032         name=name,
   (...)
   1035         shown=shown,
   1036     )

File /opt/homebrew/Caskroom/miniforge/base/envs/pygis/lib/python3.11/site-packages/leafmap/stac.py:704, in stac_tile(url, collection, item, assets, bands, titiler_endpoint, **kwargs)
    701     else:
    702         r = requests.get(titiler_endpoint.url_for_stac_item(), params=kwargs).json()
--> 704 return r["tiles"][0]

KeyError: 'tiles'

What I Did

You can swap the url value below and receive the same error. As well as swap out the m.add_mosaic_layer call & still receive a similar KeyError: 'tiles' error.

m = leafmap.Map()
url = 'https://opengeos.github.io/maxar-open-data/datasets/Kahramanmaras-turkey-earthquake-23/1050050044DE7E00.json'
# url = 'https://geospatial.glitch.me/1050050044DE7E00.json'
m.add_stac_layer(url, name="Mosaic")
# m.add_mosaic_layer(url, name="Mosaic")
m
giswqs commented 1 year ago

The TiTiler endpoint no longer supports masaicjson. See https://github.com/opengeos/leafmap/issues/180#issuecomment-1012985916.

prncevince commented 1 year ago

I believe that the Maxar Open Data examples are from 2023. How did those examples in the references previously work? Did you configure a different endpoint?

Also, you did add support for mosaicjson after #180 was figured out / closed in #206 & #208 right?

I feel like #180 doesn't tell the whole story here.

giswqs commented 1 year ago

I just released v0.24.2 that fixes this issue. Please update the package and try again. Try out this web app: https://huggingface.co/spaces/giswqs/maxar-open-data

m = leafmap.Map()
url = 'https://open.gishub.org/maxar-open-data/datasets/Kahramanmaras-turkey-earthquake-23/1050050044DE7E00.json'
m.add_stac_layer(url, name="Mosaic")
m
prncevince commented 1 year ago

Thank you for fixing and upgrading! Screenshot below of output using leafmap version 0.24.2. I did a pip install -U leafmap. However this broke jupyter lab due to pypi dependency resolution, likely on the pyrsistent package, so a mamba install jsonschema pyrsistent fixed this.

For what it's worth, redirects from say, the github.io domain to another domain, do not seem to work, so the commented line in screenshot below still does not work.

Also, it would be nice to be able to use local mosaicjson / STAC spec files.

Also, doing something like serving an http web server over localhost and linking to localhost:8000/1050050044DE7E00.json does not currently work either.

image
giswqs commented 1 year ago

I already updated the URL last night. See this.

The TiTiler endpoint is required to redner the mosaicjson. I don't think it supports rendering mosaicjons stored locally.

giswqs commented 1 year ago

If you have a resolution for rendering local mosaicjson files, let me know. I would be happy to add leafmap support for it.

prncevince commented 1 year ago

The updated URLs make sense. I'm referring to the fact that redirects error.

There seems to be scarce resources showing how to use the mosaicjson API endpoint? It's mentioned briefly in the Create Mosaic section of the Working with MosaicJSON notebook of the TiTiler docs:

Below are 2 examples that show use of the TiTiler RESTful API mosaicjson endpoint, one using your example on Maxar Open Data & the other using the example out of the TiTiler docs.

I tried using the ?url=file:/// option as specified in the old cogeo-mosaic-tiler docs where using the old version of the mosaicjson endpoint had documentation for running requests on local mosaicjson files, but this gives a [Errno 2] No such file or directory error.

giswqs commented 1 year ago

Leafmap already uses the TiTiler demo endpoint to render mosaicjson files available throught HTTP. I don't think it supports reading local mosaicjson files.