Skip to content

add_raster doesn't work in container environment #1027

Closed
@ldemaz

Description

@ldemaz

Environment Information

  • leafmap version: 0.42.6
  • Python version: 3.11.6
  • Operating System: Mac OS 14.6.1 (but running container giwqs/leafmap:latest)

Description

I am having difficult getting leafmap to display a local COG when running in a container. I have tried multiple permutations of building leafmap into a docker container, all of which show slightly different failures. These are described below.

What I Did

I will start with the most recent, which is simply directly running the image giwqs/leafmap:latest, with the following command:

docker run -it -p 8888:8888 -v $(pwd):/home/jovyan/work -v /Users/me/images/tiles:/home/tiles giswqs/leafmap:latest

Note I am mounting a few directories here to get access to local files, the most important of which is a directory containing the local COGs I want to display, which I form paths to and then try to display using these lines, adapted from several notebook cells:

import os
import sys
import pandas as pd
import re
import numpy as np
import leafmap.leafmap as leafmap

# traverse directory containing COGs to make catalog
paths = []
for root, _, files in os.walk("/home/tiles/", topdown=True):
    for f in files:
        if f.endswith(".tif"):
            file_bits = f.split("_")
            file_dict = {
                "tile": int(re.sub("tile", "", file_bits[0])),
                "year": file_bits[1].split("-")[0],
                "month": file_bits[1].split("-")[1],
                "file": f,
                "path": os.path.join(root, f)
            }
            paths.append(file_dict)

image_catalog = pd.DataFrame(paths)

# pluck out tile of interest 
image_catalogr = (
    image_catalog[image_catalog['tile']==842099]
    .groupby(["tile", "year"])
    .first()
)
tile = image_catalogr.path.iloc[0]

# display
m = leafmap.Map(zoom=20, 
    center=[pt.y.iloc[0], pt.x.iloc[0]]
)
m.add_basemap("SATELLITE")
m.add_raster(tile, bands=[1,2,3], layer_name='TRUE COLOR')
m

The first failure is that add_raster says xarray isn't installed. To fix this, I simply opened a terminal in the jupyter lab environment and ran mamba install xarray, restarted the kernel, and ran again.

This time it ran, but the COG does not display. Going on previous guidance, I then also tried adding jupyter-server-proxy and then added the following to lines to my imports:

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

Restarted and ran again. The COG still doesn't show up.

So I am stuck here. I should note I have also tried to build my own containers, using several different variants. One of these is as follows, which is simply adapting the Dockerfile in this repo, and adding the missing xarray and rioxarray, and a few other bits:

FROM jupyter/scipy-notebook:latest
RUN mamba install -c conda-forge leafmap geopandas "localtileserver>=0.10.0" osmnx -y && \
    pip install -U leafmap jsonschema==4.18.0 lonboard h5py xarray==2024.11.0 rioxarray==0.17.0 && \
    fix-permissions "${CONDA_DIR}" && \
    fix-permissions "/home/${NB_USER}"


ENV PROJ_LIB='/opt/conda/share/proj'

USER root

# Set up working directory
RUN mkdir -p /home/workdir
WORKDIR /home/workdir

# Activate the Conda environment and ensure it's available in the container
ENV PATH="/opt/conda/base:$PATH"
ENV CONDA_DEFAULT_ENV=geo

# Expose Jupyter Lab's default port
EXPOSE 8888

# Run Jupyter Lab 
ENTRYPOINT ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]

Running the same code as above, I get a different error, which has bedevilled me across multiple attempts at this, which is that I get an error saying localtileserver doesn't exist, when it does, but it is failing because:

ModuleNotFoundError: No module named 'rio_tiler.io'

A different, pip-based version of the build script bypasses the error:

# build script fixed and optimized by ChatGPT
FROM continuumio/miniconda3:24.9.2-0

# Use a single RUN command where possible for efficiency
RUN apt-get update && \
    apt-get --allow-releaseinfo-change update && \
    apt-get --allow-releaseinfo-change-suite update && \
    apt-get install -y binutils libproj-dev gdal-bin libgdal-dev g++ && \
    rm -rf /var/lib/apt/lists/*

# solution from here: https://stackoverflow.com/a/73101774
RUN cp /usr/lib/aarch64-linux-gnu/libstdc++.so.6.0.30 /opt/conda/lib/ && \
    cd /opt/conda/lib/ && \
    rm -f libstdc++.so.6 && \
    ln -s libstdc++.so.6.0.30 libstdc++.so.6

# Upgrade pip, pip-tools, and setuptools
RUN pip install --no-cache-dir --upgrade pip pip-tools setuptools

# Copy requirements.txt and install dependencies
COPY requirements.txt /tmp/requirements.txt
RUN pip install --no-cache-dir -r /tmp/requirements.txt && \
    rm /tmp/requirements.txt

# Set up working directory
RUN mkdir -p /home/workdir
WORKDIR /home/workdir

# Expose Jupyter Lab's default port
EXPOSE 8888

# Define entrypoint with corrected Jupyter Lab options
ENTRYPOINT ["jupyter", "lab", "--ip=0.0.0.0", "--port=8888", "--no-browser", "--allow-root"]

With requirements:

jupyterlab
ipywidgets==8.1.5
ipyleaflet==0.19.2
leafmap==0.41.2
localtileserver==0.10.5
numpy==2.1.3
geopandas==1.0.1
pandas==2.2.3
shapely==2.0.6
matplotlib==3.9.2

But this has the same problem with displaying COGs--they just don't show up (and I have tried adding jupyter-server-proxy).

So I am stuck here. I will not that an ordinary, non containerized install of leafmap using conda/mamba does show the COGs, but I want to containerize this for use on a cluster account.

This is probably an issue with localtileserver, but perhaps there is a fix known here for getting it to work with add_raster. Any solutions will be most appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions