3

I am trying to add a feature class to my ArcGIS Pro map (a specific group), but I don't seem to be able to find the way.

This is the piece of code I've written:

import arcpy    
arcpy.env.workspace = r'D:\ArcGIS_Pro_Projects\Optimity_Alf'    
fc_output = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Temp.gdb\LOS_Optimity_All_700m'    
fl_output = 'LOS_Optimity_All_700m'
arcpy.MakeFeatureLayer_management(fc_output, fl_output)    
aprx = arcpy.mp.ArcGISProject(r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Alf.aprx'    
m = aprx.listMaps('MyMap')[0]
m.addLayerToGroup('LOS Results', fl_output)

Where 'LOS Results' is the group.

I get the following error: ValueError: LOS Results Failed to execute (Testing).

Anyone has any idea?


Following @Hornbydd's advice I have run the following code:

import arcpy

arcpy.env.workspace = r'D:\ArcGIS_Pro_Projects\Optimity_Alf'

fc_output = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Temp.gdb\LOS_Optimity_All_700m'
fl_output = 'LOS_Optimity_All_700m'
arcpy.MakeFeatureLayer_management(fc_output, fl_output)

layer = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Temp\LOS_Optimity_All_700m.lyrx'
arcpy.SaveToLayerFile_management(fl_output, layer, 'ABSOLUTE')

lf = arcpy.mp.LayerFile(layer) # This is pointing to LOS_Optimity_All_700m
aprx = arcpy.mp.ArcGISProject(r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Alf.aprx')
m = aprx.listMaps('Optimity Map')[0]
refLyr = m.listLayers('LOS Results')[0]
m.addLayerToGroup(refLyr,lf)

It doesn't throw any errors but it doesn't add any layer to my map.

Also, is this the only way to do such a simple thing as adding a layer to a map? I have a feature class in a geodatabase with the data I'd like to plot and I have had to convert that feature class into a feature layer and then that feature layer into a layer file so that it can be displayed. Doesn't it seem over complicated?

  • 1
    Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. – Midavalo Oct 30 '17 at 15:39
  • 1
    Please edit your question to include the entire error message, including line numbers and any other info. – Midavalo Oct 30 '17 at 15:41
  • Looking at your new update it occurred to me if you want to add the layer to project that you have open then you need to reference the current project. You would do it as: aprx = arcpy.mp.ArcGISProject("CURRENT"). So your code would then be designed to be run in an active document and not as a script say from an IDE. Finally if are trying to insert layers into a group layer using a script from an IDE, then you don't show a line that actually saves your changes... – Hornbydd Oct 31 '17 at 0:08
  • That was it. I was not saving the project. Now it works. Thanks! :-) – Pitrako Junior Oct 31 '17 at 9:52
2

The problem is how you are referencing the group layer in addLayerToGroup(). To be fair to you it is very easily missed and there are no examples in the Help file to work from (shame on ESRI ;) ). You need to understand what the help file is saying when you read this method for the Map Class.

  • Parameter = target_group_layer
  • Explanation = A reference to an existing group Layer object.
  • Data Type = Layer

The KEY words here are Existing Group Layer Object. You reference your group layer by a string (the name of your group layer), that is NOT an existing Layer Object!

You also need to create a LYRX file of your layer (LOS_Optimity_All_700m) and use that.

So create your lyrx file and get a handle on that:

lf = arcpy.mp.LayerFile(r"C:\Scratch\test.lyrx") # This is pointing to LOS_Optimity_All_700m

Get a handle on the existing group layer object:

mygrp = m.listLayers("LOS Results")[0]

Then you can add it directly to the group layer:

m.addLayerToGroup(mygrp, lf)
  • I tried as suggested but, although it doesn't throw any error, it doesn't actually add the layer to the map. Also, it's a bit strange that in order to add a layer to a map I need to first convert the feature class that contains the data into a feature layer and then this feature layer into a layer file. Don't you think? – Pitrako Junior Oct 30 '17 at 16:38
0

I was recently struggling with the same problem. I created a blank group layer and saved that to a lyrx file on disk. I managed to successfully add a feature class to this group (without creating a separate lyrx file of that fc) by doing something similar to the following (I've tried to adapt to your script):

import arcpy
aprx = arcpy.mp.ArcGISProject('CURRENT')
m = aprx.listMaps('*')[0]
outputWorkspace = r'D:\ArcGIS_Pro_Projects\Optimity_Alf'
arcpy.env.workspace = outputWorkspace
fc_output = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Temp.gdb\LOS_Optimity_All_700m'
fl_output = 'LOS_Optimity_All_700m'
blank_group_layer = arcpy.mp.LayerFile(r"\\path\to\layer\file\grp_lyr.lyrx") #created previously in ArcPro and exported as lyrx file
m.addLayer(blank_group_layer, "TOP")
model_output_group = m.listLayers("name of group layer")[0]
fl_output = arcpy.MakeFeatureLayer_management(fc_output, fl_output).getOutput(0)
m.addLayerToGroup(model_output_group, fl_output, "TOP")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.