I am working in a simulation using NetLogo, the idea is to make the agents interact inside the roads of a simplified city, in order to do that I need to use some algorithms to find the fastest path between two points in the city (for example Dijkstra or A*), and to do that I need to work with a graph of the city (with nodes and vertices) but I don't know how to convert the shapefile into a graph.
My idea was to get all the intersections between two streets and start building the graph connecting those nodes. I have tried networkx
for python but I had some problems installing the library gdal
. Currently I'm using the s2g
library (here) to do it, but for example for a shp file like this
I always get this:
INFO:root:Validating pair-wise line connections of raw shapefiles (total 3 lines)
100% (3 of 3) |##########################################################################################################| Elapsed Time: 0:00:00 Time: 0:00:00
Major components statistics:
Total components: 0
Component size: max 0, median 0.0, min 0, average 0.0
Top comp. sizes: 0
I suppose that is an empty graph so I cannot work with it. Perhaps there is a better approach to my problem or an easy tool to convert shp to graphs. I mac user so I have no access to spatialite.
Best Answer
The sg module uses Fiona to read the shapefiles (see shapegraph.py ) and if you can use the module, therefore Fiona is installed.
If you cannot use nx_shp.py (because of osgeo) and you have problems with sg, you can use Fiona and Networkx to create a Networkx Graph. (GSE: How to calculate edge length in Networkx for example).
Result
You can also create a Planar Graph
New
There is a problem if some geometries are Multigeometries
For example:
With geoms[0], you decompose the Multigeometry
Therefore the script becomes