gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:20:20 26 4
gpt4 key购买 nike

我有几个关于 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
);

您可以在此链接 https://epsg.io/5186 上找到有关 EPSG 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. 当我使用以下查询计算几何表上的距离时,它给出的是度数,而不是米。考虑到地球的变形,有什么办法可以将这个度数转换为米?

    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。其中:内联期间的 SQL 函数“st_distancesphere”

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

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


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

  1. 我的空间数据列是 geometry(POINT, 5186),因此表格定义如下。注意不是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_stores 表中填充了 3M 数据,空间数据分布至少 10 公里。我执行了以下查询。
select *
from users
where st_dwithin(location, st_setsrid(st_point(126.970769, 37.555479), 5186), 0.001)

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

enter image description here

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

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

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

考虑到空间数据分布至少 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 数据类型不是您要找的吗?正如文档所说:

无论您使用哪种空间引用系统,测量返回的单位(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说:

“您选择的类型应取决于您正在构建的应用程序的预期工作区域。您的数据会跨越全局或大片大陆区域,还是仅局限于某个州、县或市? "

需要考虑的事项:

  • 用例覆盖小区域:坚持几何并使用更适合您所在区域的 SRS。
  • 用例涵盖大面积(国家/大陆):使用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 返回以米为单位的结果:

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

关于postgresql - PostGIS 使用 epsg 5186 将米转换为度数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63144390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com