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

Is there a way to convert a linear shape file into a csv file of the following format:

Node_1, Node_2, attribute_1, ...., attribute_x
     1       2       "hello"        567845.334

I have looked at GDAL/OGR2OGR and PostGIS and I don't think either converts the data to the format I want.

I don't mind writing a script to do it, if I'm guided in the right direction.

share|improve this question

migrated from Oct 11 '11 at 11:01

This question came from our site for professional and enthusiast programmers.

In your example, Node_1, Node_2 are the actual coordinates of your feature, right? I mean, from your example, I would actually assume that you want to export the topology from the shapefile; as in Node_1 is your FNODE, Node_2 is your TNODE and the geometry is stored only once. If so, the answer is very different and you will need something besides OGR to build the topology. – Ragi Yaser Burhum Aug 18 '09 at 20:27
@rburhum i want to implement my table in a classic network algorithm. The format is usually, node 1 to node 2 with a cost of X – dassouki Aug 19 '09 at 2:07
up vote 5 down vote accepted

The utility programs included in OGR (OGR2OGR) will not do this directly, but it will be very easy to use OGR to accomplish this.

You would need to write a script/program in any of the languages supported by OGR. You could just have it open the shapefile, read each feature, and write out the points + data in a CSV format.

The sample C++ code on the Wiki actually shows nearly every OGR API call required for this.

share|improve this answer
Thanks for the swift response. I'm looking at first python example, which outputs from a SHP to CSV file. The example is done for a point layer, is there any significant difference to convert it for a line layer? What would be the format of the Node name? Will it be a number? Can I link it to another point layer that has all the node names in it? – dassouki Aug 5 '09 at 17:30
In a line, the features will be ogr.wkbLine instead of ogr.wkbPoint. You can return the name as a string using feature.GetFieldAsString(col). There is no way to handle "linking' directly in OGR, so you'd have to open the other data table and search it yourself, or copy the names across in GIS first. – Reed Copsey Aug 5 '09 at 17:35
That makes sense; well I’ll try it out and keep you updated on how I make out. I'm doing all this so i can do some network analysis on a line layer. – dassouki Aug 5 '09 at 17:39

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.