Daniel W
Daniel W

Reputation: 41

Converting a CSV with a WKT column to a Shapefile

Using ogr2ogr in Python, I am trying to convert a CSV to a shapefile. One column in the CSV, named "Polygon", contains WKT like this: POLYGON((long lat, long lat, long lat, etc.)) Currently, I can make a polygon shapefile with the correct projection, but the geometry is empty.

How can I modify my ogr2ogr arguments to correctly create the geometry using the WKT in each row? Currently I have something like this:

ogr2ogr.main(["", "-a_srs", "EPSG:4326", "-f", "ESRI Shapefile", "output.shp", "input.csv", "-nlt", "POLYGON"])

Upvotes: 4

Views: 4100

Answers (1)

mgc
mgc

Reputation: 5443

I'm not really used to ogr2ogr.py but if you know some basics with python bindings for ogr it can be done quite easily. Here is a simple example which do what you are trying to do.

import ogr, osr, csv

spatialref = osr.SpatialReference()  # Set the spatial ref.
spatialref.SetWellKnownGeogCS('WGS84')  # WGS84 aka ESPG:4326
driver = ogr.GetDriverByName("ESRI Shapefile") # Shapefile driver
dstfile = driver.CreateDataSource('output.shp') # Your output file

# Please note that it will fail if a file with the same name already exists
dstlayer = dstfile.CreateLayer("layer", spatialref, geom_type=ogr.wkbPolygon) 

# Add the other attribute fields needed with the following schema :
fielddef = ogr.FieldDefn("ID", ogr.OFTInteger)
fielddef.SetWidth(10)
dstlayer.CreateField(fielddef)

fielddef = ogr.FieldDefn("Name", ogr.OFTString)
fielddef.SetWidth(80)
dstlayer.CreateField(fielddef)

# Read the features in your csv file:
with open('/path/to/file.csv') as file_input:
    reader = csv.reader(file_input)  # Can be more intuitive with a DictReader (adapt to your needs)
    next(reader) # Skip the header
    for nb, row in enumerate(reader): 
        # WKT is in the second field in my test file :
        poly = ogr.CreateGeometryFromWkt(row[1])
        feature = ogr.Feature(dstlayer.GetLayerDefn())
        feature.SetGeometry(poly)
        feature.SetField("ID", nb) # A field with an unique id.
        feature.SetField("Name", row[0]) # The name (expected to be in the first column here)
        dstlayer.CreateFeature(feature)
    feature.Destroy()
    dstfile.Destroy()

This code assumes that your CSV has two columns, the first one with a name to use and the second one containing the geometry in WKT as asked, so for example something in the following form:

"Name","Polygons"
"Name1","POLYGON((1 1,5 1,5 5,1 5,1 1))"
"Name2","POLYGON((6 3,9 2,9 4,6 3))"

Of course it need to be adapted on your specific case, but this snippet of code can be a first start and will do the job.
(And if you want some other examples on GDAL/ogr python bindings you can have a look on these recipes)

Upvotes: 2

Related Questions