Emulate data collection. Provided a set of location, return values sampled from given raster file.
Type
Default
Details
fname_raster
str
The path to the raster file.
band
int
1
The band number to use. Defaults to 1.
Exported source
class DataCollector:def__init__(self, fname_raster:str, # The path to the raster file. band:int=1, # The band number to use. Defaults to 1. ):"Emulate data collection. Provided a set of location, return values sampled from given raster file." fc.store_attr()with rasterio.open(fname_raster) as src:self.band_data = src.read(band)self.affine = src.transformself.bounds = src.boundsdef get_values(self, gdf:gpd.GeoDataFrame # loc_id and Point/Multipoint geometry of samples where to measure. ): coords = [(x, y) for x, y in gdf.get_coordinates().values] pixel_coords = [transform.rowcol(self.affine, *pair) for pair in coords]return [self.band_data[int(x), int(y)] for (x, y) in pixel_coords]def collect(self, gdf:gpd.GeoDataFrame # loc_id and Point/Multipoint geometry of samples where to measure. ) -> gpd.GeoDataFrame:return gdf.explode(index_parts=False).assign(value=self.get_values(gdf))
geometry value
loc_id
0 POINT (-1.22304 43.26776) 0.000000
0 POINT (-1.22053 43.26270) 0.143665
0 POINT (-1.22018 43.26424) 0.146753
0 POINT (-1.21862 43.26922) 0.138506
0 POINT (-1.21596 43.26927) 0.127660