2

This question already has an answer here:

I am trying this simple task to add a feature class to an mxd (ArcGIS 10.3) but getting few errors. Here are two options I tried.

1) Preferred option - adding feature class itself into a active dataframe of the current mxd. I used the script below:

import arcpy

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

DF=mxd.activeDataFrame

arcpy.mapping.AddLayer(DF,"C:\Work\GP1500\Tests\Input.gdb\Poly1","TOP")

The addLayer line generates the error below ...... assert isinstance(add_layer, Layer) AssertionError

2) Create a layer from the Feature class and add the layer using the script *****

import arcpy

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

DF=mxd.activeDataFrame

FC="C:\Test.gdb\Test_Poly1"

arcpy.MakeFeatureLayer_management(FC,"TestPolyLayer")

inputlayer =arcpy.mapping.layer(TestPolyLayer)

arcpy.mapping.AddLayer(DF,inputlayer,"TOP")

******It generates the error while executing the second last line i.e. to define input layer with following message AttributeError: 'module' object has no attribute 'layer'

Note: the second option i used is after I saw Nick's reply in How to add feature class to MXD with ArcPy (Python)?

marked as duplicate by PolyGeo Jun 10 '15 at 8:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • That should be arcpy.mapping.AddLayer(DF,"TestPolyLayer","TOP") in the second example. Don't forget to save the MXD or the changes will be lost. – Michael Stimson Jun 10 '15 at 4:42
2

You need to first create the Layer object, and then use AddLayer to add it:

import arcpy

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

df = mxd.activeDataFrame

layer = arcpy.mapping.Layer("C:\Work\GP1500\Tests\Input.gdb\Poly1")

arcpy.mapping.AddLayer(df, layer, "TOP")
  • Thanks a lot @ian . That works and good to see a non complicated solution :) – Dnstuladhar Jun 11 '15 at 5:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.