Showing results for 
Search instead for 
Do you mean 

Mosaic a set of Tiled Imagery (handling NoData)

by Technical Evangelist on ‎06-27-2017 12:57 PM - edited on ‎02-24-2020 03:37 AM by Community Manager (4,628 Views)

Download Model




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: 


  1. At a given pixel location, no processing occurs if any input value to that operator is NoData (and NoData will subsequently be output at that location)

  2. Everything beyond the extent of an input image is considered NoData

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.


Assumptions / Limitations


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.


Input parameters:


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
on ‎11-16-2017 05:57 PM

Hi Ian


I tried the workflow you provide above. The mosaic gets generated fine but the color is not.

What technique would you use to ensure the mosaic output color look the same as the input? i tried to attach a color look up table to the final output but it didn't work out well.

Different tif inputs may have same pixel values but with different colours. E.g. pixel value 1 may show red on tif_1; pixel value 2 may show blue on tif_2. How to you speficy the color table to make tif_1 show red and tif_2 show blue (when pixel value=1)?


I have multiple tifs and I am trying to use your method to mosaic the tifs and output needs to have the same color as inputs.





by Technical Evangelist
‎11-17-2017 06:08 AM - edited ‎11-17-2017 06:09 AM

Hi Yuan,


The above technique is designed for Athematic (continuous) type imagery. It sounds like you have Thematic (discrete) data? 


Combining Thematic images is always problematic if you don't have a consistent schema for the data. For example, if in imageA DN value 1 is arable crops with a color Yellow, but in imageB, DN value 1 represents water with a color Blue, you can't simply combine the two images together - what class/color should DN 1 be in the output?


You can either recode the data so that the DN to color coding is consistent.


Or you can convert the data to athematic using this type of technique:


Once then mosaiced the data together you can convert it back to thematic if you want using something like RGB Clustering. This type of approach is great if all you're concerned about is the colors. If you actually need to retain class assignments then the only viable approach is to recode the input data to be consistent.


Hope that helps.



on ‎01-23-2019 12:39 PM

By Galma Godana

on january 23, 2019


Hi Dear,

Am trying to mosaic image using the workflow you provided above. Yet, I couldn't manage the image background as you mentioned above because the image am trying to mosaic is too many (it's around 9 path and rows) I mean it covers a larger area. Would you share me some recommendations (like vedio, or other)? 






by Technical Evangelist
on ‎01-23-2019 01:06 PM

Hi Galma


It;s not clear what you mean by "couldn't manage the image background"? What's not working? If the input images already have "background" pixels in their extent you'll need to set those to NoData prior to using this model (or modify the model to do that as part of the processing prior to merging their extents) , Would you be better off using MosaicPro to mosaic your data?





on ‎01-23-2019 01:41 PM

 Hi Lan.

Thank you for your feedback.

 I tried to remove image background, but after all mosaicing is finished and I go for image classification, the removed background is included by default and the area outside my area of interest is also combined as if I classified it which is incorrect. I couldn't found the solution so far. That's what I want mention. 


I tried also Mosaicpro yet, it gives me the same result mentioned aboved.