Reputation: 7235
I have a shapfile and a geotiff image.
import geopandas as gpd
import rasterio
from rasterio.plot import show
df = gpd.read_file('myShape.shp')
fileI = 'myFile.tiff'
data = rasterio.open(fileI)
show((data), cmap='terrain', ax=ax)
p1 = df.geometry.boundary.plot(color=None,edgecolor='red',linewidth = 2,ax=ax)
ax.axis('off');
I would like to assign the values of data
only to the red region defined by df
. Is it possible to crop the image around that region or to make a join between them?
Upvotes: 3
Views: 1688
Reputation: 757
If you just want to crop the raster to the inside of the red polygon you can for example use rasterio mask with crop=True.
from shapely.geometry import mapping
src = rasterio.open(fileI)
clipped_array, clipped_transform = rasterio.mask.mask(src, [mapping(df.iloc[0].geometry)], crop=True)
Upvotes: 4