<div dir="ltr"><div><div>Thanks Pierre,<br><br></div><div>I didn't know that array_agg didn't work with arrays, however on I do get a 3d array using array_agg (PG 9.4), what's wrong exactly with that array ? It seems to load fine with st_setvalues (if I use a single band). <br></div><br>Still, I can't figure how to load an arbitrary number of bands using your snippet. If I remove WHERE band=1 I still get a unique band.<br><br></div><div>(I fixed the raster dimensions, row and col were inverted, so that might be the reason why it looked odd to aggregate on col then row.)<br></div><div><br></div>Etienne<br></div><br><div class="gmail_quote"><div dir="ltr">Le ven. 26 févr. 2016 à 11:04, Pierre Racine <<a href="mailto:Pierre.Racine@sbf.ulaval.ca">Pierre.Racine@sbf.ulaval.ca</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Étienne,<br>
<br>
The columnx and rowy ST_SetValues() parameters are not references to each pixel values. They are the coordinates only of the first upperleft pixel to be set. Other pixels coordinates are automatically derived from the size of the passed two dimensional array (newvalueset). That means you must first aggregate your values into a two dimensional array. Try this:<br>
<br>
-- Create an array of array aggregator because array_agg does not support arrays...<br>
CREATE AGGREGATE array_agg_mult(anyarray) (<br>
SFUNC = array_cat,<br>
STYPE = anyarray,<br>
INITCOND = '{}'<br>
);<br>
<br>
-- Aggregate the test.measure values into a two dimensional array and build the raster<br>
WITH rows AS (<br>
SELECT colx, array_agg(v ORDER BY rowy) a<br>
FROM test.measure<br>
WHERE band = 1<br>
GROUP BY colx<br>
), valarray AS (<br>
SELECT array_agg_mult(ARRAY[a] ORDER BY colx) a<br>
FROM rows<br>
), raster AS (<br>
SELECT rast FROM test.rasters WHERE rid=1<br>
), newraster AS (<br>
SELECT ST_SetValues(rast, 1, 1, 1, valarray.a) rast<br>
FROM valarray, raster<br>
)<br>
SELECT (ST_PixelAsPolygons(rast)).*<br>
FROM newraster;<br>
<br>
You might have to play a bit with the col and row ordering to get exactly what you want.<br>
<br>
Pierre<br>
<br>
> -----Original Message-----<br>
> From: postgis-users [mailto:<a href="mailto:postgis-users-bounces@lists.osgeo.org" target="_blank">postgis-users-bounces@lists.osgeo.org</a>] On<br>
> Behalf Of Etienne B. Racine<br>
> Sent: Friday, February 26, 2016 8:34 AM<br>
> To: PostGIS Users Discussion<br>
> Subject: [postgis-users] Create raster from row-col table<br>
><br>
> Hi list,<br>
><br>
><br>
> I have raster data stored in a table row-col format as measure(rowy integer,<br>
> colx integer, band integer, value float).<br>
><br>
><br>
> I've tried loading a multidimensional array (row x col x bands) with no<br>
> success, however building a 2d array (row x col) works but it seems<br>
> contrived, especially once I need to stack the bands. I'm still figuring the<br>
> best way to store the rasters, but I might end up storing 39 bands in a<br>
> single raster. For now building an array isn't very fast, so I'm looking for<br>
> speed improvement as well.<br>
><br>
> I wonder what's the most efficient way to populate a raster with data of<br>
> this shape ? My actual method wouldn't work with sparse data and would<br>
> require to pad the array with nodata values.<br>
><br>
><br>
> I've put out a gist that explains my solution with sample data :<br>
> <a href="https://gist.github.com/etiennebr/370a773029160c30d165#file-rowcol-" rel="noreferrer" target="_blank">https://gist.github.com/etiennebr/370a773029160c30d165#file-rowcol-</a><br>
> raster-sql<br>
><br>
><br>
> Thanks for your help,<br>
><br>
> Etienne<br>
><br>
<br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">http://lists.osgeo.org/mailman/listinfo/postgis-users</a></blockquote></div>