PostGIS 使用 epsg 5186 将米转换为度数

PostGIS convert meters to degree using epsg 5186

我有几个关于 PostGIS 中的几何和地理的问题。

我目前正在使用 PostGIS 和 Postgresql。

我的大部分空间数据来自韩国,基本上是经纬度。

为了测试,我创建了两个具有相同纬度和经度数据但数据类型不同的 table,一个用于 SRID 4326 的地理,另一个用于 SRID 5186 的几何。

create table geometry_stores
(
    id       serial primary key,
    location geometry(POINT, 5186) not null
);

create table geography_stores
(
    id       serial primary key,
    location geography(POINT, 4326) not null
);

您可以在此找到 EPSG 5186 的更多详细信息 link https://epsg.io/5186

这是我遇到的问题列表:

  1. PostGIS有这个方法

    ST_DWithin(geometry g1, geometry g2, double precision distance_of_srid);

    distance_of_srid 是 EPSG 的一个单位吗?有什么方法可以使用 EPSG 5186 将米(例如 1 公里)转换为 distance_of_srid

  2. 我了解地理计算将点之间的距离测量为球体上的真实路径,而几何计算将点之间的距离测量为笛卡尔平面上的真实路径。那么如果我给以下查询完全相同的距离,它们应该产生不同的结果还是相同的结果?因为我的理解是 SRID 5186 的几何图形已经投影了地球的变形,那么它们应该产生相同的结果?

    select *
    from geography_stores
    where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 4326), same_distance_meter)
    
    select *
    from geometry_stores
    where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), same_distance_degree)
    
  3. 当我使用以下查询计算几何体 table 上的距离时,它给出的是度数,而不是米。考虑到地球的变形,有什么办法可以将这个度数转换为米?

    select st_distance(location, st_setsrid(st_point(126.970769, 37.555479), 5186))
    from geometry_stores
    where id = 1;
    

    我已经尝试过这个查询但是得到了一些错误 Only lon/lat coordinate systems are supported in geography. Where: SQL function "st_distancesphere" during inlining

    select st_distancesphere(location, st_setsrid(st_point(126.970769, 37.555479), 5186))
    from geometry_stores
    where id = 1;
    

我已经阅读了 PostGIS 网站上的文档和 Whosebug 中的一些问题,但仍然有这三个问题。谢谢大家的帮助。


----------------------------更新------------ --------------------------

  1. 我的空间数据列是 geometry(POINT, 5186) 所以 table 定义如下。注意不是geometry(POINT, 4326) 在计算时不转换成geometry(POINT, 5186)。我应该将我的数据存储在 geometry(POINT, 4326) 中并在计算时转换它吗?
create table geometry_stores
(
    id       serial primary key,
    location geometry(POINT, 5186) not null
);
  1. 我执行了以下查询并得到如下结果:
select st_distance(st_setsrid(st_makepoint(126.808183, 37.463557), 4326)::geography,
                   st_setsrid(st_makepoint(126.970769, 37.555479), 4326)::geography);


st_distance
--------------
17627.3138509
select st_distance(st_setsrid(st_makepoint(126.808183, 37.463557), 5186)::geometry,
                   st_setsrid(st_makepoint(126.970769, 37.555479), 5186)::geometry)

st_distance
--------------
0.186772218169622

好像第二个问的是度,第一个问的是米。请问我是不是做错了什么?

  1. 对于st_within,我在geometry_storestable中填充了3M数据,空间数据分布至少10km。我执行了以下查询。
select *
from users
where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 0.001)

此查询为我提供了 158 行,几何查看器显示如下图所示。

让我们用距离 1 而不是 0.0001 执行相同的查询

select *
from users
where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 1)

这个查询给了我 32792923 行,这是 table.

中的所有数据

考虑到空间数据分布至少 10 公里,st_within 查询似乎以 EPSG5186 的单位(度)而不是米来计算两个几何之间的距离。然后,我想知道我是否可以将米转换为 EPSG5186 的单位(度),因为我想查询米,而不是我不知道 EPSG5186 的单位(度)有多远的度数。

Is distance_of_srid a unit of EPSG?

是的。使用 geometry 类型几何的距离是使用相应空间参考系统的测量单位计算的。

Is there any way I can convert meters (e.g. 1km) to distance_of_srid with EPSG 5186?

根据documentationEPSG:5186的单位已经是米,所以你不用换算。 Bur 还请记住,使用 geography 类型几何的距离也使用 metres 计算,例如

