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

Get pixel value by lat/lon from a local tile (COG) #162

Closed giswqs closed 2 years ago

giswqs commented 2 years ago

Thanks to localtileserver @banesullivan https://github.com/banesullivan/localtileserver/issues/43, it is now possible to get pixel values from a local tile. This feature will improve the Inspector tool to support local tile.

https://github.com/banesullivan/localtileserver/blob/main/localtileserver/client.py#L183

from localtileserver import get_leaflet_tile_layer, TileClient
from ipyleaflet import Map

# First, create a tile server from local raster file
tile_client = TileClient('~/Downloads/dem.tif')

# Create ipyleaflet tile layer from that server
t = get_leaflet_tile_layer(tile_client)

# Create ipyleaflet map, add tile layer, and display
m = Map(center=tile_client.center())
m.add_layer(t)
m

tile_client.pixel(37.869365, -118.943481, units="EPSG:4326")

image

giswqs commented 2 years ago

This feature has been implemented. See notebook example https://github.com/giswqs/leafmap/blob/master/examples/notebooks/32_local_tile.ipynb

banesullivan commented 2 years ago

Available in localtileserver >=0.3.14

banesullivan commented 2 years ago

@giswqs, I really need to add some tests to this to verify localtileserver is grabbing the right values... Need to find a sample raster with simple data/big pixels. I will look in the GDAL testing data

giswqs commented 2 years ago

I just did some testing, and there are indeed some issues with the pixel values. The same image, same lon/lon with different results with Titiler endpoint and localtileserver.

import leafmap
import localtileserver
m= leafmap.Map()
url = "https://s3.eu-central-1.amazonaws.com/floris.calkoen.open.data/tile_2523.tif"
m.add_cog_layer(url, bands=[4, 3, 2])
m
remote_url = "https://s3.eu-central-1.amazonaws.com/floris.calkoen.open.data/tile_2523.tif"
local_url = "~/Downloads/landsat.tif"
lat = 53.404415
lon = 6.872292
leafmap.cog_pixel_value(lon, lat, url)
tile_client = localtileserver.TileClient(local_url)
leafmap.local_tile_pixel_value(lon, lat, tile_client)

image

banesullivan commented 2 years ago

The values aren't wildly different which gives me some hope that this may be a precision issue with either large_image/localtileserver or titiler.

I will try to add some tests in localtileserver to make sure everything is good there.

banesullivan commented 2 years ago

I will add that this is extensively tested in large_image and localtileserver simply wraps this functionality. https://github.com/girder/large_image/blob/9251babd4acaa0eecd37c4749f7d9eeac6379171/test/test_source_gdal.py#L148

But I'd still like to add some sort of user/visual test in localtileserver