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.

  • 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

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.

| improve this answer | |
  • 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? – 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 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.