I'm converting Python standalone scripts from Desktop (10.6.1) to Pro (2.3.2). I can add a raster to an open Pro project file interactively by right clicking on the raster in the Catalog and choosing "Add to Current Map". I would like to do this in a script and have tried two ways (one commented out below):
inRas = '<projectDir>/Risks/riskv0'
outAPRX = '<projectDir>/Reports/riskv0/riskv0.aprx'
templateFile = '<projectDir>/MapTemplates/risksTemplate/risksTemplate.aprx'
aprx = arcpy.mp.ArcGISProject(templateFile)
map = aprx.listMaps()[0]
map.addDataFromPath(inRas)
#refLyr = map.listLayers()[1]
#map.insertLayer(refLyr, inRas, "AFTER")
aprx.saveACopy(outAPRX)
del aprx
When I use addDataFromPath, I receive a RuntimeError for line 6 with no further information.
The listLayers/insertLayer approach results in a ValueErr at line 8 for the inRas file.
I would be grateful for any advice on adding a raster to a map in Pro.
I have developed a workaround that writes the raster to a layer file and retrieves this version for the map insertion:
inRas = '<projectDir>/Risks/riskv0'
outAPRX = '<projectDir>/Reports/riskv0/riskv0.aprx'
templateFile = '<projectDir>/MapTemplates/risksTemplate/risksTemplate.aprx'
aprx = arcpy.mp.ArcGISProject(templateFile)
map = aprx.listMaps()[0]
refLyr = map.listLayers()[1]
arcpy.MakeRasterLayer_management(inRas, "insertRas")
arcpy.SaveToLayerFile_management("insertRas", '<tempDir>/insertRas.lyrx')
insertLyr = arcpy.mp.LayerFile('<tempDir>/insertRas.lyrx')
map.insertLayer(refLyr, insertLyr, "AFTER")
aprx.saveACopy(outAPRX)
del aprx
I trust it doesn't have to be this complicated! Is there a way to work with the raster without writing/reading the temporary file?