graphs
Convenience functions for the preparation and conversion of networkX
graphs to and from cityseer
data structures. Note that the cityseer
network data structures can be created and manipulated directly, if so desired.
nx_simple_geoms
Inferring geometries from node to node. Infers straight-lined geometries connecting the x
and y
coordinates of each node-pair. The resultant edge geometry will be stored to each edge’s geom
attribute.
Parameters
A networkX
MultiGraph
with x
and y
node attributes.
Returns
A networkX
MultiGraph
with shapely
Linestring
geometries assigned to the edge geom
attributes.
nx_remove_filler_nodes
Remove nodes of degree=2. Nodes of degree=2 represent no route-choice options other than traversal to the next edge. These are frequently found on network topologies as a means of describing roadway geometry, but are meaningless from a network topology point of view. This method will find and deleted these nodes, and replaces the two edges on either side with a new spliced edge. The new edge’s geom
attribute will retain the geometric properties of the original edges.
Filler nodes may be prevalent in poor quality datasets, or in situations where curved roadways have been represented
through the addition of nodes to describe arced geometries. cityseer
uses shapely
Linestrings
to describe
arbitrary road geometries without the need for filler nodes. Filler nodes can therefore be removed, thus reducing
side-effects as a function of varied node intensities when computing network centralities.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
Returns
A networkX
MultiGraph
with nodes of degree=2 removed. Adjacent edges will be combined into a unified new edge with associated geom
attributes spliced together.
nx_remove_dangling_nodes
Remove disconnected components and optionally removes short dead-end street stubs.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
The maximum cutoff distance for removal of dead-ends. Use 0
where no despining should occur.
Remove disconnected components with fewer nodes than specified by this parameter. Defaults to 100. Set to 0 to keep all disconnected components.
Returns
A networkX
MultiGraph
with disconnected components optionally removed, and dead-ends removed where less than the despine
parameter distance.
nx_merge_parallel_edges
Check a MultiGraph for duplicate edges; which, if found, will be merged. The shortest of these parallel edges is selected and buffered by contains_buffer_dist
. If this buffer contains an adjacent edge, then the adjacent edge is merged. Edges falling outside this buffer are retained.
When candidate edges are found for merging, they are replaced by a single new edge. The new geometry selected from either:
- An imaginary centreline of the combined edges if
merge_edges_by_midline
is set toTrue
; - Else, the shortest edge is retained, with longer edges discarded.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
Whether to merge parallel edges by an imaginary centreline. If set to False, then the shortest edge will be retained as the new geometry and the longer edges will be discarded. Defaults to True.
The buffer distance to consider when checking if parallel edges sharing the same start and end nodes are sufficiently adjacent to be merged.
An optional list of OpenStreetMap target highway tags. If provided, only nodes with neighbouring edges containing a tag matching one of the target OSM highway tags will be consolidated. Requires graph prepared with via io.osm_graph_from_poly
.
Whether to only merge edges with shared OSM name
or ref
tags. False by default. Requires graph prepared with via io.osm_graph_from_poly
.
Returns
A networkX
MultiGraph
with consolidated nodes.
nx_snap_endpoints
Snaps geom endpoints to adjacent node coordinates.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
Returns
A networkX
MultiGraph
.
nx_iron_edges
Simplifies edges.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
Returns
A networkX
MultiGraph
with simplified edges.
nx_consolidate_nodes
Consolidates nodes if they are within a buffer distance of each other. Several parameters provide more control over the conditions used for deciding whether or not to merge nodes. The algorithm proceeds in two steps:
Nodes within the buffer distance of each other are merged. If selecting centroid_by_itx
then the new centroid will try to use intersections to determine the new centroid for the nodes. It will first attempt to find intersections with two through-routes, else will use intersections with one through-route.
The merging of nodes can create parallel edges with mutually shared nodes on either side. These edges are replaced by a single new edge, with the new geometry selected from either:
- An imaginary centreline of the combined edges if
merge_edges_by_midline
is set toTrue
; - Else, the shortest edge, with longer edges discarded; See
nx_merge_parallel_edges
for more information.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
The buffer distance to be used for consolidating nearby nodes. Defaults to 5.
Whether all nodes within the buffer distance are merged, or only “direct” or “indirect” neighbours. Defaults to None which will consider all nodes.
Whether the algorithm will recursively explore neighbours of neighbours if those neighbours are within the buffer distance from the prior node. Defaults to False.
Whether to favour intersections when selecting the combined centroid of merged nodes. Intersections with two straight through-routes will be favoured if found, otherwise intersections with one straight through-route are used where available. True by default.
Whether to prioritise centroid locations by OSM highway tags. For example, trunk roads will have higher priority than residential roads. Requires graph prepared with via io.osm_graph_from_poly
. Defaults to False.
Whether to merge parallel edges by an imaginary centreline. If set to False, then the shortest edge will be retained as the new geometry and the longer edges will be discarded. Defaults to True.
The buffer distance to consider when checking if parallel edges sharing the same start and end nodes are sufficiently adjacent to be merged. This is run after node consolidation has completed.
An optional list of OpenStreetMap target highway tags. If provided, only nodes with neighbouring edges containing a tag matching one of the target OSM highway tags will be consolidated. Requires graph prepared with via io.osm_graph_from_poly
.
Whether to only merge edges with shared OSM name
or ref
tags. False by default. Requires graph prepared with via io.osm_graph_from_poly
.
Returns
A networkX
MultiGraph
with consolidated nodes.
Notes
See the guide on graph cleaning for more information.
The pre-consolidation OSM street network for Soho, London. © OpenStreetMap contributors.
The consolidated OSM street network for Soho, London. © OpenStreetMap contributors.
nx_split_opposing_geoms
Split edges opposite nodes on parallel edge segments if within a buffer distance. This facilitates merging parallel roadways through subsequent use of nx_consolidate-nodes
.
The merging of nodes can create parallel edges with mutually shared nodes on either side. These edges are replaced by a single new edge, with the new geometry selected from either:
- An imaginary centreline of the combined edges if
merge_edges_by_midline
is set toTrue
; - Else, the shortest edge, with longer edges discarded; See
nx_merge_parallel_edges
for more information.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
The buffer distance to be used for splitting nearby nodes. Defaults to 5.
Whether to merge parallel edges by an imaginary centreline. If set to False, then the shortest edge will be retained as the new geometry and the longer edges will be discarded. Defaults to True.
The buffer distance to consider when checking if parallel edges sharing the same start and end nodes are sufficiently adjacent to be merged.
Whether to prioritise centroid locations by OSM highway tags. For example, trunk roads will have higher priority than residential roads. Requires graph prepared with via io.osm_graph_from_poly
. Defaults to False.
An optional list of OpenStreetMap target highway tags. If provided, only nodes with neighbouring edges containing a tag matching one of the target OSM highway tags will be consolidated. Requires graph prepared with via io.osm_graph_from_poly
.
Whether to only merge edges with shared OSM name
or ref
tags. False by default. Requires graph prepared with via io.osm_graph_from_poly
.
Returns
A networkX
MultiGraph
with consolidated nodes.
nx_decompose
Decomposes a graph so that no edge is longer than a set maximum. Decomposition provides a more granular representation of potential variations along street lengths, while reducing network centrality side-effects that arise as a consequence of varied node densities.
Setting the decompose
parameter too small in relation to the size of the graph may increase the computation time
unnecessarily for subsequent analysis. For larger-scale urban analysis, it is generally not necessary to go smaller
20m, and 50m may already be sufficient for the majority of cases.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
The maximum length threshold for decomposed edges.
Returns
A decomposed networkX
graph with no edge longer than the decompose_max
parameter. If live
node attributes were provided, then the live
attribute for child-nodes will be set to True
if either or both parent nodes were live
. Otherwise, all nodes wil be set to live=True
. The length
and imp_factor
edge attributes will be set to match the lengths of the new edges.
Notes
from cityseer.tools import mock, graphs, plot
G = mock.mock_graph()
G_simple = graphs.nx_simple_geoms(G)
G_decomposed = graphs.nx_decompose(G_simple, 100)
plot.plot_nx(G_decomposed)
Example graph prior to decomposition.
Example graph after decomposition.
nx_to_dual
Convert a primal graph representation to the dual representation. Primal graphs represent intersections as nodes and streets as edges. This method will invert this representation so that edges are converted to nodes and intersections become edges. Primal edge geom
attributes will be welded to adjacent edges and split into the new dual edge geom
attributes.
Note that a MultiGraph
is useful for primal but not for dual, so the output MultiGraph
will have single edges.
e.g. a crescent street that spans the same intersections as parallel straight street requires multiple edges in
primal. The same type of situation does not arise in the dual because the nodes map to distinct edges regardless.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
Returns
A dual representation networkX
graph. The new dual nodes will have x
and y
node attributes corresponding to the mid-points of the original primal edges. If live
node attributes were provided, then the live
attribute for the new dual nodes will be set to True
if either or both of the adjacent primal nodes were set to live=True
. Otherwise, all dual nodes wil be set to live=True
. The primal edges will be split and welded to form the new dual geom
edges. The primal LineString
geom
will be saved to the dual node’s primal_edge
attribute. primal_edge_node_a
, primal_edge_node_b
, and primal_edge_idx
attributes will be added to the new (dual) nodes, and a primal_node_id
edge attribute will be added to the new (dual) edges.
Notes
from cityseer.tools import graphs, mock, plot
G = mock.mock_graph()
G_simple = graphs.nx_simple_geoms(G)
G_dual = graphs.nx_to_dual(G_simple)
plot.plot_nx_primal_or_dual(G_simple,
G_dual,
plot_geoms=False)
Dual graph (blue) overlaid on the source primal graph (red).
nx_weight_by_dissolved_edges
Generates graph node weightings based on the ratio of directly adjacent edges to total nearby edges. This is used to control for unintended amplification of centrality measures where redundant network representations (e.g. duplicitious segments such as adjacent street, sidewalk, cycleway, busway) tend to inflate centrality scores. This method is intended for ‘messier’ network representations (e.g. OSM).
This method is only recommended for primal graph representations.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
A distance to use when buffering edges to calculate the weighting. 20m by default.
Only count a nearby adjacent edge as duplicitous if the angular difference between edges is less than max_ang_diff
. 45 degrees by default.
Returns
A networkX
graph. The nodes will have a new weight
parameter indicating the node’s contribution given the locally ‘dissolved’ context.
nx_generate_vis_lines
Generates a line_geom
property for nodes consisting MultiLineString
geoms for visualisation purposes. This method can be used if preferring to visualise the outputs as lines instead of points. The lines are assembled from the adjacent half segments.
Parameters
A networkX
MultiGraph
in a projected coordinate system, containing x
and y
node attributes, and geom
edge attributes containing LineString
geoms.
Returns
A networkX
graph. The nodes will have a new line_geom
parameter containing shapely
MultiLineString
geoms.