![](/img/trans.png)
[英]How do I select Lat & Long coordinates back out of SQL Server Geography data type field
[英]Any way to recover data from GPS coordinates that were inserted as LAT/LONG as geography data type in PostGIS?
我將所有數據點作為“POINT(43.870683 -112.359383)”插入到地理字段中,即先經緯度。 現在,當我嘗試使用 ST_X 和 ST_Y 提取坐標時,X 坐標看起來是正確的,但是 Y 坐標讓我在大西洋中出路。 我假設 PostGIS 不考慮緯度,並試圖推斷經度。 我從該插入中得到的要點是:
st_x | st_y
-----------+------------
43.870683 | -67.640617
有沒有辦法恢復正確的坐標?
編輯:版本信息
postgis_version
---------------------------------------
3.0 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
PostgreSQL 13.3
gps
----------------------------------------------------
0101000020E61000007217618A72EF4540BF1072DEFFE850C0
gps | geography(Point,4326)
插入查詢(現在我知道這是錯誤的順序,我能以某種方式恢復數據嗎?)
insert into buildings (gps) values('POINT(43.870683 -112.359383)')
select ST_X(gps::geometry), ST_Y(gps::geometry) from buildings
st_x | st_y
-----------+------------
43.870683 | -67.640617
^^^^^^^^^ very wrong, not what I input
你以 -67.640617 結束,最初讓我感到困惑,這怎么可能? 好吧,我可以得出那個確切的值(我確信它不完全是它是如何得到的,並且可能不是一個通用公式)。 考慮緯度(Y 坐標)為-90 <= 緯度 <= 90 ,負值表示赤道以南,但在 -90 之后繼續沿同一運動線移動,您開始向北移動剩下的部分。 您擁有的值可以推導出為:注意:180 從赤道 -> 南極 -> 赤道 = 180 緯度經過。
with on_earth (gps) as
( values ( POINT(43.870683, -112.359383)) )
select ST_Y(gps::geometry) "Latitude"
, sign( ST_Y(gps::geometry) ) * (180 - abs( ST_Y(gps::geometry))) "Derived Latitude"
from on_earth;
現在這引出了兩個問題:
使用 Postgres 14.1 和 PostGIS 3.1.4 直接運行 SQL 在插入 -112.359383 后我沒有得到緯度 -67.640617。
除了#2之外,以上都沒有真正解決您的問題,而是試圖理解結果。 如果答案 #2 是在輸入期間發生的轉換,而 -67 是存儲的值,那么您只需要重新加載數據。 不是你想要的答案,而是你將要面對的。 但是,如果這是一個顯示 output 轉換,則恢復是一個簡單的更新語句。
Postgres(和 AFAIK 所有其他數據庫)您可以使用舊數據值,直到至少語句復雜化。 您只需選擇反向反轉的列即可反轉您的列; 即 select ST_Y for X 和 ST_X for Y. 所以試試:
update building
set gps = point( st_y(gps::geometry) -- move Y value to X
, st_x(gps::geometry) -- move X value to Y
);
它在我的環境中工作,beploy 是我的完整測試序列。 我通常會創建一個小提琴,但db<>fiddle和SQL Fiddle都不支持 PostGIS。
create table building(id integer generated always as identity
, gps point
);
insert into building(gps) values (POINT(43.870683, -112.359383));
select * from building;
/* Result
* id | gps
* 1 | (43.870683,-112.359383)
*/
update building
set gps = point( st_y(gps::geometry) -- move Y value to X
, st_x(gps::geometry) -- move X value to Y
);
select * from building;
/* Result
* id | gps
* 1 | (-112.359383,43.870683)
*/
如果數據值的轉換發生在輸入(插入)上,這將不會恢復實際值。 但看起來你沒有什么可失去的,可能會獲得巨大的收益。 值得一試!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.