Skip to content

Rasterize vector

rasterize_vector(geodataframe, resolution=None, value_column=None, default_value=1.0, fill_value=0.0, base_raster_profile=None, buffer_value=None, merge_strategy='replace')

Transform vector data into raster data.

Parameters:

Name Type Description Default
geodataframe GeoDataFrame

The vector dataframe to be rasterized.

required
resolution Optional[float]

The resolution i.e. cell size of the output raster. Optional if base_raster_profile is given.

None
value_column Optional[str]

The column name with values for each geometry. If None, then default_value is used for all geometries.

None
default_value float

Default value burned into raster cells based on geometries.

1.0
base_raster_profile Optional[Union[Profile, dict]]

Base raster profile to be used for determining the grid on which vectors are burned in. If None, the geometries and provided resolution value are used to compute grid.

None
fill_value float

Value used outside the burned/rasterized geometry cells.

0.0
buffer_value Optional[float]

For adding a buffer around passed geometries before rasterization.

None
merge_strategy Literal[replace, add]

How to handle overlapping geometries. "add" causes overlapping geometries to add together the values while "replace" does not. Adding them together is the basis for density computations where the density can be calculated by using a default value of 1.0 and the sum in each cell is the count of intersecting geometries.

'replace'

Returns:

Type Description
Tuple[ndarray, dict]

Rasterized vector data and metadata.

Raises:

Type Description
EmptyDataFrameException

The geodataframe does not contain geometries.

InvalidColumnException

Given value_column is not in the input geodataframe.

InvalidParameterValueException

No resolution or base_raster_profile is given, or base_raster_profile has the wrong type.

NumericValueSignException

Input resolution value is zero or negative, or input buffer_value is negative.

Source code in eis_toolkit/vector_processing/rasterize_vector.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
@beartype
def rasterize_vector(
    geodataframe: gpd.GeoDataFrame,
    resolution: Optional[float] = None,
    value_column: Optional[str] = None,
    default_value: float = 1.0,
    fill_value: float = 0.0,
    base_raster_profile: Optional[Union[profiles.Profile, dict]] = None,
    buffer_value: Optional[float] = None,
    merge_strategy: Literal["replace", "add"] = "replace",
) -> Tuple[np.ndarray, dict]:
    """Transform vector data into raster data.

    Args:
        geodataframe: The vector dataframe to be rasterized.
        resolution: The resolution i.e. cell size of the output raster.
            Optional if base_raster_profile is given.
        value_column: The column name with values for each geometry.
            If None, then default_value is used for all geometries.
        default_value: Default value burned into raster cells based on geometries.
        base_raster_profile: Base raster profile
            to be used for determining the grid on which vectors are
            burned in. If None, the geometries and provided resolution
            value are used to compute grid.
        fill_value: Value used outside the burned/rasterized geometry cells.
        buffer_value: For adding a buffer around passed
            geometries before rasterization.
        merge_strategy: How to handle overlapping geometries.
            "add" causes overlapping geometries to add together the
            values while "replace" does not. Adding them together is the
            basis for density computations where the density can be
            calculated by using a default value of 1.0 and the sum in
            each cell is the count of intersecting geometries.

    Returns:
        Rasterized vector data and metadata.

    Raises:
        EmptyDataFrameException: The geodataframe does not contain geometries.
        InvalidColumnException: Given value_column is not in the input geodataframe.
        InvalidParameterValueException: No resolution or base_raster_profile is given,
            or base_raster_profile has the wrong type.
        NumericValueSignException: Input resolution value is zero or negative, or input
            buffer_value is negative.
    """

    if geodataframe.shape[0] == 0:
        raise EmptyDataFrameException("Expected geodataframe to contain geometries.")

    if resolution is None and base_raster_profile is None:
        raise InvalidParameterValueException("Expected either resolution or base_raster_profile to be given.")

    if resolution is not None and resolution <= 0:
        raise NumericValueSignException(f"Expected a positive resolution value ({dict(resolution=resolution)})")

    if value_column is not None and value_column not in geodataframe.columns:
        raise InvalidColumnException(f"Expected value_column ({value_column}) to be contained in geodataframe columns.")

    if buffer_value is not None and buffer_value < 0:
        raise NumericValueSignException(f"Expected a positive buffer_value ({dict(buffer_value=buffer_value)})")

    if base_raster_profile is not None and not isinstance(base_raster_profile, (profiles.Profile, dict)):
        raise InvalidParameterValueException(
            f"Expected base_raster_profile ({type(base_raster_profile)}) to be dict or rasterio.profiles.Profile."
        )

    if buffer_value is not None:
        geodataframe = geodataframe.copy()
        geodataframe["geometry"] = geodataframe["geometry"].apply(lambda geom: geom.buffer(buffer_value))

    return _rasterize_vector(
        geodataframe=geodataframe,
        value_column=value_column,
        default_value=default_value,
        fill_value=fill_value,
        base_raster_profile=base_raster_profile,
        resolution=resolution,
        merge_alg=getattr(MergeAlg, merge_strategy),
    )