Skip to content

Vector density

vector_density(geodataframe, resolution=None, base_raster_profile=None, buffer_value=None, statistic='density')

Compute density of geometries within raster.

Parameters:

Name Type Description Default
geodataframe GeoDataFrame

The dataframe with vectors of which density is computed.

required
resolution Optional[float]

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

None
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
buffer_value Optional[float]

For adding a buffer around passed geometries before computing density.

None

Returns:

Type Description
Tuple[ndarray, dict]

Computed density of vector data and metadata.

Source code in eis_toolkit/vector_processing/vector_density.py
10
11
12
13
14
15
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
@beartype
def vector_density(
    geodataframe: gpd.GeoDataFrame,
    resolution: Optional[float] = None,
    base_raster_profile: Optional[Union[profiles.Profile, dict]] = None,
    buffer_value: Optional[float] = None,
    statistic: Literal["density", "count"] = "density",
) -> Tuple[np.ndarray, dict]:
    """Compute density of geometries within raster.

    Args:
        geodataframe: The dataframe with vectors
            of which density is computed.
        resolution: The resolution i.e. cell size of the output raster.
            Optional if base_raster_profile is given.
        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.
        buffer_value: For adding a buffer around passed
            geometries before computing density.

    Returns:
        Computed density of vector data and metadata.
    """
    out_raster_array, out_metadata = rasterize_vector(
        geodataframe=geodataframe,
        resolution=resolution,
        base_raster_profile=base_raster_profile,
        buffer_value=buffer_value,
        value_column=None,
        default_value=1.0,
        fill_value=0.0,
        merge_strategy="add",
    )
    max_count = np.max(out_raster_array)
    if statistic == "count" or np.isclose(max_count, 0.0):
        return out_raster_array, out_metadata
    else:
        return (out_raster_array / max_count), out_metadata