leafmap icon indicating copy to clipboard operation
leafmap copied to clipboard

leafmap.array_to_image generated wrong transform when not using source GeoTIFF

Open Yimsun97 opened this issue 2 years ago • 3 comments

Environment Information

  • leafmap version: 0.30.1
  • Python version: 3.9.18
  • Operating System: Windows 10

Description

I want to add a raster file (xr.DataArray) in the memory on the map before saving to the disk. Based on the leafmap tutorial "89 image array viz", I used the leafmap.array_to_image function to convert the xr.DataArray to the correct format that can be recognized by the map.add_raster.

What I Did

  1. I found that map.add_raster generated what I expected if a string of path is assigned.
m = leafmap.Map()
m.add_raster('./worldmap.tif', cmap='coolwarm', layer_name='worldmap')
m

This will give the fig1 (as expected). fig1

  1. When I open that file using rioxarray and using the source parameter, it also worked as expected:
world = rioxarray.open_rasterio('./worldmap.tif')
world_img = leafmap.array_to_image(world, source='worldmap.tif')
m = leafmap.Map()
m.add_raster(world_img, cmap='coolwarm', layer_name='worldmap')
m

This will give the fig2 (as expected). fig2

  1. However, it failed when I assigned the cellsize, crs, and transform parameters. It seemed that the memory file got the wrong transformation or projection. As there is no example in the tutorial notebook, I'm wondering how to set these parameters to get the in-memory file visualized before saving to disk?
world = rioxarray.open_rasterio('./worldmap.tif')
world_img = leafmap.array_to_image(world, cellsize=0.5, crs='EPSG:4326')
m = leafmap.Map()
m.add_raster(world_img, cmap='coolwarm', layer_name='worldmap')
m

This will give the fig3 (maybe due to wrong projection). fig3

The GeoTIFF and the notebook files I used are attached. Thanks in advance for your time!

worldmap.zip leafmap_array_to_image.zip

Yimsun97 avatar Jan 15 '24 14:01 Yimsun97

You also need to set the transform parameter so that the extent of the image is correctly set. Use rasterio to check the transform info on the source

giswqs avatar Jan 15 '24 14:01 giswqs

I found the bug. Fixing it now.

giswqs avatar Jan 15 '24 14:01 giswqs

The bug has been fixed in #660. Run leafmap.update_package() and restart kernel to take effect.

giswqs avatar Jan 15 '24 15:01 giswqs