Hello,
I need to use the Map object method to add a layer to the map, but I can't for the life of me figure out how to make a Layer object to be added. I've tried to use the addDataFromPath method but that doesn't put the data in the order that I want it. I tried to use Make Feature Layer (https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/make-feature-layer.htm) but that doesn't work either.
How do I use AddLayer from data in a GDB so it is added in the order I want it added?
Have you tried running your commands in a python window in Pro? When I do this it automatically adds the layer to the map though not where I necessarily want it to go:
import arcpy
env = arcpy.env.workspace r'path to geodatabase'
fc = 'Parcels' # name of a a featureclass in the gdb referenced above as my workspace
arcpy.management.MakeFeatureLayer(fc,'New County Parcels')
If you are doing this from a script, I think you need to look into the arcpy.mp documentation (Arcpy Mapping Module (arcpy.mp) , especially the 'Map' classes. Especially look at the Code Example 1 below.. that shows how to insert a layer file where you want it to go, but I think it would be similar to adding a layer created using the MakeFeatureLayer tool.
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/map-class.htm
No,
I use PyCharm. I really dislike using the Python features within ArcGIS Pro (either the window or notebooks) because they are exceedingly fragile and frequently cause the entire program to crash which I find unacceptable. It also has very limited debugging features.
I found the moveLayer method within the Map object and that seems to work for me:
if county_name not in [x.name for x in maps]:
m = aprx.createMap(county_name)
m.addDataFromPath(county_fc)
m.addDataFromPath(huc_fc)
m.addDataFromPath(nhd_fc)
layers = m.listLayers()
for lyr in layers:
if lyr.name.lower() == "nhd":
nhd_layer = lyr
if lyr.name.lower() == "huc":
huc_layer = lyr
if lyr.name.lower() == "county_boundaries":
county_layer = lyr
m.moveLayer(county_layer, huc_layer, "BEFORE")
m.moveLayer(county_layer, nhd_layer, "AFTER")