isopolygons
Isopolygon calculation module for service area analysis.
This module provides functionality for calculating isopolygons around facilities using different methods and services. An isopolygon represents the area that can be reached within a specific distance (isodistance) or time (isochrone) from a facility.
The module contains an abstract base class IsopolygonCalculator and its implementations for different calculation methods.
Examples
Calculate isochrones around facilities using OpenStreetMap:
>>> from pisa.administrative_area import AdministrativeArea
>>> from pisa.facilities import Facilities
>>> from pisa.osm_road_network import OsmRoadNetwork
>>> from pisa.isopolygons import OsmIsopolygonCalculator
>>>
>>> # Get administrative area and facilities
>>> admin_area = AdministrativeArea("Timor-Leste", admin_level=1)
>>> boundaries = admin_area.get_admin_area_boundaries("Baucau")
>>> facilities = Facilities(admin_area_boundaries=boundaries)
>>> existing_facilities = facilities.get_existing_facilities()
>>>
>>> # Create a road network for travel time calculations
>>> road_network = OsmRoadNetwork(
>>> admin_area_boundaries=boundaries,
>>> mode_of_transport="walking",
>>> distance_type="travel_time"
>>> )
>>> graph = road_network.get_osm_road_network()
>>>
>>> # Calculate isochrones (5, 10, 15 minutes walking)
>>> isopolygon_calculator = OsmIsopolygonCalculator(
>>> facilities_df=existing_facilities,
>>> distance_type="travel_time",
>>> distance_values=[5, 10, 15],
>>> road_network=graph
>>> )
>>> isopolygons = isopolygon_calculator.calculate_isopolygons()
Note
To implement a new way of calculating isopolygons (e.g., using Google Maps), create a class that inherits from IsopolygonCalculator and implements calculate_isopolygons.
See also
facilitiesModule for retrieving facility locations
osm_road_networkModule for retrieving and processing road networks
population_served_by_isopolygonsModule for analyzing population coverage
Attributes
Classes
Abstract base class for isopolygon calculation. |
|
OpenStreetMap-based implementation of isopolygon calculation. |
|
Mapbox-based implementation of isopolygon calculation. |
Module Contents
- isopolygons.logger
- class isopolygons.IsopolygonCalculator(facilities_df: pandas.DataFrame, distance_type: str, distance_values: int | list[int])
Bases:
abc.ABCAbstract base class for isopolygon calculation.
- facilities_df
- distance_type
- distance_values
- abstract calculate_isopolygons() pandas.DataFrame
Calculate isopolygons for the specified facilities.
This abstract method must be implemented by subclasses to provide the actual implementation of isopolygon calculation using specific data sources and algorithms.
Specific implementations should provide detailed error handling and logging appropriate to their data sources and algorithms.
- Returns:
DataFrame containing isopolygons with the following structure:
Each row represents a facility from facilities_df
One column named
ID_{distance}for each distance value in distance_values, where {distance} is the distance value in meters or minutesEach cell contains a Shapely
PolygonorMultiPolygonrepresenting the area that can be reached within the corresponding distance
- Return type:
pandas.DataFrame
- Raises:
NotImplementedError – If this method is not implemented by a subclass
- class isopolygons.OsmIsopolygonCalculator(facilities_df: pandas.DataFrame, distance_type: str, distance_values: list[int], road_network: networkx.MultiDiGraph, node_buffer: float = 0.001, edge_buffer: float = 0.0005)
Bases:
IsopolygonCalculatorOpenStreetMap-based implementation of isopolygon calculation.
This implementation uses OpenStreetMap road network data to calculate isopolygons (isochrones or isodistances) around facilities. It leverages the NetworkX and OSMnx libraries for network analysis.
This implementation performs network-based calculations on an OSM road network, which provides accurate results but may be computationally intensive for large areas or many facilities.
- Parameters:
facilities_df (pandas.DataFrame) – DataFrame containing facility locations with
longitudeandlatitudecolumnsdistance_type (str) – Type of distance to calculate (
lengthortravel_time)distance_values (list[int]) – List of distance values in meters (for
length) or minutes (fortravel_time)road_network (networkx.MultiDiGraph) – Road network graph to use for calculations
node_buffer (float, optional) – Buffer distance to apply around network nodes. (default:
0.001)edge_buffer (float, optional) – Buffer distance to apply around network edges. (default:
0.0005)
See also
IsopolygonCalculatorAbstract base class
MapboxIsopolygonCalculatorMapbox API-based implementation
- road_network
- node_buff = 0.001
- edge_buff = 0.0005
- nearest_nodes_dict
- calculate_isopolygons() pandas.DataFrame
Calculate isopolygons for each facility at different distances.
This method generates isopolygons (areas reachable within specific travel times or distances) for each facility using the OpenStreetMap road network data.
- Returns:
DataFrame containing isopolygons with the following structure:
Each row represents a facility from facilities_df
One column named
ID_{distance}for each distance value in distance_values, where {distance} is the distance value in meters or minutesEach cell contains a Shapely
PolygonorMultiPolygonrepresenting the area that can be reached within the corresponding distance
- Return type:
pandas.DataFrame
Notes
Distances are computed with respect to the nearest node to the facility, not the facility itself
The method creates network “skeletons” and then buffers nodes and edges separately with different buffer sizes to create accurate isopolygons
- class isopolygons.MapboxIsopolygonCalculator(facilities_df: pandas.DataFrame, distance_type: str, distance_values: list[int], mode_of_transport: str, mapbox_api_token: str, base_url: str = 'https://api.mapbox.com/isochrone/v1/')
Bases:
IsopolygonCalculatorMapbox-based implementation of isopolygon calculation.
This implementation uses the Mapbox Isochrone API to calculate isopolygons (isochrones or isodistances) around facilities.
- Parameters:
facilities_df (pandas.DataFrame) – DataFrame containing facility locations with
longitudeandlatitudecolumnsdistance_type (str) – Type of distance to calculate (
lengthortravel_time)distance_values (list[int]) – List of distance values in meters (for
length) or minutes (fortravel_time) Maximum of 4 values allowed by the Mapbox APImode_of_transport (str) – The mode of transport to use (must be one of
driving,walking,cycling)mapbox_api_token (str) – A valid Mapbox API access token with Isochrone API permissions
base_url (str, optional) – The base URL for the Mapbox Isochrone API, default is ‘https://api.mapbox.com/isochrone/v1/’
See also
IsopolygonCalculatorAbstract base class
OsmIsopolygonCalculatorOSM-based implementation with precise node/edge buffering
Notes
From Mapbox docs: When providing geographic coordinates to a Mapbox API, they should be formatted in the order longitude, latitude and specified as decimal degrees in the WGS84 coordinate system. This pattern matches existing standards, including GeoJSON and KML.
This implementation is subject to Mapbox API rate limits and requires a valid Mapbox account and access token.
- mapbox_api_token
- route_profile
- distance_values
- base_url = 'https://api.mapbox.com/isochrone/v1/'
- contour_type = 'contours_meters'
- calculate_isopolygons() pandas.DataFrame
Calculate isopolygons for all facilities using the Mapbox API.
This method generates isopolygons (areas reachable within specific travel times or distances) for each facility using the Mapbox Isochrone API.
- Returns:
DataFrame containing isopolygons with the following structure:
Each row represents a facility from facilities_df
One column named
ID_{distance}for each distance value in distance_values, where {distance} is the distance value in meters or minutesEach cell contains a Shapely
PolygonorMultiPolygonrepresenting the area that can be reached within the corresponding distance
- Return type:
pandas.DataFrame
Notes
Requires a valid Mapbox API token with appropriate permissions
Subject to Mapbox API rate limits (300 requests per minute)
Makes one API request per facility (not per distance value)
The API returns GeoJSON features that are converted to Shapely geometries