[GIS] Mosaic to new raster on rasters with the same name in different locations

arcpyerror-000622mosaic-datasetrasterwalk

I have national woody vegetation raster files for Australia for years between 1988-2017. These files are separated into spatial tiles with the same name. So, what I would like to do is combine raster files with the same name together. Here is my code:

import os, arcpy

workspace  = r"C:\Users\lawtond\Rasters\Woody"
list_raster= [] # the list must exist before you can append
walk       = arcpy.da.Walk(workspace, type="GRID")
output_location  = r"C:\Users\lawtond\Rasters\Woody"

for dirpath, dirnames, filenames in walk:
    for file in filenames:
        if "woody13" in file.lower():
            list_raster.append(os.path.join(dirpath,file)) # FULL path to each raster

print (list_raster)

arcpy.MosaicToNewRaster_management (list_raster, output_location, "merged_woody_2013.tif","1", "FIRST")

However I get the following error:

Message File Name   Line    Position    
Traceback               
    <module>    <module3>   18      
    MosaicToNewRaster   C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py 13191       
ExecuteError: ERROR 000622: Failed to execute (Mosaic To New Raster). Parameters are not valid.
ERROR 000628: Cannot set input into parameter coordinate_system_for_the_raster.

What am I doing wrong here?

Best Answer

I like the way you've built a list and then fed it into your GP tool, building a list is fast. As I indicated in my comment you need to have the FULL path to the raster in your list for it to be useful, your mosaic to new dataset can't find each input raster (the list will look like ['woody13','woody13','woody13'..]) which doesn't help. Try it this way:

import os, arcpy

workspace  = r"C:\Users\lawtond\Rasters\Woody"
list_raster= [] # the list must exist before you can append
walk       = arcpy.da.Walk(workspace, type="GRID")
output_location  = r"C:\Users\lawtond\Rasters\Woody"

for dirpath, dirnames, filenames in walk:
    for file in filenames:
        if "woody13" in file.lower():
            list_raster.append(os.path.join(dirpath,file)) # FULL path to each raster

arcpy.MosaicToNewRaster_management (list_raster, output_location, "merged_woody_2013.tif","1", "FIRST")
Related Question