从 plpgsql 脚本中的行变量插入 PostGIS 对象(例如 ST_GeomFromText)
我有两个表 src_pos 和 dest_pos。
src_pos 存储经度、纬度和海拔的位置,而 dest_pos 存储 PosGIS Geometry 对象。
现在我想使用以下 plpgsql 脚本将一堆数据从 src_pos 移动到 dest_pos。 但它失败了,因为行可用(例如 row_data.longitude)无法正确解释。 我该如何克服这个问题!?
--create language 'plpgsql';
drop function createPosition();
create function createPosition() returns integer AS
$$
DECLARE
updated INTEGER = 0;
row_data src_pos%ROWTYPE;
BEGIN
FOR row_data IN SELECT * FROM src_pos
LOOP
INSERT INTO dest_pos (coord) VALUES (ST_GeomFromText('POINT(row_data.longitude row_data.latitude row_data.altitude)', 4326));
updated := updated + 1;
END LOOP;
RETURN updated;
END;
$$
LANGUAGE 'plpgsql';
I have two tables src_pos and dest_pos.
The src_pos stores positions with longitude, latitude and altitude, while the dest_pos stores PosGIS Geometry object.
Now I want to move a bunch of data from src_pos to dest_pos with following plpgsql script.
But it failed, because row vaiable (e.g. row_data.longitude) cannot be interpreted correctly.
How can I overcome this problem!?
--create language 'plpgsql';
drop function createPosition();
create function createPosition() returns integer AS
$
DECLARE
updated INTEGER = 0;
row_data src_pos%ROWTYPE;
BEGIN
FOR row_data IN SELECT * FROM src_pos
LOOP
INSERT INTO dest_pos (coord) VALUES (ST_GeomFromText('POINT(row_data.longitude row_data.latitude row_data.altitude)', 4326));
updated := updated + 1;
END LOOP;
RETURN updated;
END;
$
LANGUAGE 'plpgsql';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好的是,使用 ST_MakePoint 直接制作几何对象。这不仅比 ST_GeomFromText 更快,而且是无损的,因为您不需要将数字转换为文本再转换为数字。
Better yet, use ST_MakePoint to directly make a geometry object. This is not only faster than ST_GeomFromText, but it is lossless, since you don't need to convert numbers to text to numbers.
在评论中您给出了自己的解决方案:
这是一个非常好的解决方案。实际上,我在 postgresql 的其他领域的某些情况下做了一些相对类似的事情。
事实上,每种 PostgreSQL 类型都可以表示为文本。如果您愿意操纵这些,您可以以普通类型转换系统不允许的方式在类型之间进行传输。
In the comment you gave your own solution:
This is a perfectly good solution. I actually do something relatively similar in some cases in other areas of postgresql.
The fact is that every PostgreSQL type can be represented as text. If you are willing to manipulate these, you can transfer between types in ways that the normal type casting system does not allow.