Select to view content in your preferred language

addlayer error!!

7971
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
XanderBakker
Esri Esteemed Contributor

Not sure what you are trying to do with this code. You loop through the layers in an MXD and check if the name equals "CNNDB". If that's the case you try to set a variable "addlayer". I don't think you should try to create a layer from a layer object.

Next you try to add the layer to the dataframe (where the layer already exists), although the code does not run that far. And you intent to export the map to PDF. Is the loop through your layer really useful?

Kind regards, Xander

0 Kudos
LefterisKoumis
Frequent Contributor

Ok, I modified my code....but I still getting an error.

What I am trying to do  is to loop through the layers of the current map and if a specific layer is found, then it will be copied onto a new map and then print the pdf of the new map.

Does it make sense?

import arcpy

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

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

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

newmap = arcpy.mapping.MapDocument(r"D:\GIS_data\test\new.mxd")

newdf = arcpy.mapping.ListDataFrames(newmap)[0]

myLayers = arcpy.mapping.ListLayers(myDF)

for lyr in myLayers:

    if lyr.name == "CNNDB":

     addlayer = arcpy.mapping.Layer(lyr)

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

arcpy.RefreshTOC

arcpy.RefreshActiveView

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

0 Kudos
XanderBakker
Esri Esteemed Contributor

I see you are trying to create a new MXD through python by just accessing a non existing mxd file in a just created folder. That will not work. See the blog post below with an example of using ArcObjects inside Python for that purpose.

Create a new MXD through Python.

This is rather complex. You might want to consider creating an empty mxd file and copy that file for creating a new MXD.

Kind regards, Xander

Luke_Pinner
MVP Regular Contributor

Is this your full code? Do you have any try/except clauses in there that might be hiding earlier exceptions?

I can't see how you would even get to `arcpy.mapping.AddLayer(newdf,addlayer,"AUTO ARRANGE")` as the `addlayer = arcpy.mapping.Layer(lyr)` would raise an exception anyway as `lyr` is a layer object, not a string.

Also, the AddLayer method will raise an exception as "AUTO ARRANGE" is an invalid value, it must be one of ['TOP', 'AUTO_ARRANGE', 'BOTTOM'] Note the "_" underscore in "AUTO_ARRANGE".

Here is some code to get you started:

import arcpy

thisMap = arcpy.mapping.MapDocument("CURRENT")
myDF = arcpy.mapping.ListDataFrames(thisMap)[0]
if not arcpy.Exists(r"D:\GIS_data\test"):
    arcpy.CreateFolder_management(r"D:\GIS_data", "test")
templatemap = arcpy.mapping.MapDocument(r"D:\GIS_data\template.mxd") #an existing empty mxd
templatedf = arcpy.mapping.ListDataFrames(templatemap)[0]

myLayers = arcpy.mapping.ListLayers(myDF,"CNNDB")
if myLayers:
    arcpy.mapping.AddLayer(templatedf,myLayers[0],"AUTO_ARRANGE")
    templatemap.saveACopy(r"D:\GIS_data\test\new.mxd")
    newmap=arcpy.mapping.MapDocument(r"D:\GIS_data\test\new.mxd")




Notes:

  • arcpy.mapping.MapDocument requires an existing MXD, it can not create a new one. Use an empty mxd as a template.
  • You can pass a layer name (or partial name + wildcard *) to the arcpy.mapping.ListLayers method.
XanderBakker
Esri Esteemed Contributor

Thanx Luke, for adding an example and the good explanation. This saves me some time

0 Kudos
JamesCrandall
MVP Frequent Contributor

This is untested and I have not fully vetted this, but I think you can try to reference the path name instead of trying to add the layer from another .mxd.  You can use arcpy.Describe to get the fully qualified path of the layer:

import arcpy

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

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

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

newmap = arcpy.mapping.MapDocument(r"D:\GIS_data\test\new.mxd")

newdf = arcpy.mapping.ListDataFrames(newmap)[0]

myLayers = arcpy.mapping.ListLayers(myDF)

for lyr in myLayers:

    if lyr.name == "CNNDB":

      #set the path of the layer

      desc = arcpy.Describe(lyr)

      if hasattr(desc, "catalogPath"):

          addlayer = desc.catalogPath

#make a feaure layer to add to the new map

arcpy.MakeFeatureLayer_management(addlayer, "layernametocallit")

arcpy.mapping.AddLayer(newdf,"layernametocallit","AUTO ARRANGE")

arcpy.RefreshTOC

arcpy.RefreshActiveView

0 Kudos
9628807811
New Contributor

The addlayer variable is defined inside the for loop scope so it's not seen outside the loop, try to declare it before your loop then assign your matched layer to it.

0 Kudos
LefterisKoumis
Frequent Contributor

Sorry I am novice in Python. How do you declare a variable that if no layers are defined yet? Can I a declare without point at a layer?

0 Kudos
Luke_Pinner
MVP Regular Contributor

@Ahmed that is not correct.  For loops do not have their own namespace, so the variable is in the local scope. Consider this:

for i in range(2):
    loopvar = i * 2

print 'loopvar was defined in the loop and its value is {0}'.format(loopvar)



The above code snippet works fine and prints:

loopvar was defined in the loop and its value is 2