Skip to content

Instantly share code, notes, and snippets.

@bmoregeo
Created October 15, 2014 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmoregeo/49d9d85831dd95ba1dba to your computer and use it in GitHub Desktop.
Save bmoregeo/49d9d85831dd95ba1dba to your computer and use it in GitHub Desktop.
Batch convert CSV to Shapefile with arcpy
import arcpy
import os
def csv2shp(csv_file, field_x, field_y, shape_file):
"""
Convert a CSV file with x/y fields into a Shapefile
"""
table_view = 'csv_file_as_view'
layer = 'xy_event_layer'
arcpy.MakeTableView_management(csv_file,table_view)
arcpy.MakeXYEventLayer_management(table_view,field_x,field_y,layer)
arcpy.CopyFeatures_management(layer, shape_file)
arcpy.Delete_management(table_view)
arcpy.Delete_management(layer)
if __name__ == '__main__':
input_files = [
r'C:\Users\bmoregeo\Desktop\csvs\test1.csv',
r'C:\Users\bmoregeo\Desktop\csvs\test2.csv'
]
output_location = r'C:\Users\bmoregeo\Desktop\csvs\output'
field_x = 'x'
field_y = 'y'
for input_file in input_files:
# Create an output file that takes the name of the input file "test.csv" and
# changes the file extension to shp. Then puts it into the output location specified above
output_file = os.path.join(output_location, os.path.splitext(os.path.basename(input_file))[0] + '.shp')
print "Create %s" % output_file
csv2shp(input_file, field_x, field_y, output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment