ST_Reclassify

Updated at:

ST_Reclassify remaps pixel values in a raster object's bands to new values according to interval-based rules. The returned raster keeps the same spatial reference and resolution as the input; the number of output bands is determined by the reclassexpr parameter.

Syntax

raster ST_Reclassify(raster raster_obj,
                 cstring reclassexpr default NULL
                 cstring storageOption default '')

Parameters

Parameter Description
raster_obj The raster object to reclassify.
reclassexpr A JSON array that defines the reclassification rules for each band.
storageOption A JSON string that controls how the output raster is stored.

reclassexpr

reclassexpr is a JSON array. Each element configures one band.

Parameter Type Default Description
band integer Band index, starting from 0.
remap object Maps input pixel value ranges to output values. See remap syntax.
nodata boolean false true: pixels with nodata values produce nodata output. false: nodata pixels are treated as regular numeric values.
nodataValue float8 0 The nodata value assigned in the output.

remap syntax

Each key-value pair in remap maps an input range to an output value or range.

Interval notation

Symbol Meaning Example
[ Greater than or equal to (≥) [100 → value ≥ 100
( Greater than (>) (100 → value > 100
] Less than or equal to (≤) 100] → value ≤ 100
) Less than (<) 100) → value < 100

The default interval is (] (open on the left, closed on the right).

Complete interval examples

Expression Equivalent condition
[a,b] a ≤ value ≤ b
(a,b] a < value ≤ b
[a,b) a ≤ value < b
(a,b) a < value < b

Mapping methods

Three mapping methods are available. All use comma-separated values to define input ranges and output values.

Method Input values Output values Example
Range-to-value n+1 values (n intervals) n values "(300,400,500]":"80,90"
Range-to-range n values n values "300,400,500":"80,90,100" and "[300,400,500]":"80,90,100"
Value-to-value 1 value 1 value "10":"1"

Rules:

  • Pixel values outside all mapped ranges are treated as nodata.

  • Ranges must not overlap (the same pixel value cannot belong to two ranges).

reclassexpr examples

Example 1: Single band, range-to-value mapping

Reclassify band 0:

  • 0 < value ≤ 100 → 20

  • 100 < value ≤ 200 → 50

  • All other values → nodata

[
  {
    "band": 0,
    "remap": {
      "(0,100,200]": "20,50"
    }
  }
]

Example 2: Single band, multiple disjoint ranges

Multiple segmented values are supported. Pixel values beyond the range are set to nodata values.

[
  {
    "band": 0,
    "remap": {
      "(0,100,200]": "20,50",
      "(300,400,500]": "80,90,100"
    }
  }
]

Example 3: Two bands with nodata control

Band 0 uses nodata: true—pixels with nodata values produce nodata output, and out-of-range pixels get a nodata value of 999. Band 1 uses nodata: false—nodata pixels are calculated as regular values, and out-of-range pixels get a nodata value of 0.

[
  {
    "band": 0,
    "remap": {
      "(0,100,200]": "20,50"
    },
    "nodata": true,
    "nodataValue": 999
  },
  {
    "band": 1,
    "remap": {
      "(400,600,800]": "20,90,130"
    },
    "nodata": false,
    "nodataValue": 0
  }
]

For band 1, the range-to-range mapping "(400,600,800]":"20,90,130" maps values within each interval:

  • 400 < value ≤ 600 → interpolated between 20 and 90

  • 600 < value ≤ 800 → interpolated between 90 and 130

storageOption

Parameter Type Default Description
chunking boolean Same as input Whether to store the output as chunks.
chunkdim string Same as input Chunk dimensions. Takes effect only when chunking is true.
chunktable string '' Name of the chunk table. An empty string generates a temporary chunk table with a random name, valid for the current session only. Specify a name to retain the output across sessions.
compression string Same as input Compression algorithm. Valid values: none, jpeg, zlib, png, lzo, lz4.
quality integer Same as input Compression quality. Applies only to the jpeg algorithm.
interleaving string Same as input Interleaving method: bip (band interleaved by pixel, BIP), bil (band interleaved by line, BIL), or bsq (band sequential, BSQ).
endian string Same as input Byte order: NDR (little endian) or XDR (big endian).
celltype string Same as input Pixel type of the output raster.

Examples

The following examples reclassify band 0 of a raster using the interval (0,100,200]20,50.

Store results in a temporary table (current session only)

-- Create the temporary table
CREATE TEMP TABLE rast_reclassify_result_temp(id integer, rast raster);

-- Run reclassification and store the result
INSERT INTO rast_reclassify_result_temp(id, rast)
SELECT 1, ST_Reclassify(rast, '[{"band":0,"remap":{"(0,100,200]":"20,50"}}]')
FROM reclass_table;

Store results in a permanent table

-- Create the permanent table
CREATE TABLE rast_reclassify_result(id integer, rast raster);

-- Run reclassification and store the result
INSERT INTO rast_reclassify_result(id, rast)
SELECT 1, ST_Reclassify(rast, '[{"band":0,"remap":{"(0,100,200]":"20,50"}}]')
FROM reclass_table;
The chunktable parameter in storageOption defaults to an empty string, which creates a temporary chunk table valid only for the current session. To retain the reclassified raster across sessions, specify a chunktable name.