I am being baffled by how apparently poorly NetworkX
reads a shapefile and builds a graph out of it.
Below is a graphical example of a fake network built with 27 polylines all snapped together, so there are no topological errors (ArcGIS 10.3.1
, License: Advanced
). The coordinate system is the British National Grid (OSGB36).
Now, when doing
import networkx as nx
import matplotlib.pyplot as plt
G=nx.read_shp('C:\Users\MyName\MyFolder\TEST.shp')
nx.draw(G, with_labels=False)
The result is a graph plotted according to the spring layout, so the shape of the file is not preserved. Also, the image shows three disconnected components, which are absolutely fabricated, since all segments in my shapefile were snapped. Also, the coordinates of the nodes are imported as labels.
I wonder what the usefulness of the nx.read_shp()
function is, if you are not allowed to reproduce the shapefile exactly. This is especially true if you want to make sure that the connections are recreated accurately.
This leads me to my question: how to play with NetworkX
so that the graph is not plotted in some meaningless way, but reproduces the network shown in the shapefile?