Select to view content in your preferred language

How to add raster dataset to active map contents using ArcPy in ArcGIS Pro

4545
2
02-16-2018 04:41 PM
by Anonymous User
Not applicable

How to add raster dataset to active map contents using arcPy in ArcGIS Pro?  The code below gives me an error on line 10,  "ValueError: tempRas".  I am trying to migrate ArcMap 10.5 to ArcGIS Pro and I am having difficulty finding a solution for this problem.  I would very much appreciate your help.

import arcpy
import traceback

try:
    aprx = arcpy.mp.ArcGISProject('CURRENT')
    ras = '<path to>\\example.tif'
    
    rasTempLyr = arcpy.MakeRasterLayer_management(ras, 'tempRas')
    activeMap = aprx.activeMap
    activeMap.addLayer(rasTempLyr, 'TOP')

except:
    arcpy.AddError(traceback.format_exc())
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Throw some print statements in to confirm what is you think is there.  It can be tricky.

In fact, dump the try-except stuff altogether, the error messages returned may be more informative.

activeMap
(Read Only)

Returns the map object associated with the focused view within the application.None will be returned if there are no views open that have an associated map or when a script is run outside of the application.

0 Kudos
TobiSellekaerts
Regular Contributor

I'm having this same problem.  The error message "ValueError: tempRas" changes depending on what you called your temporary raster layer, so mine said 'ValueError: tempRaster'.  Using makeRasterLayer will add the raster immediately to a map you have open in Pro, if you are working from the python command prompt interactively.  But in a script, when you need to add another line to add the layer to a map, map.addLayer() throws an error.  It's almost like ArcGIS Pro's definition of a 'layer' is different from the 'layer' concept used by tools originated in ArcMap (i.e. makeFeatureLayer, makeRasterLayer).

I eventually got around this by skipping 'makeRasterLayer' altogether and instead going directly for map.addDataFromPath(data_path).