I am trying to write a shapefile using python 3.8 and Networkx v2.4.
import networkx as nx
import pandas as pd
f= open('Onslowedge.edgelist','rb')
G = nx.read_edgelist(f)
latlong = pd.read_csv('latlong.csv',index_col=0)
for index in latlong.index:
G.nodes[str(index)]['x'] = latlong["Longitude"][index]
G.nodes[str(index)]['y'] = latlong["Latitude"][index]
H= nx.DiGraph()
counter = 0
mapping = dict()
for node in list(G.nodes()):
xx, yy = G.nodes[str(node)]['x'], G.nodes[str(node)]['y']
H.add_node(str(node))
nx.set_node_attributes(H, {str(node): (xx, yy)}, 'loc')
mapping[node] = (xx,yy)
H1 = nx.relabel_nodes(H,mapping)
for edge in list(G.edges()):
e = (mapping[str(edge[0])], mapping[str(edge[1])])
H1.add_edge(*e)
nx.write_shp(H1, '\\shapefile')
The reason i am creating a copy of the DIGRAPH from the Graph as the networkx write_shp
function can only take a DIGRAPH as an input.
Based on a answer found in stackoverflow I relabel the nodes by their coordinates itself.
This is the error:
nx.write_shp(H1, '\\shapefile')
Traceback (most recent call last):
File "<ipython-input-61-796466305294>", line 1, in <module>
nx.write_shp(H1, '\\shapefile')
File "C:\Users\sssaha\.conda\envs\tfgpu\lib\site-packages\networkx\readwrite\nx_shp.py", line 308, in write_shp
create_feature(g, nodes, attributes)
File "C:\Users\sssaha\.conda\envs\tfgpu\lib\site-packages\networkx\readwrite\nx_shp.py", line 259, in create_feature
feature.SetField(field, data)
File "C:\Users\sssaha\.conda\envs\tfgpu\lib\site-packages\osgeo\ogr.py", line 4492, in SetField
return _ogr.Feature_SetField(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'Feature_SetField'.
Possible C/C++ prototypes are:
OGRFeatureShadow::SetField(int,char const *)
OGRFeatureShadow::SetField(char const *,char const *)
OGRFeatureShadow::SetField(int,double)
OGRFeatureShadow::SetField(char const *,double)
OGRFeatureShadow::SetField(int,int,int,int,int,int,float,int)
OGRFeatureShadow::SetField(char const *,int,int,int,int,int,float,int)
I am not sure how to resolve this or what actually is causing the problem.