error creating list with append

02-21-2017 02:26 PM
Occasional Contributor II

This should be so easy.  I have a script that runs perfectly from the ArcMap python window, but errors out when run from a script tool or IDLE.  It errors on the AddJoin because it says that finalParc does not exist.  I believe it is because the Intersect_analysis is not running correctly.  I don't know if the inFeatures list is not being created correctly or it is having trouble with the names in the list.  The point of my script is to determine if the main layer - parcBuff - is overlapped by any of the other layers.  If it is, then run an intersect_analysis on those overlapping layers to create one final layer - finalParc - that has the attributes of all those layers in it.

Here is part of the code that should accomplish that...and I need it to run through a Tool.....

#Create a cursor to go through all the layers. If layer does not intersect, apply N/A, otherwise, intersect....
#Make Layer Views......
arcpy.MakeFeatureLayer_management(parcBuff, "parcBuff")
arcpy.MakeFeatureLayer_management(zoning, "zoning")
arcpy.MakeFeatureLayer_management(fldplain, "fldplain")
arcpy.MakeFeatureLayer_management(compplan, "compplan")
arcpy.MakeFeatureLayer_management(fire, "fire")
arcpy.MakeFeatureLayer_management(school, "school")
arcpy.MakeFeatureLayer_management(irrigate, "irrigate")
arcpy.MakeFeatureLayer_management(ressoils, "ressoils")
arcpy.MakeFeatureLayer_management(soils, "soils")
arcpy.MakeFeatureLayer_management(uga, "uga")

#Cursor layers......
layerList = {"zoning", "fldplain", "compplan", "fire", "school", "irrigate", "ressoils", "soils", "uga"}
inFeatures = ["parcBuff"]
for theLayer in layerList:
     arcpy.SelectLayerByLocation_management("parcBuff", "INTERSECT", theLayer)
     selCount = int(arcpy.GetCount_management("parcBuff").getOutput(0))
     print selCount
     if (selCount != 0):
         inFeatures.append("%s" % theLayer)

#inFeatures = ["parcBuff", "zoning", "fldplain", "compplan", "uga", "soils", "ressoils", "irrigate", "school", "fire"]
arcpy.Intersect_analysis(inFeatures, finalParc, "NO_FID", "", "")

#Join the Party table to the parcel with all the information from the layers.....
#Set the variables....
layerName = "finalParc"
joinField = "ASSESSOR_N"
arcpy.AddJoin_management(layerName, joinField, party, joinField) 

Hopefully, this is enough information.  Thank you for your help!

0 Kudos
2 Replies
MVP Legendary Contributor

When you run a script outside of Arc*, it knows nothing about your working environment, you need to specify your working folder where your layers are located using arcpy.env = .....

I don't know if it matters, but your list isn't...

layerList = {"zoning", "fldplain", "compplan", "fire", "school", "irrigate", "ressoils", "soils", "uga"}

Out[7]: set

And cycling through a set can cause some issues... like the order is out of whack

for i in layerList: print(i)
Occasional Contributor II

For good practice, I did put in arcpy.env = "D:\\....." at the beginning of my script.  I did have the variables zoning, etc hardcoded at the beginning.  

I found that I was missing this line....

layerName = arcpy.MakeFeatureLayer_management(finalParc, "finalParc")

before my AddJoin_management.  Think I have been looking at it too long.  Thanks for your help.

0 Kudos