I am a bit of a newbie to more complex mapping in Python and Jupyter. I am trying to display a TIF image as an overlay on top of a Folium map I created. I tried various approaches (after intensive Google search) and still can’t make it work even though the TIF show correctly after reading it and the PNG shows correctly after the conversion. The Folium combined map doe not show the overlay at all.
Here is the code so far:
tif_file = './my_image.tif'
tif_image = rio.open(tif_file)
show(tif_image)
tif_bounds = tif_image.bounds
dst_crs = "EPSG:4326"
transform, width, height = calculate_default_transform(tif_image.crs, dst_crs, tif_image.width, tif_image.height, *tif_image.bounds)
tif_reprojected = np.zeros((tif_image.count, height, width), dtype=tif_image.dtypes[0])
reproject(
tif_image.read(),
tif_reprojected,
src_transform=tif_image.transform,
src_crs=tif_image.crs,
dst_transform=transform,
dst_crs=dst_crs,
resampling=Resampling.nearest,
)
r, g, b = tif_reprojected[0], tif_reprojected[0], tif_reprojected[0]
rgb_image = np.dstack((r, g, b))
png_path = output_path_str + './my_image.png'
pil_image = Image.fromarray((rgb_image * 255).astype(np.uint8))
pil_image.save(png_path)
png_image = Image.open(png_path)
png_image.show()
m = folium.Map(location=[(tif_bounds.bottom + tif_bounds.top) / 2, (tif_bounds.left + tif_bounds.right) / 2], zoom_start=5)
image_overlay = folium.raster_layers.ImageOverlay(name="smoke",
image=png_path,
bounds=[[tif_bounds.bottom, tif_bounds.left], [tif_bounds.top, tif_bounds.right]],
Interactive=True,
zindex=1)
image_overlay.add_to(m)
m.save('./my_image.html' )
Thanks for any help – I need it!
I have tried variations on the code I posted but nothing worked.
THANKS!