With all the cool new raster functions coming in postGIS 2.1, it's really hard to pick a favorite. One of the functions I feel like I've been waiting eons
for is ability to burn a set of geometry values in a raster with one SQL statement. My dream came true with ST_SetValues. Check this out:
A side note, people have asked how we visualize these things. We used the ad hoc web viewer we had created that we described in Minimalist Web based ASP.NET PostGIS 2+
and brother version PHP viewer version.
We do a lot of ASP.NET development and for using the ASP.NET viewer, we just set the credentials, in web.config, install the helper function in our test database running PostGIS, and then right-click View in Browser the postgis_viewer.htm with Visual Studio or Visual Web Express. The PHP version is setup much the same and in fact the postgis_viewer.htm are identical except the php version calls a PHP handler file and the ASP.NET one calls an ASP.NET http handler. The client side has some jquery magic that feeds that posts the query to the handler. The handler does the query and outputs the results which are then injected via jquery call.
Which I generated with the code below. Admittedly I could have come up with a more interesting topic than land and water area, but hopefully you get the point.
WITH usext AS (SELECT ST_Extent(ST_Transform(geom_4269,2163)) As ext
, Max(aland) As max_aland, MIN(aland) As min_aland
, MAX(awater) As max_awater, MIN(awater) As min_awater
FROM staging.tl_2012_state)
, canvas AS (SELECT ST_AddBand(
ST_AddBand(
ST_MakeEmptyRaster( ( (ST_XMax(ext) - ST_XMin(ext))/20000)::integer
, ( (ST_YMax(ext) - ST_YMin(ext))/20000)::integer
, ST_XMin(ext), ST_YMax(ext),20000, -20000, 0, 0, 2163)
, '8BUI'::text, 0, 0 )
, '8BUI'::text, 0, 0)
As rast
FROM usext)
, heatmap As (
SELECT array_agg(
(ST_Transform(geom_4269,2163)
, ((aland - min_aland)/(max_aland - min_aland)*255)::integer)::geomval
) As gvals_land
, array_agg(
(ST_Transform(geom_4269,2163)
, ((awater - min_awater)/(max_awater- min_awater)*255)::integer)::geomval
) As gvals_water
FROM staging.tl_2012_state As s CROSS JOIN usext
)
SELECT
ST_AddBand(ST_SetValues(
ST_SetValues(rast
, 1, heatmap.gvals_land)
, 2, heatmap.gvals_water)
, '8BUI'::text, 0,0) As rast
FROM canvas CROSS JOIN heatmap;