accessmod/zonalstats.py
def time_stats(
    travel_times: TravelTimesLayer,
    boundaries: BoundariesLayer,
    population: PopulationLayer,
    levels: List[int] = [30, 60, 90, 120, 150, 180, 240, 300, 360],
):
    """Aggregate travel times statistics.

    Compute accessibility statistics at the boundary level based on travel time
    and population. Calculate population covered for multiple time thresholds.

    Parameters
    ----------
    travel_times : TravelTimesLayer
        Input travel times raster layer (in minutes).
    boundaries : BoundariesLayer
        Input boundaries vector layer.
    population : PopulationLayer
        Input population raster layer (population count per pixel).
    levels : list of int, optional
        Travel times thresholds in minutes.

    Return
    ------
    metrics : dict
        Accessibility statistics for each travel time threshold and for each
        boundary.
    """
    metrics = {}
    time = np.zeros(shape=population.meta["shape"], dtype="int32")
    reproject(
        source=travel_times.read(),
        destination=time,
        src_crs=travel_times.meta["crs"],
        src_transform=travel_times.meta["transform"],
        dst_crs=population.meta["crs"],
        dst_transform=population.meta["transform"],
        src_nodata=travel_times.meta["nodata"],
        dst_nodata=-1,
        resampling=Resampling.bilinear,
    )
    logger.info("Reprojected travel times to population CRS")

    areas = enforce_crs(boundaries.read(), crs=population.meta["crs"])
    shapes = [area.__geo_interface__ for area in areas.geometry]

    ppp_src = population.read()

    for lvl in levels:
        ppp = ppp_src.copy()
        ppp[time > lvl] = 0
        stats = zonal_stats(
            shapes,
            ppp,
            affine=population.meta["transform"],
            stats=["sum"],
            nodata=-1,
        )
        metrics[lvl] = pd.Series(
            data=[stat["sum"] for stat in stats], index=areas.index
        )
        logger.info(
            f"Counted population with access in less than {lvl}mn (total={metrics[lvl].sum()})"
        )

    return metrics
Aggregate travel times statistics.

    Compute accessibility statistics at the boundary level based on travel time
    and population. Calculate population covered for multiple time thresholds.

    Parameters
    ----------
    travel_times : TravelTimesLayer
        Input travel times raster layer (in minutes).
    boundaries : BoundariesLayer
        Input boundaries vector layer.
    population : PopulationLayer
        Input population raster layer (population count per pixel).
    levels : list of int, optional
        Travel times thresholds in minutes.

    Return
    ------
    metrics : dict
        Accessibility statistics for each travel time threshold and for each
        boundary.
    
Aggregate travel times statistics.
Compute accessibility statistics at the boundary level based on travel time
and population. Calculate population covered for multiple time thresholds.

Parameters

travel_times : TravelTimesLayer
Input travel times raster layer (in minutes).
boundaries : BoundariesLayer
Input boundaries vector layer.
population : PopulationLayer
Input population raster layer (population count per pixel).
levels : list of int, optional
Travel times thresholds in minutes.

Return

metrics : dict
Accessibility statistics for each travel time threshold and for each
boundary.
Aggregate travel times statistics . Compute accessibility statistics at the boundary level based on travel time and population . Calculate population covered for multiple time thresholds . Parameters travel_times : TravelTimesLayer Input travel times raster layer ( in minutes ) . boundaries : BoundariesLayer Input boundaries vector layer . population : PopulationLayer Input population raster layer ( population count per pixel ) . levels : list of int optional Travel times thresholds in minutes . Return metrics : dict Accessibility statistics for each travel time threshold and for each boundary .
def time_stats(
    travel_times: TravelTimesLayer,
    boundaries: BoundariesLayer,
    population: PopulationLayer,
    levels: List[int] = [30, 60, 90, 120, 150, 180, 240, 300, 360],
):
    metrics = {}
    time = np.zeros(shape=population.meta["shape"], dtype="int32")
    reproject(
        source=travel_times.read(),
        destination=time,
        src_crs=travel_times.meta["crs"],
        src_transform=travel_times.meta["transform"],
        dst_crs=population.meta["crs"],
        dst_transform=population.meta["transform"],
        src_nodata=travel_times.meta["nodata"],
        dst_nodata=-1,
        resampling=Resampling.bilinear,
    )
    logger.info("Reprojected travel times to population CRS")
    areas = enforce_crs(boundaries.read(), crs=population.meta["crs"])
    shapes = [area.__geo_interface__ for area in areas.geometry]
    ppp_src = population.read()
    for lvl in levels:
        ppp = ppp_src.copy()
        ppp[time > lvl] = 0
        stats = zonal_stats(
            shapes,
            ppp,
            affine=population.meta["transform"],
            stats=["sum"],
            nodata=-1,
        )
        metrics[lvl] = pd.Series(
            data=[stat["sum"] for stat in stats], index=areas.index
        )
        logger.info(
            f"Counted population with access in less than {lvl}mn (total={metrics[lvl].sum()})"
        )
    return metrics
def time_stats ( travel_times : TravelTimesLayer , boundaries : BoundariesLayer , population : PopulationLayer , levels : List [ int ] = [ 30 , 60 , 90 , 120 , 150 , 180 , 240 , 300 , 360 ] , ) : metrics = { } time = np . zeros ( shape = population . meta [ "shape" ] , dtype = "int32" ) reproject ( source = travel_times . read ( ) , destination = time , src_crs = travel_times . meta [ "crs" ] , src_transform = travel_times . meta [ "transform" ] , dst_crs = population . meta [ "crs" ] , dst_transform = population . meta [ "transform" ] , src_nodata = travel_times . meta [ "nodata" ] , dst_nodata = - 1 , resampling = Resampling . bilinear , ) logger . info ( "Reprojected travel times to population CRS" ) areas = enforce_crs ( boundaries . read ( ) , crs = population . meta [ "crs" ] ) shapes = [ area . __geo_interface__ for area in areas . geometry ] ppp_src = population . read ( ) for lvl in levels : ppp = ppp_src . copy ( ) ppp [ time > lvl ] = 0 stats = zonal_stats ( shapes , ppp , affine = population . meta [ "transform" ] , stats = [ "sum" ] , nodata = - 1 , ) metrics [ lvl ] = pd . Series ( data = [ stat [ "sum" ] for stat in stats ] , index = areas . index ) logger . info ( f"Counted population with access in less than {lvl}mn (total={metrics[lvl].sum()})" ) return metrics
Aggregate travel times statistics.
Aggregate travel times statistics .
"""Aggregate travel times statistics.

    Compute accessibility statistics at the boundary level based on travel time
    and population. Calculate population covered for multiple time thresholds.

    Parameters
    ----------
    travel_times : TravelTimesLayer
        Input travel times raster layer (in minutes).
    boundaries : BoundariesLayer
        Input boundaries vector layer.
    population : PopulationLayer
        Input population raster layer (population count per pixel).
    levels : list of int, optional
        Travel times thresholds in minutes.

    Return
    ------
    metrics : dict
        Accessibility statistics for each travel time threshold and for each
        boundary.
    """
travel_times
TravelTimesLayer
boundaries
BoundariesLayer
population
PopulationLayer
levels
List[int]