# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/06_collector.ipynb.

# %% auto 0
__all__ = ['DataCollector']

# %% ../nbs/06_collector.ipynb 3
import rasterio
import fastcore.all as fc
import geopandas as gpd
from rasterio import transform

# %% ../nbs/06_collector.ipynb 5
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.transform
            self.bounds = src.bounds
    
    def 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))
