def spatial_outreach(multi, node_layer='taz', thru_layers=['streets'], weight=None, cost=None, attrname='outreach'): ''' Summary: Compute the spatial outreach of all nodes in a layer according to a specified edge weight (e.g. cost_time_m). Currently uses area of convex hull to measure outreach. Args: layer (TYPE, optional): the layer in which to compute spatial outreach weight (TYPE, optional): the numeric edge attribute by which to measure path lengths cost (TYPE, optional): the maximum path length attrname (str, optional): the base name to use when saving the computed outreach Returns: None ''' from shapely.geometry import MultiPoint from math import pi LAT_DIST = 110766.95237186992 / 1000.0 # in km. See http://www.csgnetwork.com/degreelenllavcalc.html LON_DIST = 101274.42720366278 / 1000.0 # in km. See http://www.csgnetwork.com/degreelenllavcalc.html def distance_matrix(nodes, weight): N = len(nodes) lengths = g.shortest_paths_dijkstra(weights=weight, source=nodes, target=nodes) d = { nodes[i]: {nodes[j]: lengths[i][j] for j in range(N)} for i in range(N) } return d def ego(n, cost, d): return [j for j in nodes if d[n][j] <= cost] def area(n, cost, d): points = [pos[n] for n in ego(n, cost, d)] return MultiPoint(points).convex_hull.area g = utility.nx_2_igraph( multi.layers_as_subgraph(thru_layers + [node_layer])) nodes = g.vs.select(lambda vertex: vertex['layer'] == node_layer)['name'] pos = { v['name']: (v['lon'] * LON_DIST, v['lat'] * LAT_DIST) for v in g.vs.select(lambda v: v['name'] in nodes) } d = distance_matrix(nodes, weight) outreach = {n: sqrt(area(n, cost, d) / pi) for n in nodes} nx.set_node_attributes(multi.G, attrname, outreach)
def local_intermodality(self, layer=None, thru_layer=None, weight=None): """ Summary: Compute the local intermodality of a set of nodes and save as a node attribute. Args: layer (str, optional): the layer for which to compute intermodality thru_layer (str, optional): the layer through which a path couns as 'intermodal' weight (str, optional): the numeric edge attribute used to weight paths Returns: None """ g = utility.nx_2_igraph(self.G) nodes = g.vs.select(layer=layer) def intermodality(v, g, nodes=nodes, weight=weight): paths = g.get_shortest_paths(v, nodes, weights=weight) total = len(nodes) intermodal = 0 for p in paths: if thru_layer in [g.vs[u]['layer'] for u in p]: intermodal += 1 return intermodal * 1.0 / total d = { v['name']: intermodality(v=v, g=g, nodes=nodes, weight=weight) for v in nodes } nx.set_node_attributes(self.G, 'intermodality', d)
def local_intermodality(self, layer = None, thru_layer = None, weight = None): """ Summary: Compute the local intermodality of a set of nodes and save as a node attribute. Args: layer (str, optional): the layer for which to compute intermodality thru_layer (str, optional): the layer through which a path couns as 'intermodal' weight (str, optional): the numeric edge attribute used to weight paths Returns: None """ g = utility.nx_2_igraph(self.G) nodes = g.vs.select(layer=layer) def intermodality(v, g, nodes = nodes, weight = weight): paths = g.get_shortest_paths(v, nodes, weights = weight) total = len(nodes) intermodal = 0 for p in paths: if thru_layer in [g.vs[u]['layer'] for u in p]: intermodal += 1 return intermodal * 1.0 / total d = {v['name'] : intermodality(v = v, g = g, nodes = nodes, weight = weight) for v in nodes} nx.set_node_attributes(self.G, 'intermodality', d)
def spatial_outreach(multi, node_layer = 'taz', thru_layers = ['streets'], weight = None, cost = None, attrname = 'outreach'): ''' Summary: Compute the spatial outreach of all nodes in a layer according to a specified edge weight (e.g. cost_time_m). Currently uses area of convex hull to measure outreach. Args: layer (TYPE, optional): the layer in which to compute spatial outreach weight (TYPE, optional): the numeric edge attribute by which to measure path lengths cost (TYPE, optional): the maximum path length attrname (str, optional): the base name to use when saving the computed outreach Returns: None ''' from shapely.geometry import MultiPoint from math import pi LAT_DIST = 110766.95237186992 / 1000.0 # in km. See http://www.csgnetwork.com/degreelenllavcalc.html LON_DIST = 101274.42720366278 / 1000.0 # in km. See http://www.csgnetwork.com/degreelenllavcalc.html def distance_matrix(nodes, weight): N = len(nodes) lengths = g.shortest_paths_dijkstra(weights = weight, source = nodes, target = nodes) d = {nodes[i] : {nodes[j] : lengths[i][j] for j in range(N) } for i in range(N)} return d def ego(n, cost, d): return [j for j in nodes if d[n][j] <= cost] def area(n, cost, d): points = [pos[n] for n in ego(n, cost, d)] return MultiPoint(points).convex_hull.area g = utility.nx_2_igraph(multi.layers_as_subgraph(thru_layers + [node_layer])) nodes = g.vs.select(lambda vertex: vertex['layer'] == node_layer)['name'] pos = {v['name'] : (v['lon'] * LON_DIST, v['lat'] * LAT_DIST) for v in g.vs.select(lambda v: v['name'] in nodes)} d = distance_matrix(nodes, weight) outreach = {n : sqrt(area(n, cost, d)/pi) for n in nodes} nx.set_node_attributes(multi.G, attrname, outreach)