Skip to content

Convert raster to dataframe

raster_to_dataframe(raster, bands=None, add_coordinates=False, nodata_value=None)

Convert raster to Pandas DataFrame.

If bands are not given, all bands are used for conversion. Selected bands are named based on their index e.g., band_1, band_2,...,band_n. If wanted, image coordinates (x, y) for each pixel can be written to dataframe by setting add_coordinates to True.

Parameters:

Name Type Description Default
raster DatasetReader

Raster to be converted.

required
bands Optional[Sequence[int]]

Selected bands from multiband raster. Indexing begins from one. Defaults to None.

None
add_coordinates bool

Determines if pixel coordinates are written into dataframe. Defaults to False.

False
nodata_value Optional[float]

Specifies the value to be considered as NoData. If None, raster's nodata value is used.

None

Returns:

Type Description
DataFrame

Raster converted to a DataFrame.

Raises:

Type Description
InvalidRasterBandException

All selected bands are not contained in the input raster.

Source code in eis_toolkit/conversions/raster_to_dataframe.py
40
41
42
43
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 raster_to_dataframe(
    raster: rasterio.io.DatasetReader,
    bands: Optional[Sequence[int]] = None,
    add_coordinates: bool = False,
    nodata_value: Optional[float] = None,
) -> pd.DataFrame:
    """Convert raster to Pandas DataFrame.

    If bands are not given, all bands are used for conversion. Selected bands are named based on their index e.g.,
    band_1, band_2,...,band_n. If wanted, image coordinates (x, y) for each pixel can be written to
    dataframe by setting add_coordinates to True.

    Args:
        raster: Raster to be converted.
        bands: Selected bands from multiband raster. Indexing begins from one. Defaults to None.
        add_coordinates: Determines if pixel coordinates are written into dataframe. Defaults to False.
        nodata_value: Specifies the value to be considered as NoData. If None, raster's nodata value is used.

    Returns:
        Raster converted to a DataFrame.

    Raises:
        InvalidRasterBandException: All selected bands are not contained in the input raster.
    """
    if bands is None or len(bands) == 0:
        bands = range(1, raster.count + 1)
    else:
        if not check_raster_bands(raster, bands):
            raise InvalidRasterBandException(f"Input raster does not contain all selected bands: {bands}.")

    data_frame = _raster_to_dataframe(
        raster=raster,
        bands=bands,
        add_coordinates=add_coordinates,
        nodata_value=nodata_value,
    )
    return data_frame