2

This question already has an answer here:

In QGIS, I can add WKT data stored in a CSV by clicking the 'Add Delimited Text Layer' tool and setting it to 'WKT'. This works fine:

WKT tool in QGIS

But how can I do it as a python script?

Ideally I'd like a script which- 1. Imports the CSV into QGIS as a polygon (drawn from the WKT field) 2. Exports the new (temporary?) polygon layer to a shapefile on my disk

I have minimal experience writing scripts in QGIS. Using help guides, I've written a script to import a CSV to a point layer, with X and Y fields defined. (using this link)

But obviously a WKT is different as you only need to define the WKT field.

Here's what my source CSV looks like: Source file

marked as duplicate by Evil Genius, Simon, ArMoraer, xunilk, Dan C Jun 7 '18 at 13:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

To import the WKT string from CSV into shapfile, you can use ogr and osr module to transform.

I write the python script to transform the field of CSV(WKT, Name) into shapfile:

import ogr, osr, cs

def csv2shp(fileInput):

spatialref = osr.SpatialReference()  # Set the spatial ref.
spatialref.SetWellKnownGeogCS('WGS84')  # WGS84 aka ESPG:4326

driver = ogr.GetDriverByName("ESRI Shapefile")
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)

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

# Read the feature in your csv file:
with open(fileInput) as file_input:
    reader = csv.reader(file_input)
    next(reader) # Skip the header
    for nb, row in enumerate(reader): 
        # WKT is in the first field in my test file :
        poly = ogr.CreateGeometryFromWkt(row[0])
        feature = ogr.Feature(dstlayer.GetLayerDefn())
        feature.SetGeometry(poly)
        feature.SetField("ID", nb) # A field with an unique id.
        feature.SetField("WKT", row[0])
        feature.SetField("Name", row[1])
        dstlayer.CreateFeature(feature)
    feature.Destroy()
    dstfile.Destroy()

return dstfile

if __name__ == "__main__":
    csv2shp(r"C:\Users\USER\Desktop\TEST.csv")

You can also use this method in QGIS plugin.

  • I've edited the script to suit my needs, but when I run it i get 'Expected an indented block (, line 6)' my line 6 reads: spatialref.SetWellKnownGeogCS('OSGB 1936') # WGS84 aka EPSG:4326 – Theo F Jun 7 '18 at 9:45
  • @TheoF I use the 'OSGB 1936' and it run correct. Maybe you should check your indentation! – sos418 Jun 8 '18 at 3:42

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