Select to view content in your preferred language

Add Feature Class to MXD

2541
2
Jump to solution
06-19-2012 02:21 PM
BondHarper
Frequent Contributor
Hi all. I have feature classes in numerous geodatabases that I want to add to some MXDs using a script. I can set up the loop, but for some reason I can't find how to add a feature class to a MXD using Python. It seems like it should be simple, so maybe I am overlooking something. (I found how to add a layer, but I don't have .lyr files)

Thanks! 🙂
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
You have to make feature layer from your feature class first.
Here's an example (feature layer 'Roads' should be added to first data frame of map document):
import arcpy inFC = r"C:\tmp\Test_CopyAttr.gdb\RoadsFC" arcpy.MakeFeatureLayer_management(inFC, "Roads")  mxd = arcpy.mapping.MapDocument(r"C:\tmp\MapProjects\Untitled1.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrLayer = arcpy.mapping.Layer("Roads") arcpy.mapping.AddLayer(df, lyrLayer, "AUTO_ARRANGE") mxd.save()  del mxd

View solution in original post

0 Kudos
2 Replies
MarcinGasior
Frequent Contributor
You have to make feature layer from your feature class first.
Here's an example (feature layer 'Roads' should be added to first data frame of map document):
import arcpy inFC = r"C:\tmp\Test_CopyAttr.gdb\RoadsFC" arcpy.MakeFeatureLayer_management(inFC, "Roads")  mxd = arcpy.mapping.MapDocument(r"C:\tmp\MapProjects\Untitled1.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrLayer = arcpy.mapping.Layer("Roads") arcpy.mapping.AddLayer(df, lyrLayer, "AUTO_ARRANGE") mxd.save()  del mxd
0 Kudos
BondHarper
Frequent Contributor
Thank you, Marcin. You had the step I was missing to automate it.
0 Kudos