Convert CSV Polygon Coordinates to Shapefile Feature Class

convertcsvpolygonshapefile

I have a CSV table as following :

ID;POLYGON
1;(-7.59443,33.55933):(-7.59416,33.55937):(-7.59414,33.55922)
2;(-7.58361,33.56655):(-7.58148,33.56655):(-7.58148,33.56486):(-7.58361,33.56486)

The coordinate system used is WGS84. Polygons have no holes and they are all single parts.

I need to convert it to a Polygon feature class/shapefile. I have access to QGIS or ArcGIS, but welcome answers in other software as well.

Any ideas on how to proceed?

Best Answer

That is a format I'm not familiar with, possibly a custom format. Below is an ArcGIS arcpy solution that reads in your CSV (assuming only 2 columns, ID and POLYGON!).

import csv
import arcpy

csvfile = 'C:/Temp/test.csv'
outpath, outshp = 'C:/Temp', 'test.shp'

outshp = arcpy.CreateFeatureclass_management (
    outpath, outshp, geometry_type='POLYGON',
    spatial_reference=arcpy.SpatialReference(4326))

arcpy.AddField_management(outshp, "ID", "LONG")

with open(csvfile) as csvfile, arcpy.da.InsertCursor(outshp, ['ID', 'SHAPE@']) as cursor:
    csvreader = csv.reader(csvfile, delimiter=';')

    #Skip header row
    next(csvreader, None)

    for id, coords in csvreader:
        coords = [eval(xy) for xy in coords.split(':')]
        # could also use the following if you don't trust `eval`
        # coords = [map(float, xy[1:-1].split(',')) for xy in coords.split(':')]
        print(id, coords)
        cursor.insertRow([id, coords])

Result:

enter image description here

Related Question