opengeos / leafmap

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

add_raster does not work on Jupyter of visual studio code using remote development #904

Closed karantai closed 2 months ago

karantai commented 2 months ago

Environment Information

Description

From my local computer I have connected to my remote server using remote development plug in , in VSCode. I have tried to visualize the geotiff images that reside on my server, in Jupyter (through VSCode) but only the basemap appears and no Geotiffs, without any warning.

Am I missing something?

What I Did

conda install conda-forge::jupyter-server-proxy
conda install conda-forge::localtileserver
conda install conda-forge::leafmap

I do not know if the following command is necessary for me:

os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'
import leafmap
m = leafmap.Map(center=client.center(), zoom=client.default_zoom)

input_file = 'path/to/test.tif'
m.add_raster(input_file, band=[4, 3, 2], vmin=1, vmax=100, layer_name="Landsat")
m
giswqs commented 2 months ago

See https://github.com/banesullivan/localtileserver/issues/181

karantai commented 2 months ago

I tried it but without positive results. A tab opens locally and it immediately opens the default San Francisco geotiff and zooms there.

Here is some extra info


--------------------------------------------------------------------------------
  Date: Thu Sep 26 12:41:05 2024 UTC

                  OS : Linux (Ubuntu 22.04)
              CPU(s) : 40
             Machine : x86_64
        Architecture : 64bit
                 RAM : 188.4 GiB
         Environment : Jupyter
         File system : ext4

  Python 3.12.6 | packaged by conda-forge | (main, Sep 22 2024, 14:16:49) [GCC
  13.3.0]

     localtileserver : 0.10.3
               click : 8.1.7
               flask : 3.0.3
       flask_caching : 1.10.1
          flask_cors : 5.0.0
         flask_restx : 1.3.0
               numpy : 2.1.1
            rasterio : 1.3.11
           rio_cogeo : 5.3.4
           rio_tiler : 6.7.0
       server_thread : 0.2.0
            werkzeug : 3.0.4
              folium : 0.17.0
          ipyleaflet : 0.19.2
jupyter_server_proxy : 4.4.0
          jupyterlab : 4.2.5
          matplotlib : 3.9.2
             shapely : 2.0.6
           traitlets : 5.14.3
--------------------------------------------------------------------------------
karantai commented 2 months ago

I connect to my remote server using a VPN, maybe that's why I cannot visualize anything?

lopezvoliver commented 2 months ago

Try forwarding the port manually

see:

https://github.com/opengeos/leafmap/discussions/677

karantai commented 2 months ago

I solved the issue by assigning a port manually in these lines:

client = TileClient(output_file, port=40_000)
t = get_leaflet_tile_layer(client, port=40_000)

Commenting this line:

#os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

and did port forwarding as mentioned here:

https://github.com/banesullivan/localtileserv)er/issues/181
giswqs commented 2 months ago

@karantai Thank you for reporting back. Where did you comment out the following line? I wondered if we should make changes to the leafmap codebase so that users don't have to make changes on their own.

#os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'
patel-zeel commented 1 month ago

Prof. @giswqs, if we keep the following line uncommented, it does not work

#os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

Before running the following MWE, I had to set port forwarding manually in the VS code. Could there be a more automated way of handling this? If not, we can probably raise a warning if this behavior is detectable.

import os
# os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

from localtileserver import get_leaflet_tile_layer, TileClient
from ipyleaflet import Map

port = 40001
client = TileClient('https://open.gishub.org/data/raster/landsat7.tif', port=port)
t = get_leaflet_tile_layer(client, port=port)

m = Map(center=client.center(), zoom=client.default_zoom)
m.add(t)
m

Also, when I run the above snippet, it works in odd executions and in even executions, generates the following error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:58, in ServerManager.get_server(key)
     [57](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:57) try:
---> [58](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:58)     return ServerManager._LIVE_SERVERS[key]
     [59](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:59) except KeyError:

KeyError: 40001

During handling of the above exception, another exception occurred:

ServerDownError                           Traceback (most recent call last)
Cell In[14], [line 9](vscode-notebook-cell:?execution_count=14&line=9)
      [7](vscode-notebook-cell:?execution_count=14&line=7) port = 40001
      [8](vscode-notebook-cell:?execution_count=14&line=8) client = TileClient('https://open.gishub.org/data/raster/landsat7.tif', port=port)
----> [9](vscode-notebook-cell:?execution_count=14&line=9) t = get_leaflet_tile_layer(client, port=port)
     [11](vscode-notebook-cell:?execution_count=14&line=11) m = Map(center=client.center(), zoom=client.default_zoom)
     [12](vscode-notebook-cell:?execution_count=14&line=12) m.add(t)

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:100, in get_leaflet_tile_layer(source, port, debug, indexes, colormap, vmin, vmax, nodata, attribution, **kwargs)
     [95](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:95) class BoundTileLayer(TileLayer, LocalTileServerLayerMixin):
     [96](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:96)     # https://github.com/jupyter-widgets/ipyleaflet/issues/888
     [97](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:97)     # https://github.com/ipython/traitlets/issues/626#issuecomment-699957829
     [98](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:98)     bounds = Union((Tuple(),), default_value=None, allow_none=True).tag(sync=True, o=True)
--> [100](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:100) source, created = get_or_create_tile_client(
    [101](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:101)     source,
    [102](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:102)     port=port,
    [103](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:103)     debug=debug,
    [104](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:104) )
    [105](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:105) url = source.get_tile_url(
    [106](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:106)     indexes=indexes,
    [107](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:107)     colormap=colormap,
   (...)
    [111](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:111)     client=True,
    [112](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:112) )
    [113](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/widgets.py:113) if attribution is None:

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:595, in get_or_create_tile_client(source, port, debug)
    [593](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:593) # Check that the tile source is valid and no server errors
    [594](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:594) try:
--> [595](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:595)     r = requests.get(source.create_url("api/metadata"))
    [596](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:596)     r.raise_for_status()
    [597](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:597) except requests.HTTPError as e:
    [598](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:598)     # Make sure to destroy the server and its thread if internally created.

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:416, in TileServerMixin.create_url(self, path, client)
    [410](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:410) if client and (
    [411](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:411)     self.client_port is not None
    [412](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:412)     or self.client_host is not None
    [413](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:413)     or self.client_prefix is not None
    [414](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:414) ):
    [415](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:415)     return self._produce_url(f"{self.client_base_url}/{path.lstrip('/')}")
--> [416](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:416) return self._produce_url(f"{self.server_base_url}/{path.lstrip('/')}")

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:351, in TileServerMixin.server_base_url(self)
    [349](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:349) @property
    [350](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:350) def server_base_url(self):
--> [351](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:351)     return f"http://{self.server_host}:{self.server_port}"

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:347, in TileServerMixin.server_host(self)
    [345](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:345) @property
    [346](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:346) def server_host(self):
--> [347](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:347)     return self.server.host

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:339, in TileServerMixin.server(self)
    [337](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:337) @property
    [338](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:338) def server(self):
--> [339](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/localtileserver/client.py:339)     return ServerManager.get_server(self._key)

File /opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:60, in ServerManager.get_server(key)
     [58](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:58)     return ServerManager._LIVE_SERVERS[key]
     [59](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:59) except KeyError:
---> [60](https://vscode-remote+ssh-002dremote-002b7b22686f73744e616d65223a2252616d616e756a616e227d.vscode-resource.vscode-cdn.net/opt/anaconda3/envs/zeel_py310/lib/python3.10/site-packages/server_thread/server.py:60)     raise ServerDownError("Server for this key has been shutdown.")

ServerDownError: Server for this key has been shutdown.