Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

core.py

convert(x, u_tag, v_tag, geometry_tag, attr_list)

Description:

Convert a row of a pandas dataframe to a tuple of (u, v, data) for use in a networkx graph

Parameters:

Returns:


combo_csv_to_graph(fpath, u_tag='u', v_tag='v', geometry_tag='Wkt', largest_G=False)

Description:

Function for generating a G object from a saved combo .csv

Parameters:

Returns:


check(x, chck_set)

Description:

Check if a value is in a set

Parameters:

Returns:


selector(x)

Description:

Selects and returns an integer if the input is an integer, otherwise returns the input as is.

Parameters:

Returns:


edges_and_nodes_gdf_to_graph(nodes_df, edges_df, node_tag='node_ID', u_tag='stnode', v_tag='endnode', geometry_tag='Wkt', largest_G=False, discard_node_col=[], checks=False, add_missing_reflected_edges=False, oneway_tag=None)

Description:

Function for generating a G object from dataframse of nodes and one of edges

Parameters:

Returns:


edges_and_nodes_csv_to_graph(fpath_nodes, fpath_edges, u_tag='stnode', v_tag='endnode', geometry_tag='Wkt', largest_G=False)

Description:

Function for generating a G object from a saved .csv of edges

Parameters:

Returns:


flatten(line)

Description:

Flattens a nested list into a single list.

Parameters:

Returns:


node_gdf_from_graph(G, crs='epsg:4326', attr_list=None, geometry_tag='geometry', xCol='x', yCol='y')

Description:

Function for generating GeoDataFrame from Graph

Parameters:

Returns:


edge_gdf_from_graph(G, crs='EPSG:4326', attr_list=None, geometry_tag='geometry', xCol='x', yCol='y', oneway_tag='oneway', single_edge=False)

Description:

Function for generating a GeoDataFrame from a networkx Graph object

Parameters:

Returns:


chck(x, poly)

Description:

Check if a point is within a polygon

Parameters:

Returns:


graph_nodes_intersecting_polygon(G, polygons, crs=None)

Description:

Function for identifying nodes of a graph that intersect polygon(s). Ensure any GeoDataFrames are in the same projection before using function, or pass a crs.

Parameters:

Returns:


graph_edges_intersecting_polygon(G, polygons, mode, crs=None, fast=True)

Description:

Function for identifying edges of a graph that intersect polygon(s). Ensure any GeoDataFrames are in the same projection before using function, or pass a crs.

Parameters:

Returns:


sample_raster(G, tif_path, property_name='RasterValue')

Description:

Function for attaching raster values to corresponding graph nodes.

Parameters:

Returns:


generate_isochrones(G, origins, thresh, weight=None, stacking=False)

Description:

Function for generating isochrones from one or more graph nodes. Ensure any GeoDataFrames / graphs are in the same projection before using function, or pass a crs

Parameters:

Returns:


make_iso_polys(G, origins, trip_times, edge_buff=10, node_buff=25, infill=False, weight='time', measure_crs='epsg:4326', edge_filters=None)

Description:

Function for adding a time value to edge dictionaries

Parameters:

Returns:


make_iso_polys_original(G, origins, trip_times, edge_buff=10, node_buff=25, infill=False, weight='time', measure_crs='epsg:4326')

Description:

Function for adding a time value to edge dictionaries

Parameters:

Returns:


find_hwy_distances_by_class(G, distance_tag='length')

Description:

Function for finding out the different highway classes in the graph and their respective lengths

Parameters:

Returns:


find_graph_avg_speed(G, distance_tag, time_tag)

Description:

Function for finding the average speed per km for the graph. It will sum up the total meters in the graph and the total time (in sec). Then it will convert m/sec to km/hr. This function needs the ‘convert_network_to_time’ function to have run previously.

Parameters:

Returns:


example_edge(G, n=1)

Description:

Prints out an example edge

Parameters:

Returns:


example_node(G, n=1)

Description:

