8.2. 栅格管理函数
8.2.1. AddRasterConstraints
AddRasterConstraints — 给一个栅格表的指定列添加一个栅格约束,约束信息包括参考系,缩放比例,块大小,对齐方式,波段,波段类型和一个标志栅格列是否是被锁住的信息。 该表必须有约束条件可约束的数据。如果添加约束成功,则返回true,否则提升报错。
用法
boolean AddRasterConstraints( name rasttable, name rastcolumn, boolean srid, boolean scale_x, boolean scale_y, boolean blocksize_x, boolean blocksize_y, boolean same_alignment, boolean regular_blocking, boolean num_bands=true , boolean pixel_types=true , boolean nodata_values=true , boolean out_db=true ,boolean extent=true ); boolean AddRasterConstraints( name rasttable, name rastcolumn, text[] VARIADIC constraints); boolean AddRasterConstraints( name rastschema, name rasttable, name rastcolumn, text[] VARIADIC constraints); boolean AddRasterConstraints( name rastschema, name rasttable, name rastcolumn, boolean srid=true, boolean scale_x=true, boolean scale_y=true, boolean blocksize_x=true, boolean blocksize_y=true, boolean same_alignment=true, boolean regular_blocking=false, boolean num_bands=true, boolean pixel_types=true, boolean nodata_values=true , boolean out_db=true , boolean extent=true );
描述
为在栅格列信息表raster_columns中的列加上约束条件。参数rastschema是表的schema名。SRID值必须是SPATIAL_REF_SYS 表中存在的。
raster2pgsql 该加载工具使用该函数来注册栅格表。
必须传递有效的约束名,参考5.2.1获取更多信息。
-
blocksize 同时设置X和Y的blocksize。
-
blocksize_x 设置每个瓦片的X值(即瓦片宽度)。
-
blocksize_y 设置每个瓦片的Y值(即高度)。
-
extent 计算整个表的几何对象的边界,并且对所有栅格添加约束,使其在几何对象的边界内。
-
num_bands 波段数。
-
pixel_types读取每个段的像元类型,确保所有的段有相同的像元类型。
-
regular_blocking 让栅格空间上唯一 (没有两个栅格可以空间上相同) ,并添加瓦片coverage对齐约束 (栅格以coverage方式对齐)。
-
same_alignment 确保任意两个瓦片都有相同的对齐方式。意味着,任意两个瓦片比较都返回true参考ST_SameAlignment。
-
srid 确保所有栅格数据有SRID值。
-
More 上述所列函数的其他参数。
注意
这个函数会引用到表中已有数据的约束。因此,需要先创建栅格列,然后加载数据到该表中。
注意
在已经创建了约束情况下,但却需要加载更多的数据到表中,可能需要先运行函数DropRasterConstraints来删除约束,如果的数据的对应的几何对象的边界已经改变了的话。
样例: Apply all possible constraints on column based on data
CREATE TABLE myrasters(rid SERIAL primary key, rast raster); INSERT INTO myrasters(rast) SELECT ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 0.3, -0.3, 2, 2, 0, 0,4326), 1, '8BSI':: text, -129, NULL); SELECT AddRasterConstraints('myrasters'::name, 'rast'::name); -- verify if registered correctly in the raster_columns view -- SELECT srid, scale_x, scale_y, blocksize_x, blocksize_y, num_bands, pixel_types, nodata_values FROM raster_columns WHERE r_table_name = 'myrasters'; srid | scale_x | scale_y | blocksize_x | blocksize_y | num_bands | pixel_types | nodata_values ------+---------+---------+-------------+-------------+-----------+-------------+-------------- 4326 | 2 | 2 | 1000 | 1000 | 1 | {8BSI} | {0}
样例: Apply single constraint
CREATE TABLE public.myrasters2(rid SERIAL primary key, rast raster); INSERT INTO myrasters2(rast) SELECT ST_AddBand(ST_MakeEmptyRaster(1000, 1000, 0.3, -0.3, 2, 2, 0, 0,4326), 1, '8BSI'::text, -129, NULL); SELECT AddRasterConstraints('public'::name, 'myrasters2'::name,'rast'::name,'regular_blocking', 'blocksize'); 注意: Adding coverage tile constraint required for regular blocking 注意: Adding spatially unique constraint required for regular blocking 注意: Adding blocksize-X constraint 注意: Adding blocksize-Y constraint addrasterconstraints ---------------------- t (1 row)
参考
6.2.1节, ST_AddBand, ST_MakeEmptyRaster, DropRasterConstraints,ST_BandPixelType, ST_SRID
8.2.2. DropRasterConstraints
DropRasterConstraints — 删除KGIS的栅格表的列上的栅格约束。如果需要重装载或更新栅格列的数据,该函数很有用。
用法
boolean DropRasterConstraints( name rasttable, name rastcolumn, boolean srid, boolean scale_x, boolean scale_y, boolean blocksize_x, boolean blocksize_y, boolean same_alignment, boolean regular_blocking, boolean num_bands=true, boolean pixel_types=true, boolean nodata_values=true, boolean out_db=true , boolean extent=true); boolean DropRasterConstraints( name rastschema, name rasttable, name rastcolumn, boolean srid=true, boolean scale_x=true, boolean scale_y=true, boolean blocksize_x=true, boolean blocksize_y=true, boolean same_alignment=true, boolean regular_blocking=false, boolean num_bands=true, boolean pixel_types=true, boolean nodata_values=true, boolean out_db=true , boolean extent=true); boolean DropRasterConstraints( name rastschema, name rasttable, name rastcolumn, text[] constraints);
描述
删除KGIS的栅格表的列上的栅格约束,栅格约束由函数AddRasterConstraints添加。如果需要重装载或更新栅格列的数据,该函数很有用。如果想删除栅格表或栅格类型的列,不需要用这个函数。
删除栅格表使用标准DDL语句:
DROP TABLE mytable;
删除一个表的栅格列,使用标准SQL:
ALTER TABLE mytable DROP COLUMN rast;
如果带有栅格类型的表或列被删除了,该表将会从记录栅格列的raster_columns表中删除。 然而如果只是栅格约束删除了,那么该表还会列出栅格列的记录,但除了栅格列名和表名外,没有其他信息。
样例
SELECT DropRasterConstraints ('myrasters','rast'); ----RESULT output --- t -- verify change in raster_columns -- SELECT srid, scale_x, scale_y, blocksize_x, blocksize_y, num_bands, pixel_types, nodata_values FROM raster_columns WHERE r_table_name = 'myrasters'; srid | scale_x | scale_y | blocksize_x | blocksize_y | num_bands | pixel_types |nodata_values ------+---------+---------+-------------+-------------+-----------+-------------+-------------- 0 | | | | | | |
参考
AddRasterConstraints
8.2.3. ST_GDALDrivers
ST_GDALDrivers — 返回的GDAL库支持的栅格格式列表。这些格式是使用函数ST_AsGDALRaster所输出的类型。
用法
setof record ST_GDALDrivers(integer OUT idx, text OUT short_name, text OUT long_name, text OUT create_options);
描述
返回的GDAL库支持的栅格格式列表,包括格式简写、全称等等。函数ST_AsGDALRaster使用的是简称。跟据编译GBDAL库所依赖的驱动类型,这些参数会有不同。 create_options参数作用是,根据每一个具体的驱动,返回一个xml格式(xml标签是:CreationOptionList/Option)的结果,其中包含驱动名称,以及可选的类型、描述和特定于驱动类型的参数。
样例 : List of Drivers
SELECT short_name, long_name FROM st_gdaldrivers() ORDER BY short_name; short_name | long_name -----------+-------------------------------------- AAIGrid | Arc/Info ASCII Grid DTED | DTED Elevation Raster EHdr | ESRI .hdr Labelled FIT | FIT Image GIF | Graphics Interchange Format (.gif) GSAG | Golden Software ASCII Grid (.grd) GSBG | Golden Software Binary Grid (.grd) GTiff | GeoTIFF HF2 | HF2/HFZ heightfield raster
样例 : List of options for each driver
-- Output the create options XML column of JPEG as a table -- -- 注意 you can use these creator options in ST_AsGDALRaster options argument SELECT (xpath('@name', g.opt))[1]::text As oname, (xpath('@type', g.opt))[1]::text As otype, (xpath('@description', g.opt))[1]::text As descrip FROM (SELECT unnest(xpath('/CreationOptionList/Option', create_options::xml)) As opt FROM st_gdaldrivers() WHERE short_name = 'JPEG') As g; oname | otype |descrip ---------------------+---------+----------------------------- PROGRESSIVE | boolean | QUALITY | int | good=100, bad=0, default=75 WORLDFILE | boolean | -- raw xml output for creator options for GeoTiff -- SELECT create_options FROM st_gdaldrivers() WHERE short_name = ’GTiff’; <CreationOptionList> <Option name="COMPRESS" type="string-select"> <Value>NONE</Value><Value>LZW</Value> <Value>PACKBITS</Value> <Value>JPEG</Value> <Value>CCITTRLE</Value> <Value>CCITTFAX3</Value><Value>CCITTFAX4</Value> <Value>DEFLATE</Value></Option> <Option name="PREDICTOR" type="int" description="Predictor Type"/> <Option name="JPEG_QUALITY" type="int" description="JPEG quality 1-100" default="75"/> <Option name="ZLEVEL" type="int" description="DEFLATE compression level1-9" default ="6"/> <Option name="NBITS" type="int" description="BITS for sub-byte files (1-7), sub-uint16 (9-15), sub-uint32 (17-31)"/> <Option name="INTERLEAVE" type="string-select" default="PIXEL"> <Value>BAND</Value> <Value>PIXEL</Value> </Option> <Option name="TILED" type="boolean" description="Switch to tiled format"/> <Option name="TFW" type="boolean" description="Write out world file"/> <Option name="RPB" type="boolean" description="Write out .RPB (RPC) file"/> <Option name="BLOCKXSIZE" type="int" description="Tile Width"/> <Option name="BLOCKYSIZE" type="int" description="Tile/Strip Height"/> <Option name="PHOTOMETRIC" type="string-select"> <Value>MINISBLACK</Value> <Value>MINISWHITE</Value><Value>PALETTE</Value> <Value>RGB</Value> <Value>CMYK</Value> <Value>YCBCR</Value> <Value>CIELAB</Value><Value>ICCLAB</Value> <Value>ITULAB</Value> </Option> <Option name="SPARSE_OK" type="boolean" description="Can newly created files have missing blocks?" default="FALSE"/> <Option name="ALPHA" type="boolean" description="Mark first extrasample as being alpha "/> <Option name="PROFILE" type="string-select" default="GDALGeoTIFF"> <Value>GDALGeoTIFF</Value> <Value>GeoTIFF</Value> <Value>BASELINE</Value> </Option> <Option name="PIXELTYPE" type="string-select"> <Value>DEFAULT</Value> <Value>SIGNEDBYTE</Value> </Option> <Option name="BIGTIFF" type="string-select" description="Force creation of BigTIFF file "> <Value>YES</Value> <Value>NO</Value> <Value>IF_NEEDED</Value> <Value>IF_SAFER</Value> </Option> <Option name="ENDIANNESS" type="string-select" default="NATIVE" description="Force endianness of created file. For DEBUG purpose mostly"> <Value>NATIVE</Value> <Value>INVERTED</Value> <Value>LITTLE</Value> <Value>BIG</Value> </Option> <Option name="COPY_SRC_OVERVIEWS" type="boolean" default="NO" description="Force copy of overviews of source dataset (CreateCopy())"/> </CreationOptionList> -- Output the create options XML column for GTiff as a table -- SELECT (xpath('@name', g.opt))[1]::text As oname, (xpath('@type', g.opt))[1]::text As otype, (xpath('@description', g.opt))[1]::text As descrip, array_to_string(xpath('Value/text()', g.opt),', ') As vals FROM (SELECT unnest(xpath('/CreationOptionList/Option', create_options::xml)) As opt FROM st_gdaldrivers() WHERE short_name = 'GTiff') As g; oname | otype | descrip --------------------+---------------+------------------------------------------------------------------------ COMPRESS | string-select | NONE, LZW, PACKBITS, JPEG, CCITTRLE, CCITTFAX3, CCITTFAX4, DEFLATE PREDICTOR | int | Predictor Type JPEG_QUALITY | int | JPEG quality 1-100 ZLEVEL | int | DEFLATE compression level 1-9 NBITS | int | BITS for sub-byte files (1-7), sub-uint16 (9-15), sub-uint32 (17-31) INTERLEAVE | string-select | BAND, PIXEL TILED | boolean | Switch to tiled format TFW | boolean | Write out world file RPB | boolean | Write out .RPB (RPC) file BLOCKXSIZE | int | Tile Width BLOCKYSIZE | int | Tile/Strip Height PHOTOMETRIC | string-select | MINISBLACK, MINISWHITE, PALETTE, RGB, CMYK, YCBCR, CIELAB, ICCLAB,ITULAB SPARSE_OK | boolean | Can newly created files have missing blocks? ALPHA | boolean | Mark first extrasample as being alpha PROFILE | string-select | GDALGeoTIFF,GeoTIFF, BASELINE PIXELTYPE | string-select | DEFAULT, SIGNEDBYTE BIGTIFF | string-select | Force creation of BigTIFF file YES, NO, IF_NEEDED, IF_SAFER ENDIANNESS | string-select | Force endianness of created file. For DEBUG purpose mostly NATIVE, INVERTED, LITTLE, BIG COPY_SRC_OVERVIEWS | boolean | Force copy of overviews of source dataset (CreateCopy()) (19 rows)
参考
ST_AsGDALRaster, ST_SRID
8.2.4. UpdateRasterSRID
UpdateRasterSRID — 根据用户指定的表名和列名,修改栅格数据的SRID值。
用法
raster UpdateRasterSRID(name schema_name, name table_name, name column_name, integer new_srid); raster UpdateRasterSRID(name table_name, name column_name, integer new_srid);
描述
根据用户指定的表名和列名,修改栅格数据的SRID值。在修改具体列的栅格SRID值前,该函数会删除该栅格列的所有约束。
注意
该函数不会影响栅格的数据(波段的像素值),只会修改栅格的元数据信息(即SRID值)。
8.3. 栅格构建函数
8.3.1. ST_AddBand
ST_AddBand—根据给定的类型和初始值以及波段位置,返回一个带有新波段的栅格,如果波段位置没有指定,则新波段添加到栅格末尾。
用法
-
raster ST_AddBand(raster rast, addbandarg[] addbandargset);
-
raster ST_AddBand(raster rast, integer index, text pixeltype, double precision initialvalue=0, double precision nodataval=NULL);
-
raster ST_AddBand(raster rast, text pixeltype, double precision initialvalue=0, double precision nodataval=NULL);
-
raster ST_AddBand(raster torast, raster fromrast, integer fromband=1, integer torastindex=at_end);
-
raster ST_AddBand(raster torast, raster[] fromrasts, integer fromband=1, integer torastindex=at_end);
-
raster ST_AddBand(raster rast, integer index, text outdbfile,integer[] outdbindex, double precision nodataval=NULL);
-
raster ST_AddBand(raster rast, text outdbfile, integer[] outdbindex,integer index=at_end, double precision nodataval=NULL);
描述
根据给定的类型和初始值以及波段位置,返回一个带有新波段的栅格,如果波段位置没有指定,则新波段添加到栅格末尾。 如果没有指定fromband值,则默认取值为1。像元类型是函数ST_BandPixelType返回的一段字符串。如果已有的位置索引已经指定,那么随后添加的波段位置索引将每次自增1。 如果指定了比像元类型最大还要大的初始值,那么初始值将被设定为所允许的像元类型的最大值。 对于那些使用数组类型参数addbandarg的函数(例如函数变体1),一个指定的并且和栅格相关的位置索引值,在这个addbandarg表示的波段添加到栅格中时候,将会被设定。 参考下面的多波段样例。
对于那些使用栅格数组类型的函数(函数变体5),如果torast参数为NULL,那么参数数组中的每个栅格的fromband波段将被集中起来生成一个新波段。
对于那些使用outdbfile参数的函数 (函数变体6和7),这个参数值必须是栅格文件的绝对路径,并且这个栅格数据文件必须也能够让kingbase的服务器进程能够访问到。
样例 : Single New Band
-- Add another band of type 8 bit unsigned integer with pixels initialized to 200UPDATE dummy_rast SET rast = ST_AddBand(rast,'8BUI'::text,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) -- uses addbandargs INSERT INTO dummy_rast(rid,rast) VALUES(10, ST_AddBand( ST_MakeEmptyRaster(100, 100, 0, 0, 1, -1, 0, 0,0), ARRAY[ ROW(1, '1BB'::text, 0, NULL), ROW(2, '4BUI'::text, 0, NULL) ]::addbandarg[] ) ); -- 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 | nodatavalue | isoutdb | path -----------+--------------+-------------+--------- 1BB | | f | 4BUI | | 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 |0 | 2
样例 : Multiple New Bands
SELECT * FROM ST_BandMetadata( ST_AddBand( ST_MakeEmptyRaster(10, 10, 0, 0, 1, -1, 0, 0, 0),ARRAY[ ROW(NULL, '8BUI', 255, 0), ROW(NULL, '16BUI', 1, 2),ROW(2, '32BUI', 100, 12), ROW(2, '32BF', 3.14, -1) ]::addbandarg[] ), ARRAY[]::integer[] ); bandnum | pixeltype | nodatavalue | isoutdb | path ---------+---------------------+-----------------+---------+------ 1 | 8BUI | | 0 | f 2 | 32BF | | -1 | f 3 | 32BUI | | 12 | f 4 | 16BUI | | 2 | f -- Aggregate the 1st band of a table of like rasters into a single raster -- with as many bands as there are test_types and as many rows (new rasters) as there are mice -- NOTE: The ORDER BY test_type is only supported in KingbaseES -- for 8.4 and below it usually works to order your data in a subselect (but not guaranteed) -- The resulting raster will have a band for each test_type alphabetical by test_type -- For mouse lovers: No mice were harmed in this exercise SELECT mouse, ST_AddBand(NULL, array_agg(rast ORDER BY test_type), 1) As rast FROM mice_studies GROUP BY mouse;
样例 : New Out-db band
SELECT * FROM ST_BandMetadata( ST_AddBand( ST_MakeEmptyRaster(10, 10, 0, 0, 1, -1, 0, 0, 0), '/home/raster/mytestraster.tif'::text, NULL::int[] ), ARRAY[]::integer[] ); bandnum | pixeltype | nodatavalue | isoutdb | path ---------+-----------+-------------+---------+--------------------------------- 1 | 8BUI | | t | /home/raster/mytestraster.tif 2 | 8BUI | | t | /home/raster/mytestraster.tif 3 | 8BUI | | t | /home/raster/mytestraster.tif
参考
ST_BandMetaData, ST_BandPixelType, ST_MakeEmptyRaster, ST_MetaData, ST_NumBands, ST_Reclass
8.3.2. ST_AsRaster
ST_AsRaster — 把KingbaseES 的几何类型对象转换成KGIS raster的栅格对象。
用法
raster ST_AsRaster( geometry geom, raster ref, text pixeltype, double precision value=1, double precision nodataval=0, booleantouched=false); raster ST_AsRaster( geometry geom, raster ref, text[] pixeltype=ARRAY['8BUI'], double precision[] value=ARRAY[1], double precision[] nodataval=ARRAY[0], boolean touched=false); raster ST_AsRaster( geometry geom, double precision scalex, double precision scaley, double precision gridx, double preci-sion gridy, text pixeltype, double precision value=1, double precision nodataval=0, double precision skewx=0, double precisionskewy=0, boolean touched=false); raster ST_AsRaster( geometry geom, double precision scalex, double precision scaley, double precision gridx=NULL, dou-ble precision gridy=NULL, text[] pixeltype=ARRAY['8BUI'], double precision[] value=ARRAY[1], double precision[] no-dataval=ARRAY[0], double precision skewx=0, double precision skewy=0, boolean touched=false); raster ST_AsRaster( geometry geom, double precision scalex, double precision scaley, text pixeltype, double precision value=1,double precision nodataval=0, double precision upperleftx=NULL, double precision upperlefty=NULL, double precision skewx=0, double precision skewy=0, boolean touched=false); raster ST_AsRaster( geometry geom, double precision scalex, double precision scaley, text[] pixeltype, double precision[]value=ARRAY[1], double precision[] nodataval=ARRAY[0], double precision upperleftx=NULL, double precision upperlefty=NULL, double precision skewx=0, double precision skewy=0, boolean touched=false); raster ST_AsRaster( geometry geom, integer width, integer height, double precision gridx, double precision gridy, text pixel-type, double precision value=1, double precision nodataval=0, double precision skewx=0, double precision skewy=0, booleantouched=false); raster ST_AsRaster( geometry geom, integer width, integer height, double precision gridx=NULL, double precision gridy=NULL,text[] pixeltype=ARRAY['8BUI'], double precision[] value=ARRAY[1], double precision[] nodataval=ARRAY[0], double pre-cision skewx=0, double precision skewy=0, boolean touched=false); raster ST_AsRaster( geometry geom, integer width, integer height, text pixeltype, double precision value=1, double precisionnodataval=0, double precision upperleftx=NULL, double precision upperlefty=NULL, double precision skewx=0, double preci-sion skewy=0, boolean touched=false); raster ST_AsRaster( geometry geom, integer width, integer height, text[] pixeltype, double precision[] value=ARRAY[1], dou-ble precision[] nodataval=ARRAY[0], double precision upperleftx=NULL, double precision upperlefty=NULL, double precisionskewx=0, double precision skewy=0, boolean touched=false);
描述
把KingbaseES的几何类型对象转换成KGIS raster的栅格对象。该函数的不同形式提供了三组方式用于设置返回栅格像元的对齐方式和像元大小。
第一组函数变体包含前两个变体。它们根据提供参考栅格生成包含相同对齐方式的栅格(scalex, scaley, gridx ,gridy),像元类型和NODATA值。 一般情况下可以通过表之间的join方式,来传递在该表中的参考栅格。
第二组函数变体包含了四个变体。可以让通过像元的大小(scalex & scaley and skewx & skewy)来设置栅格的维度。 返回的栅格的高度和宽度会被调整以便适应几何对象的边界。 在大多数情况下,必须把整型的scalex & scaley参数转换成double双精度型,以便KingbaseES可以选择正确的函数变体。
第三组函数变体包含了四个变体,可以通过提供栅格(width和height参数)来设置栅格的维度。 返回栅格的像元参数(scalex & scaley and skewx & skewy)会被调整以便适应几何对象的边界。
最后两组函数变体(即第二和第三组)的前两个变体都可以让指定对齐网格(gridx& gridy)的任意一个角点的对齐方式,而后两个函数变体可以设置左上角的参数值(upperleftx & upperlefty)。
每一组函数变体允许生成单波段栅格或者多波段栅格。 若想生成多波段栅格,必须提供一个像元类型数组(pixeltype[]),一个初始化值的数组,和一个NODATA值(用于无效值填充)数组。 如果没有提供像元类型,默认值是8BUI,value参数的值为1,而参数nodataval默认值为0。 输出栅格将会和源几何对象在同一个参考系中。唯一的区别是带有参考栅格的函数变体,返回的栅格将会带有一个和参考栅格有相同SRID值。
可选参数touched会默认设置为false,并且和GDAL ALL_TOUCHED栅格化参数值一致,这个参数决定了像元是否会和所接触的 lines 或 polygons 对象融合。
这个函数在与函数ST_AsPNG和其他ST_AsGDALRaster系列函数联合使用,从数据库中直接导出几何对象的jpeg或png文件时候特别有用。
注意
当前还不支持复杂的几何类型,比如curves、TINS、和 PolyhedralSurfaces,但一旦GDAL支持了,这个函数也将会支持。
样例 : Output geometries as PNG files
执行这些样例的前提,是 GDAL 驱动 PNG 已经启用。
-- this will output a black circle taking up 150 x 150 pixels -- SELECT ST_AsPNG(ST_AsRaster(ST_Buffer(ST_Point(1,5),10),150, 150, '8BUI'));
-- the bands map to RGB bands - the value (118,154,118) - teal -- SELECT ST_AsPNG( ST_AsRaster( ST_Buffer( ST_GeomFromText('LINESTRING(50 50,150 150,150 50)'), 10,'join=bevel'), 200,200,ARRAY['8BUI', '8BUI', '8BUI'], ARRAY[118,154,118],ARRAY[0,0,0]));
参考
ST_BandPixelType, ST_Buffer, ST_GDALDrivers, ST_AsGDALRaster, ST_AsPNG, ST_AsJPEG, ST_SRID
8.3.3. ST_Band
ST_Band — 根据一个已经存在的栅格,返回一个或更多的波段当做新的栅格。用已有的栅格来创建新的栅格很有用。
用法
raster ST_Band(raster rast, integer[] nbands = ARRAY[1]); raster ST_Band(raster rast, text nbands, character delimiter=,); raster ST_Band(raster rast, integer nband);
描述
根据一个已经存在的栅格,返回一个或更多的波段当做新的栅格。 用已有的栅格来创建新的栅格或导出一个栅格中指定的波段很有用。 如果没有选择波段,默认是波段1。在各种函数中用作一个辅助性函数,例如删除一个波段。
样例
-- Make 2 new rasters: 1 containing band 1 of dummy, -- second containing band 2 of dummy and then reclassified as a 2BUI SELECT ST_NumBands(rast1) As numb1, ST_BandPixelType(rast1) As pix1, ST_NumBands(rast2) As numb2, ST_BandPixelType(rast2) As pix2 FROM ( SELECT ST_Band(rast) As rast1, ST_Reclass(ST_Band(rast,3), '100-200):1, [200-254:2', '2 BUI') As rast2 FROM dummy_rast WHERE rid = 2) As foo; numb1 | pix1 | numb2 | pix2 -------+------+-------+------ 1 | 8BUI | 1 | 2BUI -- Return bands 2 and 3. Use text to define bands SELECT ST_NumBands(ST_Band(rast, '2,3')) As num_bands FROM dummy_rast WHERE rid=2; num_bands ---------- 2 -- Return bands 2 and 3. Use array to define bands SELECT ST_NumBands(ST_Band(rast, ARRAY[2,3])) As num_bands FROM dummy_rast WHERE rid=2;
-- Make a new raster with 2nd band of original and 1st band repeated twice, -- and another with just the third band SELECT rast, ST_Band(rast, ARRAY[2,1,1]) As dupe_band, ST_Band(rast, 3) As sing_band FROM samples.than_chunked WHERE rid=35;
参考
ST_AddBand, ST_NumBands, , ST_Reclass
8.3.4. ST_MakeEmptyRaster
ST_MakeEmptyRaster — 根据给定的维度(宽度和高度),参考坐标或 X,Y 位置(通常在栅格左上角或左下角), 像元大小和旋转信息(scalex, scaley, skewx & skewy)和SRID值。 如果传递了一个栅格对象,则返回一个新的栅格,新栅格和原栅格有一样的大小、对齐方式和SRID值。 如果SRID值省略了,则SRID值设置为0(未知),返回一个空栅格(没有波段的栅格)。
用法
raster ST_MakeEmptyRaster(raster rast); raster ST_MakeEmptyRaster( integer width, integer height, float8 upperleftx, float8 upperlefty, float8 scalex, float8 scaley,float8 skewx,float8 skewy, integer srid=unknown); raster ST_MakeEmptyRaster( integer width, integer height, float8 upperleftx, float8 upperlefty, float8 pixelsize);
描述
根据给定的维度(宽度和高度),参考坐标或 X,Y 位置(通常在栅格左上角或左下角),像元大小和旋转信息(scalex,scaley,skewx & skewy)和SRID值。 如果传递了一个栅格对象,则返回一个新的栅格,新栅格和原栅格有一样的大小、对齐方式和SRID值,但没有波段。 如果SRID值省略了,则SRID值设置为0(未知),返回一个空栅格(没有波段的栅格)。
最后一种形式使用单个参数来定义像元大小(pixelsize)。scalex用于定义这个参数,scaley定义该参数的负值。skewx 和 skewy 设置为0。
如果传递了一个栅格对象,则返回一个新的栅格,新栅格和原栅格有一样的大小、对齐方式和SRID值,但没有波段。
如果没有指定SRID值,默认设置为0,创建一个空栅格后,可能需要添加波段并编辑它。
参考函数ST_AddBand怎样定义波段和函数ST_SetValue怎样初始化像元值。
样例
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
参考
ST_AddBand, ST_MetaData, ST_ScaleX, ST_ScaleY, ST_SetValue, ST_SkewX, ST_SkewY
8.3.5. ST_Tile
ST_Tile —根据输入的栅格和输出的栅格维度,返回一个栅格集合
用法
setof raster ST_Tile( raster rast, int[] nband, integer width, integer height, boolean padwithnodata=FALSE, double precision no-dataval=NULL); setof raster ST_Tile( raster rast, integer nband, integer width, integer height, boolean padwithnodata=FALSE, double precisionnodataval=NULL); setof raster ST_Tile( raster rast, integer width, integer height, boolean padwithnodata=FALSE, double precision nodataval=NULL);
描述
根据输入的栅格和输出的栅格维度,返回一个栅格集合。
如果参数padwithnodata = FALSE,栅格的右侧和左侧的边界瓦片可能和剩余瓦片的维度不同。 如果参数padwithnodata = TRUE,那么所有的瓦片维度将会和填充的边界的瓦片维度相同,NODATA值也相同。 如果栅格波段没有指定NODATA值,可以通过nodataval参数指定该值。
注意
如果输入栅格的指定波段在数据库之外,输出的栅格也将在数据库之外。
样例
WITH foo AS ( SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0), 2, '8BUI', 10, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 3, 0, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0), 2, '8BUI', 20, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 6, 0, 1, -1, 0, 0, 0), 1, '8BUI', 3, 0), 2, '8BUI', 30, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 0, -3, 1, -1, 0, 0, 0), 1, '8BUI', 4, 0), 2, '8BUI', 40, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 3, -3, 1, -1, 0, 0, 0), 1, '8BUI', 5, 0), 2, '8BUI', 50, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 6, -3, 1, -1, 0, 0, 0), 1, '8BUI', 6, 0), 2, '8BUI', 60, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 0, -6, 1, -1, 0, 0, 0), 1, '8BUI', 7, 0), 2, '8BUI', 70, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 3, -6, 1, -1, 0, 0, 0), 1, '8BUI', 8, 0), 2, '8BUI', 80, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 6, -6, 1, -1, 0, 0, 0), 1, '8BUI', 9, 0), 2, '8BUI', 90, 0) AS rast ), bar AS ( SELECT ST_Union(rast) AS rast FROM foo ), baz AS ( SELECT ST_Tile(rast, 3, 3, TRUE) AS rast FROM bar ) SELECT ST_DumpValues(rast) FROM baz; st_dumpvalues ------------------------------------------ (1,"{{1,1,1},{1,1,1},{1,1,1}}") (2,"{{10,10,10},{10,10,10},{10,10,10}}") (1,"{{2,2,2},{2,2,2},{2,2,2}}") (2,"{{20,20,20},{20,20,20},{20,20,20}}") (1,"{{3,3,3},{3,3,3},{3,3,3}}") (2,"{{30,30,30},{30,30,30},{30,30,30}}") (1,"{{4,4,4},{4,4,4},{4,4,4}}") (2,"{{40,40,40},{40,40,40},{40,40,40}}") (1,"{{5,5,5},{5,5,5},{5,5,5}}") (2,"{{50,50,50},{50,50,50},{50,50,50}}") (1,"{{6,6,6},{6,6,6},{6,6,6}}") (2,"{{60,60,60},{60,60,60},{60,60,60}}") (1,"{{7,7,7},{7,7,7},{7,7,7}}") (2,"{{70,70,70},{70,70,70},{70,70,70}}") (1,"{{8,8,8},{8,8,8},{8,8,8}}") (2,"{{80,80,80},{80,80,80},{80,80,80}}") (1,"{{9,9,9},{9,9,9},{9,9,9}}") (2,"{{90,90,90},{90,90,90},{90,90,90}}") (18 rows) WITH foo AS ( SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 0, 0, 1, -1, 0, 0, 0), 1, '8BUI', 1, 0), 2, '8BUI', 10, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 3, 0, 1, -1, 0, 0, 0), 1, '8BUI', 2, 0), 2, '8BUI', 20, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 6, 0, 1, -1, 0, 0, 0), 1, '8BUI', 3, 0), 2, '8BUI', 30, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 0, -3, 1, -1, 0, 0, 0), 1, '8BUI', 4, 0), 2, '8BUI', 40, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 3, -3, 1, -1, 0, 0, 0), 1, '8BUI', 5, 0), 2, '8BUI', 50, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 6, -3, 1, -1, 0, 0, 0), 1, '8BUI', 6, 0), 2, '8BUI', 60, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 0, -6, 1, -1, 0, 0, 0), 1, '8BUI', 7, 0), 2, '8BUI', 70, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 3, -6, 1, -1, 0, 0, 0), 1, '8BUI', 8, 0), 2, '8BUI', 80, 0) AS rast UNION ALL SELECT ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(3, 3, 6, -6, 1, -1, 0, 0, 0), 1, '8BUI', 9, 0), 2, '8BUI', 90, 0) AS rast ), bar AS ( SELECT ST_Union(rast) AS rast FROM foo ), baz AS ( SELECT ST_Tile(rast, 3, 3, 2) AS rast FROM bar ) SELECT ST_DumpValues(rast) FROM baz; st_dumpvalues ------------------------------------------ (1,"{{10,10,10},{10,10,10},{10,10,10}}") (1,"{{20,20,20},{20,20,20},{20,20,20}}") (1,"{{30,30,30},{30,30,30},{30,30,30}}") (1,"{{40,40,40},{40,40,40},{40,40,40}}") (1,"{{50,50,50},{50,50,50},{50,50,50}}") (1,"{{60,60,60},{60,60,60},{60,60,60}}") (1,"{{70,70,70},{70,70,70},{70,70,70}}") (1,"{{80,80,80},{80,80,80},{80,80,80}}") (1,"{{90,90,90},{90,90,90},{90,90,90}}") (9 rows)
参考
ST_Union
8.3.6. ST_FromGDALRaster
ST_FromGDALRaster — 根据一个可以GDAL 支持的栅格数据,创建一个栅格。
用法
raster ST_FromGDALRaster(bytea gdaldata, integer srid=NULL);
描述
根据一个可以GDAL 支持的栅格数据,创建一个栅格。参数gdaldata是bytea类型,需要包含GDAL栅格数据的内容。
如果SRID值为NULL,该函数会从GDAL栅格中自行指定SRID值。如果SRID已经在参数中了,那么会覆盖GDAL栅格中的任何SRID值。
样例
WITH foo AS ( SELECT ST_AsPNG(ST_AddBand(ST_AddBand(ST_AddBand(ST_MakeEmptyRaster(2, 2, 0, 0, 0.1, -0.1, 0, 0, 4326), 1, '8BUI', 1, 0), 2, '8BUI', 2, 0), 3, '8BUI', 3, 0)) AS png ), bar AS ( SELECT 1 AS rid, ST_FromGDALRaster(png) AS rast FROM foo UNION ALL SELECT 2 AS rid, ST_FromGDALRaster(png, 3310) AS rast FROM foo ) SELECT rid, ST_Metadata(rast) AS metadata, ST_SummaryStats(rast, 1) AS stats1, ST_SummaryStats(rast, 2) AS stats2, ST_SummaryStats(rast, 3) AS stats3 FROM bar ORDER BY rid; rid | metadata | stats1 | stats2 | stats3 -----+---------------------------+---------------+---------------+---------------- 1 | (0,0,2,2,1,-1,0,0,0,3) | (4,4,1,0,1,1) | (4,8,2,0,2,2) | (4,12,3,0,3,3) 2 | (0,0,2,2,1,-1,0,0,3310,3) | (4,4,1,0,1,1) | (4,8,2,0,2,2) | (4,12,3,0,3,3) (2 rows)
参考
ST_AsGDALRaster