我目前正在使用Postgres 9.1
我的目标是使用多边形剪辑PostGIS栅格。然后,我想要为该多边形中包含的每个栅格像素设置一个postgres数组或分隔的值集。这是我到目前为止的查询:
SELECT p.gid, (ST_ValueCount(ST_Clip(r.rast, p.geom))).value AS fval
FROM temp_raster AS r, temp_shapefile AS p
WHERE (r.start_time = 1384516800)
GROUP BY gid, fval;
这将输出:
gid | fcstval
-----+---------
1 | 0
1 | 2
2 | 0
2 | 2
3 | 5
4 | 0
4 | 1
4 | 2
4 | 3
4 | 5
这个数据很好,但我想为每个gid设置一组值,如下所示:
gid | fcstval
-----+----------
1 | 0,2
2 | 0,2
3 | 5
4 | 0,1,2,3,5
我遇到的麻烦是尝试将这些值聚合成数组或分隔的字符串。这是我对阵列的尝试:
SELECT p.gid, array_agg((ST_ValueCount(ST_Clip(r.rast, p.geom))).value) AS fval
FROM temp_raster AS r, temp_shapefile AS p
WHERE (r.start_time = 1384516800)
GROUP BY gid;
这不起作用,并提供错误:
ERROR: set-valued function called in context that cannot accept a set
我的猜测是因为我无法以这种方式调用array_agg。我有点难以弄清楚如何做到这一点。一个子查询或许?虽然为了让这个工作起作用,但我还没有想出任何东西。
感谢您的帮助!
答案 0 :(得分:0)
好吧,我想我想出了数组。如果我想要它在一个字符串中,我可以将我的数组转换为字符串。但是,如果有人有任何清理它的建议我会很感激,因为这似乎不是最简单的标记
SELECT p.gid, array_agg((subquery).tempval) as fcstval
FROM pih_raster AS r, hwy_pih_vertex_buf AS p,
(
SELECT p.gid AS tempgid, (ST_ValueCount(ST_Clip(r.rast, p.geom))).value AS tempval
FROM pih_raster AS r, hwy_pih_vertex_buf AS p
WHERE (r.start_time <= 1384624800) GROUP BY tempgid, tempval
) AS subquery
WHERE (r.start_time <= 1384624800) AND ((subquery).tempgid = gid) GROUP BY p.gid;
这是输出:
gid | fcstval
-----+-------------
1 | {0,2}
2 | {0,2}
3 | {5}
4 | {0,1,2,3,5}