I have a number of rail networks, in the form of shapefiles of the "line" type. I want to turn them into a more abstract graph format, i.e. the kind with vertices and edges, not chart-type graphs. Moreover I want these data to be usable outside my GIS programs, in Python. Basically what I want is to extract something along the lines of the TSPLIB format, except with only vertices and edges. For my immediate purposes, I don't even need weights or anything. Just the topology.

Is there any relatively pain-free way to do this in QGIS or GRASS? I haven't quite lucked out yet.


NetworkX ( A python module for the creation, manipulation, and study of the structure, dynamics, and functions of complex networks ) has a read_shp() function which generates a graphs from shapefiles. You might want to check it out. Here's a short tutorial.

No TPSLIB export as far as I know but does support writing the graphs into formats like GML, GraphML, Pickle, adjacency lists, and GEXF to name a few. The full list of output formats can be found here. I hope this helps. Good luck.

  • I tried that and it isn't doing quite what I need—the situation is made more complicated by the fact that I'm not entirely sure what I actually NEED for my inquiry—but it's a lead and I appreciate it. I'll accept this answer. – readyready15728 Dec 26 '12 at 1:20

To turn your lines into edges, and give them an id, you may want to use assign_vertex_id inside PostGIS in case your lines are in PostgreSQL/PostGIS. Using that, you will get a unique id for your rail network lines. To get the vertices i.e. start and end point of the lines you can simply add two extra columns for them and run a simple update to fill values in those columns.

I hope that helped! Merry xmas! ;)

  • 1
    You seem to be describing the dual of the graph. To create the graph of the question, you need to assign unique identifiers to vertices--giving the same ID to any coincident vertices--and edges need no identifiers at all. – whuber Dec 24 '12 at 15:47
  • I don't know anything about the PostGIS stuff but I will say thanks and merry xmas likewise. – readyready15728 Dec 26 '12 at 1:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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