[GIS] Arcpy module to read csv files and create polyline shapefiles

arcpycsvfunctionline

I am creating a module in arcpy that will loop through a folder with csv files in it. I have both of them written, and they work fine on my computer. However when I send them to anyone else they won't run. I do not have any hard coded paths in them and cannot figure out the problem. The error says 'IOError: [Errno 2] no such file or directory:u'COTS_234AR.csv'' in regards to the line

#Open File
vehicle = open(input, "r")

Am I overlooking something?

Code that calls module:

#Import Modules
import arcpy
from arcpy import env
import sys 
import csv
import Script1

#environment settings
arcpy.env.overwriteOutput = True

#set workspace parameter: ONLY USER DEFINED INPUT-
#SHOULD BE IN FORMAT SIMILAR TO "M:\GIS\FinalExam"
filename = arcpy.env.workspace = sys.argv[1]

#create list of CSV files in folder
csvs = arcpy.ListFiles("*.csv")

#Create for loop of CSV files
try:
   for csv in csvs:
     #set parameters
        input = csv
        desc = arcpy.Describe(csv)
        output = (desc.basename + ".shp")
        #call in module
        Script1.scriptOne(input, filename, output)

except:
   print "There has been an error processing the scriptOne module."#
#retrieve all messages 
print(arcpy.GetMessages())

This is the actual module:

def scriptOne(input, filename, output):
    #import modules
    import csv
    import arcpy
    from arcpy import env

    #environment settings
    env.overwriteOutput = True

    #Open File
    vehicle = open(input, "r")

    #create csvReader object to process the header
    csvReader = csv.reader(vehicle)
    header = csvReader.next()
    latIndex = header.index("LAT")
    lonIndex = header.index("LON")
    out = arcpy.CreateFeatureclass_management(filename +"\\", output, "POLYLINE", "", "", "", 4326) 
    spatialRef =arcpy.Describe(out).spatialReference

    # Write the array to the feature class as a polyline feature
    vertexArray = arcpy.Array()

    # Loop through the lines in the file and get each coordinate
    for row in csvReader:
        # Get the lat/lon values of the current GPS reading
        lat = row[latIndex]
        lon = row[lonIndex]
        # Make a point from the coordinate and add it to the array
        vertex = arcpy.Point(lon,lat)
        vertexArray.add(vertex)

        #create insert cursor to make polyline shapefile
    with arcpy.da.InsertCursor(output, ("SHAPE@",)) as cursor:
        polyline = arcpy.Polyline(vertexArray, spatialRef)
        cursor.insertRow((polyline,))  

Best Answer

As csv isn't an arcpy module, it can't see arcpy.env.workspace to know your workspace path. Because of that it's looking for COTS_234AR.csv and not M:\GIS\FinalExam\COTS_234AR.csv (or whatever your workspace path is).

You need to pass input as a full path to the csv, not just the csv name.

import os # Add os to your imports

for csv in csvs:
    #set parameters
    input = os.path.join(filename, csv) # Full path to CSV file
    desc = arcpy.Describe(csv)
    output = (desc.basename + ".shp")
    #call in module
    Script1.scriptOne(input, filename, output)

Alternatively you could use arcpy.TableToTable_conversion to import your csv file as a temp table that an arcpy.da.SearchCursor() can access and then process from that to create your lines.

Related Question