Board logo

標題: 座標轉換 [打印本頁]

作者: ghostkcleung    時間: 2023-3-21 11:01     標題: 座標轉換

本帖最後由 ghostkcleung 於 2023-3-21 11:05 編輯

平時我地用 Google Map 嘅座標叫做 WGS84,
我想將呢款坐標轉成 HK1980 grid system,
政府網站都有提供呢個 Tool,
但係我想 Offline 計,但係自己唔識測量,
啲數學符號我見都未見過,
請問有無師兄可以教吓我點計 ?

例如

22.291931, 114.162219
要轉成 817002.297, 834757.773

Thanks

[attach]2364673[/attach]
作者: chleung82    時間: 2023-3-21 13:47

python code
  1. from pyproj import Transformer
  2. Transformer.from_crs("EPSG:4326", "EPSG:2326").transform(22.291931,114.162219)
  3. Transformer.from_crs("EPSG:2326","EPSG:4326").transform(817002.297, 834757.773)
複製代碼

作者: ghostkcleung    時間: 2023-3-21 16:19

自己用 C 寫 native code 難唔難?

python code
chleung82 發表於 2023-3-21 13:47

作者: fred2088    時間: 2023-3-21 17:55

本帖最後由 fred2088 於 2023-3-21 10:40 編輯

回復 3 #ghostkcleung

Try GPT

via HKEPC Reader for Android

As an AI language model, I cannot write a program in any specific programming language. However, I can provide the general steps that can be used to convert WGS84 coordinates to HK1980 grid system:

1. Obtain the WGS84 coordinates of the location you want to convert.
2. Convert the WGS84 coordinates to Hong Kong 1980 (HK1980) using the Helmert transformation parameters.
3. Apply the scale factor to the converted coordinates.
4. Convert the coordinates to the Hong Kong Grid System by applying the projection formula.

Here is a sample code in Python that can be used to perform the conversion:

