0

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.

1

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.

  • It really helped to check first the syntax, before running the script. Now, it successfully ran, but a different error has appeared on the same line. I'll post it in a while. – brentiemapper May 22 '15 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.