[postgis-users] Rasterize a vector

Bborie Park bkpark at ucdavis.edu
Tue May 29 11:06:09 PDT 2012


On 05/29/2012 10:56 AM, Paolo Cavallini wrote:
> Il 29/05/2012 19:03, Bborie Park ha scritto:
>> Read it again.  The very first variant listed on that page has the
>> ST_AsRaster(geom geometry, rast raster, ...)
> 
> Yes, that's what I was trying, without success. Let's say I have two adjacent
> polygons, and I want a raster where the cells have the value of one of the fields of
> the the polygons (say, the id). Moreover, I want the resulting raster to be aligned
> to an existing raster (this is a very common task in GIS, easily done in GRASS, GDAL,
> SAGA, etc.). I do not see an option in:
> 
> ST_AsRaster(geometry geom, raster ref, text pixeltype, double precision value=1,
> double precision nodataval=0, boolean touched=false);
> 
> to write in each cell the value of the raster. Moreover, a simple
> 
>  CREATE TABLE rasterized as
>    select b.rid, ST_AsRaster(a.geom, b.rast, '4BUI') as rast
>    from province a, hdr b;
> 
> results in (N geometries * N tiles) records, whereas I needed (N tiles) records, and
> for each tile a raster with a different value for each province.
> 
> All the best, and thanks.

WITH ref AS (
	SELECT ST_MakeEmptyRaster(1, 1, 0, 0, 1, -1, 0, 0, 0) AS rast
)
SELECT
	ST_AsRaster(g.geom, ref.rast, '32BUI', g.id, 0)
FROM mygeomtable g
CROSS JOIN ref

That would convert your geometries into rasters, all of which have the
same alignment with pixel values set to the particular geometry's id.
So, for 20 input geometries, there would be 20 output rasters.

-bborie

-- 
Bborie Park
Programmer
Center for Vectorborne Diseases
UC Davis
530-752-8380
bkpark at ucdavis.edu



More information about the postgis-users mailing list