使用ST_Dwithin进行PostGIS查询并输出到表并在单独的列中添加距离

时间:2017-03-19 20:17:37

标签: postgis

我正在努力解决如何将实际距离添加到我使用查询创建的表中。这就是我所拥有的:

CREATE TABLE cdot.rail_risk AS
SELECT DISTINCT ON (t2.gid) t2.gid, t2.status, t2.railroad, t2.geom
FROM cdot.streams t1
    LEFT JOIN cdot.rail_lines_100k t2 ON ST_DWithin(t1.geom, t2.geom, 100)
WHERE t2.status = 'IN SERVICE'

ORDER BY t2.gid, ST_Distance(t1.geom, t2.geom);

我要做的是添加一个数字列'dist'(四舍五入到0位)并填充距离。我尝试了几种不同的选择,但都没有。这是一次失败的尝试:

CREATE TABLE cdot.rail_risk AS
SELECT DISTINCT ON (t2.gid) t2.gid, t2.status, t2.railroad, t2.geom
FROM cdot.streams t1
    LEFT JOIN cdot.rail_lines_100k t2 ON ST_DWithin(t1.geom, t2.geom, 100)
WHERE t2.status = 'IN SERVICE'
ADD COLUMN dist numeric
UPDATE rail_risk SET dist= ROUND((ST_Distance (t1.geom, t2.geom)):: numeric,0)
ORDER BY dist;

我是postgis的新手,我正在努力学习这一点,感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我不知道你写了什么,但它远离正确的sql查询。 '添加列'是'alter table'的选项,select中的update不会那样工作...如果你需要编写sql查询请至少阅读SQL的基础知识。

你需要另一个专栏,只需在select子句中添加它......

CREATE TABLE cdot.rail_risk AS
SELECT DISTINCT ON (t2.gid) t2.gid, t2.status, t2.railroad, t2.geom, ROUND((ST_Distance (t1.geom, t2.geom)):: numeric,0) as dist
FROM cdot.streams t1
    LEFT JOIN cdot.rail_lines_100k t2 ON ST_DWithin(t1.geom, t2.geom, 100)
WHERE t2.status = 'IN SERVICE'
ORDER BY (dist);