Created
October 15, 2014 13:50
-
-
Save bmoregeo/49d9d85831dd95ba1dba to your computer and use it in GitHub Desktop.
Batch convert CSV to Shapefile with arcpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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