I am trying to develop a plot of a raster image with a background map. I used rasterio
for reading the raster file and contextily
for obtaining the background map following this tutorial.
The plotted map is wrong in terms of geocoordinates. To elaborate, the raster is not aligning with the feature it is representing (river) webmap. How can I fix this? What is going wrong?
Google colab file with a reproducible example.
pth_raster = '/content/raster_data_resampled.tif'
data, out_path, affine = reproject_raster(in_path=pth_raster, band_to_read=1, out_folder="./raster_temp", destination_crs='epsg:3857') #<-- for webmap epsg:3857
src = rasterio.open(out_path)
src.crs
>CRS.from_epsg(3857)
basemap, basemap_extent = ctx.bounds2img(*src.bounds)
import matplotlib.cm as cm
my_cmap = cm.jet
my_cmap.set_under('k', alpha=0)
plt.figure(figsize=(10,10))
plt.imshow(basemap, extent=basemap_extent)
plt.imshow(src.read(1), extent=basemap_extent, cmap=my_cmap, interpolation='none', clim=[-900, 1e5]) #<-- clim for removing no data (-9999)
plt.show()