Skip to content

Extract values from raster

extract_values_from_raster(raster_list, geodataframe, raster_column_names=None)

Extract raster values using point data to a DataFrame.

If custom column names are not given, column names are file_name for singleband files and file_name_bandnumber for multiband files. If custom column names are given, there should be column names for each raster provided in the raster list.

Parameters:

Name Type Description Default
raster_list Sequence[DatasetReader]

List to extract values from.

required
geodataframe GeoDataFrame

Object to extract values with.

required
raster_column_names Optional[Sequence[str]]

List of optional column names for bands.

None

Returns:

Type Description
DataFrame

Dataframe with x & y coordinates and the values from the raster file(s) as columns.

Raises:

Type Description
NonMatchingParameterLengthsException

raster_list and raster_columns_names have different lengths.

Source code in eis_toolkit/raster_processing/extract_values_from_raster.py
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@beartype
def extract_values_from_raster(
    raster_list: Sequence[rasterio.io.DatasetReader],
    geodataframe: gpd.GeoDataFrame,
    raster_column_names: Optional[Sequence[str]] = None,
) -> pd.DataFrame:
    """Extract raster values using point data to a DataFrame.

       If custom column names are not given, column names are file_name for singleband files
       and file_name_bandnumber for multiband files. If custom column names are given, there
       should be column names for each raster provided in the raster list.

    Args:
        raster_list: List to extract values from.
        geodataframe: Object to extract values with.
        raster_column_names: List of optional column names for bands.

    Returns:
        Dataframe with x & y coordinates and the values from the raster file(s) as columns.

    Raises:
        NonMatchingParameterLengthsException: raster_list and raster_columns_names have different lengths.
    """
    if raster_column_names == []:
        raster_column_names = None

    if raster_column_names is not None and len(raster_list) != len(raster_column_names):
        raise NonMatchingParameterLengthsException("Raster list and raster columns names have different lengths.")

    data_frame = _extract_values_from_raster(
        raster_list=raster_list, geodataframe=geodataframe, raster_column_names=raster_column_names
    )

    return data_frame