Saturday 13 May 2017

layers - Adding feature class to map (within specific group) using ArcPy with ArcGIS Pro?


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?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...