--美国航天飞机雷达地形测量任务下载高程数据
wget http://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF/srtm_12_03.zip
unzip srtm_12_03.zip
gdalinfo srtm_12_03.tif
-- 使用raster2pgsql命令导入栅格数据
apt-get update
apt-get upgrade
apt install postgis
raster2pgsql -s 4326 -I -C -M /var/lib/postgis/data/tif/analysed_sst_Layer1.tif -F -t 256x256 public.sst | psql -h 192.168.1.204 -p 15432 -U postgres -d gis_db -W
4326是文件的的SRID(空间参考信息),/var/lib/postgis/data/tif/analysed_sst_Layer1.tif 是文件所在位置,public.sst是将数据存储在public模式下的sst表中,这个表不用提前建,我所用的命令会自动建表;localhost主机名称,15432端口,postgres用户名,gis_db是第1步中添加了postgis拓展的数据库名。
--验证导入表结构
test_raster=# \d srtm_12_03Table "public.srtm_12_03"Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+-----------------------------------------rid | integer | | not null | nextval('srtm_12_03_rid_seq'::regclass)rast | raster | | |
Indexes:"srtm_12_03_pkey" PRIMARY KEY, btree (rid)
"srtm_12_03_st_convexhull_idx" gist (st_convexhull(rast))--验证导入表数据
test_raster=# select count(1) from srtm_12_03;count
-------29768
(1 row)
--栅格导出支持的格式--我们使用gtiff
SELECT short_name, long_name, can_write
FROM st_gdaldrivers()
ORDER BY short_name;--栅格导出--使用psql
--编辑数据库配置文件,开启栅格导出功能,postgresql.conf,文件尾部追加2行配置
postgis.gdal_enabled_drivers = 'ENABLE_ALL'
postgis.enable_outdb_rasters = True
--重启数据库
pg_ctrl restart
--导出
--psql -c "COPY (SELECT ST_AsGDALRaster(rast, 'GTiff') AS rast FROM test_raster WHERE id = 1) TO '.tif'"
psql -c "COPY (SELECT ST_AsGDALRaster(rast, 'GTiff') AS rast FROM srtm_12_03 WHERE rid = 1) TO '/home/postgres/aa.tif'" test_raster
--栅格SQL导出--a)创建临时表
CREATE TABLE tmp_out AS
SELECT lo_from_bytea(0,ST_AsGDALRaster(ST_Union(rast), 'GTiff')) AS loidFROM srtm_12_03
WHERE rid=1;
--栅格SQL导出--b)文件流导出到本地
SELECT lo_export(loid, '/tmp/aa.tif') FROM tmp_out;
--栅格SQL导出--c)解除大对象
SELECT lo_unlink(loid) FROM tmp_out;
--栅格导出tiff
SELECT ST_AsGDALRaster(rast, 'GTiff') As rastjpg FROM dummy_rast WHERE rid=2;
--栅格文件(大对象)使用SQL导入 (这里rast是oid类型)
INSERT INTO test_raster_4_1 (rast) VALUES (lo_import('/tmp/aa.tif'));
SELECT ST_Metadata(rast) AS metadata FROM srtm_12_03 WHERE rid = 1;
metadata
----------------------------------
(-124.97333333333333,50,32,32,0.0008333333333333334,-0.0008333333333333334,0,0,4326,1)
SELECT ST_PixelAsPolygons(rast) AS pixels FROM srtm_12_03 WHERE rid = 1;
pixels
----------------------------------
(0103000020E61000000100000005000000703D0AD7A33C5FC0AE47E17A14FE4840FC62C92F963C5FC0AE47E17A14FE4840FC62C92F963C5FC0C6925F2CF9FD4840703D0AD7A33C5FC0C6925F2CF9FD4840703D0AD7A33C5FC0AE47E17A14FE4840,3,32,19)