ST_World2RastCoord

Updated at:
Copy as MD

Converts world coordinates to raster pixel coordinates (column and row numbers) using an inverse affine transformation at the specified pyramid level.

Syntax

point ST_World2RastCoord(raster raster_obj, integer pyramidLevel, point coord);
geometry ST_World2RastCoord(raster raster_obj, integer pyramidLevel, geometry geom);

Parameters

Parameter Type Description
raster_obj raster The raster object.
pyramidLevel integer The pyramid level.
coord point The world coordinates to convert.
geom geometry The geometry to convert.

Usage notes

  • The raster object must have complete spatial reference information.

  • In the returned value, the x coordinate is the column number and the y coordinate is the row number.

  • Pass a point to receive a point. Pass a geometry to receive a geometry.

Examples

Convert world coordinates to pixel coordinates:

SELECT st_world2rastcoord(rast, 0, '(117.3378,26.9020)'::point)
FROM tb_dem
WHERE id = 2;

 st_world2rastcoord
--------------------
 (53205,32518)

The result (53205,32518) means column 53205, row 32518 at pyramid level 0.

Round-trip verification with ST_Rast2WorldCoord:

SELECT ST_AsText(
  ST_world2RastCoord(rast, 0, ST_Rast2WorldCoord(rast, 0, 'POINT(511 0)'::geometry))
)
FROM tb_world2rast;

  st_astext
--------------
 POINT(511 0)

This example converts pixel coordinates to world coordinates using ST_Rast2WorldCoord, then converts them back to pixel coordinates using ST_World2RastCoord. The round-trip result matches the original input.