Skip to content

Rasterize vector

rasterize_vector(geodataframe, raster_profile, value_column=None, default_value=1.0, fill_value=0.0, 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
raster_profile Union[Profile, dict]

The raster profile used for output grid properties. Needs to include at least CRS, transform, width and height.

required
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
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
ndarray

Rasterized vector data..

Raises:

Type Description
EmptyDataFrameException

The geodataframe does not contain geometries.

InvalidColumnException

Given value_column is not in the input geodataframe.

NonMatchingCrsException

The input GeoDataFrame and raster profile have mismatching CRS.

NumericValueSignException

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

Source code in eis_toolkit/vector_processing/rasterize_vector.py
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
@beartype
def rasterize_vector(
    geodataframe: gpd.GeoDataFrame,
    raster_profile: Union[profiles.Profile, dict],
    value_column: Optional[str] = None,
    default_value: float = 1.0,
    fill_value: float = 0.0,
    buffer_value: Optional[float] = None,
    merge_strategy: Literal["replace", "add"] = "replace",
) -> np.ndarray:
    """Transform vector data into raster data.

    Args:
        geodataframe: The vector dataframe to be rasterized.
        raster_profile: The raster profile used for output grid properties.
            Needs to include at least CRS, transform, width and height.
        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.
        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..

    Raises:
        EmptyDataFrameException: The geodataframe does not contain geometries.
        InvalidColumnException: Given value_column is not in the input geodataframe.
        NonMatchingCrsException: The input GeoDataFrame and raster profile have mismatching CRS.
        NumericValueSignException: Input resolution value is zero or negative, or input
            buffer_value is negative.
    """
    if geodataframe.empty:
        raise EmptyDataFrameException("Expected geodataframe to contain geometries.")
    if raster_profile.get("crs") != geodataframe.crs:
        raise NonMatchingCrsException("Expected coordinate systems to match between raster and GeoDataFrame.")
    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.")
    check_raster_profile(raster_profile)

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

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

    raster_width = raster_profile.get("width")
    raster_height = raster_profile.get("height")
    raster_transform = raster_profile.get("transform")

    out_image = _rasterize_vector(
        geodataframe=geodataframe,
        raster_width=raster_width,
        raster_height=raster_height,
        raster_transform=raster_transform,
        value_column=value_column,
        default_value=default_value,
        fill_value=fill_value,
        merge_alg=getattr(MergeAlg, merge_strategy),
    )
    return out_image