snkit package

snkit - a spatial networks toolkit

class snkit.Network(nodes: <MagicMock id='140654272341984'> | None = None, edges: <MagicMock id='140654272341984'> | None = None)[source]

Bases: object

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

Parameters:
  • nodes (geopandas.geodataframe.GeoDataFrame, optional)

  • edges (geopandas.geodataframe.GeoDataFrame, optional)

nodes
Type:

geopandas.geodataframe.GeoDataFrame

edges
Type:

geopandas.geodataframe.GeoDataFrame

set_crs(crs: CRS | None = None, epsg: int | None = None, allow_override: bool = False) None[source]

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

Parameters:
  • 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 (optional if crs is specified) – EPSG code specifying output projection.

  • allow_override (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: CRS | None = None, epsg: int | None = None) None[source]

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

Parameters:
  • 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 (optional if crs is specified) – EPSG code specifying output projection.

to_file(filename: str, nodes_layer: str | None = 'nodes', edges_layer: str | None = 'edges', **kwargs: Any) None[source]

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

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

Parameters:
  • 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: <MagicMock id='140654272341984'> | None = None, edges: <MagicMock id='140654272341984'> | None = None)[source]

Bases: object

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

Parameters:
  • nodes (geopandas.geodataframe.GeoDataFrame, optional)

  • edges (geopandas.geodataframe.GeoDataFrame, optional)

nodes
Type:

geopandas.geodataframe.GeoDataFrame

edges
Type:

geopandas.geodataframe.GeoDataFrame

set_crs(crs: CRS | None = None, epsg: int | None = None, allow_override: bool = False) None[source]

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

Parameters:
  • 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 (optional if crs is specified) – EPSG code specifying output projection.

  • allow_override (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: CRS | None = None, epsg: int | None = None) None[source]

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

Parameters:
  • 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 (optional if crs is specified) – EPSG code specifying output projection.

to_file(filename: str, nodes_layer: str | None = 'nodes', edges_layer: str | None = 'edges', **kwargs: Any) None[source]

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

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

Parameters:
  • 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: Network, id_col: str = 'component_id') Network[source]

Add column of connected component IDs to network edges and nodes

snkit.network.add_endpoints(network: Network) Network[source]

Add nodes at line endpoints

snkit.network.add_ids(network: Network, id_col: str | None = 'id', edge_prefix: str | None = 'edge', node_prefix: str | None = 'node') Network[source]

Add or replace an id column with ascending ids

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

Add or replace from_id, to_id to edges

snkit.network.add_vertex(line: <MagicMock id='140654256182896'>, point: <MagicMock id='140654272020448'>) <MagicMock id='140654256182896'>[source]

Add a vertex to a line at a point

snkit.network.concat_dedup(dfs: List[DataFrame]) <MagicMock id='140654272341984'>[source]

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

snkit.network.d_within(geom: <MagicMock id='140654256096368'>, gdf: <MagicMock id='140654272341984'>, distance: int | float) <MagicMock id='140654272341984'>[source]

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

snkit.network.drop_duplicate_geometries(gdf: <MagicMock id='140654272341984'>, keep: str | None = 'first') <MagicMock id='140654272341984'>[source]

Drop duplicate geometries from a dataframe

snkit.network.edges_intersecting(line: <MagicMock id='140654256182896'>, edges: <MagicMock id='140654272341984'>, tolerance: int | float | None = 1e-09) <MagicMock id='140654272341984'>[source]

Find edges intersecting line

snkit.network.edges_intersecting_points(line: <MagicMock id='140654256182896'>, edges: <MagicMock id='140654272341984'>, tolerance: int | float | None = 1e-09) <MagicMock id='140654272341984'>[source]

Return intersection points of intersecting edges

snkit.network.edges_within(point: <MagicMock id='140654272020448'>, edges: <MagicMock id='140654272341984'>, distance: int | float) <MagicMock id='140654272341984'>[source]

Find edges within a distance of point

snkit.network.geometry_column_name(gdf: <MagicMock id='140654272341984'>) str[source]

Get geometry column name, fall back to ‘geometry’

snkit.network.geoms_to_array(geoms: ~typing.List[<MagicMock id='140654256096368'>]) Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes][source]
snkit.network.get_connected_components(network: Network) List[Set[Any]][source]

Get connected components within network and id to each individual graph

snkit.network.get_endpoints(network: Network) <MagicMock id='140654272341984'>[source]

Get nodes for each edge endpoint

