banesullivan / localtileserver

🌐 dynamic tile server for visualizing rasters in Jupyter with ipyleaflet or folium
https://localtileserver.banesullivan.com
MIT License
304 stars 29 forks source link

Get pixel value by lat/lon #43

Closed giswqs closed 2 years ago

giswqs commented 2 years ago

Is it possible to get pixel value by lat/lon for a local tile? Leafmap already has the functionality for retrieving pixel value by lat/lon for remote COGs using titiler endpoint or Planetary Computer endpoint. I would love to implement similar functionality for local tiles.

https://leafmap.org/common/#leafmap.common.cog_pixel_value https://github.com/giswqs/leafmap/blob/40973aed104376d04280d9215ce2d9e464681d3f/leafmap/common.py#L1187

banesullivan commented 2 years ago

Yes, this is already implemented though not really exposed in the TileClient. The server's endpoint to do this is /pixel/{left}/{right} which you can see on my deployed server here: https://tileserver.banesullivan.com/swagger/

To use this locally with the TileClient, do:

from localtileserver import TileClient
import requests

client = TileClient('/Users/bane/Desktop/TC_NG_SFBay_US_Geo.tif')
url = client.create_url('/pixel/0/0')
data = requests.get(url).json()
print(data)

output:

{'r': 99,
 'g': 75,
 'b': 48,
 'a': 255,
 'bands': {'1': 99.0, '2': 75.0, '3': 48.0},
 'left': 0,
 'top': 0,
 'units': 'pixels'}

I will leave this open to implement a method on the TileClient class to do this (as well as all the other endpoints)

banesullivan commented 2 years ago

The above is in pixel space. You can use the units parameter to use world coordinates.

I'll document this

giswqs commented 2 years ago

That's great to know. I look forward to the implementation. Many thanks!

banesullivan commented 2 years ago

This functionality is changing a bit in #44