As part of a larger model it is often necessary to locate and input a varying number of input images. These might represent multiple image tiles covering a larger extent, or might be multi-date layers covering generally the same extent.
You can use the Multi Filename Input and Iterator operators to stitch tiles together into a single image. This requires an understanding of NoData, how Spatial Modeler uses it and how to work with it to achieve the desired result.
See this thread for a general discussion of NoData in Spatial Modeler, largely pertaining to handling NoData if there's only a single input raster in the Model.
In this simple scenario though, we need to input two or more images which either abut against each other (no overlap) or have minimal overlap. Taking the simplest example of two butt-joining images, the approach in Spatial Modeler seems simple - perform a Conditional test which outputs ImageA if it exists (is something other than 0) or output ImageB otherwise.
|Figure 1: Partially overlapping image tiles|
We need to remember that Spatial Modeler's default processing extent (when an Operator needs to consider two or more input spatial streams) is the intersection of the inputs. So by default, considering the above Conditional test would result in only the dark blue Intersection Extent being output, which doesn't fulfill the requirement of a mosaicing operation. So we will need to use a Define Processing Area operator to over-ride the defaults and specify the Union extent instead.
We also need to be aware of two basic rules of NoData in Spatial Modeler:
These two rules have a serious consequence if trying to assemble two or more "super-setting" datasets, such as those shown in Figure 1 above. Even if we are now processing based on the Union of all inputs, more then 2/3rds of ImageA exists in ImageB's NoData area (i.e. all areas beyond the extent of ImageB). So processing any pixels in that light blue area of ImageA will be considering inputs of Data (from ImageA) and NoData (from ImageB) and therefore the output will be NoData. Similarly in the light blue area of ImageB you would be comparing NoData from ImageA with Data from ImageB, resulting in NoData again. Only in the overlap area is Data being compared with Data. So we would end up with an output image that has the union extent of both inputs, but all except the dark blue Intersection Area would be NoData.
Now extend that problem to inputting a thousand image tiles!
The solution is to use the Replace NoData With operator to (unsurprisingly) replace the NoData masks with a value that is hopefully not being used in the input data tiles. Refer to the article linked above for some background on the importance of being able to differentiate between, for example, a value of 0 being used to represent "background", and valid pixel locations where the DN value of the image data is 0. One approach to this is by replacing valid 0s with 1s prior to converting NoData to 0s.
Here's the basic model:
|Figure 2: mosaic_tiles_v16_1_0.gmdx|
How does it work? The Multi Filename Input operator will search the specified directories, find the images that match the search criteria and build a List of filenames. The first Iterator is basically wrapped around a Raster Input operator which iterates through that list of filenames and converts them into a List of rasters.
|Figure 3: Iterator 1 sub-model|
The List of rasters is then passed to a Define Processing Area operator in which the only non-default setting is to set the processing area to the Union of all the inputs rather than the intersection. Another List of rasters is produced, but now the processing extent is set to that of the entire extent covered by all input images. This List then goes into and controls a second Iterator sub-model.
|Figure 4: Iterator 2 sub-model|
The second Iterator iterates through each input raster stream (but using the extent of the union of all input raster streams), sets any valid (non-NoData) DN values of 0 to 1, then replaces NoData locations with a value of 0. Finally it splits each raster steam into it's constituent bands (assuming that the input images were three band images) and outputs three lists of rasters - one for each band.
After the second Iterator the model then takes the list of Band 1 rasters and Stack Layers them (so if you input 1000 image tiles to the model you'd have a 1000 layer stack), but immediately calculates the Stack Max. Since "background" has been set to 0 this effectively "flattens" the data back down to a single layer mosaic of all of the Band 1 data that was input to the Model.
The same is done for Bands 2 and 3 and then the flattened results of those branches are themselves Stack Layered to form a 3 band mosaic raster stream.
One final thought - we would want the two white rectangles at top-right and lower-left of the above diagram (Figure 1) to be NoData in the output mosaiced data. So a Set to NoData is used to convert DNs of 0 back to NoData.
This model is designed to process input images with 3 bands. It will require minor adjustments to handle more, or less, bands.
The model also assumes that 0s in the input images represent valid values. If instead there are wedges of 0s at the edges of the images which represent background areas, these should first be set to NoData prior to running the Model. Alternatively the model will need to be edited to reflect the different differing interpretations that should be placed on specific DN values.
The model also assumes that the inputs are unsigned integers (u8, u16, etc) and that 0 is the minimum value. If instead you are mosaicing, for example, multiple DEMs in s16 or float values (with areas that drop below sea level), you should change the value that NoData is replaced with to an appropriate value which is less than any values that could occur as valid data. As well as removing the branches that deal with bands 2 and 3 of course! Remember to change the Set to NoData value as well.
Directory: Select a directory which will be searched for input images which match the Extension pattern
Extension: Enter a wildcard search string to identify the imagery to be input to the model, For example *.ntf would select all files with an extension of .ntf
Search Sub-directories: Defaults to False, in which case just the directory identified by Directory will be searched. If set to True, Directory and all sub-directories of Directory will be searched to compile the list of input files.
Output: Specify the name (and extension, if a format other than IMG is desired) of the output mosaiced image file
|Figure 5: Run dialog|