All Products
Search
Document Center

PolarDB:ST_MosaicFrom

Last Updated:Mar 28, 2026

Merges an array of raster objects into a single new raster object, with optional resampling, nodata handling, color balance, and parallel processing.

Syntax

raster ST_MosaicFrom(
  raster source[],
  cstring chunkTableName,
  cstring storageOption DEFAULT '{}',
  cstring mosaicOption  DEFAULT '{}'
)

Parameters

ParameterDescription
sourceThe raster objects to merge.
chunkTableNameThe name of the chunk table that stores the output raster. Must comply with database table naming conventions.
storageOptionA JSON string that controls how the output raster is stored. Default: '{}'. See Storage options.
mosaicOptionA JSON string that controls the mosaic algorithm. Default: '{}'. See Mosaic options.

Storage options

The storageOption parameter accepts the following JSON fields.

FieldTypeDefaultDescription
chunkingbooleanSame as source rastersSpecifies whether to store the output raster as chunks.
chunkdimstringSame as source rastersThe chunk dimensions. Valid only when chunking is true. Format: (w,h,b).
interleavingstringSame as source rastersThe interleaving type. Valid values: bip (pixel interleaving), bil (row interleaving), bsq (band interleaving), auto (determined automatically).
compressionstringSame as source rastersThe compression format. Valid values: none, jpeg, zlib, png, lzo, lz4, zstd, snappy, jp2k.
qualityintegerSame as source rastersThe image quality after compression. Takes effect only when compression is jpeg or jp2k.

Mosaic options

The mosaicOption parameter accepts the following JSON fields.

FieldTypeDefaultDescription
sridintegerSRID of the upper-left rasterThe spatial reference identifier (SRID) of the output raster. Must be specified together with cell_size.
cell_sizefloat8[]Pixel size of the upper-left rasterThe pixel size of the output raster as [cell_size_x, cell_size_y]. Must be specified together with srid.
resampletext'Near'The resampling method. Valid values: 'Near', 'Average', 'Cubic', 'Bilinear'.
nodataboolfalseSpecifies whether nodata values from the source rasters are treated as valid. If true, pixels with nodata values are not resampled. If false, pixels with nodata values are resampled.
nodataValuefloat8 or float8[]NULLThe nodata value to assign to the output raster. Set to a scalar to apply the same value to all bands, or to an array whose length equals the number of bands.
color_balanceboolfalseSpecifies whether to apply color balancing.
stepinteger0.1The step size used during color balancing. Valid only when color_balance is true. A larger step speeds up processing but may reduce accuracy.
iterationinteger50000The number of color balancing iterations. Valid only when color_balance is true. More iterations produce better results but take longer.
parallelinteger1The degree of parallelism (DOP). Valid values: 1 to 64.

Description

ST_MosaicFrom merges multiple raster objects into a single output raster.

Source raster requirements

All source rasters must satisfy the following conditions:

  • Same number of bands.

  • All georeferenced, or none georeferenced. When all rasters are georeferenced, world coordinates are used for the mosaic operation; SRIDs and affine parameters may differ across rasters.

  • Pixel types may differ.

How color balancing works

ST_MosaicFrom uses gamma correction-based color balancing, applied in three steps:

  1. Generate a global color plane. All source rasters are resampled and polynomial fitting is applied to produce a global pixel matrix, which serves as the color reference.

  2. Calculate a local color plane. For each source raster, bilinear interpolation derives a local color plane that maps individual raster colors to the global pixel matrix.

  3. Apply gamma correction. For each pixel position, two values are computed—one from the local color plane and one from the global color plane. The gamma correction parameter is then calculated to align the two values, producing a color-balanced output.

Examples

Basic mosaic

INSERT INTO raster_obj
VALUES (
  1,
  ST_MosaicFrom(
    ARRAY(SELECT raster_obj FROM raster_table WHERE id < 10),
    'chunk_table_mosaic',
    '',
    ''
  )
);

Mosaic with SRID and cell size

UPDATE raster_table
SET raster_obj = ST_MosaicFrom(
  ARRAY(SELECT raster_obj FROM raster_table WHERE id < 10),
  'chunk_table_mosaic',
  '',
  '{"srid":4326,"cell_size":[0.00002,0.00002]}'
)
WHERE id = 11;

Mosaic with nodata handling

INSERT INTO rat_mosaicfrom
VALUES (
  110,
  ST_MosaicFrom(
    ARRAY(SELECT rast FROM rat_mosaicfrom WHERE id < 5),
    'rbt_mosaic',
    '{"chunking":true,"chunkdim":"(256,256,3)","compression":"jpeg","quality":75,"interleaving":"bsq","celltype":"8bui"}',
    '{"resample":"Average","srid":4326,"cell_size":[0.00002,0.00002],"nodata":true,"nodataValue":[255,255,255]}'
  )
);

Mosaic with parallelism

INSERT INTO raster_obj
VALUES (
  1,
  ST_MosaicFrom(
    ARRAY(SELECT raster_obj FROM raster_table WHERE id < 10),
    'chunk_table_mosaic',
    '',
    '{"srid":4326,"cell_size":[0.00002,0.00002],"parallel":4}'
  )
);

Mosaic with color balancing and parallelism

INSERT INTO raster_obj
VALUES (
  1,
  ST_MosaicFrom(
    ARRAY(SELECT raster_obj FROM raster_table WHERE id < 10),
    'chunk_table_mosaic',
    '',
    '{"srid":4326,"cell_size":[0.00002,0.00002],"color_balance":true,"parallel":4}'
  )
);