All Products
Search
Document Center

PolarDB:GanosBase: Obtain the spatial extent of raster pixels

Last Updated:Mar 28, 2026

GanosBase 4.3 introduces six functions that convert raster pixels into spatial geometry objects, enabling vector-raster multimodal analysis directly in SQL without external GIS software.

New functions

The six functions belong to two groups:

  • Singular functions (ST_PixelAsPolygon, ST_PixelAsPoint, ST_PixelAsCentroid): return a single geometry for one specified pixel.

  • Plural (set-returning) functions (ST_PixelAsPolygons, ST_PixelAsPoints, ST_PixelAsCentroids): return one row per pixel in the raster.

FunctionDescriptionReferences
ST_PixelAsPolygonReturns the spatial range of the specified pixel as a rectangle.ST_PixelAsPolygon
ST_PixelAsPointReturns the spatial range of the specified pixel as a point. The point can be the upper-left corner or the centroid of the geometry object.ST_PixelAsPoint
ST_PixelAsCentroidReturns the centroid of the specified pixel's spatial range.ST_PixelAsCentroid
ST_PixelAsPolygonsReturns the spatial and attribute information of all pixels in the raster object. The returned result includes the row number, column number, band number, pixel value, and corresponding spatial polygon range of the pixel in the raster object.ST_PixelAsPolygons
ST_PixelAsPointsReturns the spatial and attribute information of all pixels in the raster object. The returned result includes the row number, column number, band number, pixel value, and corresponding spatial point information of the pixel. The spatial point can be the upper-left corner or the centroid of the object.ST_PixelAsPoints
ST_PixelAsCentroidsReturns the spatial and attribute information of all pixels in the raster object. The returned result includes the row number, column number, band number, pixel value, and information about the centroid of the pixel's spatial range.ST_PixelAsCentroids

Best practice: identify devices by temperature range

This example uses temperature raster data and device location vector data in China to identify devices that measure temperatures within a specified range. The workflow has three stages: import raster and vector data, extract the spatial range of pixels that match the temperature condition, then run a spatial containment analysis against the device table.

Data used:

  • Temperature data for China in a NetCDF raster file

  • Device location data for China in shapefile format

The spatial reference system used in this example is WGS84 (SRID 4326).

Import data

Import temperature data

-- Create the GANOS_RASTER extension
CREATE EXTENSION GANOS_RASTER CASCADE;

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

-- Import the NetCDF temperature data and set the spatial reference to SRID 4326
INSERT INTO temperature
VALUES (1, ST_SetSrid(ST_ImportFrom('chunk_table', 'OSS://<id>:<key>@<endpoint>/bucket/path/file.nc'), 4326));

After importing, verify the metadata:

SELECT st_metadata(rast) FROM temperature WHERE id = 1;

Import device data

-- Create the ganos_fdw extension
CREATE EXTENSION ganos_fdw;

-- Register the shapefile as a foreign table
SELECT ST_RegForeignTables('OSS://<id>:<key>@<endpoint>/bucket/path/file.shp');

-- Import data into the database and create a spatial index
CREATE TABLE devices AS SELECT * FROM <foreign_table_name>;
CREATE INDEX idx_devices_geom ON devices USING Gist(geom);
The preceding method uses FDW (Foreign Data Wrapper) to import device data from Object Storage Service (OSS). For other import methods, see Best practices for importing vector and raster data to GanosBase.

Extract temperature range

The following statement converts temperature pixels into polygons and merges those in the 27°C–28°C range (raw values 270–280) into a single geometry:

-- Merge pixels with temperatures between 27°C and 28°C
WITH tmp AS (
    SELECT (ST_PixelAsPolygons(rast)).*
    FROM temperature
    WHERE id = 1
)
SELECT ST_Union(geom)
FROM tmp
WHERE value >= 270 AND value < 280;

For high-resolution raster data, this statement may be slow because it processes every pixel. Use ST_Reclassify to mark out-of-range pixels as nodata before calling ST_PixelAsPolygons, which reduces the number of rows returned:

-- Step 1: Reclassify — pixels outside 270–280 become nodata (value 0)
WITH tmp AS (
    SELECT ST_Reclassify(
               rast,
               '[{"band":0,"remap":{"(-100,270,280, 1000]":"0,1,0"}, "nodata":false, "nodataValue":0}]',
               '{"chunktable":"reclass_chunk_table"}'
           ) AS rast
    FROM temperature
    WHERE id = 1
),

-- Step 2: Extract polygons only for the retained pixels
tmp2 AS (
    SELECT (ST_PixelAsPolygons(rast)).*
    FROM tmp
)
SELECT ST_Union(geom)
FROM tmp2;

For the full ST_Reclassify syntax, see ST_Reclassify.

Spatial overlay analysis

Run a spatial containment analysis to count devices located within the extracted temperature range:

WITH tmp AS (
    SELECT (ST_PixelAsPolygons(rast)).*
    FROM temperature WHERE id = 1
),
tmp2 AS (
    SELECT ST_Union(geom) AS geom
    FROM tmp
    WHERE value >= 270 AND value < 279
)
SELECT count(*)
FROM devices, tmp2
WHERE ST_Contains(tmp2.geom, devices.geom);

Expected result:

 count
-------
  1879

Conclusion

GanosBase provides the storage, computing, and analysis capabilities for spatio-temporal raster data to simplify complex vector-raster multimodal analysis into a few GeoSQL statements. Spatiotemporal data can be processed in databases instead of traditional GIS software. This simplifies program logic, reduces development complexity and maintenance costs, and allows industry professionals with advanced GIS capabilities on the cloud.