Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Using ogr2ogr and python, I'm running into a wall with trying to create a csv with WKTs for a shapefile that contains multipart polygons. Currently this is this code I'm using (found it on interweb):

#Purpose: To export a shapefile to WKT

from osgeo import ogr
import sys, os

input = ogr.Open(sys.argv[1])


layer_in = input.GetLayer()
layer_in.ResetReading()
feature_in = layer_in.GetNextFeature()

outfile = open(sys.argv[1] + ".wkt", "w")

while feature_in is not None:

    geom = feature_in.GetGeometryRef()
    geom_name = geom.GetGeometryName()

    outfile.write(str(geom)+ '\n')

    feature_in = layer_in.GetNextFeature()

So, do I need to use a different geometry container or do I need to evaluation each feature and if it's a multipart use a different set of geometry classes? I've looked around in the gdal/ogr documentation but I'm having a hard time reconciling it.

thanks in advance, matt.

share|improve this question
2  
Do you need to use python? "ogr2ogr -f csv -lco GEOMETRY=AS_WKT out.csv in.shp layer" should do the job. –  kyle Oct 25 '12 at 17:25
    
I have tried that but I need to format some of the attribute data within the shapefile in addition to wkt generation. Is there a way to do this at the cmd? –  Matt Oct 25 '12 at 18:00
    
No, there isn't. But you are on the right track. I checked the docs and I am not sure why your way didn't work, it appears __str__() does return the wkt. –  kyle Oct 25 '12 at 19:33
    
@Matt, how dramatically do you need to format the attribute data? Are you merely renaming fields, or are you changing the datatypes entirely? I think you could use ogr2ogr with a clever OGR SQL expression and still accomplish a fair amount of reformatting. –  elrobis Oct 25 '12 at 21:46
    
The reformatting is datatypes I guess, i.e. float to integer. One option is to convert the datatype with arcpy then ogr2ogr at the cmd. Can you do this with OGR SQL? –  Matt Oct 25 '12 at 21:56

1 Answer 1

If you can't use ogr2ogr, and you are looking for wkt, use ogr.Geometry.ExportToWkt():

from osgeo import ogr
import sys, os
input = ogr.Open(sys.argv[1])
layer_in = input.GetLayer()
layer_in.ResetReading()
outfile = open(sys.argv[1] + ".wkt", "w")
for f in layer:
    geom = feature_in.GetGeometryRef()
    geom_name = geom.GetGeometryName()
    wkt = geom.ExportToWkt()
    outfile.write(wkt + '\n')

With a few code changes for brevity. I tested it briefly and was able to get wkt.

share|improve this answer
    
Thanks for the help. I'm still wondering if this will work for multiparts though. The result set states 'polygon' for each feature yet when done in ogr2ogr cmd there's 'multipolygons' in addition to just polygons. –  Matt Oct 25 '12 at 21:18
    
Okay, I see. I will look into when I get a chance. I seem to remember something coming about recently that has to do with that. Your geometry type is MultiPolygon and Polygon? –  kyle Oct 25 '12 at 21:28
    
yes, both geometries. Thanks for the help. –  Matt Oct 25 '12 at 21:49

Your Answer

 
discard

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.