Given a raster TIF file and a point feature class in a geodatabase, I am trying to extract the values of the raster on the points and add them as a new column on the point feature class. I am using the following piece of code:
import ....
gdb_path = r'.......gdb'
pnts = gpd.read_file(gdb_path, driver='FileGDB', layer='...')
raster_path = r'.........tif'
#building list of pairs of coordinates
temp = [pnts['geometry'].x, pnts['geometry'].y]
coords = list(map(list, zip(*temp)))
with rasterio.open(raster_path) as src:
pnts['values'] = src.sample(coords)
The code above creates a new column on the points feature class where the value for each point is a generator object sample_gen at 0x000..... instead of the actual raster value.
I could loop point by point instead, like this:
raster = rasterio.open(raster_path)
for id, row in pnts.iterrows():
for val in raster.sample([(row['geometry'].x, row['geometry'].y)]):
pnts.at[id, 'value'] = val[0]
This seems to work ok, but it takes a really long time (I have 4 millions of points). So, this solution is not valid for my purposes.
Could anyone shed some light on how can I make the first piece of code produce what I need? Or any other approach?