Graph extraction#
The graph building module provides tools to create geospatially enriched graphs from road network data.
- class libadalina_core.graph_extraction.MandatoryColumns(value)[source]#
Enum representing mandatory columns required in the input DataFrame.
- class libadalina_core.graph_extraction.OneWay(value)[source]#
Enum representing road directions.
- Backward = 'backward'#
Goes against the direction of the geometry.
- Both = 'both'#
Both directions are allowed.
- Forward = 'forward'#
Follows the direction of the geometry.
- class libadalina_core.graph_extraction.OpenStreetMapReader(road_types: RoadTypes = RoadTypes.ALL)[source]#
A class to read OpenStreetMap (OSM) data files and convert them into a GeoDataFrame with standardized columns.
- from_dataframe(df: DataFrame) GeoDataFrame[source]#
Convert a pandas DataFrame of OSM data to a GeoDataFrame with the required columns.
- Parameters:
df (pd.DataFrame) – The OSM data as a pandas DataFrame. Must contain ‘osm_id’, ‘name’, ‘oneway’, and ‘geometry’ columns.
- Returns:
A GeoDataFrame containing only the mandatory columns with standardized names.
- Return type:
gpd.GeoDataFrame
- read(file_path: str) GeoDataFrame[source]#
Read an OSM data file and return a GeoDataFrame with the required columns. Accepts CSV, Shapefile, and GeoPackage formats.
- Parameters:
file_path (str) – The path to the OSM data file. Must contain ‘osm_id’, ‘name’, ‘oneway’, and ‘geometry’ columns.
- Returns:
A GeoDataFrame containing only the mandatory columns with standardized names.
- Return type:
gpd.GeoDataFrame
- class libadalina_core.graph_extraction.RoadTypes(value)[source]#
Enum representing different types of roads for filtering purposes.
- ALL = 'all'#
Keep all roads.
- CAR_ONLY = 'only_car'#
Keep only roads accessible by car.
- MAIN_ROADS = 'main_roads'#
Keep only main roads (motorways, trunks, primary).
- libadalina_core.graph_extraction.build_graph(roads_df: DataFrame | GeoDataFrame | DataFrame, name: str = 'graph', joined_df: DataFrame | GeoDataFrame | DataFrame | None = None, aggregate_functions: list[AggregationFunction] | None = None, buffer_radius_meters: float = 100, dataframe_only=False) Graph | DataFrame[source]#
Build a directed graph from a DataFrame of roads. Optionally, enrich the graph with data from another DataFrame using spatial join and aggregation. By default, each edge of the graph is already enriched with distance information.
- Parameters:
roads_df (DataFrame) – Road network as a DataFrame. Must contain at least columns of MandatoryColumns enumeration.
name (str) – The name of the graph. Defaults to ‘graph’.
joined_df (DataFrame | None) – DataFrame to join with the roads DataFrame. If None, no join is performed. Defaults to None.
aggregate_functions (list[AggregationFunction]) – List of aggregation functions to apply during the spatial aggregation after the join. If None, no aggregation is performed. Defaults to None. See also AggregationFunction and spatial_aggregation for more details.
buffer_radius_meters (float) – The radius in meters to buffer the road segments before performing the spatial join. If joined_df is None, this parameter is ignored. Defaults to 100 meters.
dataframe_only (bool) – If True, the function stops before building a networkx Graph and returns a Spark DataFrame instead.
- Returns:
The resulting directed graph as a networkx DiGraph or a Spark DataFrame if dataframe_only is True.
- Return type:
nx.Graph | pyspark.sql.DataFrame
- libadalina_core.graph_extraction.graph_to_geopackage(graph: Graph, file_path: str)[source]#
Write a networkx graph to a geopackage.
The function first uses graph_to_pandas to convert the graph into a pandas DataFrame, and then save it to file as a GeoDataFrame in a geopackage format.
- Parameters:
graph (networkx.Graph) – The networkx graph to write.
file_path (str) – The path to the geopackage.
- libadalina_core.graph_extraction.graph_to_pandas(graph: Graph) DataFrame[source]#
Convert a networkx graph to a pandas DataFrame.
- Parameters:
graph (nx.Graph) – The networkx graph to convert.
- Returns:
A pandas DataFrame containing the edges of the graph.
- Return type:
pd.DataFrame
- libadalina_core.graph_extraction.graph_to_shapefile(graph: Graph, path: str)[source]#
Write a networkx graph to a shapefile.
- Parameters:
graph (networkx.Graph) – The networkx graph to write.
path (str) – The path to the shapefile.
- libadalina_core.graph_extraction.path_to_geopackage(graph: Graph, path: list, file_path: str)[source]#
Save a path in a networkx graph to a geopackage file.
The function first uses path_to_pandas to convert the path into a pandas DataFrame and then saves it as a file in a geopackage format.
- libadalina_core.graph_extraction.path_to_pandas(graph: Graph, path: list) DataFrame[source]#
Convert a path in a networkx graph to a pandas DataFrame.
- Parameters:
graph (nx.Graph) – The networkx graph containing the path.
path (list) – A list of nodes representing the path.
- Returns:
A pandas DataFrame containing the edges of the path.
- Return type:
- libadalina_core.graph_extraction.paths_to_geopackage(graph: Graph, paths: list, file_path: str)[source]#
Save paths in a networkx graph to a geopackage file.
The function first uses paths_to_pandas to convert the path into a pandas DataFrame and then saves it as a file in a geopackage format.
- libadalina_core.graph_extraction.paths_to_pandas(graph: Graph, paths: list) DataFrame[source]#
Convert paths in a networkx graph to a pandas DataFrame.
- Parameters:
graph (nx.Graph) – The networkx graph containing the path.
paths (list) – A list of paths.
- Returns:
A pandas DataFrame containing the edges of the paths.
- Return type: