Select to view content in your preferred language

Match three rasters in a if/else loop?

608
4
Jump to solution
02-01-2021 06:16 AM
KathleenHoenke
Frequent Contributor

I had created a script with help from the community that takes two rasters of the same name and merges them together (here). I now need to bring in a third raster and mosaic them together as well. However, when I go about matching them, I get this error

Traceback (most recent call last):
File "<string>", line 45, in <module>
IndexError: list index out of range

 I wanted to see if I'm going about this the wrong way or if this is even possible to match in this way? TIA!

 

import arcpy
import os
from arcpy import env

#not a fan of stating the obvious, but change the paths below to your fgdbs
#also keep the r then surround the path in quotes

#fgdb of rasters being merged
pluv_fgdb = r'E:\FATHOM\2020FATHOM\Analysis\Pluvial_1in100\Pluvial_1in100_Processed.gdb'

fluv_fgdb = r'E:\FATHOM\2020FATHOM\Analysis\Fluvial_1in100\Fluvial_1in100_Processed.gdb'

nhd_fgdb = r'E:\FATHOM\2020FATHOM\Analysis\NHDplusMedium_flowlines_Rast_Expand_HUC02.gdb'


#preferably empy fgdb to put outputs
output_fgdb = r'E:\FATHOM\2020FATHOM\Analysis\Final_Floodplain_Bounds_No_NHD.gdb'


#list all the rasters

arcpy.env.workspace = pluv_fgdb

pluv_rasters = arcpy.ListRasters("*", "GRID")
print (pluv_rasters)

arcpy.env.workspace = fluv_fgdb

fluv_rasters = arcpy.ListRasters()
print (fluv_rasters)

arcpy.env.workspace = nhd_fgdb

nhd_rasters = arcpy.ListRasters()
print (nhd_rasters)

#make a dictionary of source raster path
#to clipping fc and source name
#NB relies on names after the '_' being equal
input_floodplain_pieces_dict = {}

for pluv_raster in pluv_rasters:
    for fluv_raster in fluv_rasters:
        for nhd_raster in nhd_rasters:
            if pluv_raster.split("_Mask")[1] == fluv_raster.split("_Mask")[1] == nhd_raster.split("_")[-1]:
                pluv_raster_path = os.path.join(pluv_fgdb, pluv_raster)
                fluv_raster_path = os.path.join(fluv_fgdb, fluv_raster)
                nhd_raster_path = os.path.join(nhd_gdb, nhd_raster)
                out_raster_name = nhd_raster + "_Floodplain"
                out_raster_path = os.path.join(output_fgdb, out_raster_name)
                OutMedResFloodplain = arcpy.management.MosaicToNewRaster(pluv_raster_path, fluv_raster_path, nhd_raster_path, output_fgdb, out_raster_name, "NAD_1983_Albers", "8_BIT_UNSIGNED", "30", "1", "Last", "First") 
                

 

 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I've not gone though the whole code, only the error you have, but:

The mosaic to new raster tool Mosaic To New Raster (Data Management)—ArcGIS Pro | Documentation expects the input rasters argument to be a list.  The tool is misinterpreting each raster as an incorrect argument to the tool.

try:

OutMedResFloodplain = arcpy.management.MosaicToNewRaster([pluv_raster_path, fluv_raster_path, nhd_raster_path], output_fgdb, out_raster_name, "NAD_1983_Albers", "8_BIT_UNSIGNED", "30", "1", "Last", "First") 

 

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

I'd get rid of any references to the dictionary, it just confuses things.

It's likely when you call the index [1] on the .split list.  You should really add print statements to see what's going on - such as:

print(pluv_raster.split("_Mask")[1])
KathleenHoenke
Frequent Contributor

Thank you - that did work and help me to make sure I got the 0 and 1s correct.  

Now I'm receiving this error:

arcgisscripting.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.

 

0 Kudos
DavidPike
MVP Frequent Contributor

I've not gone though the whole code, only the error you have, but:

The mosaic to new raster tool Mosaic To New Raster (Data Management)—ArcGIS Pro | Documentation expects the input rasters argument to be a list.  The tool is misinterpreting each raster as an incorrect argument to the tool.

try:

OutMedResFloodplain = arcpy.management.MosaicToNewRaster([pluv_raster_path, fluv_raster_path, nhd_raster_path], output_fgdb, out_raster_name, "NAD_1983_Albers", "8_BIT_UNSIGNED", "30", "1", "Last", "First") 

 

KathleenHoenke
Frequent Contributor

Perfect, thank you!

0 Kudos