Converts world coordinates (longitude/latitude) to raster pixel coordinates (column and row) using an inverse affine transformation.
Syntax
point ST_World2RastCoord(raster raster_obj, integer pyramidLevel, point coord);
geometry ST_World2RastCoord(raster raster_obj, integer pyramidLevel, geometry geom);Parameters
| Parameter | Description |
|---|---|
raster_obj | The raster object. Must have a valid spatial reference system identifier (SRID). |
pyramidLevel | The pyramid level of the raster. |
coord | The world coordinates of the cell, as a point value. |
geom | The world coordinates of the cell, as a geometry value. |
Description
Returns the pixel coordinates of a cell as column and row numbers. The x coordinate of the result is the column number in the raster; the y coordinate is the row number.
Both overloads accept world coordinates and return equivalent column and row values — the point overload returns a point, and the geometry overload returns a geometry. The raster object must have a valid SRID.
Examples
Example 1: Point input
Convert a geographic coordinate to its corresponding pixel position in a DEM raster at pyramid level 0.
SELECT st_world2rastcoord(rast, 0, '(117.3378,26.9020)'::point)
FROM tb_dem
WHERE id = 2;Output:
st_world2rastcoord
--------------------
(53205,32518)Example 2: Round-trip verification with geometry input
Convert a pixel position to world coordinates using ST_Rast2WorldCoord, then pass the result back to ST_World2RastCoord to confirm the round-trip is lossless.
SELECT ST_AsText(
ST_world2RastCoord(rast, 0, ST_Rast2WorldCoord(rast, 0, 'POINT(511 0)'::geometry))
)
FROM tb_world2rast;Output:
st_astext
--------------
POINT(511 0)See also
ST_Rast2WorldCoord— the inverse function; converts pixel coordinates (column and row) back to world coordinates