如何将纬度/经度对转换为 PostGIS 地理类型?
我正在尝试将一堆纬度/经度对加载到 PostGIS 地理类型中,以便能够按位置查询。
特别是,我有一个带有浮动纬度和经度列以及地理(Point,4326)列的表。我想做
update mytable set geography = ???
文档似乎建议以下操作应该有效:
update mytable set geography = ST_GeogFromText('POINT(' || latitude || ' ' ||
longitude || ')');
它不起作用。我不知道它对这一点的解释是什么,但它只允许经度位于-90到90之间,所以它显然不是经度。
那么,我该怎么办?
I'm trying to load a bunch of latitude/longitude pairs into a PostGIS geography type so as to be able to query by location.
In particular I have a table with float latitude and longitude columns and a geography(Point, 4326)
column. I would like to do
update mytable set geography = ???
The documentation appears to suggest that the following should work:
update mytable set geography = ST_GeogFromText('POINT(' || latitude || ' ' ||
longitude || ')');
It doesn't. I don't know what it's interpreting this point as meaning, but it only allows the longitude to lie between -90 and 90, so it's clearly not a longitude.
So, what do I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
...叹。我的愚蠢。显然正确的顺序是经度、纬度。我误以为两个坐标具有相同的范围(-180 到 180),因此认为发生了更微妙的事情。
...sigh. Stupidity on my part. Apparently the correct order is longitude, latitude. I was fooled into thinking that both coordinates had the same range (-180 to 180) so thought something more subtle was going on.
以下是创建地理类型的一些不同方法:
将数字
long
和lat
列转换为geog
地理类型:转换
geom
几何列 (< a href="http://epsg.io/4326" rel="noreferrer">SRID=4326) 使用简单的转换转换为geog
地理类型:将投影的 geom 几何列转换为 geog 地理类型:
请注意,最后两个示例适用于任何几何类型。此外,从几何到地理的转换通常是隐式的,并且这些示例无需
::geography
即可工作,但是显式转换通常是这些事情的良好实践。Here are some different ways to make geography types:
Convert numeric
long
andlat
columns to ageog
geography type:Convert a
geom
geometry column (SRID=4326) to ageog
geography type using a simple cast:Transform a projected
geom
geometry column to ageog
geography type:Note that the last two examples work on any geometry type. Also, the conversion from geometry to geography is often implicit, and these examples work without
::geography
, however explicit casts are usually a good practice for these things.要执行 lat 和 lng 之间的交换,您可以使用:
带或不带 srid。
To perform exchange between lat and lng you may use:
with or without srid.