PostGIS 使用 epsg 5186 将米转换为度

kis*_*Tae 2 postgresql postgis

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

我目前正在使用 PostGIS 和 Postgresql。

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

为了进行测试,我创建了两个具有相同纬度和经度数据但数据类型不同的表,一个用于 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
);
Run Code Online (Sandbox Code Playgroud)

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

这是我收到的问题清单:

  1. PostGIS有这个方法

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

    distance_of_sridEPSG的单位吗?有什么方法可以将米(例如 1 公里)转换为distance_of_sridEPSG 5186 吗?

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

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

    select st_distance(location, st_setsrid(st_point(126.970769, 37.555479), 5186))
    from geometry_stores
    where id = 1;
    
    Run Code Online (Sandbox Code Playgroud)

    我尝试过这个查询,但出现了一些错误 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;
    
    Run Code Online (Sandbox Code Playgroud)

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


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

  1. 我的空间数据的列是 Geometry(POINT, 5186),因此表定义如下。注意,计算时不将其转换为geometry(POINT, 5186)并不是geometry(POINT, 4326)。我应该将数据存储在几何(POINT,4326)中并在计算时将其转换吗?
create table geometry_stores
(
    id       serial primary key,
    location geometry(POINT, 5186) not null
);
Run Code Online (Sandbox Code Playgroud)
  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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

似乎第二个查询给了我学位,而第一个查询给了我米。请问我的查询中做错了什么吗?

  1. 对于st_within,我在geometry_stores表中填充了3M数据,空间数据分布在至少10公里范围内。我执行了以下查询。
select *
from users
where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 0.001)
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

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

select *
from users
where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 1)
Run Code Online (Sandbox Code Playgroud)

这个查询给了我 32792923 行,这是表中的所有数据。

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

Jim*_*nes 6

distance_of_srid是EPSG的单位吗?

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

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

根据documentation, 的单位EPSG: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)
Run Code Online (Sandbox Code Playgroud)

那么,如果我给以下查询提供完全相同的距离,它们应该产生不同的结果还是相同的结果?因为我的理解是,SRID 5186 的几何图形已经在地球变形的情况下进行投影,那么它们应该产生相同的结果?

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

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)
Run Code Online (Sandbox Code Playgroud)

当我使用以下查询计算几何表上的距离时,它给出的是度,而不是米。考虑到地球的扭曲,有什么办法可以将这个度数转换为米吗?

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

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

4326只是为了好玩,以下查询使用 计算显式定义椭球体的两点之间的距离ST_DistanceSpheroid,并将坐标从 转换为geometrygeography这基本上具有相同的作用:

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
Run Code Online (Sandbox Code Playgroud)

关于何时使用geometrygeographydocumentation

“您选择的类型应取决于您正在构建的应用程序的预期工作区域。您的数据会跨越全球或大片大陆地区,还是位于州、县或市本地?”

需要考虑的事项:

  • 用例覆盖小区域:坚持geometry并使用更适合您所在区域的 SRS。
  • 用例覆盖大面积(国家/大陆):使用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)
Run Code Online (Sandbox Code Playgroud)

这意味着POINT(183030.248454493 540476.713582621)POINT(126.808183 37.463557)是相同的坐标对,但在不同的参考系中。以下查询将清楚地表明geography和都5186返回以米为单位的结果:

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
Run Code Online (Sandbox Code Playgroud)