snkit package

snkit - a spatial networks toolkit

class snkit.Network(nodes=None, edges=None)[source]

Bases: object

A Network is composed of nodes (points in space) and edges (lines)

nodes : geopandas.geodataframe.GeoDataFrame, optional edges : geopandas.geodataframe.GeoDataFrame, optional

nodes : geopandas.geodataframe.GeoDataFrame edges : geopandas.geodataframe.GeoDataFrame

set_crs(crs=None, epsg=None, allow_override=False)[source]

Set the coordinate reference system (CRS) of the network nodes and edges.

crs : pyproj.CRS, optional if epsg is specified
The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
epsg : int, optional if crs is specified
EPSG code specifying output projection.
allow_override : bool, default False
If the nodes or edges GeoDataFrame already has a CRS, allow to replace the existing CRS, even when both are not equal.
to_crs(crs=None, epsg=None)[source]

Transform network nodes and edges geometries to a new coordinate reference system (CRS).

crs : pyproj.CRS, optional if epsg is specified
The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
epsg : int, optional if crs is specified
EPSG code specifying output projection.
to_file(filename, nodes_layer='nodes', edges_layer='edges', **kwargs)[source]

Write nodes and edges to a geographic data file with layers.

Any additional keyword arguments are passed through to geopandas.GeoDataFrame.to_file.

filename : str
Path to geographic data file with layers
nodes_layer : str, optional, default ‘nodes’
Layer name for nodes.
edges_layer : str, optional, default ‘edges’
Layer name for edges.

Submodules

snkit.network module

Network representation and utilities

class snkit.network.Network(nodes=None, edges=None)[source]

Bases: object

A Network is composed of nodes (points in space) and edges (lines)

nodes : geopandas.geodataframe.GeoDataFrame, optional edges : geopandas.geodataframe.GeoDataFrame, optional

nodes : geopandas.geodataframe.GeoDataFrame edges : geopandas.geodataframe.GeoDataFrame

set_crs(crs=None, epsg=None, allow_override=False)[source]

Set the coordinate reference system (CRS) of the network nodes and edges.

crs : pyproj.CRS, optional if epsg is specified
The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
epsg : int, optional if crs is specified
EPSG code specifying output projection.
allow_override : bool, default False
If the nodes or edges GeoDataFrame already has a CRS, allow to replace the existing CRS, even when both are not equal.
to_crs(crs=None, epsg=None)[source]

Transform network nodes and edges geometries to a new coordinate reference system (CRS).

crs : pyproj.CRS, optional if epsg is specified
The value can be anything accepted by pyproj.CRS.from_user_input(), such as an authority string (eg “EPSG:4326”) or a WKT string.
epsg : int, optional if crs is specified
EPSG code specifying output projection.
to_file(filename, nodes_layer='nodes', edges_layer='edges', **kwargs)[source]

Write nodes and edges to a geographic data file with layers.

Any additional keyword arguments are passed through to geopandas.GeoDataFrame.to_file.

filename : str
Path to geographic data file with layers
nodes_layer : str, optional, default ‘nodes’
Layer name for nodes.
edges_layer : str, optional, default ‘edges’
Layer name for edges.
snkit.network.add_component_ids(network: snkit.network.Network, id_col: str = 'component_id') → snkit.network.Network[source]

Add column of connected component IDs to network edges and nodes

snkit.network.add_endpoints(network)[source]

Add nodes at line endpoints

snkit.network.add_ids(network, id_col='id', edge_prefix='edge', node_prefix='node')[source]

Add or replace an id column with ascending ids

snkit.network.add_topology(network, id_col='id')[source]

Add or replace from_id, to_id to edges

snkit.network.add_vertex(line, point)[source]

Add a vertex to a line at a point

snkit.network.concat_dedup(dfs)[source]

Concatenate a list of GeoDataFrames, dropping duplicate geometries - note: repeatedly drops indexes for deduplication to work

snkit.network.d_within(geom, gdf, distance)[source]

Find the subset of a GeoDataFrame within some distance of a shapely geometry

snkit.network.drop_duplicate_geometries(gdf, keep='first')[source]

Drop duplicate geometries from a dataframe

snkit.network.edges_intersecting(line, edges, tolerance=1e-09)[source]

Find edges intersecting line

snkit.network.edges_intersecting_points(line, edges, tolerance=1e-09)[source]

Return intersection points of intersecting edges

snkit.network.edges_within(point, edges, distance)[source]

