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

时间:2022-06-09 16:05:07

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.

我正在尝试将一些纬度/经度对加载到PostGIS地理类型中,以便能够按位置进行查询。

In particular I have a table with float latitude and longitude columns and a geography(Point, 4326) column. I would like to do

特别是,我有一个包含浮动纬度和经度列的表格和一个地理(Point, 4326)列。我想做

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.

它不是。我不知道这个点是什么意思,但它只允许经度在-90到90之间,所以它显然不是经度。

So, what do I do?

那么,我该怎么办呢?

3 个解决方案

#1


36  

...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.

…叹息。我的愚蠢。显然,正确的顺序是经度,纬度。我被愚弄了,以为两个坐标都有相同的范围(-180到180),所以我认为有更微妙的事情正在发生。

#2


7  

Here are some ways to make geography types:

以下是一些制作地理类型的方法:

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

    将数值long和lat列转换为geog地理类型:

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

    使用简单的cast将geom几何列(SRID=4326)转换为geog地理类型:

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

    将投影的geom几何柱转换为geog地理类型:

    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.

注意,最后两个示例适用于任何几何类型。此外,从几何到地理的转换通常是隐性的,这些例子没有::地理学,然而显式的转换通常是这些东西的良好实践。

#3


5  

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.

有或没有srid。

#1


36  

...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.

…叹息。我的愚蠢。显然,正确的顺序是经度,纬度。我被愚弄了,以为两个坐标都有相同的范围(-180到180),所以我认为有更微妙的事情正在发生。

#2


7  

Here are some ways to make geography types:

以下是一些制作地理类型的方法:

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

    将数值long和lat列转换为geog地理类型:

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

    使用简单的cast将geom几何列(SRID=4326)转换为geog地理类型:

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

    将投影的geom几何柱转换为geog地理类型:

    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.

注意,最后两个示例适用于任何几何类型。此外,从几何到地理的转换通常是隐性的,这些例子没有::地理学,然而显式的转换通常是这些东西的良好实践。

#3


5  

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.

有或没有srid。