Select to view content in your preferred language

How to add layer in mxd and save it?

6038
8
Jump to solution
05-29-2015 12:59 PM
waqarahmad
Deactivated User

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
Honored 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
Deactivated User

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
Deactivated User

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
Deactivated User

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
Honored 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
Deactivated User

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
Deactivated User

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

0 Kudos