MakeXYEventLayer No Errors, but No Results

363
3
01-04-2011 10:07 AM
JohnStephens
Occasional Contributor
I am writing a python script that batch processes some creation of XY events based on csv tables.  The script is set up as a tool where the user provides 3 fields (the first two are needed to find the specific csv file and the second is the path to all of the csv data).  Anyway, when I run the tool it processes with no errors, but the XY Event Layer is not added to the proper data frame or the map at all.  However, when I run snippets of the code in the ArcMap python window it works and the event layer is added to the map appropriately. 

Here is the code, I tried to simplify it a bit:

import arcpy, sys
from arcpy import env

str1 = sys.argv[1]
str2 = sys.argv[2]
filesPath = sys.argv[3]
env.workspace = filesPath

# Update file path
filesPath +=  str1 + str2

# Set mxd file and get a list of data frames
mxd = arcpy.mapping.MapDocument("Current")
dfList = arcpy.mapping.ListDataFrames(mxd)

try:
    # Loops through data frame and adds XY events for csv files
    for df in dfList:
        if df.name == "2010":
            mxd.activeView = df
            fileTemp = filesPath
            fileTemp += df.name + ".csv"
            outLayer = str1 + str2 + df.name + "_XYEvents"
            arcpy.MakeXYEventLayer_management('"' + fileTemp + '"', "X", "Y", outLayer)

except:
    print arcpy.GetMessages()


The weird thing is, if I try to run it again it tells me the file name already exists.  So it appears that it created the event layer, but it was just not added to the map.  I tried to use AddLayer, but python did not like that.
Tags (2)
0 Kudos
3 Replies
CharlesMcCready
New Contributor
I know this is an old thread but did you ever solve this.  I have the same issue in 9.3.1 and can't for the life of me figure it out.

Thanks

Chuck
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Here is an example on how I was able to add an XY event layer to an MXD (using arcpy for ArcGIS 10):

import arcpy
from arcpy import env
from arcpy import mapping
env.workspace = r"C:\TEMP\Python\test.gdb"
env.overwriteOutput = 1

table = "XY"

#Make XY event layer
arcpy.MakeXYEventLayer_management(table, "X", "Y", "XY_event")

#Save XY event to a layer file
arcpy.SaveToLayerFile_management("XY_event", r"C:\temp\Python\XY_event.lyr")

#Add layer file to MXD
mxd = mapping.MapDocument(r"C:\temp\python\Airports.mxd")
for df in mapping.ListDataFrames(mxd):
    lyr = mapping.Layer(r"C:\temp\python\XY_event.lyr")
    mapping.AddLayer(df, lyr)
    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()

mxd.save()  

del lyr, mxd, df
0 Kudos
curtvprice
MVP Esteemed Contributor
The arcpy example just posted by Jake S seems like it should address your original issue.
but I think these extra quotes may cause trouble:

            arcpy.MakeXYEventLayer_management('"' + fileTemp + '"', "X", "Y", outLayer)

instead:
            arcpy.MakeXYEventLayer_management(fileTemp, "X", "Y", outLayer)
0 Kudos