Raster Reference
The functions given below are the ones which a user of PostGIS Raster is
likely to need and which are currently available in PostGIS Raster. There are other functions which are required support
functions to the raster objects which are not of use to a general
user.
raster is a new PostGIS type for storing and analyzing raster data.
For more information about Raster, please refer to PostGIS Raster Home Page.
Loading and Creating Rasters
For most use cases, you will create PostGIS rasters by loading existing raster files using the packaged raster2pgsql raster loader.
Using the raster2pgsql Raster Loader
The raster2pgsql.py is a raster loader python script that utilizes Python, PyGDAL, and NumPy to convert any GDAL supported raster format into sql suitable for loading into a PostGIS raster table.
It is capable of loading folders of raster files as well as creating overviews of rasters.
An example session using the loader to create an input file and uploading it might look like this:
python raster2pgsql.py -s 4269 -I -r *.tif -F myschema.demelevation -o elev.sql
psql -d gisdb -f elev.sql
A conversion and upload can be done all in one step using UNIX pipes:
python raster2pgsql.py -s 4269 -I -r *.tif -F myschema.demelevation | psql -d gisdb
For the examples in this reference we will be using a raster table of dummy rasters - Formed with the following code
CREATE TABLE dummy_rast(rid integer, rast raster);
INSERT INTO dummy_rast(rid, rast)
VALUES (1,
('01' -- little endian (uint8 ndr)
||
'0000' -- version (uint16 0)
||
'0000' -- nBands (uint16 0)
||
'0000000000000040' -- scaleX (float64 2)
||
'0000000000000840' -- scaleY (float64 3)
||
'000000000000E03F' -- ipX (float64 0.5)
||
'000000000000E03F' -- ipY (float64 0.5)
||
'0000000000000000' -- skewX (float64 0)
||
'0000000000000000' -- skewY (float64 0)
||
'00000000' -- SRID (int32 0)
||
'0A00' -- width (uint16 10)
||
'1400' -- height (uint16 20)
)::raster
),
-- Raster: 5 x 5 pixels, 3 bands, PT_8BUI pixel type, NODATA = 0
(2, ('01000003009A9999999999A93F9A9999999999A9BF000000E02B274A' ||
'41000000007719564100000000000000000000000000000000FFFFFFFF050005000400FDFEFDFEFEFDFEFEFDF9FAFEF' ||
'EFCF9FBFDFEFEFDFCFAFEFEFE04004E627AADD16076B4F9FE6370A9F5FE59637AB0E54F58617087040046566487A1506CA2E3FA5A6CAFFBFE4D566DA4CB3E454C5665')::raster);
Raster Management Functions
AddRasterColumn
Adds a raster column to an existing table and generates column constraint on the new column to enforce srid.
text AddRasterColumn
varchar
table_name
varchar
column_name
integer
srid
varchar[]
pixel_types
boolean
out_db
boolean
regular_blocking
double precision[]
no_data_values
double precision
scale_x
double precision
scale_y
integer
blocksize_x
integer
blocksize_y
geometry
envelope
text AddRasterColumn
varchar
schema_name
varchar
table_name
varchar
column_name
integer
srid
varchar[]
pixel_types
boolean
out_db
boolean
regular_blocking
double precision[]
no_data_values
double precision
scale_x
double precision
scale_y
integer
blocksize_x
integer
blocksize_y
geometry
envelope
text AddRasterColumn
varchar
catalog_name
varchar
schema_name
varchar
table_name
varchar
column_name
integer
srid
varchar[]
pixel_types
boolean
out_db
boolean
regular_blocking
double precision[]
no_data_values
double precision
scale_x
double precision
scale_y
integer
blocksize_x
integer
blocksize_y
geometry
envelope
Description
Adds a raster column to an existing table and also generates constraints on the new column. Currently only constrains the SRID.
The schema_name is the name of the table schema (unused
for pre-schema PostgreSQL installations). The srid
must be an integer value reference to an entry in the SPATIAL_REF_SYS
table. The pixel_types must be an array of pixel types as described in , one for each band.
An error is thrown if the schemaname doesn't exist
(or not visible in the current search_path) or the specified SRID,
pixel types are invalid.
raster2pgsql.py loader uses this function to register raster tables
Views and derivatively created spatial tables will need to be registered in raster_columns manually,
since AddRasterColumn also adds a raster column which is not needed when you already have a raster column.
Examples
SELECT AddRasterColumn('public', 'myrasters', 'rast',4326,
'{8BUI,8BUI,8BUI,8BUI}',
false, true, '{255,255,255,255}', 0.25,-0.25,200,300, null);
public.myrasters.rast
srid:4326 pixel_types:{8BUI,8BUI,8BUI,8BUI}
out_db:false
regular_blocking:true
nodata_values:'{255,255,255,255}'
scale_x:'0.25'
scale_y:'-0.25' blocksize_x:'200' blocksize_y:'300' extent:NULL
--- After loading the data, you can currect the geometry column
UPDATE raster_columns SET extent = (SELECT ST_Union(ST_Envelope(rast)) FROM myrasters)
WHERE r_table_schema = 'public'
AND r_table_name = 'myrasters'
AND r_column = 'rast';
See Also
, , , ,
DropRasterColumn
Removes a raster column from a table.
text DropRasterColumn
varchar
table_name
varchar
column_name
text DropRasterColumn
varchar
schema_name
varchar
table_name
varchar
column_name
text DropRasterColumn
varchar
catalog_name
varchar
schema_name
varchar
table_name
varchar
column_name
Description
Removes a raster column from a table. Note that
schema_name will need to match the r_table_schema field of the table's
row in the raster_columns table.
Examples
SELECT DropRasterColumn ('my_schema','my_raster_table','rast');
----RESULT output ---
my_schema.my_raster_table.rast effectively removed.
See Also
,
DropRasterTable
Drops a table and all its references in
raster_columns.
text DropRasterTable
varchar
table_name
text DropRasterTable
varchar
schema_name
varchar
table_name
text DropRasterTable
varchar
catalog_name
varchar
schema_name
varchar
table_name
Description
Drops a table and all its references in raster_columns. Note:
uses current_schema() on schema-aware pgsql installations if schema is
not provided.
Examples
SELECT DropRasterTable ('my_schema','my_raster_table');
----RESULT output ---
my_schema.my_raster_table dropped.
See Also
,
PostGIS_Raster_Lib_Build_Date
Reports full raster library build date
text PostGIS_Raster_Lib_Build_Date
Description
Reports raster build date
Examples
SELECT PostGIS_Raster_Lib_Build_Date();
postgis_raster_lib_build_date
-----------------------------
2010-04-28 21:15:10
See Also
PostGIS_Raster_Lib_Version
Reports full raster version and build configuration
infos.
text PostGIS_Raster_Lib_Version
Description
Reports full raster version and build configuration
infos.
Examples
SELECT PostGIS_Raster_Lib_Version();
postgis_raster_lib_version
-----------------------------
0.1.6d
See Also
Raster Constructors
ST_MakeEmptyRaster
Returns an empty raster of given dimensions, pixel x y, skew and spatial reference system with no bands. If a raster is passed in, returns a new raster with same meta data properties.
raster ST_MakeEmptyRaster
raster rast
raster ST_MakeEmptyRaster
integer width
integer height
float8 ipx
float8 ipy
float8 scalex
float8 scaley
float8 skewx
float8 skewy
raster ST_MakeEmptyRaster
integer width
integer height
float8 ipx
float8 ipy
float8 scalex
float8 scaley
float8 skewx
float8 skewy
integer srid
raster ST_MakeEmptyRaster
integer width
integer height
double precision upperleftx
double precision upperlefty
double precision scale
Description
Returns an empty raster of given dimensions, pixel width in spatial coordinates x (ipx), pixel height y in spatial coordinates (ipy), (scale - uses same size for pixel height (ipy) and width (ipx)), skew and spatial reference system with no bands. If an existing raster is passed in,
it returns a new raster with the same meta data settings (minus number of bands and band types). If no srid is specified it defaults to -1, though this may change to 0 in future.
After you create an empty raster you
probably want to add bands to it and hmm maybe edit it. Refer to to define bands and to set pixel values.
Examples
INSERT INTO dummy_rast(rid,rast)
VALUES(3, ST_MakeEmptyRaster( 100, 100, 0.0005, 0.0005, 1, 1, 0, 0, 4326) );
--use an existing raster as template for new raster
INSERT INTO dummy_rast(rid,rast)
SELECT 4, ST_MakeEmptyRaster(rast)
FROM dummy_rast WHERE rid = 3;
-- output meta data of rasters we just added
SELECT rid, (md).*
FROM (SELECT rid, ST_MetaData(rast) As md
FROM dummy_rast
WHERE rid IN(3,4)) As foo;
-- output --
rid | upperleftx | upperlefty | width | height | scalex | scaley | skewx | skewy | srid | numbands
-----+------------+------------+-------+--------+------------+------------+-------+-------+------+----------
3 | 0.0005 | 0.0005 | 100 | 100 | 1 | 1 | 0 | 0 | 4326 | 0
4 | 0.0005 | 0.0005 | 100 | 100 | 1 | 1 | 0 | 0 | 4326 | 0
See Also
, , , , , , ,
Raster Accessors
ST_GeoReference
Returns the georeference meta data in GDAL or ESRI format as commonly seen in a world file. Default is GDAL.
text ST_GeoReference
raster rast
text ST_GeoReference
raster rast
text format
Description
Returns the georeference meta data including carriage return in GDAL or ESRI format as commonly seen in a world file. Default is GDAL if no type specified. type is string 'GDAL' or 'ESRI'.
Difference between format representations is as follows:
GDAL:
scalex
skewy
skewx
scaley
upperleftx
upperlefty
ESRI:
scalex
skewy
skewx
scaley
upperleftx + scalex*0.5
upperlefty + scaley*0.5
Examples
SELECT ST_GeoReference(rast, 'ESRI') As esri_ref, ST_GeoReference(rast, 'GDAL') As gdal_ref
FROM dummy_rast WHERE rid=1;
esri_ref | gdal_ref
--------------+--------------
2.0000000000 | 2.0000000000
0.0000000000 : 0.0000000000
0.0000000000 : 0.0000000000
3.0000000000 : 3.0000000000
1.5000000000 : 0.5000000000
2.0000000000 : 0.5000000000
See Also
, ,
ST_Height
Returns the height of the raster in pixels?
integer ST_Height
raster rast
Description
Returns the height of the raster.
Examples
SELECT rid, ST_Height(rast) As rastheight
FROM dummy_rast;
rid | rastheight
-----+------------
1 | 20
2 | 5
See Also
ST_MetaData
Returns basic meta data about a raster object such as skew, rotation, upper,lower left etc.
record ST_MetaData
raster rast
Description
Returns basic meta data about a raster object such as skew, rotation, upper,lower left etc. Columns returned
upperleftx | upperlefty | width | height | scalex | scaley | skewx | skewy | srid | numbands
Examples
SELECT rid, (foo.md).*
FROM (SELECT rid, ST_MetaData(rast) As md
FROM dummy_rast) As foo;
rid | upperleftx | upperlefty | width | height | scalex | scaley | skewx | skewy | srid | numbands
-----+------------+------------+-------+--------+-----------+-----------+-------+-------+------+----------
1 | 0.5 | 0.5 | 10 | 20 | 2 | 3 | 0 | 0 | 0 | 0
2 | 3427927.75 | 5793244 | 5 | 5 | 0.05 | -0.05 | 0 | 0 | -1 | 3
See Also
,
ST_NumBands
Returns the number of bands in the raster object.
integer ST_NumBands
raster rast
Description
Returns the number of bands in the raster object.
Examples
SELECT rid, ST_NumBands(rast) As numbands
FROM dummy_rast;
rid | numbands
----+----------
1 | 0
2 | 3
See Also
ST_ScaleX
Returns the x size of pixels in units of coordinate reference system?
float8 ST_ScaleX
raster rast
Description
Returns the x size of pixels in units of coordinate reference system?
Changed: 2.0.0. In WKTRaster versions this was called ST_PixelSizeX.
Examples
SELECT rid, ST_ScaleX(rast) As rastpixwidth
FROM dummy_rast;
rid | rastpixwidth
-----+--------------
1 | 2
2 | 0.05
See Also
ST_ScaleY
Returns the y size of pixels in units of coordinate reference system?
float8 ST_ScaleY
raster rast
Description
Returns the y size of pixels in units of coordinate reference system. May be negative. Refer to World File
for more details.
Changed: 2.0.0. In WKTRaster versions this was called ST_PixelSizeY.
Examples
SELECT rid, ST_ScaleY(rast) As rastpixheight
FROM dummy_rast;
rid | rastpixheight
-----+---------------
1 | 3
2 | -0.05
See Also
ST_Raster2WorldCoordX
Returns the geometric x coordinate upper left of a raster, column and row. Numbering of columns
and rows starts at 1.
float8 ST_Raster2WorldCoordX
raster rast
integer xcolumn
float8 ST_Raster2WorldCoordX
raster rast
integer xcolumn
integer yrow
Description
Returns the upper left x coordinate of a raster column row in geometric units of the georeferenced raster.
Numbering of columns and rows starts at 1 but if you pass in a negative number or number higher than number of
columns in raster, it will give you
coordinates outside of the raster file to left or right with the assumption that the
skew and pixel sizes are same as selected raster.
For non-skewed rasters, providing the x column is sufficient. For skewed rasters,
the georeferenced coordinate is a function of the ST_ScaleX and SkewX and row and column.
An error will be raised if you give just the x column for a skewed raster.
Examples
-- non-skewed raster providing column is sufficient
SELECT rid, ST_Raster2WorldCoordX(rast,1) As x1coord,
ST_Raster2WorldCoordX(rast,2) As x2coord,
ST_ScaleX(rast) As pixelx
FROM dummy_rast;
rid | x1coord | x2coord | pixelx
-----+------------+-----------+--------
1 | 0.5 | 2.5 | 2
2 | 3427927.75 | 3427927.8 | 0.05
-- for fun lets skew it
SELECT rid, ST_Raster2WorldCoordX(rast,1,1) As x1coord,
ST_Raster2WorldCoordX(rast,2,3) As x2coord,
ST_ScaleX(rast) As pixelx
FROM (SELECT rid, ST_SetSkew(rast,100.5,0) As rast FROM dummy_rast) As foo;
rid | x1coord | x2coord | pixelx
-----+------------+-----------+--------
1 | 0.5 | 203.5 | 2
2 | 3427927.75 | 3428128.8 | 0.05
See Also
, , ,
ST_Raster2WorldCoordY
Returns the geometric y coordinate upper left corner of a raster, column and row. Numbering of columns
and rows starts at 1.
float8 ST_Raster2WorldCoordY
raster rast
integer yrow
float8 ST_Raster2WorldCoordY
raster rast
integer xcolumn
integer yrow
Description
Returns the upper left y coordinate of a raster column row in geometric units of the georeferenced raster.
Numbering of columns and rows starts at 1 but if you pass in a negative number or number higher than number of
columns/rows in raster, it will give you
coordinates outside of the raster file to left or right with the assumption that the
skew and pixel sizes are same as selected raster tile.
For non-skewed rasters, providing the y column is sufficient. For skewed rasters,
the georeferenced coordinate is a function of the ST_ScaleY and SkewY and row and column.
An error will be raised if you give just the y row for a skewed raster.
Examples
-- non-skewed raster providing row is sufficient
SELECT rid, ST_Raster2WorldCoordY(rast,1) As y1coord,
ST_Raster2WorldCoordY(rast,3) As y2coord,
ST_ScaleY(rast) As pixely
FROM dummy_rast;
rid | y1coord | y2coord | pixely
-----+---------+-----------+--------
1 | 0.5 | 6.5 | 3
2 | 5793244 | 5793243.9 | -0.05
-- for fun lets skew it
SELECT rid, ST_Raster2WorldCoordY(rast,1,1) As y1coord,
ST_Raster2WorldCoordY(rast,2,3) As y2coord,
ST_ScaleY(rast) As pixely
FROM (SELECT rid, ST_SetSkew(rast,0,100.5) As rast FROM dummy_rast) As foo;
rid | y1coord | y2coord | pixely
-----+---------+-----------+--------
1 | 0.5 | 107 | 3
2 | 5793244 | 5793344.4 | -0.05
See Also
, , ,
ST_SkewX
Returns the georeference X skew (or rotation parameter)
float8 ST_SkewX
raster rast
Description
Returns the georeference X skew (or rotation parameter). Refer to World File
for more details.
Examples
SELECT rid, ST_SkewX(rast) As skewx, ST_SkewY(rast) As skewy,
ST_GeoReference(rast) as georef
FROM dummy_rast;
rid | skewx | skewy | georef
-----+-------+-------+--------------------
1 | 0 | 0 | 2.0000000000
: 0.0000000000
: 0.0000000000
: 3.0000000000
: 0.5000000000
: 0.5000000000
:
2 | 0 | 0 | 0.0500000000
: 0.0000000000
: 0.0000000000
: -0.0500000000
: 3427927.7500000000
: 5793244.0000000000
See Also
, ,
ST_SkewY
Returns the georeference Y skew (or rotation parameter)
float8 ST_SkewY
raster rast
Description
Returns the georeference Y skew (or rotation parameter). Refer to World File
for more details.
Examples
SELECT rid, ST_SkewX(rast) As skewx, ST_SkewY(rast) As skewy,
ST_GeoReference(rast) as georef
FROM dummy_rast;
rid | skewx | skewy | georef
-----+-------+-------+--------------------
1 | 0 | 0 | 2.0000000000
: 0.0000000000
: 0.0000000000
: 3.0000000000
: 0.5000000000
: 0.5000000000
:
2 | 0 | 0 | 0.0500000000
: 0.0000000000
: 0.0000000000
: -0.0500000000
: 3427927.7500000000
: 5793244.0000000000
See Also
, ,
ST_SRID
Returns the spatial reference identifier of the raster as defined in spatial_ref_sys table.
integer ST_SRID
raster rast
Description
Returns the spatial reference identifier of the raster object as defined in the spatial_ref_sys table.
From PostGIS 2.0+ the srid of a non-georeferenced raster/geometry is 0 instead of the prior -1.
Examples
SELECT ST_SRID(rast) As srid
FROM dummy_rast WHERE rid=1;
srid
----------------
0
See Also
,
ST_UpperLeftX
Returns the upper left x coordinate of raster in projected spatial ref.
float8 ST_UpperLeftX
raster rast
Description
Returns the upper left x coordinate of raster in projected spatial ref.
Examples
SELECt rid, ST_UpperLeftX(rast) As ulx
FROM dummy_rast;
rid | ulx
-----+------------
1 | 0.5
2 | 3427927.75
See Also
, ,
ST_UpperLeftY
Returns the upper left y coordinate of raster in projected spatial ref.
float8 ST_UpperLeftY
raster rast
Description
Returns the upper left y coordinate of raster in projected spatial ref.
Examples
SELECT rid, ST_UpperLeftY(rast) As uly
FROM dummy_rast;
rid | uly
-----+---------
1 | 0.5
2 | 5793244
See Also
, ,
ST_Width
Returns the width of the raster in pixels?
integer ST_Width
raster rast
Description
Returns the width of the raster.
Examples
SELECT ST_Width(rast) As rastwidth
FROM dummy_rast WHERE rid=1;
rastwidth
----------------
10
See Also
ST_World2RasterCoordX
Returns the column in the raster of the point geometry (pt) or a x y world coordinate (xw, yw) represented
in world spatial reference system of raster.
integer ST_World2RasterCoordX
raster rast
geometry pt
integer ST_World2RasterCoordX
raster rast
double precision xw
integer ST_World2RasterCoordX
raster rast
double precision xw
double precision yw
Description
Returns the column in the raster of the point geometry (pt) or a x y world coordinate (xw, yw). A point, or (both xw and yw world coordinates are required if a raster is skewed). If a raster
is not skewed then xw is sufficient. World coordinates are in the spatial reference coordinate system of the raster.
Examples
SELECT rid, ST_World2RasterCoordX(rast,3427927.8) As xcoord,
ST_World2RasterCoordX(rast,3427927.8,20.5) As xcoord_xwyw,
ST_World2RasterCoordX(rast,ST_GeomFromText('POINT(3427927.8 20.5)',ST_SRID(rast))) As ptxcoord
FROM dummy_rast;
rid | xcoord | xcoord_xwyw | ptxcoord
-----+---------+---------+----------
1 | 1713964 | 1713964 | 1713964
2 | 1 | 1 | 1
See Also
, ,
ST_World2RasterCoordY
Returns the row in the raster of the point geometry (pt) or a x y world coordinate (xw, yw) represented
in world spatial reference system of raster.
integer ST_World2RasterCoordY
raster rast
geometry pt
integer ST_World2RasterCoordY
raster rast
double precision xw
integer ST_World2RasterCoordY
raster rast
double precision xw
double precision yw
Description
Returns the row in the raster of the point geometry (pt) or a x y world coordinate (xw, yw). A point, or (both xw and yw world coordinates are required if a raster is skewed). If a raster
is not skewed then xw is sufficient. World coordinates are in the spatial reference coordinate system of the raster.
Examples
SELECT rid, ST_World2RasterCoordY(rast,20.5) As ycoord,
ST_World2RasterCoordY(rast,3427927.8,20.5) As ycoord_xwyw,
ST_World2RasterCoordY(rast,ST_GeomFromText('POINT(3427927.8 20.5)',ST_SRID(rast))) As ptycoord
FROM dummy_rast;
rid | ycoord | ycoord_xwyw | ptycoord
-----+-----------+-------------+-----------
1 | 7 | 7 | 7
2 | 115864471 | 115864471 | 115864471
See Also
, ,
Raster Band Accessors and Constructors
ST_AddBand
Returns a raster with the new band of given type added with given initial value
raster ST_AddBand
raster rast
text pixeltype
raster ST_AddBand
raster rast
text pixeltype
double precision initialvalue
raster ST_AddBand
raster rast
text pixeltype
double precision initialvalue
double precision nodataval
raster ST_AddBand
raster rast
integer index
text pixeltype
raster ST_AddBand
raster rast
integer band_num
text pixeltype
double precision initialvalue
raster ST_AddBand
raster rast
integer band_num
text pixeltype
double precision initialvalue
double precision nodataval
Description
Returns a raster with a new band added in given position (band number), of given type, of given initial value, and of given nodata value. If no index is specified, the band is added to the end. Pixel type is a string representation
of one of the pixel types specified in . If an existing index is specified all subsequent bands >= that index are incremented by 1.
If an initial value greater than the max of the pixel type is specified, then the initial value is set to the highest value allowed by the pixel type.
Examples
-- Add another band of type 8 bit unsigned integer with pixels initialized to 200
UPDATE dummy_rast
SET rast = ST_AddBand(rast,'8BUI',200)
WHERE rid = 1;
-- Create an empty raster 100x100 units, with upper left right at 0, add 2 bands (band 1 is 0/1 boolean bit switch, band2 allows values 0-15)
INSERT INTO dummy_rast(rid,rast)
VALUES(10, ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(100,100,0,0,1,-1,0,0, -1), '1BB'), '4BUI') );
-- output meta data of raster bands to verify all is right --
SELECT (bmd).*
FROM (SELECT ST_BandMetaData(rast,generate_series(1,2)) As bmd
FROM dummy_rast WHERE rid = 10) AS foo;
--result --
pixeltype | hasnodatavalue | nodatavalue | isoutdb | path
-----------+----------------+-------------+---------+------
1BB | f | 0 | f |
4BUI | f | 0 | f |
-- output meta data of raster -
SELECT (rmd).width, (rmd).height, (rmd).numbands
FROM (SELECT ST_MetaData(rast) As rmd
FROM dummy_rast WHERE rid = 10) AS foo;
-- result --
upperleftx | upperlefty | width | height | scalex | scaley | skewx | skewy | srid | numbands
------------+------------+-------+--------+------------+------------+-------+-------+------+----------
0 | 0 | 100 | 100 | 1 | -1 | 0 | 0 | -1 | 2
See Also
, , , ,
ST_BandMetaData
Returns basic meta data for a specific raster band. band num 1 is assumed if none-specified.
record ST_BandMetaData
raster rast
record ST_BandMetaData
raster rast
integer bandnum
Description
Returns basic meta data about a raster band. Columns returned
pixeltype | hasnodatavalue | nodatavalue | isoutdb | path.
If raster contains no bands then an error is thrown.
Examples
SELECT rid, (foo.md).*
FROM (SELECT rid, ST_BandMetaData(rast,1) As md
FROM dummy_rast WHERE rid=2) As foo;
rid | pixeltype | hasnodatavalue | nodatavalue | isoutdb | path
-----+-----------+----------------+-------------+---------+------
2 | 8BUI | t | 0 | f |
See Also
,
ST_BandNoDataValue
Returns the value in a given band that represents no data. If no band num 1 is assumed.
integer ST_BandNoDataValue
raster rast
integer ST_BandNoDataValue
raster rast
integer bandnum
Description
Returns the value that represents no data for the band
Examples
SELECT ST_BandNoDataValue(rast,1) As bnval1,
ST_BandNoDataValue(rast,2) As bnval2, ST_BandNoDataValue(rast,3) As bnval3
FROM dummy_rast
WHERE rid = 2;
bnval1 | bnval2 | bnval3
--------+--------+--------
0 | 0 | 0
See Also
ST_BandIsNoData
Returns true if the band is filled with only nodata values.
boolean ST_BandIsNoData
raster rast
integer band
boolean forceChecking
boolean ST_BandIsNoData
raster rast
integer band
boolean ST_BandIsNoData
raster rast
boolean forceChecking
boolean ST_BandIsNoData
raster rast
Description
Returns true if the band is filled with only nodata
values. Band 1 is assumed if not specified. If the last argument
is TRUE, the entire band is checked pixel by pixel. Otherwise,
the function simply returns the value of the isnodata flag for
the band. The default value for this parameter is FALSE, if not
specified.
If the flag is dirty (this is, the result is different
using TRUE as last parameter and not using it) you should
update the raster to set this flag to true, by using ST_SetBandIsNodata function,
or ST_SetBandNodataValue function with TRUE as last argument. The
loader (raster2pgsql.py) currently can not properly set the
flag while loading raster data. See .
Examples
-- Create dummy table with one raster column
create table dummy_rast (rid integer, rast raster);
-- Add raster with two bands, one pixel/band. In the first band, nodatavalue = pixel value = 3.
-- In the second band, nodatavalue = 13, pixel value = 4
insert into dummy_rast values(1,
(
'01' -- little endian (uint8 ndr)
||
'0000' -- version (uint16 0)
||
'0200' -- nBands (uint16 0)
||
'17263529ED684A3F' -- scaleX (float64 0.000805965234044584)
||
'F9253529ED684ABF' -- scaleY (float64 -0.00080596523404458)
||
'1C9F33CE69E352C0' -- ipX (float64 -75.5533328537098)
||
'718F0E9A27A44840' -- ipY (float64 49.2824585505576)
||
'ED50EB853EC32B3F' -- skewX (float64 0.000211812383858707)
||
'7550EB853EC32B3F' -- skewY (float64 0.000211812383858704)
||
'E6100000' -- SRID (int32 4326)
||
'0100' -- width (uint16 1)
||
'0100' -- height (uint16 1)
||
'6' -- hasnodatavalue and isnodata value set to true.
||
'2' -- first band type (4BUI)
||
'03' -- novalue==3
||
'03' -- pixel(0,0)==3 (same that nodata)
||
'0' -- hasnodatavalue set to false
||
'5' -- second band type (16BSI)
||
'0D00' -- novalue==13
||
'0400' -- pixel(0,0)==4
)::raster
);
select st_bandisnodata(rast, 1) from dummy_rast where rid = 1; -- Expected true
select st_bandisnodata(rast, 2) from dummy_rast where rid = 1; -- Expected false
See Also
,, ,
ST_BandPath
Returns system file path to a band stored in file system. If no bandnum specified, 1 is assumed.
text ST_BandPath
raster rast
text ST_BandPath
raster rast
integer bandnum
Description
Returns system file path to a band. Throws an error if called with an in db band.
Examples
See Also
ST_BandPixelType
Returns the type of pixel for given band. If no bandnum specified, 1 is assumed.
text ST_BandPixelType
raster rast
text ST_BandPixelType
raster rast
integer bandnum
Description
Returns the value that represents no data for the band
There are 11 pixel types. Pixel Types supported are as follows:
1BB - 1-bit boolean
2BUI - 2-bit unsigned integer
4BUI - 4-bit unsigned integer
8BSI - 8-bit signed integer
8BUI - 8-bit unsigned integer
16BSI - 16-bit signed integer
16BUI - 16-bit unsigned integer
32BSI - 32-bit signed integer
32BUI - 32-bit unsigned integer
32BF - 32-bit float
64BF - 64-bit float
Examples
SELECT ST_BandPixelType(rast,1) As btype1,
ST_BandPixelType(rast,2) As btype2, ST_BandPixelType(rast,3) As btype3
FROM dummy_rast
WHERE rid = 2;
btype1 | btype2 | btype3
--------+--------+--------
8BUI | 8BUI | 8BUI
See Also
ST_HasNoBand
Returns true if there is no band with given band number. If no band number is specified, then band number 1 is assumed.
boolean ST_HasNoBand
raster rast
boolean ST_HasNoBand
raster rast
integer bandnum
Description
Returns true if there is no band with given band number. If no band number is specified, then band number 1 is assumed.
Availability: 2.0.0
Examples
SELECT rid, ST_HasNoBand(rast) As hb1, ST_HasNoBand(rast,2) as hb2,
ST_HasNoBand(rast,4) as hb4, ST_NumBands(rast) As numbands
FROM dummy_rast;
rid | hb1 | hb2 | hb4 | numbands
-----+-----+-----+-----+----------
1 | t | t | t | 0
2 | f | f | t | 3
See Also
Raster Pixel Accessors and Settors
ST_PixelAsPolygon
Returns the geometry that bounds the pixel (for now a rectangle) for a particular band, row, column. If no band
specified band num 1 is assumed.
geometry ST_PixelAsPolygon
raster rast
integer columnx
integer rowy
geometry ST_PixelAsPolygon
raster rast
integer bandnum
integer columnx
integer rowy
Description
Returns the geometry that bounds the pixel (for now a rectangle) for a particular band, row, column. If no band
specified band num 1 is assumed.
Examples
-- get raster pixel polygon
SELECT i,j, ST_AsText(ST_PixelAsPolygon(foo.rast, i,j)) As b1pgeom
FROM dummy_rast As foo
CROSS JOIN generate_series(1,2) As i
CROSS JOIN generate_series(1,1) As j
WHERE rid=2;
i | j | b1pgeom
---+---+-----------------------------------------------------------------------------
1 | 1 | POLYGON((3427927.75 5793244,3427927.8 5793244,3427927.8 5793243.95,...
2 | 1 | POLYGON((3427927.8 5793244,3427927.85 5793244,3427927.85 5793243.95, ..
See Also
, ,
ST_Value
Returns the value of a given band in a given columnx, rowy pixel or at a particular geometric point. Band numbers start at 1 and assumed to be 1 if not specified. If hasnodata is set to true, then only non nodata pixels are considered. If hasnodata is not passed in then reads it from metadata of raster.
double precision ST_Value
raster rast
geometry pt
double precision ST_Value
raster rast
geometry pt
boolean hasnodata
double precision ST_Value
raster rast
integer bandnum
geometry pt
double precision ST_Value
raster rast
integer bandnum
geometry pt
boolean hasnodata
double precision ST_Value
raster rast
integer columnx
integer rowy
double precision ST_Value
raster rast
integer columnx
integer rowy
boolean hasnodata
double precision ST_Value
raster rast
integer bandnum
integer columnx
integer rowy
double precision ST_Value
raster rast
integer bandnum
integer columnx
integer rowy
boolean hasnodata
Description
Returns the value of a given band in a given columnx, rowy pixel or at a given geometry point. Band numbers start at 1 and band is assumed to be 1 if not specified.
If hasnodata is set to true, then only non nodata pixels are considered. If hasnodata is set to false, then all pixels are considered.
Enhanced: 2.0.0 hasnodata optional argument was added.
Examples
-- get raster values at particular postgis geometry points
-- the srid of your geometry should be same as for your raster
SELECT rid, ST_Value(rast, foo.pt_geom) As b1pval, ST_Value(rast, 2, foo.pt_geom) As b2pval
FROM dummy_rast CROSS JOIN (SELECT ST_SetSRID(ST_Point(3427927.77,5793243.76),-1) As pt_geom) As foo
WHERE rid=2;
rid | b1pval | b2pval
-----+--------+--------
2 | 252 | 79
-- general fictitious example using a real table
SELECT rid, ST_Value(rast, 3, sometable.geom) As b3pval
FROM sometable
WHERE ST_Intersects(rast,sometable.geom);
SELECT rid, ST_Value(rast, 1,1,1) As b1pval,
ST_Value(rast, 2,1,1) As b2pval, ST_Value(rast, 3,1,1) As b3pval
FROM dummy_rast
WHERE rid=2;
rid | b1pval | b2pval | b3pval
-----+--------+--------+--------
2 | 253 | 78 | 70
--- Get all values in bands 1,2,3 of each pixel --
SELECT x, y, ST_Value(rast, 1, x, y) As b1val,
ST_Value(rast, 2, x, y) As b2val, ST_Value(rast, 3, x, y) As b3val
FROM dummy_rast CROSS JOIN
generate_series(1,1000) As x CROSS JOIN generate_series(1,1000) As y
WHERE rid = 2 AND x <= ST_Width(rast) AND y <= ST_Height(rast);
x | y | b1val | b2val | b3val
---+---+-------+-------+-------
1 | 1 | 253 | 78 | 70
1 | 2 | 253 | 96 | 80
1 | 3 | 250 | 99 | 90
1 | 4 | 251 | 89 | 77
1 | 5 | 252 | 79 | 62
2 | 1 | 254 | 98 | 86
2 | 2 | 254 | 118 | 108
:
:
--- Get all values in bands 1,2,3 of each pixel same as above but returning the upper left point point of each pixel --
SELECT ST_AsText(ST_SetSRID(
ST_Point(ST_UpperLeftX(rast) + ST_ScaleX(rast)*x,
ST_UpperLeftY(rast) + ST_ScaleY(rast)*y),
ST_SRID(rast))) As uplpt
, ST_Value(rast, 1, x, y) As b1val,
ST_Value(rast, 2, x, y) As b2val, ST_Value(rast, 3, x, y) As b3val
FROM dummy_rast CROSS JOIN
generate_series(1,1000) As x CROSS JOIN generate_series(1,1000) As y
WHERE rid = 2 AND x <= ST_Width(rast) AND y <= ST_Height(rast);
uplpt | b1val | b2val | b3val
-----------------------------+-------+-------+-------
POINT(3427929.25 5793245.5) | 253 | 78 | 70
POINT(3427929.25 5793247) | 253 | 96 | 80
POINT(3427929.25 5793248.5) | 250 | 99 | 90
:
--- Get a polygon formed by union of all pixels
that fall in a particular value range and intersect particular polygon --
SELECT ST_AsText(ST_Union(pixpolyg)) As shadow
FROM (SELECT ST_Translate(ST_MakeEnvelope(
ST_UpperLeftX(rast), ST_UpperLeftY(rast),
ST_UpperLeftX(rast) + ST_ScaleX(rast),
ST_UpperLeftY(rast) + ST_ScaleY(rast), 0
), ST_ScaleX(rast)*x, ST_ScaleY(rast)*y
) As pixpolyg, ST_Value(rast, 2, x, y) As b2val
FROM dummy_rast CROSS JOIN
generate_series(1,1000) As x CROSS JOIN generate_series(1,1000) As y
WHERE rid = 2
AND x <= ST_Width(rast) AND y <= ST_Height(rast)) As foo
WHERE
ST_Intersects(
pixpolyg,
ST_GeomFromText('POLYGON((3427928 5793244,3427927.75 5793243.75,3427928 5793243.75,3427928 5793244))',0)
) AND b2val != 254;
shadow
------------------------------------------------------------------------------------
MULTIPOLYGON(((3427928 5793243.9,3427928 5793243.85,3427927.95 5793243.85,3427927.95 5793243.9,
3427927.95 5793243.95,3427928 5793243.95,3427928.05 5793243.95,3427928.05 5793243.9,3427928 5793243.9)),((3427927.95 5793243.9,3427927.95 579324
3.85,3427927.9 5793243.85,3427927.85 5793243.85,3427927.85 5793243.9,3427927.9 5793243.9,3427927.9 5793243.95,
3427927.95 5793243.95,3427927.95 5793243.9)),((3427927.85 5793243.75,3427927.85 5793243.7,3427927.8 5793243.7,3427927.8 5793243.75
,3427927.8 5793243.8,3427927.8 5793243.85,3427927.85 5793243.85,3427927.85 5793243.8,3427927.85 5793243.75)),
((3427928.05 5793243.75,3427928.05 5793243.7,3427928 5793243.7,3427927.95 5793243.7,3427927.95 5793243.75,3427927.95 5793243.8,3427
927.95 5793243.85,3427928 5793243.85,3427928 5793243.8,3427928.05 5793243.8,
3427928.05 5793243.75)),((3427927.95 5793243.75,3427927.95 5793243.7,3427927.9 5793243.7,3427927.85 5793243.7,
3427927.85 5793243.75,3427927.85 5793243.8,3427927.85 5793243.85,3427927.9 5793243.85,
3427927.95 5793243.85,3427927.95 5793243.8,3427927.95 5793243.75)))
--- Checking all the pixels of a large raster tile can take a long time.
--- You can dramatically improve speed at some lose of precision by orders of magnitude
-- by sampling pixels using the step optional parameter of generate_series.
-- This next example does the same as previous but by checking 1 for every 4 (2x2) pixels and putting in the last checked
-- putting in the checked pixel as the value for subsequent 4
SELECT ST_AsText(ST_Union(pixpolyg)) As shadow
FROM (SELECT ST_Translate(ST_MakeEnvelope(
ST_UpperLeftX(rast), ST_UpperLeftY(rast),
ST_UpperLeftX(rast) + ST_ScaleX(rast)*2,
ST_UpperLeftY(rast) + ST_ScaleY(rast)*2, 0
), ST_ScaleX(rast)*x, ST_ScaleY(rast)*y
) As pixpolyg, ST_Value(rast, 2, x, y) As b2val
FROM dummy_rast CROSS JOIN
generate_series(1,1000,2) As x CROSS JOIN generate_series(1,1000,2) As y
WHERE rid = 2
AND x <= ST_Width(rast) AND y <= ST_Height(rast) ) As foo
WHERE
ST_Intersects(
pixpolyg,
ST_GeomFromText('POLYGON((3427928 5793244,3427927.75 5793243.75,3427928 5793243.75,3427928 5793244))',0)
) AND b2val != 254;
shadow
------------------------------------------------------------------------------------
MULTIPOLYGON(((3427927.9 5793243.85,3427927.8 5793243.85,3427927.8 5793243.95,
3427927.9 5793243.95,3427928 5793243.95,3427928.1 5793243.95,3427928.1 5793243.85,3427928 5793243.85,3427927.9 5793243.85)),
((3427927.9 5793243.65,3427927.8 5793243.65,3427927.8 5793243.75,3427927.8 5793243.85,3427927.9 5793243.85,
3427928 5793243.85,3427928 5793243.75,3427928.1 5793243.75,3427928.1 5793243.65,3427928 5793243.65,3427927.9 5793243.65)))
See Also
, , , ,
, , ,
, , ,
, , ,
,
ST_SetValue
Returns modified raster resulting from setting the value of a given band in a given columnx, rowy pixel or at a pixel that intersects a particular geometric point. Band numbers start at 1 and assumed to be 1 if not specified.
raster ST_SetValue
raster rast
geometry pt
double precision newvalue
raster ST_SetValue
raster rast
integer bandnum
geometry pt
double precision newvalue
raster ST_SetValue
raster rast
integer columnx
integer rowy
double precision newvalue
raster ST_SetValue
raster rast
integer bandnum
integer columnx
integer rowy
double precision newvalue
Description
Returns modified raster resulting from setting the specified pixel value to new value for the designed band given the row column location or a geometric point location.
If no band is specified, then band 1 is assumed.
Setting by geometry currently only works for points.
Examples
-- Geometry example
SELECT (foo.geomval).val, ST_AsText(ST_Union((foo.geomval).geom))
FROM (SELECT ST_DumpAsPolygons(
ST_SetValue(rast,1,
ST_Point(3427927.75, 5793243.95),
50)
) As geomval
FROM dummy_rast
where rid = 2) As foo
WHERE (foo.geomval).val < 250
GROUP BY (foo.geomval).val;
val | st_astext
-----+-------------------------------------------------------------------
50 | POLYGON((3427927.75 5793244,3427927.75 5793243.95,3427927.8 579324 ...
249 | POLYGON((3427927.95 5793243.95,3427927.95 5793243.85,3427928 57932 ...
-- Store the changed raster --
UPDATE dummy_rast SET rast = ST_SetValue(rast,1, ST_Point(3427927.75, 5793243.95),100)
WHERE rid = 2 ;
See Also
,
Raster Editors
ST_SetGeoReference
Set Georeference 6 georeference parameters in a single call. Numbers should be separated by white space. Accepts inputs in GDAL or ESRI format. Default is GDAL.
raster ST_SetGeoReference
raster rast
text georefcoords
raster ST_SetGeoReference
raster rast
text georefcoords
text format
Description
Set Georeference 6 georeference parameters in a single call. Accepts inputs in 'GDAL' or 'ESRI' format. Default is GDAL. If 6 coordinates are not provided will return null.
Difference between format representations is as follows:
GDAL:
scalex skewy skewx scaley upperleftx upperlefty
ESRI:
scalex skewy skewx scaley upperleftx + scalex*0.5 upperlefty + scaley*0.5
Examples
UPDATE dummy_rast SET rast = ST_SetGeoReference(rast, '2 0 0 3 0.5 0.5','GDAL')
WHERE rid=1;
-- same coordinates set in 'ESRI' format
UPDATE dummy_rast SET rast = ST_SetGeoReference(rast, '2 0 0 3 1.5 2','ESRI')
WHERE rid=1;
See Also
, , , ,
ST_SetScale
Sets the x and y size of pixels in units of coordinate reference system. Number units/pixel width/height
raster ST_SetScale
raster rast
float8 xy
raster ST_SetScale
raster rast
float8 x
float8 y
Description
Sets the x and y size of pixels in units of coordinate reference system. Number units/pixel width/height. If
only one unit passed in, assumed x and y are the same number.
Changed: 2.0.0 In WKTRaster versions this was called ST_SetPixelSize. This was changed in 2.0.0.
Examples
UPDATE dummy_rast
SET rast = ST_SetScale(rast,1.5)
WHERE rid = 2;
SELECT ST_ScaleX(rast) As pixx, ST_ScaleY(rast) As pixy, Box2D(rast) As newbox
FROM dummy_rast
WHERE rid = 2;
pixx | pixy | newbox
------+------+----------------------------------------------
1.5 | 1.5 | BOX(3427927.75 5793244,3427935.25 5793251.5)
UPDATE dummy_rast
SET rast = ST_SetScale(rast,1.5,0.55)
WHERE rid = 2;
SELECT ST_ScaleX(rast) As pixx, ST_ScaleY(rast) As pixy, Box2D(rast) As newbox
FROM dummy_rast
WHERE rid = 2;
pixx | pixy | newbox
------+------+--------------------------------------------
1.5 | 0.55 | BOX(3427927.75 5793244,3427935.25 5793247)
See Also
, ,
ST_SetSkew
Sets the georeference X and Y skew (or rotation parameter). If only one is passed in sets x and y to same number.
raster ST_SetSkew
raster rast
float8 skewxy
raster ST_SetSkew
raster rast
float8 skewx
float8 skewy
Description
Sets the georeference X and Y skew (or rotation parameter). If only one is passed in sets x and y to same number. Refer to World File
for more details.
Examples
-- Example 1
UPDATE dummy_rast SET rast = ST_SetSkew(rast,1,2) WHERE rid = 1;
SELECT rid, ST_SkewX(rast) As skewx, ST_SkewY(rast) As skewy,
ST_GeoReference(rast) as georef
FROM dummy_rast WHERE rid = 1;
rid | skewx | skewy | georef
----+-------+-------+--------------
1 | 1 | 2 | 2.0000000000
: 2.0000000000
: 1.0000000000
: 3.0000000000
: 0.5000000000
: 0.5000000000
-- Example 2 set both to same number:
UPDATE dummy_rast SET rast = ST_SetSkew(rast,0) WHERE rid = 1;
SELECT rid, ST_SkewX(rast) As skewx, ST_SkewY(rast) As skewy,
ST_GeoReference(rast) as georef
FROM dummy_rast WHERE rid = 1;
rid | skewx | skewy | georef
-----+-------+-------+--------------
1 | 0 | 0 | 2.0000000000
: 0.0000000000
: 0.0000000000
: 3.0000000000
: 0.5000000000
: 0.5000000000
See Also
,, ,
ST_SetSRID
Sets the SRID of a raster to a particular integer srid defined in the spatial_ref_sys table.
raster ST_SetSRID
raster
rast
integer
srid
Description
Sets the SRID on a raster to a particular integer value.
This function does not transform the raster in any way -
it simply sets meta data defining the spatial ref of the coordinate reference system that it's currently in.
Useful for transformations later.
See Also
,
ST_SetUpperLeft
Sets the value of the upper left corner of the pixel to projected x,y coordinates
raster ST_SetUpperLeft
raster rast
double precision x
double precision y
Description
Set the value of the uppler left corner of raster to the projected x coordinates
Examples
SELECT ST_SetUpperLeft(rast,-71.01,42.37)
FROM dummy_rast
WHERE rid = 2;
See Also
,
Raster Band Editors
ST_SetBandNoDataValue
Sets the value for the given band that represents no data. Band 1 is assumed if no band is specified. To mark a band as having no nodata value, set the nodata value = NULL
integer ST_SetBandNoDataValue
raster rast
double precision val
integer ST_SetBandNoDataValue
raster rast
integer band
double precision val
Description
Sets the value that represents no data for the band. Band 1 is assumed if not specified. This will effect and results.
Examples
-- change just first band no data value
UPDATE dummy_rast
SET rast = ST_SetBandNoDataValue(rast,1, 254)
WHERE rid = 2;
-- change no data band value of bands 1,2,3
UPDATE dummy_rast
SET rast =
ST_SetBandNoDataValue(
ST_SetBandNoDataValue(
ST_SetBandNoDataValue(
rast,1, 254)
,2,99),
3,108)
WHERE rid = 2;
-- wipe out the nodata value this will ensure all pixels are considered for all processing functions
UPDATE dummy_rast
SET rast = ST_SetBandNoDataValue(rast,1, NULL)
WHERE rid = 2;
See Also
,
ST_SetBandIsNoData
Sets the isnodata flag of the band to TRUE. You may
want to call this function if ST_BandIsNoData(rast, band) !=
ST_BandIsNodata(rast, band, TRUE). This is, if the isnodata flag
is dirty. Band 1 is assumed if no band is specified.
integer ST_SetBandIsNoData
raster rast
integer band
integer ST_SetBandIsNoData
raster rast
Description
Sets the isnodata flag for the band to true. Band 1 is
assumed if not specified. This function should be called only
when the flag is considered dirty. This is, when the result
calling is different using
TRUE as last argument and without using it
Currently, the loader (raster2pgsql.py) is not able
to set the isnodata flag for bands. So, this is the fastest way
to set it to TRUE, without changing any other band value
Examples
-- Create dummy table with one raster column
create table dummy_rast (rid integer, rast raster);
-- Add raster with two bands, one pixel/band. In the first band, nodatavalue = pixel value = 3.
-- In the second band, nodatavalue = 13, pixel value = 4
insert into dummy_rast values(1,
(
'01' -- little endian (uint8 ndr)
||
'0000' -- version (uint16 0)
||
'0200' -- nBands (uint16 0)
||
'17263529ED684A3F' -- scaleX (float64 0.000805965234044584)
||
'F9253529ED684ABF' -- scaleY (float64 -0.00080596523404458)
||
'1C9F33CE69E352C0' -- ipX (float64 -75.5533328537098)
||
'718F0E9A27A44840' -- ipY (float64 49.2824585505576)
||
'ED50EB853EC32B3F' -- skewX (float64 0.000211812383858707)
||
'7550EB853EC32B3F' -- skewY (float64 0.000211812383858704)
||
'E6100000' -- SRID (int32 4326)
||
'0100' -- width (uint16 1)
||
'0100' -- height (uint16 1)
||
'4' -- hasnodatavalue set to true, isnodata value set to false (when it should be true)
||
'2' -- first band type (4BUI)
||
'03' -- novalue==3
||
'03' -- pixel(0,0)==3 (same that nodata)
||
'0' -- hasnodatavalue set to false
||
'5' -- second band type (16BSI)
||
'0D00' -- novalue==13
||
'0400' -- pixel(0,0)==4
)::raster
);
select st_bandisnodata(rast, 1) from dummy_rast where rid = 1; -- Expected false
select st_bandisnodata(rast, 1, TRUE) from dummy_rast where rid = 1; -- Expected true
-- The isnodata flag is dirty. We are going to set it to true
update dummy_rast set rast = st_setbandisnodata(rast, 1) where rid = 1;
select st_bandisnodata(rast, 1) from dummy_rast where rid = 1; -- Expected true
See Also
,, ,
Raster Outputs
ST_AsBinary
Return the Well-Known Binary (WKB) representation of the raster without SRID meta data.
bytea ST_AsBinary
raster rast
Description
Returns the Binary representation of the raster. There are 2 variants of the function. The first
variant takes no endian encoding parameter and defaults to little endian. The second variant takes a second argument
denoting the encoding - using little-endian ('NDR') or big-endian ('XDR') encoding.
This is useful in binary cursors to pull data out of the
database without converting it to a string representation.
Examples
SELECT ST_AsBinary(rast) As rastbin
FROM dummy_rast WHERE rid=1;
rastbin
---------------------------------------------------------------------------------
\001\000\000\000\000\000\000\000\000\000\000\000@\000\000\000\000\000\000\010@\
000\000\000\000\000\000\340?\000\000\000\000\000\000\340?\000\000\000\000\000\00
0\000\000\000\000\000\000\000\000\000\000\012\000\000\000\012\000\024\000
See Also
Raster Processing Functions
Box2D
Returns the box 2d representation of the enclosing box of the raster
box2d Box2D
raster rast
Description
Returns the box representing the extent of the raster.
The polygon is defined by the corner points of the bounding box
((MINX, MINY),
(MAXX, MAXY))
Examples
SELECT rid, Box2D(rast) As rastbox
FROM dummy_rast;
rid | rastbox
----+-------------------------------------------------
1 | BOX(0.5 0.5,20.5 60.5)
2 | BOX(3427927.75 5793243.5,3427928 5793244)
See Also
ST_ConvexHull
Return the convex hull geometry of the raster including pixel values equal to BandNoDataValue.
For regular shaped and non-skewed
rasters, this gives the same answer as ST_Envelope so only useful for irregularly shaped or skewed rasters.
geometry ST_ConvexHull
raster rast
Description
Return the convex hull geometry of the raster including the NoDataBandValue band pixels. For regular shaped and non-skewed
rasters, this gives more or less the same answer as ST_Envelope
so only useful for irregularly shaped or skewed rasters.
ST_Envelope floors the coordinates and hence add a little buffer around the raster so the answer is subtly
different from ST_ConvexHull which does not floor.
Examples
Refer to PostGIS Raster Specification
for a diagram of this.
-- Note envelope and convexhull are more or less the same
SELECT ST_AsText(ST_ConvexHull(rast)) As convhull,
ST_AsText(ST_Envelope(rast)) As env
FROM dummy_rast WHERE rid=1;
convhull | env
--------------------------------------------------------+-----------------------
POLYGON((0.5 0.5,20.5 0.5,20.5 60.5,0.5 60.5,0.5 0.5)) | POLYGON((0 0,20 0,20 60,0 60,0 0))
-- now we skew the raster
-- note how the convex hull and envelope are now different
SELECT ST_AsText(ST_ConvexHull(rast)) As convhull,
ST_AsText(ST_Envelope(rast)) As env
FROM (SELECT ST_SetRotation(rast,0.1,0.1) As rast
FROM dummy_rast WHERE rid=1) As foo;
convhull | env
--------------------------------------------------------+------------------------------------
POLYGON((0.5 0.5,20.5 1.5,22.5 61.5,2.5 60.5,0.5 0.5)) | POLYGON((0 0,22 0,22 61,0 61,0 0))
See Also
, ,
ST_DumpAsPolygons
Returns a set of geomval (geom,val) rows, from a given raster band. If no band number is specified, band num defaults to 1.
setof geomval ST_DumpAsPolygons
raster rast
setof geomval ST_DumpAsPolygons
raster rast
integer band_num
Description
This is a set-returning function (SRF). It returns a set of
geomval rows, formed by a geometry (geom) and a pixel band value (val).
Each polygon is the union of all pixels for that band that have the same pixel value denoted by val.
ST_DumpAsPolygon is useful for polygonizing rasters. It is the
reverse of a GROUP BY in that it creates new rows. For example it
can be used to expand a single raster into multiple POLYGONS/MULTIPOLYGONS.
Availability: Requires GDAL 1.7 or higher.
If there is a no data value set for a band, pixels with that value will not be returned.
Examples
SELECT val, ST_AsText(geom) As geomwkt
FROM (
SELECT (ST_DumpAsPolygons(rast)).*
FROM dummy_rast
WHERE rid = 2
) As foo
WHERE val BETWEEN 249 and 251
ORDER BY val;
val | geomwkt
-----+--------------------------------------------------------------------------
249 | POLYGON((3427927.95 5793243.95,3427927.95 5793243.85,3427928 5793243.85,
3427928 5793243.95,3427927.95 5793243.95))
250 | POLYGON((3427927.75 5793243.9,3427927.75 5793243.85,3427927.8 5793243.85,
3427927.8 5793243.9,3427927.75 5793243.9))
250 | POLYGON((3427927.8 5793243.8,3427927.8 5793243.75,3427927.85 5793243.75,
3427927.85 5793243.8, 3427927.8 5793243.8))
251 | POLYGON((3427927.75 5793243.85,3427927.75 5793243.8,3427927.8 5793243.8,
3427927.8 5793243.85,3427927.75 5793243.85))
See Also
,,,
ST_Envelope
Returns the polygon representation of the extent of the raster.
geometry ST_Envelope
raster rast
Description
Returns the polygon representation of the extent of the raster in spatial coordinate units defined by srid. It is a float8 minimum bounding box represented as a polygon.
The polygon is defined by the corner points of the bounding box
((MINX, MINY),
(MINX, MAXY),
(MAXX, MAXY),
(MAXX, MINY),
(MINX, MINY))
Examples
SELECT rid, ST_AsText(ST_Envelope(rast)) As envgeomwkt
FROM dummy_rast;
rid | envgeomwkt
-----+--------------------------------------------------------------------
1 | POLYGON((0 0,20 0,20 60,0 60,0 0))
2 | POLYGON((3427927 5793243,3427928 5793243,
3427928 5793244,3427927 5793244, 3427927 5793243))
See Also
, ,
ST_Intersection
Returns a set of geometry-pixelvalue pairs resulting from intersection of a raster band with a geometry. If
no band number is specified, band 1 is assumed.
setof geomval ST_Intersection
geometry geom
raster rast
setof geomval ST_Intersection
geometry geom
raster rast
integer band_num
setof geomval ST_Intersection
raster rast
geometry geom
setof geomval ST_Intersection
raster rast
integer band_num
geometry geom
Description
Return the intersections of the geometry with the vectorized parts of
the raster and the values associated with those parts, if really their intersection is not empty. If no band number is specified
band 1 is assumed.
Examples
SELECT foo.rid, foo.gid,
ST_AsText((foo.geomval).geom) As geomwkt, (foo.geomval).val
FROM
(
SELECT A.rid, g.gid , ST_Intersection(A.rast, g.geom) As geomval
FROM dummy_rast AS A CROSS JOIN
(VALUES (1, ST_Point(3427928, 5793243.85) ) ,
(2, ST_GeomFromText('LINESTRING(3427927.85 5793243.75,3427927.8 5793243.75,3427927.8 5793243.8)') ),
(3, ST_GeomFromText('LINESTRING(1 2, 3 4)') )
) As g(gid,geom)
WHERE A.rid =2 ) As foo;
rid | gid | geomwkt | val
-----+-----+---------------------------------------------------------------------------------------------
2 | 1 | POINT(3427928 5793243.85) | 249
2 | 1 | POINT(3427928 5793243.85) | 253
2 | 2 | POINT(3427927.85 5793243.75) | 254
2 | 2 | POINT(3427927.8 5793243.8) | 251
2 | 2 | POINT(3427927.8 5793243.8) | 253
2 | 2 | LINESTRING(3427927.8 5793243.75,3427927.8 5793243.8) | 252
2 | 2 | MULTILINESTRING((3427927.8 5793243.8,3427927.8 5793243.75),...) | 250
2 | 3 | GEOMETRYCOLLECTION EMPTY
See Also
, ,
ST_Polygon
Returns a polygon geometry formed by the union of pixels that have a pixel value that is not no data value. If no band number is specified, band num defaults to 1.
geometry ST_Polygon
raster rast
geometry ST_Polygon
raster rast
integer band_num
Description
Availability: Requires GDAL 1.7 or higher.
Examples
-- by default no data band value is 0 or not set, so polygon will return a square polygon
SELECT ST_AsText(ST_Polygon(rast)) As geomwkt
FROM dummy_rast
WHERE rid = 2;
geomwkt
--------------------------------------------
POLYGON((3427927.8 5793243.75,3427927.75 5793243.75,3427927.75 5793243.8,3427927.75 5793243.85,3427927.75 5793243.9,
3427927.75 5793244,3427927.8 5793244,3427927.85 5793244,3427927.9 5793244,3427928 5793244,3427928 5793243.95,
3427928 5793243.85,3427928 5793243.8,3427928 5793243.75,3427927.85 5793243.75,3427927.8 5793243.75))
-- now we change the no data value of first band
UPDATE dummy_rast SET rast = ST_SetBandNoDataValue(rast,1,254)
WHERE rid = 2;
SELECt rid, ST_BandNoDataValue(rast)
from dummy_rast where rid = 2;
-- ST_Polygon excludes the pixel value 254 and returns a multipolygon
SELECT ST_AsText(ST_Polygon(rast)) As geomwkt
FROM dummy_rast
WHERE rid = 2;
geomwkt
---------------------------------------------------------
MULTIPOLYGON(((3427927.9 5793243.95,3427927.85 5793243.95,3427927.85 5793244,3427927.9 5793244,3427927.9 5793243.95)),
((3427928 5793243.85,3427928 5793243.8,3427927.95 5793243.8,3427927.95 5793243.85,3427927.9 5793243.85,3427927.9 5793243.9,3427927.9 5793243.95,3427927.95 5793243.95,3427928 5793243.95,3427928 5793243.85)),
((3427927.8 5793243.75,3427927.75 5793243.75,3427927.75 5793243.8,3427927.75 5793243.85,3427927.75 5793243.9,3427927.75 5793244,3427927.8 5793244,
3427927.8 5793243.9,3427927.8 5793243.85,3427927.85 5793243.85,3427927.85 5793243.8,3427927.85 5793243.75,3427927.8 5793243.75)))
-- Or if you want the no data value different for just one time
SELECT ST_AsText(
ST_Polygon(
ST_SetBandNoDataValue(rast,1,252)
)
) As geomwkt
FROM dummy_rast
WHERE rid =2;
geomwkt
---------------------------------
POLYGON((3427928 5793243.85,3427928 5793243.8,3427928 5793243.75,3427927.85 5793243.75,3427927.8 5793243.75,3427927.8 5793243.8,3427927.75 5793243.8,3427927.75 5793243.85,3427927.75 5793243.9,3427927.75 5793244,3427927.8 5793244,3427927.85 5793244,3427927.9 5793244,3427928 5793244,3427928 5793243.95,3427928 5793243.85),
(3427927.9 5793243.9,3427927.9 5793243.85,3427927.95 5793243.85,3427927.95 5793243.9,3427927.9 5793243.9))
See Also
,
Raster Operators
&&
Returns TRUE if A's bounding box overlaps B's.
boolean &&
raster
A
raster
B
Description
The && operator returns TRUE if the bounding box of raster A overlaps the bounding box of raster B.
This operand will make use of any indexes that may be available on the
rasters.
Examples
SELECT A.rid As a_rid, B.rid As b_rid, A.rast && B.rast As overlap
FROM dummy_rast AS A CROSS JOIN dummy_rast AS B LIMIT 3;
a_rid | b_rid | overlap
-------+-------+---------
2 | 2 | t
2 | 3 | f
2 | 1 | f
&<
Returns TRUE if A's bounding box is to the left of B's.
boolean &<
raster
A
raster
B
Description
The &< operator returns TRUE if the bounding box of raster A
overlaps or is to the left of the bounding box of raster B, or more accurately, overlaps or is NOT to the right
of the bounding box of raster B.
This operand will make use of any indexes that may be available on the
geometries.
Examples
SELECT A.rid As a_rid, B.rid As b_rid, A.rast &< B.rast As overleft
FROM dummy_rast AS A CROSS JOIN dummy_rast AS B;
a_rid | b_rid | overleft
------+-------+----------
2 | 2 | t
2 | 3 | f
2 | 1 | f
3 | 2 | t
3 | 3 | t
3 | 1 | f
1 | 2 | t
1 | 3 | t
1 | 1 | t
&>
Returns TRUE if A's bounding box is to the right of B's.
boolean &>
raster
A
raster
B
Description
The &> operator returns TRUE if the bounding box of raster A
overlaps or is to the right of the bounding box of raster B, or more accurately, overlaps or is NOT to the left
of the bounding box of raster B.
This operand will make use of any indexes that may be available on the
geometries.
Examples
SELECT A.rid As a_rid, B.rid As b_rid, A.rast &> B.rast As overright
FROM dummy_rast AS A CROSS JOIN dummy_rast AS B;
a_rid | b_rid | overright
-------+-------+----------
2 | 2 | t
2 | 3 | t
2 | 1 | t
3 | 2 | f
3 | 3 | t
3 | 1 | f
1 | 2 | f
1 | 3 | t
1 | 1 | t
Raster and Raster Band Spatial Relationships
ST_Intersects
Returns TRUE if rast pixel in a band with non-nodata band value intersects
with a geometry/raster. If band is not specified it defaults to 1. If hasnodata is omitted, reads from band meta data.
boolean ST_Intersects
raster
rast
geometry
geommin
boolean ST_Intersects
raster
rast
integer
band
geometry
geommin
boolean ST_Intersects
geometry
geomA
raster
rast
boolean ST_Intersects
geometry
geommin
raster
rast
integer
band
boolean ST_Intersects
raster
rast
boolean
hasnodata
geometry
geommin
boolean ST_Intersects
geometry
geommin
raster
rast
boolean
hasnodata
boolean ST_Intersects
geometry
geommin
raster
rast
integer
band
boolean
hasnodata
boolean ST_Intersects
raster
rast
integer
band
boolean
hasnodata
geometry
geommin
Description
Returns true if the geometry intersects with the raster. Nodata values are taken into account so that if the geometry intersects only with nodata values, the function returns false.
hasnodata may modify this behavior: if set to false, nodata value are not taken into account and the function returns true as soon as the geometry intersects with the convex hull of the raster.
This operand will make use of any indexes that may be available on the
geometries / rasters.
Examples
SELECT A.rid, g.gid , ST_Intersects(A.rast, g.geom) As inter
FROM dummy_rast AS A CROSS JOIN
(VALUES (1, ST_Point(3427928, 5793243.85) ) ,
(2, ST_GeomFromText('LINESTRING(3427927.85 5793243.75,3427927.8 5793243.75,3427927.8 5793243.8)') ),
(3, ST_GeomFromText('LINESTRING(1 2, 3 4)') )
) As g(gid,geom)
WHERE A.rid =2 ;
rid | gid | inter
-----+-----+-------
2 | 1 | t
2 | 2 | t
2 | 3 | f
See Also