emax
emax

Reputation: 7235

is it possible to make a join between a geotiff image with a geopandas dataframe?

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');

enter image description here

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

Answers (1)

Christoph Rieke
Christoph Rieke

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

Related Questions