Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for visualizing in-memory raster #650

Closed
giswqs opened this issue Dec 23, 2023 · 0 comments · Fixed by #651
Closed

Add support for visualizing in-memory raster #650

giswqs opened this issue Dec 23, 2023 · 0 comments · Fixed by #651
Labels
Feature Request New feature or request

Comments

@giswqs
Copy link
Member

giswqs commented Dec 23, 2023

Just figured out how to visualize in-memory raster dataset with localtileserver, making it possible to visualize rasterio/xarray datasets without writing files to hard drive. This is exciting!

import rasterio
from ipyleaflet import Map
from localtileserver import TileClient, get_leaflet_tile_layer

# Open a rasterio dataset
dataset = rasterio.open('https://open.gishub.org/data/raster/srtm90.tif')
data_array = dataset.read(1)

# Do some processing on the data array
data_array[data_array < 1000] = 0

# Create rasterio dataset in memory
memory_file = rasterio.MemoryFile()
raster_dataset = memory_file.open(driver='GTiff',
                                  height=data_array.shape[0],
                                  width=data_array.shape[1],
                                  count=1,
                                  dtype=str(data_array.dtype),
                                  crs=dataset.crs,
                                  transform=dataset.transform)

# Write data array values to the rasterio dataset
raster_dataset.write(data_array, 1)
raster_dataset.close()

# Read the dataset from memory
dataset_reader = rasterio.open(raster_dataset.name, mode='r')

# Create a TileClient and a Leaflet map
client = TileClient(dataset_reader)
t = get_leaflet_tile_layer(client, cmap='terrain', vmin=100, vmax=3000)
m = Map(center=client.center(), zoom=client.default_zoom)
m.add_layer(t)
m

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant