Simplify

_clean_merge_mapping(edge_mapping, new_edge, old_edges, directed)[source]

For the two-degree node merge operation, it cleans the new-old edges mapping dictionary by reporting original edges to the newest edge. It makes sure that all edges in the mapping dictionary dict are in the resulting graph.

get_dead_ends(graph, node_filter=<function no_filter>, only_strict=False)[source]

Return the list of dead end in the given graph. A dead end is defined as a node having only one neighbor. For directed graphs, a strict dead end is a node having a unique predecessor and no successors. A weak dead end is a node having a unique predecessor that is also its unique successor.

Parameters
  • graph (nx.Graph) – Graph to parse.

  • node_filter – Evaluates to true if a node can be considered as dead end, false otherwise. (Default value = no_filter)

  • only_strict – If true, remove only strict dead ends. Used only for directed graphs. (Default value = False)

Returns

List of node name that are dead ends.

Return type

list

Return type

list

remove_dead_ends(graph, node_filter=<function no_filter>, only_strict=False)[source]

Remove dead ends from a given graph. A dead end is defined as a node having only one neighbor. For directed graphs, a strict dead end is a node having a unique predecessor and no successors. A weak dead end is a node having a unique predecessor that is also its unique successor.

Parameters
  • graph (nx.Graph) – Graph to simplify

  • node_filter – Evaluates to true if a node can be removed, false otherwise. (Default value = no_filter)

  • only_strict – If true, remove only strict dead ends. Used only for directed graphs. (Default value = False)

remove_isolates(graph)[source]

Removes all isolates nodes in the given graph.

Parameters

graph (nx.Graph) – A graph on which to remove all isolates

Returns

Number of removed isolates

Return type

int

Return type

int

remove_nan_attributes(graph, remove_nan=True, remove_none=True, copy=False)[source]

Remove the nan and None values from nodes and edges attributes.

Parameters
  • graph (nx.Graph) – Graph (or subclass)

  • remove_nan – If true, remove the nan values (test is val is np.nan) (Default value = True)

  • remove_none – If true, remove the None values (test is val is None) (Default value = True)

  • copy – If True, a copy of the graph is returned, otherwise the graph is modified inplace. (Default value = False)

Returns

The modified graph if copy is true.

Return type

None or nx.Graph

remove_self_loop_edges(graph)[source]

Remove self loop edges on nodes of the given graph.

Parameters

graph (nx.Graph) – A graph on which to remove all self loops.

Returns

The number of removed self loops

Return type

int

Return type

int

remove_small_connected_components(graph, minimum_allowed_size)[source]

Remove all connected components having strictly less than minimum_allowed_size.

Parameters
  • graph (nx.Graph) – The graph on which to remove connected components

  • minimum_allowed_size (int) – The minimum number of nodes where a connected component is kept.

Returns

The number of removed connected components

Return type

int

Return type

int

trim_graph_with_polygon(graph, polygon, as_view=False, method='intersects')[source]

Trim a graph with a given polygon. Keep only the nodes that intersect (or are within) the polygon.

Parameters
  • graph (GeoGraph, GeoDiGraph, GeoMultiGraph or GeoMultiDiGraph) – A GeoGraph (or subclass)

  • polygon (Polygon or MultiPolygon) – A shapely.Polygon describing the area to keep

  • as_view (bool) – If True, a view of the given graph is returned

  • method (str) – If set to "intersects", the shapely.intersects is used (keeps nodes and edges that intersects the polygon). If set to "within", the shapely.within is used (keep nodes and edges that are strictly into the polygon). (Default value = “intersects”)

Returns

The modified graph if as_view is True.

Return type

None or GeoGraph

two_degree_node_merge(graph, node_filter=<function no_filter>)[source]

Merge edges that connects two nodes with a unique third node.

Parameters
Returns

Dictionary indicating for each new edge the merged ones.

Return type

dict

Return type

dict

two_degree_node_merge_for_directed_graphs(graph, node_filter=<function no_filter>)[source]

Merge edges that connects two nodes with a unique third node. A potential node to merge n must have exactly two different neighbors u and v with one of the following set of edges:

  • (u, n) and (n, v)

  • (u, n), (n, u), (n, v) and (v, n)

For the first case, a merging edge (u, v) is added. Under the latter, two edges (u, v) and (v, u) are added. The added edges will have a geometry corresponding to concatenation of the two replaced edges. If a replaced edge doesn’t have a geometry, the added edge will not have a geometry as well. Edges geometries must be well ordered (first node must match with line’s first extremity), otherwise lines concatenation may not be consistent (see order_well_lines).

Parameters
  • graph (GeoDiGraph or GeoMultiDiGraph) – Given graph to modify

  • node_filter – Evaluates to true if a given node can be merged. (Default value = no_filter)

Returns

merged_edges – Dictionary indicating for each new edge the merged ones.

Return type

dict

two_degree_node_merge_for_undirected_graphs(graph, node_filter=<function no_filter>)[source]

Merge edges that connects two nodes with a unique third node for undirected graphs. Potential nodes to merge are nodes with two edges connecting two different nodes. If a replaced edge doesn’t have a geometry, the added edge will not have a geometry as well.

Parameters
  • graph (GeoGraph or GeoMultiGraph) – Graph to modify

  • node_filter – Evaluates to true if a given node can be merged. (Default value = no_filter)

Returns

Dictionary indicating for each new edge the merged ones.

Return type

dict

Return type

dict