6

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 internet):

#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.

5
  • 4
    Do you need to use python? "ogr2ogr -f csv -lco GEOMETRY=AS_WKT out.csv in.shp layer" should do the job.
    – user10353
    Commented Oct 25, 2012 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
    Commented Oct 25, 2012 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.
    – user10353
    Commented Oct 25, 2012 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
    Commented Oct 25, 2012 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
    Commented Oct 25, 2012 at 21:56

1 Answer 1

3

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.

2
  • 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
    Commented Oct 25, 2012 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?
    – user10353
    Commented Oct 25, 2012 at 21:28

Your Answer

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

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