I am interpolating point data to generate dynamic flood inundation maps in a for loop. This produces a flood map in each iteration where the pixel values show the probability of water presence. However, I'm unable to make the dry pixels (values < 0.2) transparent so the base map can be seen when added.
Initially I created a geodataframe of predicted probabilities. Then generated raster files using rasterio library using the condition ( <0.2 = np.nan) to set dry pixels. My code is:
geom = [Point(xy) for xy in zip(df.X, df.Y)]
crs = {'init': 'epsg:27700'}
gdf = geopandas.GeoDataFrame(df, crs=crs, geometry=geom)
###Rasterization
#Set up filenames
rst_fn = 'dem_9m_study_area.asc' #template raster
out_fn = 'Raster.tif'
rst = rasterio.open(rst_fn)
#copy and update the metadata from the input raster for the output
meta = rst.meta.copy()
meta.update(compress='lzw')
with rasterio.open(out_fn, 'w', **meta) as out:
out_arr = out.read(1)
shapes = ((geom,value) for geom, value in zip(gdf.geometry, gdf.Prob))
burned = features.rasterize(shapes=shapes, fill=0, out=out_arr, transform=out.transform)
burned[burned < 0.2] = np.nan
out.write_band(1, burned)
Now i would like to import the saved raster and plot it over a background raster which is also in the same coordinate (EPSG:27700), and also show the color bar.
I tried this:
plt.figure(num=None, figsize=(10, 8), dpi=80, facecolor='w', edgecolor='k')
plt.imshow(burned, cmap='Blues')
plt.colorbar()
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Flood Extent: T=%i h'%op)
Extent with values <0.2 set to nan
this works fine without the background though the x and y coordinates are not showing correctly. But does not work if I add this to the above code:
bmap = rasterio.open("background_upton.tif") #import basemap
src = bmap.read(1)
plt.imshow(src, cmap = 'pink')
I also tried the method described in "Adding a background map to plots": https://geopandas.readthedocs.io/en/latest/gallery/plotting_basemap_background.html
But this does not seem to solve the problem. It would be great if I can get some suggestions how to solve the issue. I want to overlay the extent map using this background image