Prints out an example node

Parameters:

Returns:


convert_network_to_time(G, distance_tag, graph_type='drive', road_col='highway', output_time_col='time', speed_dict=None, walk_speed=4.5, factor=1, default=20)

Description:

Function for adding a time value to graph edges. Ensure any graphs are in the same projection before using function, or pass a crs.

Parameters:

Returns:


first_val(x)

Description:

Get the first value of a list, or the value itself if it is not a list

Parameters:

Returns:


Description:

Function for querying travel times from the Mapbox “driving traffic” API. Queries are only made for the specified road types.

Parameters:

Returns:


calculate_OD(G, origins, destinations, fail_value, weight='time', weighted_origins=False, one_way_roads_exist=False, verbose=False)

Description:

Function for generating an origin: destination matrix

Parameters:

Returns:


disrupt_network(G, property, thresh, fail_value)

Description:

Function for disrupting a graph given a threshold value against a node’s value. Any edges which bind to broken nodes have their ‘time’ property set to fail_value

Parameters:

Returns:


randomly_disrupt_network(G, edge_frac, fail_value)

Description:

Function for randomly disurpting a network. NOTE: requires the graph to have an ‘edge_id’ value in the edge data dictionary. This DOES NOT have to be unique.

Parameters:

Returns:


gravity_demand(G, origins, destinations, weight, maxtrips=100, dist_decay=1, fail_value=99999999999)

Description:

Function for generating a gravity-model based demand matrix. Note: 1 trip will always be returned between an origin and a destination, even if weighting would otherwise be 0.

Parameters:

Returns:


unbundle_geometry(c)

Description:

Function for unbundling complex geometric objects. Note: shapely MultiLineString objects quickly get complicated. They may not show up when you plot them in QGIS. This function aims to make a .csv ‘plottable’

Parameters:

Returns:


save(G, savename, wpath, pickle=True, edges=True, nodes=True)

Description:

function used to save a graph object in a variety of handy formats

Parameters:

Returns:


add_missing_reflected_edges(G, one_way_tag=None, verbose=False)

Description:

function for adding any missing reflected edges - makes all edges bidirectional. This is essential for routing with simplified graphs

Parameters:

Returns:


remove_duplicate_edges(G, max_ratio=1.5)

Description:

function for deleting duplicated edges - where there is more than one edge connecting a node pair. USE WITH CAUTION - will change both topological relationships and node maps

Parameters:

Returns:


convert_to_MultiDiGraph(G)

Description:

takes any graph object, loads it into a MultiDiGraph type Networkx object

Parameters:

Returns:


simplify_junctions(G, measure_crs, in_crs='epsg:4326', thresh=25, verbose=False)

Description:

simplifies topology of networks by simplifying node clusters into single nodes.

Parameters:

Returns:


get_paths_to_simplify(G, strict=True)

Description:

Create a list of all the paths to be simplified between endpoint nodes.

Parameters:

Returns:


is_endpoint(G, node, strict=True)

Description:

Return True if the node is a “real” endpoint of an edge in the network, otherwise False. OSM data includes lots of nodes that exist only as points to help streets bend around curves. An end point is a node that either: 1) is its own neighbor, ie, it self-loops. 2) or, has no incoming edges or no outgoing edges, ie, all its incident edges point inward or all its incident edges point outward. 3) or, it does not have exactly two neighbors and degree of 2 or 4. 4) or, if strict mode is false, if its edges have different OSM IDs.

Parameters:

Returns:


build_path(G, node, endpoints, path)

Description:

Recursively build a path of nodes until you hit an endpoint node.

Parameters:

Returns:


custom_simplify(G, strict=True)

Description:

Simplify a graph’s topology by removing all nodes that are not intersections or dead-ends. Create an edge directly between the end points that encapsulate them, but retain the geometry of the original edges, saved as attribute in new edge.

Parameters:

Returns:


cut(line, distance)

Description:

Cuts a line in two at a distance from its starting point