```
# Import necessary libraries
import math

# Define constants
a = 6378137.0
f = 1/298.257223563
e2 = 2*f - f**2
k0 = 0.9996
dx = -162.0
dy = -39.0
dz = 178.0
rx = -0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
作者: ghostkcleung    時間: 2023-3-23 19:43

真係,我用 ChatGPT 叫佢用 C 寫個 function 出嚟,
佢真係出到,依家試。
作者: ghostkcleung    時間: 2023-3-23 20:41

整體上係可行,但係有啲位以人嘅角度嚟講係寫得蠢咗啲,
例如佢 define 好多 constant ,其實可以 include ,
之類…不過已經好勁。
作者: hdyebx    時間: 2023-3-24 17:40

唔知你對座標有幾多認識,不過要用嚟個function,應該都識唔少
Wgs 84 要轉 hk1980 其實係個兩part 嘅過程
第一part 係將wgs84 轉hk80 datum
第二part 係將hk80 datum 轉 hk80 grid
地政處有份嘢叫 explanatory notes on geodetic datums in hong kong, 入面有數有formula做以上個兩part. 睇下幫唔幫到你
作者: ghostkcleung    時間: 2023-3-24 18:05

師兄唔好咁講,睇咗份嘢,
我就係正正見到啲數學符號已經 R 晒頭,
問咗 ChatGPT,好似有啲概念,
應該係將啲嘢代返入去咁上下,
小弟未接觸過微積分。

我想整一件玩具,一個 GPS Module ,
一塊 Arduino ( 可以係 Uno, mega, esp... )
一個顯示 ( 可以係 LCD TFT, 可以係 1602 ... )

GPS 直接透過 NMEA 讀取座標無難道,
將座標 print 出 display 亦無難度,
依家就係爭呢一步轉換。

唔知你對座標有幾多認識,不過要用嚟個function,應該都識唔少
Wgs 84 要轉 hk1980 其實係個兩part 嘅過程
...
hdyebx 發表於 2023-3-24 17:40

作者: hdyebx    時間: 2023-3-24 18:15

本帖最後由 hdyebx 於 2023-3-24 18:17 編輯

唔使微積分,應該都係代數同trig,等我研究下先。
ching 想整舊咁嘅嘢有咩用途?
作者: ghostkcleung    時間: 2023-3-24 19:17

無乜用途架其實,
成世人諗咗好多嘢,
攞一件係真正有用嘅都攞唔出,哈哈。

唔使微積分,應該都係代數同trig,等我研究下先。
ching 想整舊咁嘅嘢有咩用途? ...
hdyebx 發表於 2023-3-24 18:15

作者: ghostkcleung    時間: 2023-4-10 08:39

睇住條 Formula,
一知半解代返啲符號入去,
終於叫做轉到,應該係有啲 Roundup 上嘅誤差,
2,3 米範圍內,不過無所謂,反正 GPS 本身都自己都誤差。

測試結果:

lat: 22.48577143777777820
lon: 114.00029998111111240
n: 838478.464
e: 818098.341
  1. #include <math.h>
  2. #include <iostream>
  3. #include <iomanip>

  4. using namespace std;

  5. double dms_to_dd(int deg, int min, double sec)
  6. {
  7.     return deg + (double)min / 60 + sec / 3600;
  8. }

  9. template <typename T>
  10. double deg2rad(T deg)
  11. {
  12.     return (double)deg * M_PI / 180.0;
  13. }

  14. /*
  15. The Formula is referenced from the following PDF document:
  16. https://www.geodetic.gov.hk/common/data/pdf/explanatorynotes_c.pdf
  17. */

  18. const double e2 = 0.006722670022;
  19. const double A0 = 1 - e2 / 4 - 3 * e2 * e2 / 64;
  20. const double A2 = 3 * (e2 + e2 * e2 / 4) / 8;
  21. const double A4 = 15 * e2 * e2 / 256;
  22. const double a = 6378388;
  23. const double N0 = 819069.80;
  24. const double M0 = 2468395.728;
  25. const double Vs = 6381480.502;
  26. const double lon0 = deg2rad(dms_to_dd(114, 10, 42.80));
  27. const double E0 = 836694.05;
  28. const double iso_lat = 1.005792635;

  29. void wgs84_to_hk1980(double lat, double lon, double &x, double &y)
  30. {
  31.     lat += 5.5 / 3600;
  32.     lon -= 8.8 / 3600;

  33.     lat = deg2rad(lat);
  34.     lon = deg2rad(lon);

  35.     double M = a * (A0 * lat - A2 * sin(2 * lat) + A4 * sin(4 * lat));
  36.     double lonP = lon - lon0;

  37.     x = N0 + M - M0 + Vs * sin(lat) * lonP * lonP / 2 * cos(lat);
  38.     y = E0 + Vs * lonP * cos(lat) + Vs * pow(lonP, 3) / 6 * pow(cos(lat), 3) * (iso_lat - pow(tan(lat), 2));
  39. }

  40. int main()
  41. {

  42.     /*
  43.     This example is referenced from the following PDF document:
  44.     https://www.geodetic.gov.hk/common/data/parameter/7P_ITRF96_HK80_V1(Tra_Chi).pdf

  45.     Lat: 22d, 29m, 8.777176s
  46.     Lon: 114d, 00m, 1.079932s
  47.     N: 838477.970m
  48.     E: 818097.267m
  49.     */

  50.     double lat = dms_to_dd(22, 29, 8.777176);
  51.     double lon = dms_to_dd(114, 00, 1.079932);

  52.     double x, y;

  53.     wgs84_to_hk1980(lat, lon, x, y);

  54.     cout << fixed << setprecision(17);
  55.     cout << "lat: " << lat << endl;
  56.     cout << "lon: " << lon << endl;

  57.     cout << fixed << setprecision(3);
  58.     cout << "n: " << x << endl;
  59.     cout << "e: " << y << endl;

  60.     return 0;
  61. }
複製代碼





歡迎光臨 電腦領域 HKEPC Hardware (https://www.hkepc.com/forum/) Powered by Discuz! 7.2