QGIS 1.8 on Mac: I'm hoping to migrate several dozen local maps from my 30 year old Win3.1 plotting system to QGIS. Polygons are in X,Y (CSV), first point repeated at the end to close it, with polygons separated by a blank line. They're each in local coordinates (meters from a corner). I can transform to UTM and replace the polygon delimiter by something else if needed, but it would be easier not to have to.

The format is



I've tried Import as Text Layer: the attribute table shows the values correctly, but each coordinate only shows as a point, the polygon separator lines are omitted, and the layer can't be edited. Tried to export it as vector layer as http://maps.cga.harvard.edu/qgis/wkshop/import_csv.php suggests- export fails. I can't find the mmqgis mentioned in several posts here; the Points2One plugin doesn't do anything.

Any suggestions?

  • 1
    By the sounds of it, you will need to format your data a wee bit. Points2One won't do anything because it has no information on how to join the points together by the sounds of it. It needs an identifier to say which polygon a point belongs to. Can you show a bit of the data just to be clear on the layout? Commented Apr 23, 2013 at 13:02
  • in the following, @ is a new line: X,Y@ 10,10@ 20,20@ 20,30@ 10,10@ @ 5,5@ 5,6@ 6,5@ 5,5@ @ etc
    – johnsankey
    Commented Apr 23, 2013 at 18:50

2 Answers 2


You will need to do some formatting of your data to make them into polygons. Merely labeling a field as WKT will not help. If you have a lot of files (which it sounds like you do), the most effective way will be to automate your solution by writing a script.

I was going to explain how when I decided that the simplest way would be to write an example script (apologies if I am teaching you to suck eggs!):

import os
csvFolder = r"C:\myFolder\mySubFolder"

def writePolyToFile(outFile, polygon, polyId):
    wkt = "POLYGON((" + ','.join(polygon) + "))\n"
    outFile.write(str(polyId) + ';' + wkt)

def makePolys(inPath, outPath):
        with open(inPath,'r') as inFile:
            contents = inFile.readlines()
            polyId = 0
            polygon = []

            for line in contents:
                line = line.rstrip('\n')
                if polyId == 0:

                    if line != 'x,y':
                        print("Unexpected file contents detected in", inPath)

                    outFile = open(outPath,'w')
                    polyId += 1

                elif len(line) == 0:
                    writePolyToFile(outFile, polygon, polyId)
                    polygon = []
                    polyId += 1

                    polygon.append(line.replace(',',' '))

        writePolyToFile(outFile, polygon, polyId) #append the last polygon after EOF
        print('Conversion to WKT OK for', inPath)

        print('WARNING: conversion to WKT failed for', inPath)

def iterateFiles():
    csvFiles = [each for each in os.listdir(csvFolder) if each.endswith('.csv')]

    for file in csvFiles:
        inPath = os.path.join(csvFolder, file)
        newName = "WKT_" + file
        outPath = os.path.join(csvFolder, newName)
        makePolys(inPath, outPath)

if __name__ == "__main__":

This is a very simple Python script which will iterate over a folder of CSV files as a batch process. It does a crude logic check that the first line of the file follows your format of 'x,y'. It then collects all the points into an array until it finds a blank line and recasts the array as a WKT string which it writes to an output file of the same name as the original but prefixed by 'WKT_' (so 'firstFile.csv' exports to 'WKT_firstFile.csv' preserving the original file).

Change the following line csvFolder = r"C:\myFolder\mySubFolder" to point to a folder containing all the CSV files your want to convert (make sure you keep that 'r' at the start of the path!).

You can now open the resulting files using the normal 'Add vector layer' dialog instead of the 'Add delimited text' dialog.

Here is a sample of my test:





1;POLYGON((10 10,20 20,20 30,10 10))
2;POLYGON((5 5,5 6,6 5,5 5))
3;POLYGON((15 15,30 30,30 40,15 15))
  • Thanks, MappaGnosis, I'll give it a try. I know almost no Python, but should be able to make it work.
    – johnsankey
    Commented Apr 24, 2013 at 12:06
  • It's my experience that to render geometries all you need is a csv file with a WKT field containing any valid WKT geometries, and no extraneous rows containing invalid geometries or nulls. For example, a csv file with a column named "WKT" and a field value of: "MULTILINESTRING ((10 10, 20 20, 10 40), (40 40, 30 30, 40 20, 30 10))" should display a linestring. The syntax has to be correct. QGIS 1.8 will ask for the coordinate system.
    – user12711
    Commented Apr 24, 2013 at 13:37
  • @User12711 the OP can't simply rename a field 'WKT' but must recast all the data into WKT before it can be used. The current format of the data is a simple list of x,y pairs with blank lines to denote a new polygon and no polygon Ids. This is far from satisfying the requirements of WKT. So while QGIS can read WKT, it needs some work to reformat the data first. Commented Apr 24, 2013 at 14:05
  • @MappaGnosis Hi, I never specified " the OP can't simply rename a field 'WKT' " I simply stated WKT field be present and populated with valid entries, and provided a link to wikipedia with the proper syntax for the geometries.
    – user12711
    Commented Apr 24, 2013 at 14:30
  • Before I started working with Python, I tried a manual test, a file containing polygon((.805 10.273,1.535 10.273,1.535 10.363,1.96 10.222)) With add vector file, I get "is not a valid or recognized data source". I can't get delimited text to show anything sensible either. I assume there's some header or similar I'm missing?
    – johnsankey
    Commented Apr 24, 2013 at 14:50

QGIS will load a spatial CSV just fine if it includes a field named "WKT" with the coordinates. Make sure the geometry follows the proper syntax http://en.wikipedia.org/wiki/Well-known_text.

Open your CSV file in a spreadsheet first to make certain there are no rows with Nulls or Blanks in the WKT field, and that every representation is a valid WKT, else delete that row.

simply load that .CSV file into QGIS 1.8 as a file, changing to type from shapefile etc. to CSV format

  • WKT is a long way from CSV - it would take an awfully long time to textedit 10k or so polygons to that format.
    – johnsankey
    Commented Apr 23, 2013 at 18:49
  • 1
    WKT (Well Known Text) isn't a file format, it's a format specifying a text based geometry. It certainly can be included within a CSV file, as a column of geometries in lieu of binary, and loaded into QGIS. I've done that.
    – user12711
    Commented Apr 24, 2013 at 13:14
  • 1
    You are not wrong... but the original data is a long way from being compliant with the specifications for WKT (viz - there is no WKT field), which is why your answer doesn't provide a solution (note the format described by the OP in response to my original question). My solution is a method of HOW to recast the data into WKT contained within a CSV. We could write some similar code to create a shapefile, it doesn't matter. The important thing is to reformat the data to be more "geographic". Commented Apr 24, 2013 at 14:11
  • @MappaGnosis Hi, I had clicked a Thumbs-up on your post to agree with it. My post simply specified that you use a WKT with valid entries, and then linked to Wikipedia with the definitions for the proper syntax to use in the column to make it valid. A lot of GIS users aren't aware that a CSV file can be used spatially, and then when they find out they aren't sure of how to implement it. I hope in the future that users will find that post helpful if they need to know how to create a GIS enabled CSV with geometries, and do a search on stackexchange.
    – user12711
    Commented Apr 24, 2013 at 14:23

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.