Parameters:

Returns:


salt_long_lines(G, source, target, thresh=5000, factor=1, attr_list=None, geometry_tag='Wkt')

Description:

Adds in new nodes to edges greater than a given length

Parameters:

Returns:


pandana_snap(G, point_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True, time_it=False)

Description:

snaps points to a graph at very high speed

Parameters:

Returns:


pandana_snap_c(G, point_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True, time_it=False)

Description:

snaps points to a graph at a faster speed than pandana_snap.

Parameters:

Returns:


pandana_snap_to_many(G, point_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True, time_it=False, k_nearest=5, origin_id='index')

Description:

snaps points their k nearest neighbors in the graph.

Parameters:

Returns:


pandana_snap_single_point(G, shapely_point, source_crs='epsg:4326', target_crs='epsg:4326')

Description:

snaps a point to a graph at very high speed

Parameters:

Returns:


pandana_snap_points(source_gdf, target_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True)

Description:

snaps points to another GeoDataFrame at very high speed

Parameters:

Returns:


join_networks(base_net, new_net, measure_crs, thresh=500)

Description:

joins two networks together within a binding threshold

Parameters:

Returns:


clip(G, bound, source_crs='epsg:4326', target_crs='epsg:4326', geom_col='geometry', largest_G=True)

Description:

Removes any edges that fall beyond a polygon, and shortens any other edges that do so

Parameters:

Returns:


new_edge_generator(passed_geom, infra_type, iterator, existing_legitimate_point_geometries, geom_col, project_WGS_UTM)

Description:

Generates new edge and node geometries based on a passed geometry. WARNING: This is a child process of clip(), and shouldn’t be run on its own

Parameters:

Returns:


project_gdf(gdf, to_crs=None, to_latlong=False)

Description:

Taken from OSMNX

Parameters:

Returns:


gn_project_graph(G, to_crs=None)

Description:

Taken from OSMNX. Project graph from its current CRS to another.

Parameters:

Returns:


euclidean_distance(lat1, lon1, lat2, lon2)

Description:

Calculate the great circle distance between two points on the earth (specified in decimal degrees)

Parameters:

Returns:


utm_of_graph(G)

Description:

Calculates the UTM coordinate reference system (CRS) for a given graph.

Parameters:

Returns:


find_kne(point, lines, near_idx)

Description:

Find the nearest edge (kne) to a given point from a set of lines.

Parameters:

Returns:


get_pp(point, line)

Description:

Get the projected point (pp) of ‘point’ on ‘line’.

Parameters:

Returns:


split_line(line, pps)

Description:

Split ‘line’ by all intersecting ‘pps’ (as multipoint).

Parameters:

Returns:


update_nodes(nodes, new_points, ptype, road_col, node_key_col, poi_key_col, node_highway_pp, node_highway_poi, measure_crs='epsg:3857', osmid_prefix=9990000000)

Description:

Update nodes with a list (pp) or a GeoDataFrame (poi) of new_points.

Parameters:

Returns:


update_edges(edges, new_lines, replace=True, nodes_meter=None, pois_meter=None)

Description:

Update edge info by adding new_lines; or,

Parameters:

Returns:


nearest_edge(row, Rtree, knn, edges_meter)

Description:

Finds the nearest edge(s) to a given point.

Parameters:

Returns:


advanced_snap(G, pois, u_tag='stnode', v_tag='endnode', node_key_col='osmid', edge_key_col='osmid', poi_key_col=None, road_col='highway', oneway_tag='oneway', path=None, threshold=500, knn=5, measure_crs='epsg:3857', factor=1, verbose=False)

Description:

Connect and integrate a set of POIs into an existing road network.

Parameters:

Returns:


add_intersection_delay(G, intersection_delay=7, time_col='time', highway_col='highway', filter=['projected_footway', 'motorway'])

Description:

Find node intersections. For all intersection nodes, if directed edge is going into the intersection then add delay to the edge.

Parameters:

Returns: