ArcPy – How to Batch Mosaic Rasters Using arcpy.MosaicToNewRaster_management (ERROR 000628 Solution)

arcgis-10.1arcpyerror-000628python

I am trying to batch mosaic rasters using arcpy.MosaicToNewRaster_management in arcpy:

    #This is for mosaicking a lot of rasters
    import arcpy
    from arcpy import env
    arcpy.CheckOutExtension("Spatial")
    env.overwriteOutput = True

    #Set the current workspace
    env.workspace = r"C:\thesis\for_sampling\sampling_outputs\new_loss"
    env.nodata = "MINIMUM"
    env.compression = "LZ77"

    coordinate_system =  'GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],\
                       #UNIT["Degree",0.0174532925199433],AUTHORITY["EPSG",4326]]'

    list_all_rasters = ["newlossGreater_Luzonforests_onlyloss_20N_120E.tif;newlossGreater_Luzonforests_onlyloss_30N_120E.tif;\
newlossGreater_Luzonforests_onlyloss_20N_110E.tif","newlossGreater_Palawanforests_onlyloss_20N_120E.tif;\
newlossGreater_Palawanforests_onlyloss_10N_110E.tif;\newlossGreater_Palawanforests_onlyloss_20N_110E.tif",\
"newlossGreater_Negros_Panayforests_onlyloss_10N_120E.tif;newlossGreater_Negros_Panayforests_onlyloss_10N_110E.tif",\
"newlossGreater_Mindanaoforests_onlyloss_10N_120E.tif;newlossGreater_Mindanaoforests_onlyloss_10N_110E.tif;\
newlossGreater_Mindanaoforests_onlyloss_20N_110E.tif"]

    output_list = ["Luzon_loss.tif","Palawan_losss.tif","Negros_Panay_loss.tif","Mindanao_loss.tif"]

    for raster in  range(0,4):
       output_list = ["Luzon_loss.tif","Palawan_losss.tif","Negros_Panay_loss.tif","Mindanao_loss.tif"]
       env.workspace = r"C:\thesis\for_sampling\sampling_outputs\new_loss"
       print raster # checking the list
       arcpy.MosaicToNewRaster_management(list_all_rasters[raster], r"C:\thesis\for_sampling\sampling_outputs" , output_list[raster], 1, coordinate_system)
       print raster+ " is ok!"   
    print "Finish all of them!"

The error talks about coordinate systems. I've tried to use "GCS_WGS_1984.prj" but it didn't work. I also put that .prj file into the current workspace, still the same error:

    arcpy.MosaicToNewRaster_management(list_all_rasters[raster], r"C:\thesis\for_sampling\sampling_outputs" , output_list[raster], 1, coordinate_system)
    File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 11613, in MosaicToNewRaster
raise e
    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. 

I also tried to change the variable: coordinate_system = arcpy.SpatialReference(4326), but the error persists.

Best Answer

You need to read the help file of the Mosaic to new raster tool. If you look at the Syntax section it lists the parameters of the tool and what they are. You need to be following the same order. Looking at your code you have a mysterious 1 after output name and before coordinate system. This is not the order of parameters as dictated in the syntax section.

Also I think those back slashes in list_all_rasters are not appropriate, they should be removed. As you have set the workspace those raster must exist in that folder.

Finally you don't need the lines where you set output_list and workpace within your for loop as you have already set them and they remain static within the for loop.

Related Question