SELECT 
  ST_Distance(
   'SRID=4326;POINT(127.49 36.65)'::geometry,
   'SRID=4326;POINT(128.06 36.43)'::geometry) AS geometry_distance,
  ST_Distance(
   'SRID=4326;POINT(127.49 36.65)'::geography,
   'SRID=4326;POINT(128.06 36.43)'::geography) AS geography_distance
   ;
 geometry_distance | geography_distance 
-------------------+--------------------
 0.610982814815612 |     56578.57823391
(1 Zeile)

Then if I give exactly the same distance to the following queries, they are supposed to yield different results or same results? because my understanding is that geometry with SRID 5186 is already projected with distortion of earth, then they should yield the same results?

结果会有所不同。它们可能具有相同的测量单位,但它们并不投影在同一表面上。以下示例将坐标从 4326 转换为 5186 并计算距离:

SELECT 
  ST_Distance(
   'SRID=4326;POINT(127.49 36.65)'::geography,
   'SRID=4326;POINT(128.06 36.43)'::geography),
  ST_Distance(
   ST_Transform('SRID=4326;POINT(127.49 36.65)'::geometry,5186),
   ST_Transform('SRID=4326;POINT(128.06 36.43)'::geometry,5186));

  st_distance   |   st_distance    
----------------+------------------
 56578.57823391 | 56582.0899018353
(1 Zeile)

When I calculate distance on the geometry table with the following query, it gives me a degree, not meters. Is there any way I can convert this degree to meters with consideration of distortion of the earth?

数据类型 geography 不是您要找的吗?正如文档所说:

无论您使用哪种空间参考系统,单位 return 由测量(ST_Distance、ST_Length、ST_Perimeter、ST_Area) 和 ST_DWithin 的输入以米为单位。

只是为了好玩,以下查询使用 ST_DistanceSpheroid 计算明确定义 4326 椭球体的两点之间的距离,并将坐标从 geometry 转换为 geography,基本上做同样的事情:

SELECT 
  ST_DistanceSpheroid(  
   'POINT(127.49 36.65)',
   'POINT(128.06 36.43)',
   'SPHEROID["WGS 84",6378137,298.257223563]'),
  ST_Distance( 
   'SRID=4326;POINT(127.49 36.65)'::geography,
   'SRID=4326;POINT(128.06 36.43)'::geography);

 st_distancespheroid |  st_distance   
---------------------+----------------
    56578.5782339123 | 56578.57823391

关于何时使用 geometrygeography documentation 说:

"您选择的类型应取决于您正在构建的应用程序的预期工作区域。您的数据是跨越全球还是大片大陆区域,或者是某个州本地的,县还是直辖市?

需要考虑的事项:

  • 用例覆盖面积小:坚持geometry并使用更适合您所在地区的 SRS。
  • 用例覆盖大面积(countries/continents):使用geography - 虽然它可能会慢一点。
  • 你想使用的功能支持geography吗?大多数 PostGIS 函数支持!查看此 matrix 了解更多详情。如果你想使用的功能不支持 geography,你别无选择,只能使用 geometry ;-) 由于你的用例主要覆盖韩国,我认为使用 EPSG 5186 没有问题.

编辑:关于问题更新。

您不能简单地更改 SRID 几何图形以将其转换为另一个参考系统!你所做的是得到一个 WGS84 坐标对并简单地交换它的 SRID,这不是它的工作方式。为此,您必须始终 使用ST_Transform。看看应用后的坐标是什么样子的:

SELECT 
  ST_AsText(ST_Transform('SRID=4326;POINT(126.808183 37.463557)'::geometry,5186));

                st_astext                 
------------------------------------------
 POINT(183030.248454493 540476.713582621)
(1 Zeile)

表示POINT(183030.248454493 540476.713582621)POINT(126.808183 37.463557)是相同的坐标对,只是在不同的参考系中。以下查询将明确 geography5186 return 的结果均以米为单位:

SELECT 
  --Transforming from 4326 to 5186 and calculating the distance
  ST_Distance(
    ST_Transform('SRID=4326;POINT(126.808183 37.463557)'::geometry, 5186),
    ST_Transform('SRID=4326;POINT(126.970769 37.555479)'::geometry, 5186)),
  -- Distance using geography
  ST_Distance(
    'SRID=4326;POINT(126.808183 37.463557)'::geography,
    'SRID=4326;POINT(126.970769 37.555479)'::geography);

   st_distance    |  st_distance  
------------------+---------------
 17627.3383377316 | 17627.3138509