3

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?

6
  • Do you have any python ability? This wouldn't be too difficult in either QGIS or ArcGIS in python. Commented Sep 2, 2018 at 23:02
  • Unfortunately I have no idea about the source of data :/
    – geocoder
    Commented Sep 2, 2018 at 23:05
  • @Luke, they have no holes, they are singleparts. Thanks for Your interaction
    – geocoder
    Commented Sep 2, 2018 at 23:12
  • 1
    @AndreSilva, the software to use doesn't matter, i juste need to convert the CSV to polygon/shapfile
    – geocoder
    Commented Sep 2, 2018 at 23:13
  • Separated by semicolons
    – geocoder
    Commented Sep 2, 2018 at 23:31

4 Answers 4

5

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

3

You can convert your coordinates to Well-Known Text (WKT) if you open your CSV in Excel and use this formula:

="POLYGON(("&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B2,","," "),"(",""),")",""),":",",")&"))"

Where B2 is your existing coordinates.

Then copy/paste values back in so your new CSV has the formula's output, and using QGIS (there is probably a way using ArcGIS too), import it in using the "Create a Layer from a Delimited Text File" option:

enter image description here

enter image description here

enter image description here

Then you can right-click the layer in the Layers Panel, select "Save As", and save it as a shapefile (you can also change the coordinate system if you want).enter image description here

1

You can easily achieve this with FME Desktop if you have a license or you can use a fully functional trial license to test it out. Below is a screenshot of the workspace. You need to save the data in csv/text file as per the attached file. The workspace was using FME Desktop 2018.0

FME Workspace Converting the data to Polygon

The files can be downloaded from here:

https://www.dropbox.com/s/znzxnx2qyg5q1rq/csv22none.zip?dl=0

1
import shapefile
import csv
import ast
with open(r"Sample_Data.csv", encoding="utf8") as csvfile:
    # dict create
    dictReader=csv.DictReader(csvfile)
    # create writable shapefile
    w = shapefile.Writer('Sample_Data.shp')
    # create attribute
    w.field('id', 'C')
    w.field('nieghborho', 'C')
    w.field('plan_no', 'C')
    # row from dict
    for row in dictReader:
        # write row attribute
        w.record(row['id'], row['nieghborhood'], row['plan_no'])
        # write geom
        w.poly([ast.literal_eval(row['geom'])])
    # create prj file
    prj = open("Sample_Data.prj", "w")
    epsg = 'GEOGCS["WGS 84",'
    epsg += 'DATUM["WGS_1984",'
    epsg += 'SPHEROID["WGS 84",6378137,298.257223563]]'
    epsg += ',PRIMEM["Greenwich",0],'
    epsg += 'UNIT["degree",0.0174532925199433]]'
    # save prj file
    prj.write(epsg)
    # close above shapefile
    w.close()

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.