vendredi 10 février 2017

NetworkX: add edges to a graph from a shapefile

Vote count: 0

Given the following test shapefile, which is made of polylines only:

enter image description here

I was able to reproduce the nodes of the spatial network represented in the shapefile:

import networkx as nx
import matplotlib.pyplot as plt

G=nx.read_shp('C:\Users\MyName\MyFolder\TEST.shp') #Read shapefile as graph
pos = {k: v for k,v in enumerate(G.nodes())} #Get the node positions based on their real coordinates
X=nx.Graph() #Empty graph
X.add_nodes_from(pos.keys()) #Add nodes preserving real coordinates
nx.draw_networkx_nodes(X,pos,node_size=100,node_color='r')
plt.xlim(450000, 470000)
plt.ylim(430000, 450000)

enter image description here Basically I have used a temporary graph G to extract the positions of the nodes that eventually appeared as part of the graph X. This seems to have worked just fine.

My question: following the same idea of using G to extract information from the shapefile, how could I plot the edges?

If I do something like this

X.add_edges_from(pos.keys())

Then I get this error, pointing at the line above:

TypeError: object of type 'int' has no len()

asked 1 min ago

Let's block ads! (Why?)



NetworkX: add edges to a graph from a shapefile

Aucun commentaire:

Enregistrer un commentaire