GOSTnets package¶
Submodules¶
GOSTnets.calculate_od_raw module¶
-
GOSTnets.calculate_od_raw.calculateOD_csv(G, originCSV, destinationCSV='', oLat='Lat', oLon='Lon', dLat='Lat', dLon='Lon', crs={'init': 'epsg:4326'}, fail_value=-1, weight='time', calculate_snap=False)¶ Calculate OD matrix from csv files of points
Parameters: - G – describes the road network. Often extracted using OSMNX
- origins (string) – path to csv file with locations for calculating access
- destinations (string) – path to csv with destination locations for calculating access
- oLat (string) –
- oLon (string) –
- dLat (string) –
- dLon (string) –
- crs (dict) – crs of input origins and destinations, defaults to {‘init’:’epsg:4326’}
- fail-value (int) – value to put in OD matrix if no route found, defaults to -1
- weight (string) – variable in G used to define edge impedance, defaults to ‘time’
- calculate_snap (bool) – variable to add snapping distance to travel time, default is false
Returns: numpy array: 2d OD matrix with columns as index of origins and rows as index of destinations
-
GOSTnets.calculate_od_raw.calculateOD_gdf(G, origins, destinations, fail_value=-1, weight='time', calculate_snap=False, wgs84={'init': 'epsg:4326'})¶ Calculate Origin destination matrix from GeoDataframes
- Args:
- G (networkx graph): describes the road network. Often extracted using OSMNX origins (geopandas dataframe): source locations for calculating access destinations (geopandas dataframe): destination locations for calculating access calculate_snap (boolean, optioinal): variable to add snapping distance to travel time, default is false wgs84 (CRS dictionary, optional): CRS fo road network to which the GDFs are projected
- Returns:
- numpy array: 2d OD matrix with columns as index of origins and rows as index of destinations
-
GOSTnets.calculate_od_raw.calculate_gravity(od, oWeight=[], dWeight=[], decayVals=[0.01, 0.005, 0.001, 0.0007701635, 0.0003850818, 0.0001925409, 9.62704e-05, 3.85082e-05, 1e-05])¶
GOSTnets.conversion_utils module¶
-
GOSTnets.conversion_utils.rasterize_od_results(inD, outFile, field, template=None)¶ Convert gridded point data frame to raster of commensurate size and resolution
INPUT inD [ geopandas data frame ] - OD matrix as point data frame outFile [ string ] - path to save output raster field [ string ] - field to rasterize
RETURNS None
GOSTnets.core module¶
-
GOSTnets.core.add_intersection_delay(G, intersection_delay=7, time_col='time', highway_col='highway', filter=['projected_footway', 'motorway'])¶ Find node intersections. For all intersection nodes, if directed edge is going into the intersection then add delay to the edge. If the highest rank road at an intersection intersects a lower rank road, then the highest rank road does not get delayed. This assumes the highest rank road has the right-of-way.
Parameters: - G – a base network object (nx.MultiDiGraph)
- intersection_delay – The number of seconds to delay travel time at intersections
Filter: The filter is a list of highway values where the type of highway does not get an intersection delay.
Returns: a base network object (nx.MultiDiGraph)
-
GOSTnets.core.add_missing_reflected_edges(G, one_way_tag=None, verbose=False)¶ function for adding any missing reflected edges - makes all edges bidirectional. This is essential for routing with simplified graphs
Parameters: - G – a graph object
- one_way_tag – if exists, then values that are True are one-way and will not be reflected
-
GOSTnets.core.add_missing_reflected_edges_old(G, one_way_tag=None)¶ to-do: delete this function, it is slower, creating a unique edge list slows things down with a big graph
function for adding any missing reflected edges - makes all edges bidirectional. This is essential for routing with simplified graphs
Parameters: - G – a graph object
- one_way_tag – if exists, then values that are True are one-way and will not be reflected
-
GOSTnets.core.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)¶ Connect and integrate a set of POIs into an existing road network.
Given a road network in the form of two GeoDataFrames: nodes and edges, link each POI to the nearest edge (road segment) based on its projection point (PP) and generate a new integrated road network including the POIs, the projected points, and the connection edge.
Credit for original code: Yuwen Chang, 2020-08-16
- Make sure all three input GeoDataFrames have defined crs attribute. Try something like gdf.crs or gdf.crs = ‘epsg:4326’. They will then be converted into epsg:3857 or specified measure_crs for processing.
Parameters: - (GeoDataFrame) (edges) – a gdf of POI (geom: Point)
- (GeoDataFrame) – a gdf of road network nodes (geom: Point)
- (GeoDataFrame) – a gdf of road network edges (geom: LineString)
- (str) (path) – The node tag id in the returned graph
- (str) – The edge tag id in the returned graph
- (str) – a unique key column of pois should be provided, e.g., ‘index’, ‘osmid’, ‘poi_number’, etc. Currently, this will be renamed into ‘osmid’ in the output. [NOTE] For use in pandana, you may want to ensure this column is numeric-only to avoid processing errors. Preferably use unique integers (int or str) only, and be aware not to intersect with the node key, ‘osmid’ if you use OSM data, in the nodes gdf.
- (str) – The tag to be used for oneway edges
- (str) – directory path to use for saving optional shapefiles (nodes and edges). Outputs will NOT be saved if this arg is not specified.
- (int) (measure_crs) – the max length of a POI connection edge, POIs withconnection edge beyond this length will be removed. The unit is in meters as crs epsg is set to 3857 by default during processing.
- (int) – k nearest neighbors to query for the nearest edge. Consider increasing this number up to 10 if the connection output is slightly unreasonable. But higher knn number will slow down the process.
- (int) – preferred EPSG in meter units. Suggested to use the correct UTM projection.
- factor – allows you to scale up / down unit of returned new_footway_edges if other than meters. Set to 1000 if length in km.
Returns: G (graph): the original gdf with POIs and PPs appended and with connection edges appended and existing edges updated (if PPs are present)pois_meter (GeoDataFrame): gdf of the POIs along with extra columns, such as the associated nearest lines and PPs new_footway_edges (GeoDataFrame): gdf of the new footway edges that connect the POIs to the orginal graph
-
GOSTnets.core.assign_traffic_times(G, mb_token, accepted_road_types=['trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link', 'motorway', 'motorway_link'], verbose=False, road_col='infra_type', id_col='id')¶ Function for querying travel times from the Mapbox “driving traffic” API. Queries are only made for the specified road types.
Parameters: - G – a graph object of the road network
- mb_token – Mapbox token (retrieve from Mapbox account, starts with “pk:”)
- road_types – a list of OSM road types for which to query traffic-aware travel time, defaults to main roads
- verbose – Set to true to monitor progress of queries and notify if any queries failed, defaults to False
- road_col – key for the road type in the edge data dictionary, defaults to ‘infra_type’
- id_col – key for the id in the edge data dictionary, defaults to ‘id’
Returns: The original graph with two new data properties for the edges: ‘mapbox_api’ (a boolean set to True if the edge succesfuly received a trafic time value) and ‘time_traffic’ (travel time in seconds)
-
GOSTnets.core.calculate_OD(G, origins, destinations, fail_value, weight='time', weighted_origins=False, one_way_roads_exist=False, verbose=False)¶ Function for generating an origin: destination matrix
Parameters: - G – a graph containing one or more nodes
- fail_value – the value to return if the trip cannot be completed (implies some sort of disruption / disconnected nodes)
- origins – a list of the node IDs to treat as origins points
- destinations – a list of the node IDs to treat as destinations
- weight – use edge weight of ‘time’ unless otherwise specified
- weighted_origins – equals ‘true’ if the origins have weights. If so, the input to ‘origins’ must be dictionary instead of a list, where the keys are the origin IDs and the values are the weighted demands.
One_way_roads_exist: If the value is ‘True’, then even if there are more origins than destinations, it will not do a flip during processing.
Returns: a numpy matrix of format OD[o][d] = shortest time possible
-
GOSTnets.core.clip(G, bound, source_crs='epsg:4326', target_crs='epsg:4326', geom_col='geometry', largest_G=True)¶ Removes any edges that fall beyond a polygon, and shortens any other edges that do so
Parameters: - G – a graph object.
- bound – a shapely polygon object
- source_crs – crs object in format ‘epsg:4326’
- target_crs – crs object in format ‘epsg:4326’
- geom_col – label name for geometry object
- largest_G – if True, takes largest remaining subgraph of G as G
-
GOSTnets.core.combo_csv_to_graph(fpath, u_tag='u', v_tag='v', geometry_tag='Wkt', largest_G=False)¶ Function for generating a G object from a saved combo .csv
Parameters: - fpath – path to a .csv containing edges (WARNING: COMBO CSV only)
- u_tag – specify column containing u node ID if not labelled ‘u’
- v_tag – specify column containing u node ID if not labelled ‘v’
- geometry_tag – specify column containing u node ID if not
Returns: a multidigraph object
-
GOSTnets.core.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)¶ Function for adding a time value to graph edges. Ensure any graphs are in the same projection before using function, or pass a crs.
DEFAULT SPEEDS:
speed_dict = { ‘residential’: 20, # kmph ‘primary’: 40, # kmph ‘primary_link’:35, ‘motorway’:50, ‘motorway_link’: 45, ‘trunk’: 40, ‘trunk_link’:35, ‘secondary’: 30, ‘secondary_link’:25, ‘tertiary’:30, ‘tertiary_link’: 25, ‘unclassified’:20, ‘projected_footway’:3.5 }Parameters: - G – a graph containing one or more nodes
- distance_tag – the key in the dictionary for the field currently containing a distance in meters
- road_col – key for the road type in the edge data dictionary
- road_col – key for the time value in the output graph
- graph_type – set to either ‘drive’ or ‘walk’. IF walk - will set time = walking time across all segments, using the supplied walk_speed. IF drive - will use a speed dictionary for each road type, or defaults as per the note below.
- speed_dict – speed dictionary to use. If not supplied, reverts to defaults
- walk_speed – specify a walkspeed in km/h
- factor – allows you to scale up / down distances if saved in a unit other than meters. Set to 1000 if length in km.
- default – if highway type not in the speed_dict, use this speed as the default. If default is None, then the conversion will be skipped
Returns: The original graph with a new data property for the edges called ‘time’
-
GOSTnets.core.convert_to_MultiDiGraph(G)¶ takes any graph object, loads it into a MultiDiGraph type Networkx object :param G: a graph object
-
GOSTnets.core.custom_simplify(G, strict=True)¶ 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: - G – networkx multidigraph
- strict (bool) – if False, allow nodes to be end points even if they fail all other rules but have edges with different OSM IDs
Returns: networkx multidigraph
-
GOSTnets.core.disrupt_network(G, property, thresh, fail_value)¶ 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: - G – REQUIRED a graph containing one or more nodes and one or more edges
- property – the element in the data dictionary for the edges to test
- thresh – values of data[property] above this value are disrupted
- fail_value – The data[‘time’] property is set to this value to simulate the removal of the edge
Returns: a modified graph with the edited ‘time’ attribute
-
GOSTnets.core.edge_gdf_from_graph(G, crs='EPSG:4326', attr_list=None, geometry_tag='geometry', xCol='x', yCol='y', oneway_tag='oneway', single_edge=False)¶ Function for generating a GeoDataFrame from a networkx Graph object
Parameters: - G – (required) a graph object G
- crs – (optional) projection of format {‘init’ :’epsg:4326’}. Defaults to WGS84. Note: here we are defining the crs of the input geometry -we do NOT reproject to this crs. To reproject, consider using geopandas’ to_crs method on the returned gdf.
- attr_list – (optional) list of the keys which you want to be moved over to the GeoDataFrame.
- geometry_tag – (optional) the key in the data dictionary for each edge which contains the geometry info.
- xCol – (optional) if no geometry is present in the edge data dictionary, the function will try to construct a straight line between the start and end nodes, if geometry information is present in their data dictionaries. Pass the Longitude info as ‘xCol’.
- yCol – (optional) likewise, determining the Latitude tag for the node’s data dictionary allows us to make a straight line geometry where an actual geometry is missing.
- single_edge – If True then one edge/row in the returned GeoDataFrame will represent a bi-directional edge. An extra ‘oneway’ column will be added
Returns: a GeoDataFrame object of the edges in the graph
-
GOSTnets.core.edges_and_nodes_csv_to_graph(fpath_nodes, fpath_edges, u_tag='stnode', v_tag='endnode', geometry_tag='Wkt', largest_G=False)¶ Function for generating a G object from a saved .csv of edges
Parameters: - fpath_nodes – path to a .csv containing nodes
- fpath_edges – path to a .csv containing edges
- u_tag – optional. specify column containing u node ID if not labelled ‘stnode’
- v_tag – specify column containing v node ID if not labelled ‘endnode’
- geometry_tag – specify column containing geometry if not labelled ‘Wkt’
Returns: a multidigraph object
-
GOSTnets.core.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)¶ Function for generating a G object from a saved .csv of edges
Parameters: - fpath_nodes – path to a .csv containing nodes
- fpath_edges – path to a .csv containing edges
- u_tag – optional. specify column containing the node ID. This is used to only include entries that have a value.
- u_tag – optional. specify column containing u node ID if not labelled ‘stnode’
- v_tag – specify column containing v node ID if not labelled ‘endnode’
- geometry_tag – specify column containing geometry if not labelled ‘Wkt’
- largest_G – If largest_G is true, then only the largest graph will be returned
- discard_node_col – default is empty, all columns in the nodes_df will be copied to the nodes in the graph. If a list is filled, all the columns specified will be dropped.
Checks: if True, will perfrom a validation checks and return the nodes_df with a ‘node_in_edge_df’ column
Add_missing_reflected_edges: if contains a tag, then the oneway column is used to see whether reverse edges need to be added. This is much faster than using the add_missing_reflected_edges after a graph is already created.
Oneway_tag: if oneway_tag exists, then missing reflected edges won’t be added where an edge’s oneway_tag equals True
Returns: a multidigraph object
-
GOSTnets.core.euclidean_distance(lat1, lon1, lat2, lon2)¶ Calculate the great circle distance between two points on the earth (specified in decimal degrees)
Parameters: - lat1 – lat1
- lon1 – lon1
- lat2 – lat2
- lon2 – lon2
-
GOSTnets.core.example_edge(G, n=1)¶ Prints out an example edge
Parameters: - G – a graph object
- n – n - number of edges to print
-
GOSTnets.core.example_node(G, n=1)¶ Prints out an example node
Parameters: - G – a graph object
- n – number of nodes to print
-
GOSTnets.core.find_graph_avg_speed(G, distance_tag, time_tag)¶ 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: - G – a graph containing one or more nodes
- distance_tag – the key in the dictionary for the field currently containing a distance in meters
- time_tag – time to traverse the edge in seconds
Returns: The average speed for the whole graph in km per hr
-
GOSTnets.core.find_hwy_distances_by_class(G, distance_tag='length')¶ Function for finding out the different highway classes in the graph and their respective lengths
Parameters: - G – a graph object
- distance_tag – specifies which edge attribute represents length
Returns: a dictionary that has each class and the total distance per class
-
GOSTnets.core.generate_isochrones(G, origins, thresh, weight=None, stacking=False)¶ 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: - G – a graph containing one or more nodes
- orgins – a list of node IDs that the isochrones are to be generated from
- thresh – The time threshold for the calculation of the isochrone
- weight – Name of edge weighting for calculating ‘distances’. For isochrones, should be time expressed in seconds. Defaults to time expressed in seconds.
- stacking – If True, returns number of origins that can be reached from that node. If false, max = 1
Returns: The original graph with a new data property for the nodes and edges included in the isochrone
-
GOSTnets.core.gn_project_graph(G, to_crs=None)¶ Taken from OSMNX. Project graph from its current CRS to another. If to_crs is None, project the graph to the UTM CRS for the UTM zone in which the graph’s centroid lies. Otherwise, project the graph to the CRS defined by to_crs.
Parameters: - G – networkx.MultiDiGraph the graph to be projected
- to_crs – string or pyproj.CRS if None, project graph to UTM zone in which graph centroid lies, otherwise project graph to this CRS
Returns: networkx.MultiDiGraph the projected graph
-
GOSTnets.core.graph_edges_intersecting_polygon(G, polygons, mode, crs=None, fast=True)¶ 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: - G – a Graph object
- polygons – a GeoDataFrame containing one or more polygons
- mode – a string, either ‘contains’ or ‘intersecting’
- crs – If passed, will reproject both polygons and graph edge gdf to this projection.
- fast – (default: True): we can cheaply test whether an edge intersects a polygon gdf by checking whether either the start or end nodes are within a polygon. If both are, then we return ‘contained’; if at least one is, we can return ‘intersects’. If we set fast to False, then we iterate through each geometry one at a time, and check to see whether the geometry object literally intersects the polygon geodataframe, one at a time. May be computationally intensive!
Returns: a list of the edges intersecting the polygons
-
GOSTnets.core.graph_nodes_intersecting_polygon(G, polygons, crs=None)¶ Function for generating GeoDataFrame from Graph. Note: ensure any GeoDataFrames are in the same projection before using function, or pass a crs
Parameters: - G – a Graph object OR node geodataframe
- crs – a crs object of form {‘init’:’epsg:XXXX’}. If passed, matches both inputs to this crs.
Returns: a list of the nodes intersecting the polygons
-
GOSTnets.core.gravity_demand(G, origins, destinations, weight, maxtrips=100, dist_decay=1, fail_value=99999999999)¶ 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 otherewise be 0. :param origins: a list of node IDs. Must be in G. :param destinations: a list of node IDs Must be in G. :param weight: the gravity weighting of the nodes in the model, e.g. population :param fail_value: the data[‘time’] property is set to this value to simulate the removal of the edge :param maxtrips: normalize the number of trips in the resultant function to this number of trip_times :param dist_decay: parameter controlling the aggresion of discounting based on distance :returns: a numpy array describing the demand between o and d in terms of number of trips
-
GOSTnets.core.join_networks(base_net, new_net, measure_crs, thresh=500)¶ joins two networks together within a binding threshold
Parameters: - base_net – a base network object (nx.MultiDiGraph)
- new_net – the network to add on to the base (nx.MultiDiGraph)
- measure_crs – the crs number of the measurement (epsg code)
- thresh – binding threshold - unit of the crs - default 500m
-
GOSTnets.core.make_iso_polys(G, origins, trip_times, edge_buff=10, node_buff=25, infill=False, weight='time', measure_crs='epsg:4326', edge_filters=None)¶ Function for adding a time value to edge dictionaries
Parameters: - G – a graph object
- origins – a list object of node IDs from which to generate an isochrone poly object
- trip_times – a list object containing the isochrone values
- edge_buff – the thickness with witch to buffer included edges
- node_buff – the thickness with witch to buffer included nodes
- infill – If True, will remove any holes in isochrones
- weight – The edge weight to use when appraising travel times.
- measure_crs – measurement crs, object of form {‘init’:’epsg:XXXX’}
Edge_filters: you can optionally add a dictionary with key values, where the key is the attribute and the value you want to ignore from creating isochrones. An example might be an underground subway line.
-
GOSTnets.core.make_iso_polys_original(G, origins, trip_times, edge_buff=10, node_buff=25, infill=False, weight='time', measure_crs='epsg:4326')¶ Function for adding a time value to edge dictionaries
Parameters: - G – a graph object
- origins – a list object of node IDs from which to generate an isochrone poly object
- trip_times – a list object containing the isochrone values
- edge_buff – the thickness with witch to buffer included edges
- node_buff – the thickness with witch to buffer included nodes
- infill – If True, will remove any holes in isochrones
- weight – The edge weight to use when appraising travel times.
- measure_crs – measurement crs, object of form {‘init’:’epsg:XXXX’}
-
GOSTnets.core.new_edge_generator(passed_geom, infra_type, iterator, existing_legitimate_point_geometries, geom_col, project_WGS_UTM)¶ 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: - passed_geom – a shapely Linestring object
- infra_type – the road / highway class of the passed geometry
- iterator – helps count the new node IDs to keep unique nodes
- existing_legitimate_point_geometries – a dictionary of points already created / valid in [u:geom] format
- project_WGS_UTM – projection object to transform passed geometries
- geom_col – label name for geometry object
-
GOSTnets.core.node_gdf_from_graph(G, crs='epsg:4326', attr_list=None, geometry_tag='geometry', xCol='x', yCol='y')¶ Function for generating GeoDataFrame from Graph
Parameters: - G – a graph object G
- crs – projection of format {‘init’ :’epsg:4326’}. Defaults to WGS84. note: here we are defining the crs of the input geometry - we do NOT reproject to this crs. To reproject, consider using geopandas’ to_crs method on the returned gdf.
- attr_list – list of the keys which you want to be moved over to the GeoDataFrame, if not all. Defaults to None, which will move all.
- geometry_tag – specify geometry attribute of graph, default ‘geometry’
- xCol – if no shapely geometry but Longitude present, assign here
- yCol – if no shapely geometry but Latitude present, assign here
Returns: a geodataframe of the node objects in the graph
-
GOSTnets.core.pandana_snap(G, point_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True, time_it=False)¶ snaps points to a graph at very high speed :param G: a graph object, or the node geodataframe of a graph :param point_gdf: a geodataframe of points, in the same source crs as the geometry of the graph object :param source_crs: The crs for the input G and input point_gdf in format ‘epsg:32638’ :param target_crs: The measure crs how distances between points are calculated. The returned point GeoDataFrame’s CRS does not get modified. The crs object in format ‘epsg:32638’ :param add_dist_to_node_col: return distance to nearest node in the units of the target_crs :return: returns a GeoDataFrame that is the same as the input point_gdf but adds a column containing the id of the nearest node in the graph, and the distance if add_dist_to_node_col == True
-
GOSTnets.core.pandana_snap_c(G, point_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True, time_it=False)¶ snaps points to a graph at a faster speed than pandana_snap. :param G: a graph object, or the node geodataframe of a graph :param point_gdf: a geodataframe of points, in the same source crs as the geometry of the graph object :param source_crs: The crs for the input G and input point_gdf in format ‘epsg:32638’ :param target_crs: The measure crs how distances between points are calculated. The returned point GeoDataFrame’s CRS does not get modified. The crs object in format ‘epsg:32638’ :param add_dist_to_node_col: return distance to nearest node in the units of the target_crs :param time_it: return time to complete function :return: returns a GeoDataFrame that is the same as the input point_gdf but adds a column containing the id of the nearest node in the graph, and the distance if add_dist_to_node_col == True
-
GOSTnets.core.pandana_snap_points(source_gdf, target_gdf, source_crs='epsg:4326', target_crs='epsg:4326', add_dist_to_node_col=True)¶ snaps points to another GeoDataFrame at very high speed
Parameters: - source_gdf – a geodataframe of points
- target_gdf – a geodataframe of points, in the same source crs as the geometry of the source_gdfsg:32638’
- target_crs – crs object in format ‘epsg:32638’
- add_dist_to_node_col – return distance in metres to nearest node
Returns: returns a GeoDataFrame that is the same as the input source_gdf but adds a column containing the id of the nearest node in the target_gdf, and the distance if add_dist_to_node_col == True
-
GOSTnets.core.pandana_snap_single_point(G, shapely_point, source_crs='epsg:4326', target_crs='epsg:4326')¶ snaps a point to a graph at very high speed
Parameters: - G – a graph object
- shapely_point – a shapely point (ex. Point(x, y)), in the same source crs as the geometry of the graph object
- source_crs – crs object in format ‘epsg:32638’
- target_crs – crs object in format ‘epsg:32638’
- add_dist_to_node_col – return distance in metres to nearest node
-
GOSTnets.core.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')¶ snaps points their k nearest neighbors in the graph. :param G: a graph object :param point_gdf: a geodataframe of points, in the same source crs as the geometry of the graph object :param source_crs: The crs for the input G and input point_gdf in format ‘epsg:32638’ :param target_crs: The desired crs returned point GeoDataFrame. The crs object in format ‘epsg:32638’ :param add_dist_to_node_col: return distance to nearest node in the units of the target_crs :param time_it: return time to complete function
-
GOSTnets.core.project_gdf(gdf, to_crs=None, to_latlong=False)¶ Taken from OSMNX
Project a GeoDataFrame from its current CRS to another. If to_crs is None, project to the UTM CRS for the UTM zone in which the GeoDataFrame’s centroid lies. Otherwise project to the CRS defined by to_crs. The simple UTM zone calculation in this function works well for most latitudes, but may not work for some extreme northern locations like Svalbard or far northern Norway.
Parameters: - gdf – geopandas.GeoDataFrame the GeoDataFrame to be projected
- to_crs – string or pyproj.CRS if None, project to UTM zone in which gdf’s centroid lies, otherwise project to this CRS
- to_latlong – bool if True, project to settings.default_crs and ignore to_crs
Returns: the projected GeoDataFrame
-
GOSTnets.core.randomly_disrupt_network(G, edge_frac, fail_value)¶ 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: - G – a graph containing one or more nodes and one or more edges
- edge_frac – the percentage of edges to destroy. Integer rather than decimal, e.g. 5 = 5% of edges
- fail_value – the data[‘time’] property is set to this value to simulate the removal of the edge
Returns: a modified graph with the edited ‘time’ attribute the list of edge IDs randomly chosen for destruction
-
GOSTnets.core.remove_duplicate_edges(G, max_ratio=1.5)¶ 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 :param G: a graph object :param max_ratio: most of the time we see duplicate edges that are clones of each other. Sometimes, however, there are valid duplicates. These occur if multiple roads connect two junctions uniquely and without interruption - e.g. two roads running either side of a lake which meet at either end. The idea here is that valid ‘duplicate edges’ will have geometries of materially different length. Hence, we include a ratio - defaulting to 1.5 - beyond which we are sure the duplicates are valid edges, and will not be deleted.
-
GOSTnets.core.reproject_graph(input_net, source_crs, target_crs)¶ to-do: delete, is not working
Converts the node coordinates of a graph. Assumes that there are straight lines between the start and end nodes.
Parameters: - input_net – a base network object (nx.MultiDiGraph)
- source_crs – The projection of the input_net (epsg code)
- target_crs – The projection input_net will be converted to (epsg code)
-
GOSTnets.core.salt_long_lines(G, source, target, thresh=5000, factor=1, attr_list=None)¶ Adds in new nodes to edges greater than a given length
Parameters: - G – a graph object
- source – crs object in format ‘epsg:4326’
- target – crs object in format ‘epsg:32638’
- thresh – distance in metres after which to break edges.
- factor – edge lengths can be returned in units other than metres by specifying a numerical multiplication factor. Factor behavior divides rather than multiplies.
- attr_dict – list of attributes to be saved onto new edges.
-
GOSTnets.core.sample_raster(G, tif_path, property_name='RasterValue')¶ Function for attaching raster values to corresponding graph nodes. Ensure any GeoDataFrames / graphs are in the same projection before using function, or pass a crs
Parameters: - G – a graph containing one or more nodes
- tif_path – a raster or path to a tif
- property_name – a property name for the value of the raster attached to the node
Returns: a graph
-
GOSTnets.core.save(G, savename, wpath, pickle=True, edges=True, nodes=True)¶ function used to save a graph object in a variety of handy formats
Parameters: - G – a graph object
- savename – the filename, WITHOUT extension
- wpath – the write path for where the user wants the files saved
- pickle – if set to false, will not save a pickle of the graph
- edges – if set to false, will not save an edge gdf
- nodes – if set to false, will not save a node gdf
-
GOSTnets.core.simplify_junctions(G, measure_crs, in_crs={'init': 'epsg:4326'}, thresh=25, verbose=False)¶ simplifies topology of networks by simplifying node clusters into single nodes.
Parameters: - G – a graph object
- measure_crs – the crs to make the measurements inself.
- in_crs – the current crs of the graph’s geometry properties. By default, assumes WGS 84 (epsg 4326)
- thresh – the threshold distance in which to simplify junctions. By default, assumes 25 meters
-
GOSTnets.core.unbundle_geometry(c)¶ 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: c – any object. This helper function is usually applied in lambda format against a pandas / geopandas dataframe. The idea is to try to return more simple versions of complex geometries for LineString and MultiLineString type objects. Returns: an unbundled geometry value that can be plotted.
-
GOSTnets.core.utm_of_graph(G)¶
GOSTnets.fetch_od module¶
-
GOSTnets.fetch_od.CreateODMatrix(infile, infile_2, lat_name='Lat', lon_name='Lon', UID='ID', Pop=None, call_type='OSRM', rescue=0, rescue_num=0, MB_Token='', sleepTime=5, osrmHeader='')¶ make sure lat_name and Lon_names are the same column names in both your infile (origins) and infile_2 (destinations) :param infile: string for folder path containing input data of the origins. This can also be a geodataframe of the data instead. :param infile_2: string for folder path containing input data of the destinations. This can also be a geodataframe of the data instead. :param lat_name: Latitude column name. :param lon_name: Longitude column name :param UID: Origin Unique Identifier column name (e.g. District, Name, Object ID…). This is mainly helpful for joining the output back to the input data / a shapefile, and is non-essential in terms of the calculation. It can be text or a number. :param Pop: Population / weighting column name :param call_type: Server call type - “OSRM” for OSRM, “MB” for Mapbox, “MBT” for Mapbox traffic, or “Euclid” for Euclidean distances (as the crow flies) :param MB_Token: Mapbox private key if using the “MB” or “MBT” call types
-
GOSTnets.fetch_od.MarketAccess(new, lambder_list=[0.01, 0.005, 0.001, 0.0007701635, 0.0003850818, 0.0001925409, 9.62704e-05, 3.85082e-05, 1e-05])¶ Calculate Market Access for a given range of lambdas
-
GOSTnets.fetch_od.ReadMe(ffpath)¶
GOSTnets.fetch_pois module¶
-
class
GOSTnets.fetch_pois.OsmObject(a, poly, tags, path='')¶ Bases:
objecteducation = {‘amenity’:[‘school’, ‘kindergarten’,’university’, ‘college’]} health = {‘amenity’:[‘clinic’, ‘pharmacy’, ‘hospital’, ‘health’]}
crs = {‘init’ :’epsg:4326’} buf_width = 0.0005
- for a in amenities:
- curr_amenity = amenities[a] current = AmenityObject(a, bbox, tags, path) current.GenerateOSMPOIs() current.RemoveDupes(buf_width, crs) current.Save(a)
-
GenerateOSMPOIs()¶
-
RelationtoPoint(string)¶
-
RemoveDupes(buf_width, crs='epsg:4326')¶
-
Save(outFolder)¶
-
prepForMA()¶ - prepare results data frame for use in the OSRM functions in OD
- add Lat and Lon fields
- Add unique identifier
- remove other geometry fields
GOSTnets.load_osm module¶
-
class
GOSTnets.load_osm.OSM_to_network(osmFile, includeFerries=False)¶ Bases:
objectObject to load OSM PBF to networkX objects.
Object to load OSM PBF to networkX objects. EXAMPLE: G_loader = losm.OSM_to_network(bufferedOSM_pbf) G_loader.generateRoadsGDF() G = G.initialReadIn() snap origins and destinations o_snapped = gn.pandana_snap(G, origins) d_snapped = gn.pandana_snap(G, destinations)
-
fetch_roads(data_path)¶ Extracts roads from an OSM PBF
Parameters: data_path – The directory of the shapefiles consisting of edges and nodes Returns: a road GeoDataFrame
-
fetch_roads_and_ferries(data_path)¶ Extracts roads and ferries from an OSM PBF
Parameters: data_path – The directory of the shapefiles consisting of edges and nodes Returns: a road GeoDataFrame
-
filterRoads(acceptedRoads=['primary', 'primary_link', 'secondary', 'secondary_link', 'motorway', 'motorway_link', 'trunk', 'trunk_link'])¶ Extract certain times of roads from the OSM before the netowrkX conversion
Parameters: acceptedRoads – [ optional ] acceptedRoads [ list of strings ] Returns: None - the raw roads are filtered based on the list of accepted roads
-
generateRoadsGDF(in_df=None, outFile='', verbose=False)¶ post-process roads GeoDataFrame adding additional attributes
Parameters: - in_df – Optional input GeoDataFrame
- outFile – optional parameter to output a csv with the processed roads
Returns: Length of line in kilometers
-
get_all_intersections(shape_input, idx_osm=None, unique_id='osm_id', verboseness=False)¶ Processes GeoDataFrame and splits edges as intersections
Parameters: - shape_input – Input GeoDataFrame
- idx_osm – The geometry index name
- unique_id – The unique id field name
Returns: returns processed GeoDataFrame
-
initialReadIn(fpath=None, wktField='Wkt')¶ Convert the OSM object to a networkX object
Parameters: - fpath – path to CSV file with roads to read in
- wktField – wktField name
Returns: Networkx Multi-digraph
-
line_length(line, ellipsoid='WGS-84')¶ Returns length of a line in kilometers, given in geographic coordinates. Adapted from https://gis.stackexchange.com/questions/4022/looking-for-a-pythonic-way-to-calculate-the-length-of-a-wkt-linestring#answer-115285
Parameters: - line – a shapely LineString object with WGS-84 coordinates
- ellipsoid (string) – string name of an ellipsoid that geopy understands (see http://geopy.readthedocs.io/en/latest/#module-geopy.distance)
Returns: Length of line in kilometers
-
GOSTnets.network_clean module¶
-
GOSTnets.network_clean.clean_network(G, wpath='', output_file_name='', UTM={'init': 'epsg:3857'}, WGS={'init': 'epsg:4326'}, junctdist=50, verbose=False)¶ Topologically simplifies an input graph object by collapsing junctions and removing interstital nodes :param G: a graph object containing nodes and edges. Edges should have a property called ‘Wkt’ containing geometry objects describing the roads. :param wpath: the write path - a drive directory for inputs and output :param output_file_name: This will be the output file name with ‘_processed’ appended :param UTM: The epsg code of the projection, in metres, to apply the junctdist :param WGS: the current crs of the graph’s geometry properties. By default, assumes WGS 84 (epsg 4326) :param junctdist: distance within which to collapse neighboring nodes. simplifies junctions. Set to 0.1 if not simplification desired. 50m good for national (primary / secondary) networks :param verbose: if True, saves down intermediate stages for dissection
GOSTnets.optimization module¶
-
GOSTnets.optimization.optimize_facility_locations(OD, facilities, p, existing_facilities=None, verbose=False, execute=True, write='')¶ Function for identifying spatially optimal locations of facilities (P-median problem)
Parameters: - OD – an Origin:Destination matrix, origins as rows, destinations as columns, in pandas DataFrame format.
- facilities – The ‘destinations’ of the OD-Matrix. MUST be a list of objects included in OD.columns (or subset) if certain nodes are unsuitable for facility locations
- p – the number of facilities to solve for
- existing_facilities – facilities to always include in the solution. MUST be in ‘facilities’ list
- verbose – print a bunch of status updates
- execute – should the problem be executed
- write – outPath to write problem
-
GOSTnets.optimization.optimize_max_coverage(OD, p_facilities=5, max_coverage=2000, origins_pop_series=None, existing_facilities=None)¶
-
GOSTnets.optimization.optimize_partial_set_coverage(OD, pop_coverage=0.8, max_coverage=2000, origins_pop_series=None, existing_facilities=None)¶
-
GOSTnets.optimization.optimize_set_coverage(OD, max_coverage=2000, existing_facilities=None)¶
GOSTnets.osm_parser module¶
source: https://gist.github.com/Tofull/49fbb9f3661e376d2fe08c2e9d64320e
-
class
GOSTnets.osm_parser.Node(id, lon, lat)¶ Bases:
object
-
class
GOSTnets.osm_parser.OSM(filename_or_stream)¶ Bases:
object
-
GOSTnets.osm_parser.download_osm(left, bottom, right, top, proxy=False, proxyHost='10.0.4.2', proxyPort='3128', cache=False, cacheTempDir='/tmp/tmpOSM/', verbose=True)¶ Return a filehandle to the downloaded data from osm api.
-
GOSTnets.osm_parser.fetch_roads_OSM(osm_path, acceptedRoads=['motorway', 'motorway_link', 'trunk', 'trunk_link', 'primary', 'primary_link', 'secondary', 'secondary_link', 'tertiary', 'tertiary_link'])¶ Returns a GeoDataFrame of OSM roads from an OSM file
Parameters: - osm_path – path to OSM file
- acceptedRoads (list) – list of OSM road types
Returns: A GeoDataFrame of OSM roads
-
GOSTnets.osm_parser.haversine(lon1, lat1, lon2, lat2, unit_m=True)¶ Calculate the great circle distance between two points on the earth (specified in decimal degrees) default unit : km
-
GOSTnets.osm_parser.line_length(line, ellipsoid='WGS-84')¶ Returns length of a line in meters, given in geographic coordinates. Adapted from https://gis.stackexchange.com/questions/4022/looking-for-a-pythonic-way-to-calculate-the-length-of-a-wkt-linestring#answer-115285
Parameters: - line – a shapely LineString object with WGS-84 coordinates
- ellipsoid (string) – string name of an ellipsoid that geopy understands (see http://geopy.readthedocs.io/en/latest/#module-geopy.distance)
Returns: Length of line in meters
-
GOSTnets.osm_parser.read_osm(filename_or_stream, only_roads=True)¶ Read graph in OSM format from file specified by name or by stream object.
Examples: >>> G=nx.read_osm(nx.download_osm(-122.33,47.60,-122.31,47.61)) >>> import matplotlib.pyplot as plt >>> plt.plot([G.node[n][‘lat’]for n in G], [G.node[n][‘lon’] for n in G], ‘o’, color=’k’) >>> plt.show()
Parameters: - filename_or_stream – filename or stream object
- ellipsoid (string) – string name of an ellipsoid that geopy understands (see http://geopy.readthedocs.io/en/latest/#module-geopy.distance)
Returns G: networkx multidigraph