Implementing merge() in ArcPy map algebra

Discussion created by curtvprice Champion on Sep 1, 2014
Latest reply on Oct 28, 2014 by curtvprice

The merge function (which we've used for years in GRID, ArcView, and ArcGIS 8x, 9x) simply doesn't exist in ArcPy map algebra, unless I'm missing something...


Here's my hack. Note this is set up so it should work inside a map algebra expression. It also returns an integer raster if all the inputs are integer, otherwise float.


Just asking the community (and especially Esri SA team folks - I know you're out there) whether this is a viable approach.


import os
import arcpy

def MergeRasters(raster_list, out_raster=None):  
    """ArcPy Map Algebra implementation of the old merge() function. 

     Curtis Price, 
    # output raster name
    if not out_raster:
        out_raster = arcpy.CreateScratchName(
            "mrg", "", "raster", arcpy.env.workspace)  
    # determine raster type for output
    ptype = "32_BIT_SIGNED"  
    for ras in raster_list:  
        if not arcpy.Describe(ras).isInteger:  
            ptype = "32_BIT_FLOAT"  
    # create temporary raster
    tmpRaster = arcpy.CreateScratchName("", "mrg", "raster",  
    arcpy.Mosaic_management(raster_list, tmpRaster, "FIRST")  
    # Copy the raster, enforcing environment (extent, snap, mask, etc)  
    mRaster = ApplyEnvironment(tmpRaster)
    # Clean up  
    return mRaster


Note, this may work sort of like the merge() function, but is not true arcpy map algebra local operator, so we lose out on the benefits of using it with complex statements like


Int(Merge(raster1, SetNull(raster2, 2), raster3 + 2))


Please vote up my idea: Bring back Spatial Analyst Merge tool



curtvprice - script bug fix

curtvprice - Used ApplyEnvironment function  instead of just multiplying by one

curtvprice - added note about complex statements and link to