Select to view content in your preferred language

addlayer error!!

8890
22
08-06-2014 01:58 PM
LefterisKoumis
Frequent Contributor

I am new in python but this script should be easy to process. Instead I get:

arcpy.mapping.AddLayer(myDF,addlayer) NameError: name 'addlayer' is not defined

Here is the script:

import arcpy

thisMap = arcpy.mapping.MapDocument("CURRENT")

myDF = arcpy.mapping.ListDataFrames(thisMap)[0]

myLayers = arcpy.mapping.ListLayers(myDF)

for lyr in myLayers:

    if lyr.name == "CNNDB":

      addlayer = arcpy.mapping.Layer(lyr)

      print "got it"

arcpy.mapping.AddLayer(myDF,addlayer,"AUTO ARRANGE")

arcpy.CreateFolder_management(r"D:\GIS_data", "test")

arcpy.mapping.ExportToPDF(thisMap, r"D:\GIS_data\test\Project.pdf",300,"BEST",layers_attributes="LAYERS_AND_ATTRIBUTES")

#Do you see any errors?  Thank you.

0 Kudos
22 Replies
LefterisKoumis
Frequent Contributor

So, Luke, in my case  why the addlayer is undefined?

0 Kudos
XanderBakker
Esri Esteemed Contributor

That was explained by Luke earlier. In case it finds the layer with the name you specify you try to pass a layer object to the arcpy.mapping.Layer() that expects a string...

DanPatterson_Retired
MVP Emeritus

Is it possible that making a conventional toolbox (*.tbx) would have been a simpler approach?  I am just wondering why you have opted for this level of complexity when the tasks to perform seem relatively simple?  I am a curious as to what routes people take (ie ArcObjects, versus Addins vs tbx's versus standalone applications)

CARMETSandrine
Deactivated User

I understand your point, but I have more than 500 shapefiles with their labels (contour field) to create and then export all of them to CAD. Even if I create a toolbox, it takes to much time to do:

     - Clip Raster

     - Create Contour

     - Convert Labels to Annotations

     - Export to CAD

So I thought by creating a tool that compile all this functions will saved me time and then share it to my colleagues.

And it was a good opportunity to see how python works for future tasks.

0 Kudos
XanderBakker
Esri Esteemed Contributor

I agree with Dan, what you describe can be done with a conventional toolbox that holds a simple python script to perform the analysis.

0 Kudos
XanderBakker
Esri Esteemed Contributor

It seems this is also suggested to you at your thread: How to use defined parameters in a tool ?

I think it's better to continue this discussion at your own thread

0 Kudos
CARMETSandrine
Deactivated User

OK I think I just can not fully explain myself.

I will just continue to do it manually.

Thanks for every thing.

0 Kudos
CARMETSandrine
Deactivated User

OK ,

but for the"convert labels to annotation", is there a simple way  to ingrate it into a python script ? (because that's what makes me lose time, imagine I have more than 500 shapefiles to create and then convert all the labels to annotation to export all of them to CAD).

That's why I was asking you to understand the "add layer" , to after activate the labels and then convert into annotation, all of this in the same python script.

(Sorry if my english is not good, I'm french)

Thanks.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Don't worry about your English, it's fine.

The help page with examples for the tool you need can be found here:

Tiled Labels To Annotation (Cartography)

Kind regards, Xander

0 Kudos
JamesCrandall
MVP Alum

I just whipped this together, and it doesn't fully meet your requirments but it does the first half of your task.  You will need to get the rest implemented (from converting to anno onwards).  Also, I ran into little problems that mostly got solved by NOT using .shp files and just setup File Geodatabases --- which is not a bad plan anyway.  Especially dealing with rasters for some reason, processing them outside of .gdb's is kwirky.


ras = r'H:\Documents\inputraster.tif'
ras_ws = r'H:\Documents\rasters\clippedrasters.gdb'
ras_countours = r'H:\Documents\rasters\contours.gdb'
shp_ws = r'H:\Documents\shps'
arcpy.env.workspace = shp_ws
rascount = 1
for fc in arcpy.ListFeatureClasses():
    print fc
    #create the clip rasters
    arcpy.Clip_management(ras,"#", ras_ws + "\\ras" + str(rascount),fc, "0", "ClippingGeometry")
    rascount = rascount + 1


#create the contours from the clipped rasters
### see if spatial analyst extension is available for use
availability = arcpy.CheckExtension("Spatial")
if availability == "Available":
     arcpy.CheckOutExtension("Spatial")
     arcpy.AddMessage("SA Ext checked out")
else:
     arcpy.AddError("%s extension is not available (%s)"%("Spatial Analyst Extension",availability))
     arcpy.AddError("Please ask someone who has it checked out but not using to turn off the extension")
     


arcpy.env.workspace = ras_ws
contourcount = 1
for ras in arcpy.ListRasters():
    outname = ras_countours + "\\contour" + str(contourcount)
    print "ountname: " + outname
    arcpy.sa.Contour(ras, outname, 200, 0)
    contourcount = contourcount + 1


#check the extension back in
arcpy.CheckInExtension("Spatial")