Adding Layers to a APRX python.

1267
7
Jump to solution
10-22-2019 06:09 PM
deleted-user-yC5VkbyXzrQR
Occasional Contributor

Does anyone know why line 1 - 3 does not add feature to the map to the map. 

But line 6 - 8 adds the feature to the map. 

Only difference is that I'm using the Path to the Aprx and not "Current". I was under the impression you can add add to the map with line 6 - 8 in a stand alone script. 

aprx = arcpy.mp.ArcGISProject(r'xxxxxx\\MyProject4\\MyProject5.aprx')
mpDEM = aprx.listMaps("Map2")[0]
mpDEM.addDataFromPath(r"C:\xxxxxxx\Adam.gdb\ProjectBoundary")


aprx = arcpy.mp.ArcGISProject("CURRENT")
mpDEM = aprx.listMaps("Map2")[0]
mpDEM.addDataFromPath(r"C:\xxxxxxx\Adam.gdb\ProjectBoundary")
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

Adam Arcichowski

wild guess  is that that method requires a "layer"

Layer—ArcPy | ArcGIS Desktop 

and not a featureclass in a gdb.  Use Save to Layer file to make a lyrx out of the featureclass and try loading that file instead of the featureclass reference

the "Current" one works because the aprx is open

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Adam Arcichowski

wild guess  is that that method requires a "layer"

Layer—ArcPy | ArcGIS Desktop 

and not a featureclass in a gdb.  Use Save to Layer file to make a lyrx out of the featureclass and try loading that file instead of the featureclass reference

the "Current" one works because the aprx is open

View solution in original post

0 Kudos
deleted-user-yC5VkbyXzrQR
Occasional Contributor

Thanks, Dan 

I've tried for hours but I don't get it. I tried to Make

I want to add a Layer to an existing APRX/Map not to the Current APRX I have open.

I even tried to add a layer with ESRI's example below.  I can't get the below example to work. 

import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
m = aprx.listMaps("Yosemite National Park")[0]
for lyr in m.listLayers():
m.removeLayer(lyr)
lf = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\Yosemite.lyrx")
m.addLayer(lf)
aprx.save()
del aprx





This below script should work no ? 

aprx = arcpy.mp.ArcGISProject(r"......\MyProject8\MyProject8.aprx")
mp = aprx.listMaps("Map")[0]
a = arcpy.mp.LayerFile(r".....\TestSlope\prjbdyoutada.lyrx")
mp.addLayer(a)
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

the last one... did you add

aprx.save()

after line 4?

Just to make sure... you don't have the project open do you when you run the last example do you?  It won't update.

The ones that use "Current" are designed to run in the python window inside of ArcGIS Pro.  The code for adding an existing layer to an aprx should be run in a standalone python IDE like spyder and not in pro's python window

deleted-user-yC5VkbyXzrQR
Occasional Contributor

Yeah it was the save thing. 

I figured out what I have to do with the code below. 

arcpy.AddMessage("Creating Project Boundary Layer...")
arcpy.management.MakeFeatureLayer(Project_Boundary, "ProjectBoundary",Expression)
arcpy.SaveToLayerFile_management("ProjectBoundary", ProjDataSave+ r"\ProjectBoundary")
lf = arcpy.mp.LayerFile(ProjDataSave + r"\ProjectBoundary.lyrx")
mpDEM.addLayer(lf)‍‍‍‍‍
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Adam, you might want to mark the correct answer to close out the thread.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

guess I will mark it "assumed answered"

0 Kudos
deleted-user-yC5VkbyXzrQR
Occasional Contributor

Sorry about that.

0 Kudos