19

Is there a way to convert a linear shapefile 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.

winnewoerp
1,74912 silver badges24 bronze badges
asked Aug 5, 2009 at 16:53
2
  • 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. Commented Aug 18, 2009 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 Commented Aug 19, 2009 at 2:07

1 Answer 1

13

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.

answered Aug 5, 2009 at 16:58
3
  • Thanks for the swift response. I'm looking at gdal.org/ogr/ogr%5Fapitut.html 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? Commented Aug 5, 2009 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. Commented Aug 5, 2009 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. Commented Aug 5, 2009 at 17:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.