How to add layer in mxd and save it?

5595
8
Jump to solution
05-29-2015 12:59 PM
waqarahmad
New Contributor III

I have a layer Test generated by arcpy.MakeFeatureLayer_management and Dissolved by arcpy.Dissolve_management ..

I know that this layer is in  .gdb  data source..

How can i add this layer to my .MXD and save that .MXD using PYTHON?

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

You need to create a Layer Object from your feature class then add that layer to your mxd.

Adding shapefile as layer in ArcGIS Desktop using Python/ArcPy? - Geographic Information Systems Sta...

View solution in original post

8 Replies
TimWitt2
MVP Alum

This might help: ArcGIS Desktop

and look at the save method here: ArcGIS Help 10.1

waqarahmad
New Contributor III

ok I got how to save MXD , and add Layer in MXD...

But that layer is exist on disk with .lyr extension ..

My layer data is in .gdb datasource....   how to add layer from .gdb source to MXD?
Am i Getting it right?

0 Kudos
SepheFox
Frequent Contributor

Hi Waqar, you just need to make a feature layer first, then add the layer the way Tim describes:

C = r"C:\...\featureclass"
arcpy
.MakeFeatureLayer_management(FC, "nameoffeatureclass")

waqarahmad
New Contributor III

Sir I am making Feature layer first.

arcpy.MakeFeatureLayer_management creates datasource of the layer in .gdb

how to add that datasouce in MXD?

0 Kudos
IanMurray
Frequent Contributor

You need to create a Layer Object from your feature class then add that layer to your mxd.

Adding shapefile as layer in ArcGIS Desktop using Python/ArcPy? - Geographic Information Systems Sta...

JoshuaBixby
MVP Esteemed Contributor

The accepted answer on StackExchange works for ArcGIS Desktop, which is what the OP was asking about, but the accepted answer won't work for ArcGIS Pro.

The arcpy.mapping Layer function was removed in ArcGIS Pro, it has been replaced with LayerFile.  If one looks at the Summary for Layer:

Summary

Provides access to layer properties and methods. It can either reference layers in a map document (.mxd) or layers in a layer (.lyr) file.

It turns out, the accepted answer on StackExchange was never supposed to work but it does.  Originally, arcpy.mapping Layer was designed to work on layers in MXDs or existing layer files, but there was a bug that allowed it to work directly against some data sources (but not all).  Since the hack was fairly common place, the bug wasn't fixed, until ArcGIS Pro.  To avoid confusion over changed functionality and having people submit bug reports, Layer was renamed LayerFile to emphasize how it is designed to work.

I can't remember right off the top what the workflow is in Pro.  I will post it later if I find it or remember.

SepheFox
Frequent Contributor

After you make the feature layer, use the add layer scrip. For example:

arcpy.MakeFeatureLayer_management("C:/data/mexico.gdb/cities","cities_lyr")

AddLayer (data_frame, "cities_lyr")

waqarahmad
New Contributor III

Thanks all of you for your help...........

0 Kudos