opengeos / solara-geospatial

A collection of solara web apps for geospatial applications
https://giswqs-solara-geospatial.hf.space
MIT License
120 stars 21 forks source link

Solara-geospatial failed to access local files #2

Closed kirimaru-jp closed 5 months ago

kirimaru-jp commented 1 year ago

I would like to develop a GEE web app allowing shape file upload (similar to this), hence I hope I could use geemap together with Solara (e.g. I could store some raster data on Hugging Face, and let Solara access them). At first, I followed your Solara tutorial and Load raster with geemap to test with your raster file dem.tif stored locally. I could follow these 2 tutorials separately without any problems. I use Windows 10, with geemap 0.23.1 and solara 1.17.5.

However, when I tried to combine them,

import ee
import geemap
import os

import solara

zoom = solara.reactive(4)
center = solara.reactive([40, -100])

class Map(geemap.Map):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # self.add_ee_data()
        self.local_data()
        self.add_layer_manager()
        self.add_inspector()

    def add_ee_data(self):
        # Add Earth Engine dataset
        dem = ee.Image('USGS/SRTMGL1_003')
        landsat7 = ee.Image('LANDSAT/LE7_TOA_5YEAR/1999_2003').select(
            ['B1', 'B2', 'B3', 'B4', 'B5', 'B7']
        )
        states = ee.FeatureCollection("TIGER/2018/States")

        # Set visualization parameters.
        vis_params = {
            'min': 0,
            'max': 4000,
            'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
        }

        # Add Earth Engine layers to Map
        self.addLayer(dem, vis_params, 'SRTM DEM', True, 0.5)
        self.addLayer(
            landsat7,
            {'bands': ['B4', 'B3', 'B2'], 'min': 20, 'max': 200, 'gamma': 2.0},
            'Landsat 7',
            False,
        )
        self.addLayer(states, {}, "US States")

    def local_data(self):
        out_dir = os.path.expanduser('~/Downloads')
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        dem = os.path.join(out_dir, 'dem.tif')
        self.add_raster(dem, colormap='terrain', layer_name='DEM')
        # self.add_local_tile(dem, palette='terrain', layer_name="DEM")

@solara.component
def Page():
    with solara.Column(style={"min-width": "500px"}):
        # solara components support reactive variables
        # solara.SliderInt(label="Zoom level", value=zoom, min=1, max=20)
        # using 3rd party widget library require wiring up the events manually
        # using zoom.value and zoom.set
        Map.element(  # type: ignore
            zoom=zoom.value,
            on_zoom=zoom.set,
            center=center.value,
            on_center=center.set,
            scroll_wheel_zoom=True,
            add_google_map=True,
            height="700px",
        )
        solara.Text(f"Zoom: {zoom.value}")
        solara.Text(f"Center: {center.value}")

It failed with the following error when I run the code above with solara run .\geemap_solara.py:

