leafmap.array_to_image generated wrong transform when not using source GeoTIFF
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
- 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).
- When I open that file using rioxarray and using the
sourceparameter, 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).
- However, it failed when I assigned the
cellsize,crs, andtransformparameters. 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).
The GeoTIFF and the notebook files I used are attached. Thanks in advance for your time!
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
I found the bug. Fixing it now.
The bug has been fixed in #660. Run leafmap.update_package() and restart kernel to take effect.