Find edges within a distance of point

snkit.network.geometry_column_name(gdf)[source]

Get geometry column name, fall back to ‘geometry’

snkit.network.geoms_to_array(geoms)[source]
snkit.network.get_connected_components(network)[source]

Get connected components within network and id to each individual graph

snkit.network.get_endpoints(network)[source]

Get nodes for each edge endpoint

snkit.network.intersection_endpoints(geom, output=None)[source]

Return the points from an intersection geometry

It extracts the starting and ending points of intersection geometries recursively and appends them to output. This doesn’t handle polygons or collections of polygons.

snkit.network.intersects(geom, gdf, tolerance=1e-09)[source]

Find the subset of a GeoDataFrame intersecting with a shapely geometry

snkit.network.line_endpoints(line)[source]

Return points at first and last vertex of a line

Link nodes to all edges within some distance

Link nodes to all edges within some distance

snkit.network.matching_gdf_from_geoms(gdf, geoms)[source]

Create a geometry-only GeoDataFrame with column name to match an existing GeoDataFrame

snkit.network.merge_edges(network, id_col='id', by=None)[source]

Merge edges that share a node with a connectivity degree of 2

network : snkit.network.Network id_col : string by : List[string], optional

list of columns to use when merging an edge path - will not merge if edges have different values.
snkit.network.merge_multilinestring(geom)[source]

Merge a MultiLineString to LineString

snkit.network.nearest(geom, gdf)[source]

Find the element of a GeoDataFrame nearest a shapely geometry

snkit.network.nearest_edge(point, edges)[source]

Find nearest edge to a point

snkit.network.nearest_node(point, nodes)[source]

Find nearest node to a point

snkit.network.nearest_point_on_edges(point, edges)[source]

Find nearest point on edges to a point

snkit.network.nearest_point_on_line(point, line)[source]

Return the nearest point on a line

snkit.network.nearest_vertex_idx_on_line(point, line)[source]

Return the index of nearest vertex to a point on a line

snkit.network.node_connectivity_degree(node, network)[source]
snkit.network.nodes_intersecting(line, nodes, tolerance=1e-09)[source]

Find nodes intersecting line

snkit.network.read_file(filename, nodes_layer='nodes', edges_layer='edges', **kwargs)[source]

Read a geographic data file with layers containing nodes and edges.

Any additional keyword arguments are passed through to geopandas.read_file.

filename : str
Path to geographic data file with layers
nodes_layer : str, optional, default ‘nodes’
Layer name for nodes, or None if nodes should not be read.
edges_layer : str, optional, default ‘edges’
Layer name for edges, or None if edges should not be read.
snkit.network.round_geometries(network, precision=3)[source]

Round coordinates of all node points and vertices of edge linestrings to some precision

snkit.network.set_precision(geom, precision)[source]

Set geometry precision

snkit.network.snap_line(line, points, tolerance=1e-09)[source]

Snap a line to points within tolerance, inserting vertices as necessary

snkit.network.snap_nodes(network, threshold=None)[source]

Move nodes (within threshold) to edges

snkit.network.split_edge_at_points(edge, points, tolerance=1e-09)[source]

Split edge at point/multipoint

snkit.network.split_edges_at_intersections(network, tolerance=1e-09)[source]

Split network edges where they intersect line geometries

snkit.network.split_edges_at_nodes(network: snkit.network.Network, tolerance: float = 1e-09, chunk_size: Optional[int, None] = None)[source]

Split network edges where they intersect node geometries.

N.B. Can operate in parallel if SNKIT_PROCESSES is in the environment and a positive integer.

Args:

network: Network object to split edges for. tolerance: Proximity within which nodes are said to intersect an edge. chunk_size: When splitting in parallel, set the number of edges per

unit of work.
Returns:
Network with edges split at nodes (within proximity tolerance).
snkit.network.split_line(line, points, tolerance=1e-09)[source]

Split line at point or multipoint, within some tolerance

snkit.network.split_multilinestrings(network)[source]

Create multiple edges from any MultiLineString edge

Ensures that edge geometries are all LineStrings, duplicates attributes over any created multi-edges.

snkit.network.to_networkx(network, directed=False, weight_col=None)[source]

Return a networkx graph

snkit.utils module

Generic utilities

snkit.utils.tqdm_standin(iterator, *_, **__)[source]

Alternative to tqdm, with no progress bar - ignore any arguments after the first