Traceback (most recent call last):
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 364, in _create_widget
    widget = self.component.widget(**kwargs)
  File "D:\codes\GEE\apps\solara-geospatial\geemap_rubber1.py", line 16, in __init__
    self.local_data()
  File "D:\codes\GEE\apps\solara-geospatial\geemap_rubber1.py", line 50, in local_data
    self.add_raster(dem, colormap='terrain', layer_name='DEM')
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\geemap\geemap.py", line 2612, in add_raster
    tile_layer, tile_client = get_local_tile_layer(
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\geemap\common.py", line 10611, in get_local_tile_layer
    tile_layer = get_leaflet_tile_layer(
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\localtileserver\widgets.py", line 111, in get_leaflet_tile_layer
    source, created = get_or_create_tile_client(
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\localtileserver\client.py", line 849, in get_or_create_tile_client
    raise e
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\localtileserver\client.py", line 843, in get_or_create_tile_client
    r.raise_for_status()
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: INTERNAL SERVER ERROR for url: http://localhost:61973/api/metadata?&filename=C%3A%5CUsers%5Ckirimaru-jp%5CDownloads%5Cdem.tif

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\solara\server\app.py", line 446, in load_app_widget
    widget, render_context = _run_app(
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\solara\server\app.py", line 410, in _run_app
    result = render(solara_context, container, handle_error=True, initial_state=app_state)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2142, in render
    _rc.render(element, _rc.container)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1433, in render
    widget = self._reconsolidate(self.element, default_key="/", parent_key=ROOT_KEY)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1898, in _reconsolidate
    kwargs = reconsolidate_children()
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1882, in reconsolidate_children
    new_kwargs = self._visit_children_values(kwargs, key, parent_key, self._reconsolidate)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2120, in _visit_children_values
    return {k: self._visit_children_values(v, f"{key}{k}/", parent_key, f) for k, v in value.items()}
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2120, in <dictcomp>
    return {k: self._visit_children_values(v, f"{key}{k}/", parent_key, f) for k, v in value.items()}
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2115, in _visit_children_values
    values = [self._visit_children_values(v, f"{key}{index}/", parent_key, f) for index, v in enumerate(value)]
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2115, in <listcomp>
    values = [self._visit_children_values(v, f"{key}{index}/", parent_key, f) for index, v in enumerate(value)]
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2112, in _visit_children_values
    return f(value, key, parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1898, in _reconsolidate
    kwargs = reconsolidate_children()
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1882, in reconsolidate_children
    new_kwargs = self._visit_children_values(kwargs, key, parent_key, self._reconsolidate)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2120, in _visit_children_values
    return {k: self._visit_children_values(v, f"{key}{k}/", parent_key, f) for k, v in value.items()}
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2120, in <dictcomp>
    return {k: self._visit_children_values(v, f"{key}{k}/", parent_key, f) for k, v in value.items()}
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2115, in _visit_children_values
    values = [self._visit_children_values(v, f"{key}{index}/", parent_key, f) for index, v in enumerate(value)]
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2115, in <listcomp>
    values = [self._visit_children_values(v, f"{key}{index}/", parent_key, f) for index, v in enumerate(value)]
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2112, in _visit_children_values
    return f(value, key, parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1794, in _reconsolidate
    widget = self._reconsolidate(child_context.root_element_next, "/", new_parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1898, in _reconsolidate
    kwargs = reconsolidate_children()
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1882, in reconsolidate_children
    new_kwargs = self._visit_children_values(kwargs, key, parent_key, self._reconsolidate)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2120, in _visit_children_values
    return {k: self._visit_children_values(v, f"{key}{k}/", parent_key, f) for k, v in value.items()}
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2120, in <dictcomp>
    return {k: self._visit_children_values(v, f"{key}{k}/", parent_key, f) for k, v in value.items()}
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2115, in _visit_children_values
    values = [self._visit_children_values(v, f"{key}{index}/", parent_key, f) for index, v in enumerate(value)]
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2115, in <listcomp>
    values = [self._visit_children_values(v, f"{key}{index}/", parent_key, f) for index, v in enumerate(value)]
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 2112, in _visit_children_values
    return f(value, key, parent_key)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 1903, in _reconsolidate
    widget, orphan_ids = el._create_widget(kwargs)
  File "D:\ProgramData\miniconda3\envs\geemap\lib\site-packages\reacton\core.py", line 377, in _create_widget
    raise RuntimeError(f"Could not create widget {self.component.widget} with {kwargs}") from e
RuntimeError: Could not create widget <class 'geemap_rubber1.Map'> with {'zoom': 4, 'center': [40, -100], 'scroll_wheel_zoom': True, 'add_google_map': True, 'height': '700px'}

Is there any way to fix it, or could you suggest a different way? Many thanks!

giswqs commented 1 year ago

I don't think Localtileserver supports solara. One workaround is to host the COG somewhere else and make it available through HTTP. Then you can use Map.add_cog_layer(). Or upload the image to your GEE account.

kirimaru-jp commented 1 year ago

Thank you very much! I did upload my images to my GEE account, and it worked great!