Service Provision¶
Overview¶
Service provision analysis allocates the demand of buildings (for example, the number of school-age children) to services with limited capacity (school places), given a matrix of travel costs between them and a normative threshold (minutes or metres). It shows which buildings are served within the threshold, which are served only by services farther away, and which services are overloaded or underused.
objectnat.get_service_provision() computes the allocation and returns a objectnat.ProvisionResult.
Helper functions join the metrics back to the buildings and services and build link geometries;
objectnat.recalculate_links() and objectnat.clip_provision() adjust a computed result.
Inputs¶
buildings: GeoDataFrame with a numeric demand column (buildings_demand_column, default"demand");services: GeoDataFrame with a numeric capacity column (services_capacity_column, default"capacity");distance_matrix: costs with rowsbuildings.indexand columnsservices.index, in the units ofthreshold. It may be a dense DataFrame (inforNaNfor unreachable pairs) or a pandas sparse DataFrame (SparseDtypewithNaNorinfas fill value) that stores only reachable pairs. Sparse matrices are read entry by entry without densifying, so large areas fit in memory. IduEdu’sod_matrixreturns such a sparse matrix (see Building-to-service travel-time matrix).
Method¶
The allocation is a gravity-based probabilistic model; it runs in ObjectNat’s Rust extension over the entries of
the (sparse) matrix. Only building–service pairs with a finite cost d of at most 3 * threshold can receive
demand. Each such pair has a gravity weight 1 / (d + 1)²: the closer the building, the larger the weight,
and a building twice as close gets on average about four times as much. The 1 is added so that a building at zero
cost, in the same place as the service, gets a finite weight. The allocation runs in rounds:
Search radius. The first round considers the pairs with a cost of up to about half the threshold,
(threshold + 1) / 2; every round widens this radius 1.5 times, so nearby buildings are served first.Offers. Every service with free places takes the buildings with unmet demand inside the radius, keeps the ones with the largest weights (first the top tenth of them; the share grows as buildings and services drop out) and distributes its free places among them at random, in proportion to their weights.
Acceptance. A building that got more offers than it needs accepts its unmet demand, choosing among the offers at random in proportion to their size; the rest of the places stay free.
Next round. Buildings with no unmet demand and services with no free places drop out, and the rounds go on until nothing more can be allocated.
The random streams are seeded per object and iteration from seed, so a fixed seed gives the same result
regardless of the number of CPU cores. Different seeds give alternative allocations with the same model.
Outputs¶
objectnat.ProvisionResult contains
flow: sparse building × service matrix of allocated demand;demand_rows: per buildingdemand,demand_left,supplied_demands_withinandsupplied_demands_without(allocated from services within / beyondthreshold),min_dist(cost to the nearest service),avg_dist(flow-weighted mean cost) andprovision_value=supplied_demands_within / demand;capacity_rows: per servicecapacity,capacity_left,carried_capacity_within,carried_capacity_withoutandservice_load(total allocated demand);distance_matrixandthresholdused for the calculation.
objectnat.get_provision_buildings() and objectnat.get_provision_services() join these metrics to copies
of the input GeoDataFrames; objectnat.get_provision_links() returns one straight line per non-zero flow, from the
representative point of the building to the service, with building_index, service_index, demand
(allocated units) and distance (cost).
- objectnat.get_service_provision(*, buildings, distance_matrix, services, threshold, buildings_demand_column='demand', services_capacity_column='capacity', seed=0)[source]¶
Compute service provision between demand locations and service facilities.
The function implements a gravity-based allocation model: service capacity is distributed across nearby demand points with weights that decay with the square of distance (or generalized cost). Closer buildings receive proportionally higher shares of the available capacity. Only building-service pairs within
3 * thresholdtake part in the allocation, which runs in ObjectNat’s Rust extension over those pairs only.- Parameters:
buildings (
GeoDataFrame) – GeoDataFrame of demand locations. Must include a numeric demand column.distance_matrix (
DataFrame) – OD cost matrix where rows arebuildings.indexand columns areservices.index. Units must matchthreshold. Either a dense DataFrame (inf/NaN= unreachable) or, for large areas, a pandas sparse DataFrame (SparseDtypewith aNaNorinffill value) that stores only the reachable pairs.services (
GeoDataFrame) – GeoDataFrame of service facilities. Must include a numeric capacity column.threshold (
float) – Normative cost threshold. Units are the same as indistance_matrix.buildings_demand_column (
str) – Column name of building demand values. Default is"demand".services_capacity_column (
str) – Column name of service capacity values. Default is"capacity".seed (
int) – Seed of the random draws in the allocation. A fixed seed yields reproducible results (independent of the number of CPU cores); vary it to sample alternative allocations. Default is0.
- Returns:
Dataclass with a sparse building-service flow matrix, building metrics, service metrics, the aligned distance matrix, and the provision threshold.
- Return type:
- class objectnat.ProvisionResult(flow, demand_rows, capacity_rows, distance_matrix, threshold)[source]¶
Result of a service provision calculation.
- Parameters:
- flow¶
Sparse building-service allocation matrix. Rows match building indices, columns match service indices, and non-zero values are allocated demand units.
- demand_rows¶
Per-building metrics indexed like
flow.index. Includes original demand, remaining demand, supplied demand within/beyond the threshold, the cost to the nearest service, the flow-weighted mean cost and provision value.
- capacity_rows¶
Per-service metrics indexed like
flow.columns. Includes original capacity, remaining capacity, carried capacity within/beyond the threshold, and total service load.
- distance_matrix¶
Aligned OD cost matrix used for the calculation (dense, or pandas sparse when a sparse matrix was passed). Its index and columns must match
flow.
- threshold¶
Normative distance or time threshold used to split flows into within-threshold and beyond-threshold metrics.
- objectnat.get_provision_buildings(buildings_gdf, provision_result)[source]¶
Join provision demand metrics to buildings.
Existing columns with the same names as provision metrics are dropped and replaced. The returned object preserves the input type and index.
- Return type:
- Parameters:
buildings_gdf (GeoDataFrame | DataFrame)
provision_result (ProvisionResult)
- objectnat.get_provision_services(services_gdf, provision_result)[source]¶
Join provision capacity metrics to services.
Existing columns with the same names as provision metrics are dropped and replaced. The returned object preserves the input type and index.
- Return type:
- Parameters:
services_gdf (GeoDataFrame | DataFrame)
provision_result (ProvisionResult)
- objectnat.get_provision_links(buildings_gdf, services_gdf, provision_result)[source]¶
Build building-service link geometries from non-zero provision flows.
Services are reprojected to the buildings CRS when both CRS values are set and differ. Empty flows return an empty
GeoDataFrame. Links whose building or service ids are absent from the supplied GeoDataFrames are filtered out.- Return type:
- Parameters:
buildings_gdf (GeoDataFrame)
services_gdf (GeoDataFrame)
provision_result (ProvisionResult)
Provision value of residential buildings for the example schools with a 10-minute threshold on the intermodal
travel-time matrix (seed=0); dots are schools. Non-residential buildings and streets are
grey; the black line is the district boundary.¶
The same result with objectnat.get_provision_links(): one line per non-zero flow, width proportional to
the allocated demand. Dark links lie within the threshold, light ones beyond it (up to 3 * threshold); the
light links carry the demand that the schools within 10 minutes cannot accommodate.¶
Recalculation and clipping¶
objectnat.recalculate_links() removes the flows whose cost exceeds new_max_dist and rebuilds the
aggregate metrics without a new allocation. The removed demand is not redistributed to other services; run
objectnat.get_service_provision() again for a full reallocation.
objectnat.clip_provision() keeps the buildings that intersect selection_zone, the links of these buildings
and the services the links refer to. All four inputs must share one CRS.
- objectnat.recalculate_links(provision_result, new_max_dist)[source]¶
Recalculate a provision result with a shorter maximum link cost.
Flows whose cost exceeds
new_max_distare removed, and building/service aggregates are recomputed without redistributing removed demand.- Return type:
- Parameters:
provision_result (ProvisionResult)
new_max_dist (float)
- objectnat.clip_provision(buildings, services, links, selection_zone)[source]¶
Clip service provision outputs to a specific geographic boundary.
Keeps only buildings that intersect
selection_zone, links that connect to the kept buildings, and services referenced by those links.- Return type:
- Parameters:
buildings (GeoDataFrame)
services (GeoDataFrame)
links (GeoDataFrame)
selection_zone (GeoDataFrame)
recalculate_links(result, 10): the links longer than 10 minutes are removed and the metrics are rebuilt;
their demand stays unallocated.¶
objectnat.clip_provision() with a 450 m circle: the buildings in the zone, all their links and the schools
these links lead to, including schools outside the zone.¶
Limitations¶
The allocation is stochastic: individual flows depend on
seed; aggregate metrics vary little between seeds for large inputs but can vary for small ones.Demand and capacity are integers (allocated in units); fractional values are truncated.
The model has no explicit choice behaviour (quality, preference, catchment rules); every reachable service within
3 * thresholdcan receive demand.The result is only as accurate as the cost matrix; the example matrix contains travel times in minutes on an intermodal graph.
Example notebooks¶
Building-to-service travel-time matrix (cost matrix with IduEdu) and Service provision.