Implementing merge() in ArcPy map algebra

Document created by curtvprice Champion on May 1, 2015
Version 1Show Document
  • View in full screen mode

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  
1 person found this helpful

Attachments

    Outcomes