Main Content

mergetiles

Merge adjacent raster tiles

Since R2024a

    Description

    example

    [B,RB] = mergetiles(A1,R1,...,An,Rn) merges multiple adjacent raster tiles, specified as pairs of rasters and raster reference objects, into the raster B and raster reference RB. The input raster tiles must form a filled quadrangle or rectangle. The order of the raster tiles does not matter.

    Examples

    collapse all

    Import adjacent raster tiles, merge the tiles into a single raster, and display the merged raster on a map.

    Read four DTED tiles into the workspace. The tiles form a quadrangle with latitudes from 39 to 41 degrees and longitudes from –107 to –105 degrees. For each use of the readgeoraster function, the first output is an array that contains elevation values, and the second output is a reference object that contains spatial referencing information.

    [A1,R1] = readgeoraster("w106/n39.dt0",OutputType="double");
    [A2,R2] = readgeoraster("w106/n40.dt0",OutputType="double");
    [A3,R3] = readgeoraster("w107/n39.dt0",OutputType="double");
    [A4,R4] = readgeoraster("w107/n40.dt0",OutputType="double");

    Merge the tiles into one raster.

    [B,RB] = mergetiles(A1,R1,A2,R2,A3,R3,A4,R4);

    Create a map and display the merged raster as a surface. Apply a colormap that is appropriate for elevation data. Then, add a labeled color bar.

    usamap(B,RB)
    geoshow(B,RB,DisplayType="surface")
    
    demcmap(B)
    
    c = colorbar;
    c.Label.String = "Elevation (m)";

    This example uses modified data from the US Geological Survey.

    The mergetiles function requires the input raster tiles to form a filled quadrangle or rectangle. If your data has missing tiles, then you can fill the gaps with values that represent missing data.

    Read three DTED tiles into the workspace:

    • w106/n39.dt0 has latitudes from 39 to 40 degrees and longitudes from –106 to –105 degrees.

    • w106/n40.dt0 has latitudes from 40 to 41 degrees and longitudes from –106 to –105 degrees.

    • w107/n39.dt0 has latitudes from 39 to 40 degrees and longitudes from –107 to –106 degrees.

    [A1,R1] = readgeoraster("w106/n39.dt0",OutputType="double");
    [A2,R2] = readgeoraster("w106/n40.dt0",OutputType="double");
    [A3,R3] = readgeoraster("w107/n39.dt0",OutputType="double");

    The tiles are bound by a quadrangle with latitudes from 39 to 41 degrees and longitudes from –107 to –105 degrees. However, the quadrangle is missing the tile with latitudes from 40 to 41 degrees and longitudes from –107 to –106.

    To merge the three DTED tiles by using the mergetiles function, you can create a fourth tile of NaN values that accounts for the missing data. If the NaN function does not support the data type of your tiles, such as uint8 values in tiles of RGB data, then you can use numeric values such as 0s or 1s.

    • Spatially reference the tile by making a copy of one of the existing reference objects. Then, change the latitude and longitude limits of the new reference object to match the limits of the missing tile.

    • Create an array of NaN values. Specify the size of the array using the raster size stored in the new reference object.

    missingR = R3;
    missingR.LatitudeLimits = [40 41];
    missingR.LongitudeLimits = [-107 -106];
    
    missingA = NaN(missingR.RasterSize);

    Merge the DTED tiles and the tile of NaN values into one raster.

    [B,RB] = mergetiles(A1,R1,A2,R2,A3,R3,missingA,missingR);

    Create a map and display the merged raster as a surface. Note that the northwest corner of the map is blank. Apply a colormap that is appropriate for elevation data. Then, add a labeled color bar.

    figure
    usamap(B,RB)
    geoshow(B,RB,DisplayType="surface")
    
    demcmap(B)
    
    c = colorbar;
    c.Label.String = "Elevation (m)";

    This example uses modified data from the US Geological Survey.

    Input Arguments

    collapse all

    Georeferenced image or data grid, specified as an M-by-N numeric or logical matrix or an M-by-N-by-P numeric or logical array.

    The data types of the input images or data grids must match.

    Spatial reference for the image or data grid, specified as a GeographicCellsReference, GeographicPostingsReference, MapCellsReference, or MapPostingsReference object.

    The object types of the input reference objects must match. In addition, the input reference objects must be consistent.

    • GeographicCellsReference objects are consistent when the values of the CellExtentInLatitude, CellExtentInLongitude, GeographicCRS, RowsStartFrom, and ColumnsStartFrom properties match.

    • GeographicPostingsReference objects are consistent when the values of the SampleSpacingInLatitude, SampleSpacingInLongitude, GeographicCRS, RowsStartFrom, and ColumnsStartFrom properties match.

    • MapCellsReference objects are consistent when the values of the CellExtentInWorldX, CellExtentInWorldY, ProjectedCRS, RowsStartFrom, and ColumnsStartFrom properties match.

    • MapPostingsReference objects are consistent when the values of the SampleSpacingInWorldX, SampleSpacingInWorldY, ProjectedCRS, RowsStartFrom, and ColumnsStartFrom properties match.

    Output Arguments

    collapse all

    Georeferenced image or data grid, returned as an M-by-N numeric or logical matrix or an M-by-N-by-P numeric or logical array.

    The data type of B matches the data type of the input images or data grids.

    Spatial reference for the merged image or data grid, returned as a GeographicCellsReference, GeographicPostingsReference, MapCellsReference, or MapPostingsReference object.

    The object type of RB matches the object type of the input reference objects.

    Tips

    To simultaneously read and merge multiple adjacent raster tiles, you can create a custom datastore. For more information about using custom datastores to read and merge multiple tiles, see Merge Multiple Raster Tiles Using Datastore.

    Version History

    Introduced in R2024a