我试图创建视图或表格,其中是从点缓冲区生成的多边形,并根据年份对所有多边形进行分组。 源表中的数据字段:
point_id(int), point_created(date), geom geometry(Point,3301)
1, 2014-05-09, point
2, 2015-01-01, point
2, 2015-02-05, point
3, 2016-02-05, point
4, 2017-02-10, point
我能够创建表格,其中我按年份对所有点进行分组,并将缓冲区生成为多面,但我需要的是根据年份之间的日期对组进行分组(组合一年以上),因此表必须如下所示:
polygon(geom), nr_of_features(int), year(string)
1, 1, 2014(all point from 2014)
2, 3, 2015(all points from 2014 to 2015)
3, 4, 2016(all points from 2014 to 2016)
4, 5, 2017(all points from 2014 to 2017)
脚本I,我现在正在使用:
CREATE TABLE my_new_table as
SELECT ST_Union(ST_Buffer(geom,10))::geometry(MultiPolygon,3301) as polygon,count(point_id)::integer as nr_of_features,
extract(year from point_created) as year
FROM my_table
group by year;
欢迎任何帮助。
答案 0 :(得分:0)
如果我理解你,你需要这样的东西:
--just for create few years
WITH RECURSIVE dates AS (
SELECT '2014-12-31'::timestamp years
UNION ALL
SELECT years + interval '1 year' FROM dates WHERE years < '2019-12-31'
)
SELECT
EXTRACT(YEAR FROM d.years) years,
p.*
FROM
points p
INNER JOIN
dates d ON p.date <= d.years
;
现在,您可以使用years
列来汇总这些点。
答案 1 :(得分:0)
由于点日期介于1994年和当前日期之间,我不得不稍微修改这个脚本,因此我将样本值“2014-12-31”改为2013-12-31和“2019-12-31”改为current_date 。它现在似乎有效,谢谢你。我有13000个点,使用此查询创建表需要173毫秒:
WITH RECURSIVE dates AS (
SELECT '1993-12-31'::timestamp years
UNION ALL
SELECT years + interval '1 year' FROM dates WHERE years < current_date
)
SELECT
EXTRACT(YEAR FROM d.years) years,
ST_Multi(ST_Collect(geom))::geometry(MultiPoint,3301) as polygon, count(years)::int as nr_features
FROM
my_table p
INNER JOIN
dates d ON p.point_created <= d.years
group by years
order by years asc;