Skip to content

Commit

Permalink
Avoid adding columns when converting to networkx
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalrussell committed Mar 23, 2022
1 parent 92995c2 commit e5b3c44
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/snkit/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,18 +694,16 @@ def to_networkx(network, directed=False, weight_col=None):
G = nx.MultiDiGraph()
# get nodes from network data
G.add_nodes_from(network.nodes.id.to_list())

# add nodal positions from geom
network.nodes["pos"] = list(
zip(network.nodes.geometry.x, network.nodes.geometry.y)
)
pos = network.nodes.set_index("id").to_dict()["pos"]
for n, p in pos.items():
G.nodes[n]["pos"] = p
for node_id, x, y in zip(network.nodes.id, network.nodes.geometry.x, network.nodes.geometry.y):
G.nodes[node_id]["pos"] = (x, y)

# get edges from network data
if weight_col is None:
network.edges["weight"] = network.edges.geometry.length
# default to geometry length
edges_as_list = list(
zip(network.edges.from_id, network.edges.to_id, network.edges.weight)
zip(network.edges.from_id, network.edges.to_id, network.edges.geometry.length)
)
else:
edges_as_list = list(
Expand Down

0 comments on commit e5b3c44

Please sign in to comment.