Database Reference
In-Depth Information
Sharing and visualizing rasters through
SQL
Inapreviouschapter,weused
gdal_translate
toexportPostGISrasterstoafile.
Thisprovidesamethodfortransferringfilesfromoneusertoanotherorfromoneloc-
ationtoanother.Theonlyproblemwiththismethodisthatyoumaynothaveaccess
to the
gdal_translate
utility.
Adifferentbutequallyfunctionalapproachistousethe
ST_AsGDALRaster()
family
of functions available in PostGIS. In addition to
ST_AsGDALRaster()
, PostGIS
provides
ST_AsTIFF()
,
ST_AsPNG()
,and
ST_AsJPEG()
tosupportthemostcom-
mon raster file formats.
ToeasilyvisualizerasterfileswithouttheneedforaGISapplication,PostGIS2.1and
laterversionsprovide
ST_ColorMap()
.Thisfunctionappliesabuilt-inoruser-speci-
fiedcolorpalettetoarasterthat,uponexportingwith
ST_AsGDALRaster()
,canbe
viewed with any image viewer, such as a web browser.
Getting ready
In this recipe, we will use
ST_AsTIFF()
and
ST_AsPNG()
to export rasters to the
GeoTIFFandPNGfileformats,respectively.Wewillalsoapply
ST_ColorMap()
so
that we can see them in any image viewer.
ThequeriesbelowcanberuninastandardSQLclientsuchas
psql
or
pgAdminIII
.
However, we can't use the returned output because the output has escaped, and
theseclientsdonotundotheescaping.ApplicationswithlowerlevelAPIfunctionscan
unescapethequeryoutput.ExamplesofthiswouldbeaPHPscriptpassingarecord
elementto
pg_unescape_bytea()
oraPythonscriptusingPsycopg2'simplicitde-
codingwhilefetchingarecord.AsamplePHPscript(
save_raster_to_file.php
)
can be found in this chapter's
data
directory.