3

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.

2 Answers2

0

This should be a comment, but I lack reputation:

I had the same error with a different case and I worked around this issue, by converting all my integer values into float or string, because shapefiles don't make - in general implementation - a difference between int and float. And the Python bindings for ogr don't do a proper type conversion for that. But I am not sure if you are try to write any integer-data into your attributes.

Maybe this helps:

xx, yy = float(G.nodes[str(node)]['x']), float(G.nodes[str(node)]['y'])

But I learned about this by checking the sourcecode of ogr.py and nx_shp.py (and altering ogr.py temporarily so I can figure out what kind of value is processed at that time). Maybe that could help you too.

0

I just encountered the same error and spent a long time to figure it out. In short, the Feature_SetField can only accept the int, str, float. In your code nx.set_node_attributes(H, {str(node): (xx, yy)}, 'loc'), the node feature is a tuple.

Remove this line or just change to nx.set_node_attributes(H, {str(node): xx}, 'loc') should solve the error.

Xudong
  • 441
  • 5
  • 16