Thursday, 3 November 2016

python - Convertion of multiple csv automatically to shp


I have more than two hundreds similar csv files with columns (x, y, value1, value2, objectName). I need to convert them automatically to shp files with x and y columns as the coordinates. Could you please advice how to do it quickly using Python scripting?


Thanx!



Answer



You would need to iterate through every shapefile in your folder with ListFiles. You might use Make XY Event Layer GP tool to create an event layer with the point shape (XY). Then you could use Copy Features GP tool.


Something like:


import arcpy,os
shpworkspace = r"C:\GIS"

arcpy.env.workspace = shpworkspace
arcpy.env.overwriteOutput = True

csvlist = arcpy.ListFiles("*.csv")

for csvfile in csvlist:
outlayer = "CSVEventLayer"
spatialreference = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
arcpy.MakeXYEventLayer_management(csvfile,"POINT_X","POINT_Y",outlayer,spatialreference,"#")


shpfile = os.path.splitext(csvfile)[0]
arcpy.CopyFeatures_management(outlayer,shpfile)
del outlayer

Here is the .csv sample file which you can use for getting the script to work with and then use your own data later on.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...