Implementing merge() in ArcPy map algebra

4631
0
05-01-2015 08:23 PM

Implementing merge() in ArcPy map algebra

The merge() function (which we've used for years in GRID, ArcView, and ArcGIS Desktop) simply doesn't exist in ArcPy map algebra.

I came up with a Python map algebra implementation (with some nice refinements from a friend on the Spatial Analyst dev team). Give it a spin and let me know if you are successful with it!

import arcpy  
from arcpy.sa import *  
arcpy.CheckOutExtension("spatial")  
  
def Merge(raster_list):   
    """Merge a list of rasters. The first non-NoData value found (cell by cell) is kept. 
       Merge([raster1, raster2, raster3, ...]) 

    This is another implementation of ArcGIS 9x Map Algebra merge() function"""  
    if type(raster_list) == str:  
        raster_list = raster_list.split(";")  
    if len(raster_list) < 2:  
        raise Exception("More than one raster required")  
    # create list of raster objects to merge
    rlist = [Raster(arcpy.Describe(ras).catalogPath) for ras in raster_list]  
    merge_ras = rlist[0]  
    for ras in rlist[1:]:  
        merge_ras = Con(IsNull(merge_ras), ras, merge_ras)  
    return merge_ras  
Version history
Last update:
‎12-12-2021 03:40 AM
Updated by: