Skip to content

Commit

Permalink
Add Network.to_file and read_file helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalrussell committed Jun 9, 2023
1 parent e7bcc26 commit 4be5de2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/snkit/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,50 @@ def to_crs(self, crs=None, epsg=None):
self.edges.to_crs(crs, epsg, inplace)
self.nodes.to_crs(crs, epsg, inplace)

def to_file(self, filename, nodes_layer="nodes", edges_layer="edges", **kwargs):
"""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.
"""
self.nodes.to_file(filename, layer=nodes_layer, **kwargs)
self.edges.to_file(filename, layer=edges_layer, **kwargs)


def read_file(filename, nodes_layer="nodes", edges_layer="edges", **kwargs):
"""Read a geographic data file with layers containing nodes and edges.
Any additional keyword arguments are passed through to `geopandas.read_file`.
Parameters
----------
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.
"""
if nodes_layer is not None:
nodes = geopandas.read_file(filename, layer=nodes_layer, **kwargs)
else:
nodes = None

if edges_layer is not None:
edges = geopandas.read_file(filename, layer=edges_layer, **kwargs)
else:
edges = None

return Network(nodes, edges)


def add_ids(network, id_col="id", edge_prefix="edge", node_prefix="node"):
"""Add or replace an id column with ascending ids"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ def test_init():
assert len(net.edges) == 0


def test_roundtrip(connected, tmp_path):
"""Should write and read back a network"""
connected.to_file(tmp_path / "connected.gpkg", driver="GPKG")
actual = snkit.network.read_file(tmp_path / "connected.gpkg")
assert_frame_equal(actual.nodes, connected.nodes)
assert_frame_equal(actual.edges, connected.edges)


def test_round_geometries(misaligned, connected):
"""Should round coordinates to some tolerance"""
rounded = snkit.network.round_geometries(misaligned, precision=0)
Expand Down

0 comments on commit 4be5de2

Please sign in to comment.