Skip to content
Snippets Groups Projects

Moves a renamed latest branch into master

12 files
+ 5015
8291
Compare changes
  • Side-by-side
  • Inline

Files

+ 91
190
@@ -19,101 +19,80 @@ from osmnx.projection import is_projected
from ..gis import osm as osm
from ..gis import identify as ident
#******************************************************************************
#******************************************************************************
# *****************************************************************************
# *****************************************************************************
def arc_lengths(network: MultiDiGraph,
arc_keys: tuple = None) -> dict:
def edge_lengths(network: MultiDiGraph, edge_keys: tuple = None) -> dict:
"""
Calculate arc lengths in a OSMnx-formatted MultiDiGraph network object.
Calculate edge lengths in a OSMnx-formatted MultiDiGraph network object.
The calculation method changes depending on whether the coordinates are
projected and depending on whether the arcs are simplified.
projected and depending on whether the edges are simplified.
Parameters
----------
network : MultiDiGraph
The object describing the network.
arc_keys : tuple, optional
The keys for the arcs under consideration. The default is None, which
means all arcs in the network will be considered.
edge_keys : tuple, optional
The keys for the edges under consideration. The default is None, which
means all edges in the network will be considered.
Returns
-------
dict
A dictionary with the lengths for each arc.
A dictionary with the lengths for each edge.
"""
# determine if the graph is projected or not
graph_is_projected = is_projected(network.graph['crs'])
# for each arc on the graph
if type(arc_keys) == type(None):
arc_keys = network.edges(keys=True) # tuple(network.edges(keys=True))
# check if edge keys were specified
if type(edge_keys) == type(None):
# no particular edge keys were provided: consider all edges (default)
edge_keys = network.edges(keys=True) # tuple(network.edges(keys=True))
# initialise length dict
length_dict = {}
for arc_key in arc_keys:
# for each edge on the graph
for edge_key in edge_keys:
# calculate it using the library
if graph_is_projected:
# calculate it using projected coordinates
if osm.KEY_OSMNX_GEOMETRY in network.edges[arc_key]:
if osm.KEY_OSMNX_GEOMETRY in network.edges[edge_key]:
# use geometry
length_dict[arc_key] = length(
network.edges[arc_key][osm.KEY_OSMNX_GEOMETRY]
length_dict[edge_key] = length(
network.edges[edge_key][osm.KEY_OSMNX_GEOMETRY]
)
else:
# use (projected) coordinates
start_point = Point(
(network.nodes[arc_key[0]][osm.KEY_OSMNX_X],
network.nodes[arc_key[0]][osm.KEY_OSMNX_Y])
(network.nodes[edge_key[0]][osm.KEY_OSMNX_X],
network.nodes[edge_key[0]][osm.KEY_OSMNX_Y])
)
end_point = Point(
(network.nodes[arc_key[1]][osm.KEY_OSMNX_X],
network.nodes[arc_key[1]][osm.KEY_OSMNX_Y])
(network.nodes[edge_key[1]][osm.KEY_OSMNX_X],
network.nodes[edge_key[1]][osm.KEY_OSMNX_Y])
)
length_dict[arc_key] = start_point.distance(end_point)
length_dict[edge_key] = start_point.distance(end_point)
else:
# calculate it using unprojected coordinates (lat/long)
if osm.KEY_OSMNX_GEOMETRY in network.edges[arc_key]:
if osm.KEY_OSMNX_GEOMETRY in network.edges[edge_key]:
# use geometry
length_dict[arc_key] = great_circle_distance_along_path(
network.edges[arc_key][osm.KEY_OSMNX_GEOMETRY]
length_dict[edge_key] = great_circle_distance_along_path(
network.edges[edge_key][osm.KEY_OSMNX_GEOMETRY]
)
else:
# use (unprojected) coordinates
length_dict[arc_key] = great_circle(
lat1=network.nodes[arc_key[0]][osm.KEY_OSMNX_Y],
lon1=network.nodes[arc_key[0]][osm.KEY_OSMNX_X],
lat2=network.nodes[arc_key[1]][osm.KEY_OSMNX_Y],
lon2=network.nodes[arc_key[1]][osm.KEY_OSMNX_X]
length_dict[edge_key] = great_circle(
lat1=network.nodes[edge_key[0]][osm.KEY_OSMNX_Y],
lon1=network.nodes[edge_key[0]][osm.KEY_OSMNX_X],
lat2=network.nodes[edge_key[1]][osm.KEY_OSMNX_Y],
lon2=network.nodes[edge_key[1]][osm.KEY_OSMNX_X]
)
# return the dict with lengths of each edge
return length_dict
#******************************************************************************
#******************************************************************************
# *****************************************************************************
# *****************************************************************************
def great_circle_distance_along_path(path: LineString) -> float:
"""
@@ -133,15 +112,10 @@ def great_circle_distance_along_path(path: LineString) -> float:
The sum of the individual great circle distances along the path.
"""
# get coordinates
lon = tuple(path.coords.xy[0])
lat = tuple(path.coords.xy[1])
# sum individual distances and return
return sum(
great_circle(
lat[:-1], # latitudes of starting points
@@ -151,8 +125,8 @@ def great_circle_distance_along_path(path: LineString) -> float:
)
)
#******************************************************************************
#******************************************************************************
# *****************************************************************************
# *****************************************************************************
def update_street_count(network: MultiDiGraph):
"""
@@ -168,18 +142,15 @@ def update_street_count(network: MultiDiGraph):
None.
"""
# update street count
street_count_dict = count_streets_per_node(network)
network.add_nodes_from(
((key, {osm.KEY_OSMNX_STREET_COUNT:value})
for key, value in street_count_dict.items())
)
#******************************************************************************
#******************************************************************************
# *****************************************************************************
# *****************************************************************************
def node_path_length(network: MultiDiGraph,
path: list,
@@ -187,7 +158,7 @@ def node_path_length(network: MultiDiGraph,
"""
Returns the length or lengths of a path defined using nodes.
If more than one arc connects adjacent nodes along the path, a length value
If more than one edge connects adjacent nodes along the path, a length value
will be returned for each possible path combination.
Parameters
@@ -195,7 +166,7 @@ def node_path_length(network: MultiDiGraph,
network : MultiDiGraph
The object describing the network.
path : list
A list of node keys or arc keys describing the path.
A list of node keys or edge keys describing the path.
return_minimum_length_only : bool, optional
If True, returns the minimum path length only. The default is True.
@@ -207,113 +178,79 @@ def node_path_length(network: MultiDiGraph,
"""
# direction matters
path_length = len(path)
if path_length == 0:
return inf
#**************************************************************************
# if the path is given as a list of node keys, then it is subjective
# i.e., it may refer to many paths, namely if parallel arcs exist
# i.e., it may refer to many paths, namely if parallel edges exist
# check if the path object qualifies as such
if not is_path(network, path):
# it does not, exit
if return_minimum_length_only:
return inf
else:
return [inf]
# prepare a list with all possible paths given as lists of arc keys
list_of_arc_key_paths = [[]] # a list of arc key lists
# prepare a list with all possible paths given as lists of edge keys
list_of_edge_key_paths = [[]] # a list of edge key lists
# for each pair of nodes in the path
for node_pair in range(path_length-1):
# get the arcs between these two nodes
arc_keys = ident.all_arcs_from_a_to_b(network,
path[node_pair],
path[node_pair+1])
number_arc_keys = len(arc_keys)
if number_arc_keys == 1:
# only one arc exists: append its key to all existing lists/paths
for arc_key_path in list_of_arc_key_paths:
arc_key_path.append(arc_keys[0])
else: # multiple arcs exist: each path identified so far has to be
# replicated a total of number_arc_keys times and then updated
number_paths = len(list_of_arc_key_paths)
# for each parallel arc
for arc_key_index in range(number_arc_keys-1):
# replicate all paths
for path_index in range(number_paths):
list_of_arc_key_paths.append(
list(list_of_arc_key_paths[path_index])
)
# paths have been replicated, now add the arcs
for arc_key_index in range(number_arc_keys):
for path_index in range(number_paths):
# add the new arc
list_of_arc_key_paths[
path_index+arc_key_index*number_paths
# get the edges between these two nodes
edge_keys = ident.get_edges_from_a_to_b(
network,
path[node_pair],
path[node_pair+1]
)
number_edge_keys = len(edge_keys)
if number_edge_keys == 1:
# only one edge exists: append its key to all existing lists/paths
for edge_key_path in list_of_edge_key_paths:
edge_key_path.append(edge_keys[0])
else: # multiple edges exist: each path identified so far has to be
# replicated a total of number_edge_keys times and then updated
number_paths = len(list_of_edge_key_paths)
# for each parallel edge
for edge_key_index in range(number_edge_keys-1):
# replicate all paths
for path_index in range(number_paths):
list_of_edge_key_paths.append(
list(list_of_edge_key_paths[path_index])
)
# paths have been replicated, now add the edges
for edge_key_index in range(number_edge_keys):
for path_index in range(number_paths):
# add the new edge
list_of_edge_key_paths[
path_index+edge_key_index*number_paths
].append(
arc_keys[arc_key_index]
edge_keys[edge_key_index]
)
#**************************************************************************
# *************************************************************************
path_lenths = [
sum(network.edges[arc_key][osm.KEY_OSMNX_LENGTH]
for arc_key in arc_key_path)
for arc_key_path in list_of_arc_key_paths
]
if return_minimum_length_only:
return min(path_lenths)
else:
sum(network.edges[edge_key][osm.KEY_OSMNX_LENGTH]
for edge_key in edge_key_path)
for edge_key_path in list_of_edge_key_paths
]
if return_minimum_length_only:
return min(path_lenths)
else:
return path_lenths
#**************************************************************************
# *************************************************************************
#******************************************************************************
#******************************************************************************
# *****************************************************************************
# *****************************************************************************
def edge_path_length(network: MultiDiGraph,
path: list,
**kwargs) -> float:
"""
Returns the total length of a path defined using arcs.
Returns the total length of a path defined using edges.
If the path does not exist, or if no path is provided, the result will be
infinity (math.inf).
@@ -323,7 +260,7 @@ def edge_path_length(network: MultiDiGraph,
network : MultiDiGraph
The object describing the network.
path : list
A list of arc keys describing the path.
A list of edge keys describing the path.
Returns
-------
@@ -333,27 +270,19 @@ def edge_path_length(network: MultiDiGraph,
"""
# check the number of
path_length = len(path)
if path_length == 0:
return inf
if ident.is_edge_path(network, path, **kwargs):
return sum(
network.edges[arc_key][osm.KEY_OSMNX_LENGTH] for arc_key in path
network.edges[edge_key][osm.KEY_OSMNX_LENGTH] for edge_key in path
)
else:
# no path provided
return inf
#******************************************************************************
#******************************************************************************
# *****************************************************************************
# *****************************************************************************
def count_ocurrences(gdf: GeoDataFrame,
column: str,
@@ -382,62 +311,34 @@ def count_ocurrences(gdf: GeoDataFrame,
"""
if type(column_entries) == list:
# find entries also present in the dict
# initialise dict
count_dict = {}
# for each key in the dict
for key in column_entries:
# # store the number of rows
# count_dict[key] = gdf[gdf[column]==key].shape[0]
# count the number of rows with this key
if isna(key):
count_dict[key] = gdf[gdf[column].isnull()].shape[0]
else:
count_dict[key] = gdf[gdf[column]==key].shape[0]
else:
# find all unique entries
# initialise dict
count_dict = {}
for entry in gdf[column]:
# check if it is already in the dict
if entry in count_dict:
# it is, skip
continue
# it is not, count and store the number of rows with said entry
if isna(entry): #type(entry) == type(None):
count_dict[entry] = gdf[gdf[column].isnull()].shape[0]
else:
count_dict[entry] = gdf[gdf[column]==entry].shape[0]
# return statement
return count_dict
#******************************************************************************
#******************************************************************************
\ No newline at end of file
# *****************************************************************************
# *****************************************************************************
Loading