snkit.network.intersection_endpoints(geom: <MagicMock id='140654256096368'>, output: ~typing.List[<MagicMock id='140654272020448'>] | None = None) List[<MagicMock id='140654272020448'>][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: <MagicMock id='140654256096368'>, gdf: <MagicMock id='140654272341984'>, tolerance: int | float | None = 1e-09) <MagicMock id='140654272341984'>[source]

Find the subset of a GeoDataFrame intersecting with a shapely geometry

snkit.network.line_endpoints(line: <MagicMock id='140654256182896'>) Tuple[<MagicMock id='140654272020448'>, <MagicMock id='140654272020448'>][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: <MagicMock id='140654272341984'>, geoms: ~typing.List[<MagicMock id='140654256096368'>]) <MagicMock id='140654272341984'>[source]

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

snkit.network.merge_edges(network: Network, id_col: str | None = 'id', by: List[str] | None = None) Network[source]

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

Parameters:
  • 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: <MagicMock id='140654256096368'>) <MagicMock id='140654256096368'>[source]

Merge a MultiLineString to LineString

snkit.network.nearest(geom: <MagicMock id='140654256096368'>, gdf: <MagicMock id='140654272341984'>) pandas.Series[Any][source]

Find the element of a GeoDataFrame nearest a shapely geometry

snkit.network.nearest_edge(point: <MagicMock id='140654272020448'>, edges: <MagicMock id='140654272341984'>) pandas.Series[Any][source]

Find nearest edge to a point

snkit.network.nearest_node(point: <MagicMock id='140654272020448'>, nodes: <MagicMock id='140654272341984'>) pandas.Series[Any][source]

Find nearest node to a point

snkit.network.nearest_point_on_edges(point: <MagicMock id='140654272020448'>, edges: <MagicMock id='140654272341984'>) <MagicMock id='140654272020448'>[source]

Find nearest point on edges to a point

snkit.network.nearest_point_on_line(point: <MagicMock id='140654272020448'>, line: <MagicMock id='140654256182896'>) <MagicMock id='140654272020448'>[source]

Return the nearest point on a line

snkit.network.nearest_vertex_idx_on_line(point: <MagicMock id='140654272020448'>, line: <MagicMock id='140654256182896'>) int[source]

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

snkit.network.node_connectivity_degree(node: str, network: Network) int[source]
snkit.network.nodes_intersecting(line: <MagicMock id='140654256182896'>, nodes: <MagicMock id='140654272341984'>, tolerance: int | float | None = 1e-09) <MagicMock id='140654272341984'>[source]

Find nodes intersecting line

snkit.network.read_file(filename: str, nodes_layer: str | None = 'nodes', edges_layer: str | None = 'edges', **kwargs: Any) Network[source]

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

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

Parameters:
  • filename – Path to geographic data file with layers

  • nodes_layer (, optional, default 'nodes') – Layer name for nodes, or None if nodes should not be read.

  • edges_layer (, optional, default 'edges') – Layer name for edges, or None if edges should not be read.

snkit.network.round_geometries(network: Network, precision: int = 3) Network[source]

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

snkit.network.set_precision(geom: <MagicMock id='140654256096368'>, precision: int) <MagicMock id='140654256096368'>[source]

Set geometry precision

snkit.network.snap_line(line: <MagicMock id='140654256182896'>, points: <MagicMock id='140654272020448'> | <MagicMock id='140654256115680'>, tolerance: int | float | None = 1e-09) <MagicMock id='140654256182896'>[source]

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

snkit.network.snap_nodes(network: Network, threshold: int | float | None = None) Network[source]

Move nodes (within threshold) to edges

snkit.network.split_edge_at_points(edge: <MagicMock id='140654308592432'>, points: <MagicMock id='140654272020448'> | <MagicMock id='140654256115680'>, tolerance: int | float | None = 1e-09) <MagicMock id='140654272341984'>[source]

Split edge at point/multipoint

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

Split network edges where they intersect line geometries

snkit.network.split_edges_at_nodes(network: Network, tolerance: int | float = 1e-09, chunk_size: int | None = None) Network[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.

Parameters:
  • 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: <MagicMock id='140654256182896'>, points: <MagicMock id='140654272020448'> | <MagicMock id='140654256115680'>, tolerance: int | float | None = 1e-09) List[<MagicMock id='140654256182896'>][source]

Split line at point or multipoint, within some tolerance

snkit.network.split_multilinestrings(network: Network, merge_parts: bool = False) Network[source]

Create multiple edges from any MultiLineString edge

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

Parameters:

merge_parts (default False) – Merge parts of geometries if they are connected, before splitting to multiple edges

snkit.network.to_networkx(network: Network, directed: bool = False, weight_col: str | None = None) Graph | DiGraph[source]

Return a networkx graph

snkit.utils module

Generic utilities

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

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