如何将纬度/经度对转换为 PostGIS 地理类型?

发布于 2024-08-26 07:16:31 字数 443 浏览 3 评论 0原文

我正在尝试将一堆纬度/经度对加载到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萧瑟寒风 2024-09-02 07:16:31

...叹。我的愚蠢。显然正确的顺序是经度、纬度。我误以为两个坐标具有相同的范围(-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.

油焖大侠 2024-09-02 07:16:31

以下是创建地理类型的一些不同方法:

  • 将数字 longlat 列转换为 geog 地理类型:

     UPDATE mytable SET geog = ST_SetSRID(ST_MakePoint(long, lat), 4326)::geography
    
  • 转换 geom 几何列 (< a href="http://epsg.io/4326" rel="noreferrer">SRID=4326) 使用简单的转换转换为 geog 地理类型:

     UPDATE mytable SET geog = geom::geography
    
  • 将投影的 geom 几何列转换为 geog 地理类型:

     UPDATE mytable SET geog = ST_Transform(geom, 4326)::geography
    

请注意,最后两个示例适用于任何几何类型。此外,从几何到地理的转换通常是隐式的,并且这些示例无需 ::geography 即可工作,但是显式转换通常是这些事情的良好实践。

Here are some different ways to make geography types:

  • Convert numeric long and lat columns to a geog geography type:

     UPDATE mytable SET geog = ST_SetSRID(ST_MakePoint(long, lat), 4326)::geography
    
  • Convert a geom geometry column (SRID=4326) to a geog geography type using a simple cast:

     UPDATE mytable SET geog = geom::geography
    
  • Transform a projected geom geometry column to a geog geography type:

     UPDATE mytable SET geog = ST_Transform(geom, 4326)::geography
    

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.

血之狂魔 2024-09-02 07:16:31

要执行 lat 和 lng 之间的交换,您可以使用:

update mytable set geography = ST_GeographyFromText('SRID=4326;POINT(' || st_x(geom) || ' ' ||  st_y(geom) || ')');

带或不带 srid。

To perform exchange between lat and lng you may use:

update mytable set geography = ST_GeographyFromText('SRID=4326;POINT(' || st_x(geom) || ' ' ||  st_y(geom) || ')');

with or without srid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文