Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

using Gdal/ OGR is there a way to convert a shp, kml, or PostGIS line layer into a network of nodes and segments so it can be used in packages such as networkx?

share|improve this question
It's probably worth noting that while OGR and NetworkX both say they support GML, the former refers to Geography Markup Language while the latter is Graph Modelling Language. – fmark Aug 5 '10 at 14:57
up vote 7 down vote accepted

Ben Reilly recently posted a link on another question to his utilitynetwork Python package, which uses the OGR bindings to convert data into networkx DiGraphs.

share|improve this answer
That is awesome :) – fmark Aug 5 '10 at 23:02

Judging from the networkx list of importable graph formats and formats supported by GDAL, I doubt such a conversion is possible.

share|improve this answer
that's a crappy deal. So How can I do network analysis then? – dassouki Aug 5 '10 at 12:53
@dassouki - Start an open source project! It's on my list, but way down at the moment. I'd like the opportunity to collaborate on this if other are interested though. – fmark Aug 5 '10 at 14:28
@fmark already have – dassouki Aug 5 '10 at 14:29
@dassouki - Sounds exciting! What stage is the project at? The homepage says TranspoTools Will be here shortly. Is there any code on the google-code repository? Everything I could see was empty. – fmark Aug 5 '10 at 14:37
@dassouki - Just to confirm, I don't know much about the transport analysis field (my GIS interest lies not much farther beyond calculating network distance at this stage), but would be happy to collaborate on an ogr2networkx type tool. – fmark Aug 5 '10 at 14:40

You haven't mentioned specifically what kind of network analysis you want to do, but if you are interested in routing, there are a couple of options:

PgRouting works with data in PostGIS to do routing analysis. It supports the Shortest Path Dijkstra, Shortest Path A*, Shortest Path Shooting Star and Traveling Salesperson (TSP) algorithms. It also does driving distance calculations. A list of example apps using PgRouting can be found at PgRouting drives the Ride the City app.

The OpenRouter project This project looks interesting, but not particularly mature at this point.

(Apologies for the quoted URLs, but I don't have enough cred to post >1 URl...)

share|improve this answer
+1 for you. I'm looking at moving away from openstreet and postgis data due to data limitations and the fact that we're ditching postgis for now. We usually provide our own data that is far better than Openstreet, but at the same time we don't want to be limited to pgrouting. The other issue is that let's say we want to implement a pythonic or arcgis-friendly garbage collection or chinese postman or Traveler sales man problems. – dassouki Aug 5 '10 at 13:30
Have you looked at SpatiaLite? This would give you an OpenSource spatial database that if file-based and platform independent. I haven't used it, but it appears that there is routing capability with SpatiaLite <>; – DavidF Aug 5 '10 at 13:50
Also, thanks for bringing NetworkX to my attention. This looks like are really cool Python module. – DavidF Aug 5 '10 at 19:39

I think the answer depends. Most GIS formats do not explicitly include adjacency information (e.g. there are no indicators in a .shp full of simple line features to indicate that two features share a common endpoint), which is a deal-breaker for reading them directly into a network.

From a cursory peek at the code, it looks like the utilitynetwork package linked by scw will re-discover that information only if the endpoints are bit-for-bit identical. This might be good enough for most needs, including yours! (I could be wrong about this, since it was indeed only a cursory peek.)

For something more robust -- for example, you may wish to create new nodes in the network where two edges cross, or you'd like to handle perturbations that make overlapping endpoints very slightly different -- you'll have to look for an approach that inspects the geometry more deeply.

I have done something similar with Boundary Generator to recover network adjacency information from polygons: wrong platform, language and geometric primitive for you, unfortunately. However, it may be insightful as to how to approach robustness issue(s).

share|improve this answer
+1 ... I'm not sure what you mean by the wrong platform? I understand your explanation, and it really blows that I can't convert the data into nodes and weights. the only 2 programs that do that from shp files are TransCAD and EMME2/3 – dassouki Aug 13 '10 at 17:30
By "wrong platform" I just meant that Boundary Generator was written for the ArcGIS environment, at least for now. :) It would need some adaptation to work with OGR. (Also needed would be a significant bit of new code to discover coincident endpoints from lines, as opposed to coincident edges from polygons. I might add this to the next version if/when I have the time. It seems like it could be very useful.) – Dan S. Aug 17 '10 at 22:34

There is some geodjango code in MarineMap that might be adaptable for the task.

share|improve this answer
cool we can discuss this to fulfill your goal of ogr2netx ahmed at – dassouki Aug 5 '10 at 14:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.