Skip to content

Instantly share code, notes, and snippets.

@colek42
Created January 10, 2017 02:31
Show Gist options
  • Save colek42/38c47059f4bfc6e3a431b2b5e67cc649 to your computer and use it in GitHub Desktop.
Save colek42/38c47059f4bfc6e3a431b2b5e67cc649 to your computer and use it in GitHub Desktop.
This script converts a folder of CSV to SHP files
#Script to take directory of csv files and turn them into a shp file using ArcGIS 10 arcpy
import os
import csv
from datetime import datetime
DIRECTORY = 'DIRECTORYHERE'
OUTSHP = 'OUTPUTHERE'
OUTDIR = 'OUTDIR'
LATFIELD = 'LATFIELD'
LONFIELD = 'LONFIELD'
DATEFIELD = 'DATEFIELD' #none for no date field
DELIMETER = '\t'
KEEPCOLUMNS = []
arcpy.CreateFeatureclass_management(OUTDIR, OUTSHP, "POINT", "", "", "", "")
cursor = arcpy.da.InsertCursor(OUTSHP)
for fn in os.listdir(directory):
if os.path.isfile(fn):
with open(fn, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=delimiter)
headers = reader.next()
keepIdx = []
latIdx = None
lonIdx = None
dateIndex = None
for idx, header in enumerate(headers):
if header in KEEPCOLUMNS:
keepIdx.append(idx)
arcpy.AddField_management(OUTSHP, header, "TEXT","","", 254)
if dateIndex != None:
if header == DATEFIELD:
dateIndex = idx
arcpy.AddField_management(OUTSHP, 'DATE', "TEXT","","", 20)
if header = LATFIELD:
latIdx = idx
if header = LONFIELD:
lonIdx = idx
for row in reader:
#Handle Time
if dateIndex != None:
#loose the end of the iso string
dateString = [dateIndex][:19]
point = arcpy.CreateObject("Point")
point.X, point.Y = float(row[lonIdx]), float(row[latIdx])
arcrow = cursor.newRow()
for i in keepIdx:
arcrow.setValue(headers[i], row[i])
if dateIndex != None:
arcrow.setValue('DATE') = dateString
arcrow.setValue('Shape') = point
cursor.insertRow(arcrow)
del arcrow
del cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment