import arcpy, os from arcpy import env env.overwriteOutput = True # -----------------------------------------------------> IF YOU WANT THE OUTPUT TO BE OVERWRITED # ------- DEFINE VARIABLES directory = r'D:\GAIA\AG\script' # -------------------------------------------------> WHERE MY STUFF IS mxd = arcpy.mapping.MapDocument(os.path.join(directory, r'toto.mxd')) # ------------> MXD REFERENCE tableCSV = os.path.join(directory, r'test.csv') # ----------------------------------> TABLE REFERENCE temp_Layer = os.path.join(directory,r'toto.lyr') # ---------------------------------> NAME FOR THE TEMPORARY LAYER TO BE GENERATED FROM 'MakeXYEventLayer_management' temp_Layer_toShapefile = os.path.join(directory,r'toto.shp') # ---------------------> NAME FOR THE SHAPEFILE TO SAVE # -------- CREATE XY EVENT TABLE AND SAVE IT AS A SHAPEFILE : arcpy.MakeXYEventLayer_management(tableCSV, "X_COORD", "Y_COORD", temp_Layer) # ----> TABLE TO XY Event LAYER (temporary Layer) (inputTable, ColumnNameForX, ColumnNameForY, LayerToSave) arcpy.CopyFeatures_management(temp_Layer, temp_Layer_toShapefile) # ----------------> COPY TEMP LAYER TO PROPER SHAPEFILE # -------- ADD SHAPEFILE TO THE MAP : layer_ToAdd = arcpy.mapping.Layer(temp_Layer_toShapefile) # ------------------------> CAST SHAPEFILE AS LAYER OBJECT df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] # ------------------------------> DATAFRAME REFERENCE IN MXD (DEFAULT = "Layers") arcpy.mapping.AddLayer(df, layer_ToAdd, "TOP") # -----------------------------------> ADD LAYER TO MXD # -------- SAVE, CLEAN UP, EXIT mxd.save() # -----------------------------------------------------------------------> SAVE MXD del mxd del temp_Layer_toShapefile # --------------------------------------------------------------------------> DELETE MEMORY REFERENCE TO THE MXD (kill lock) print 'JOB DONE'
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.