Repository navigation
Expand file tree
/
Copy pathLatLngToXY.java
More file actions
109 lines (92 loc) · 3.57 KB
/
Copy pathLatLngToXY.java
File metadata and controls
109 lines (92 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.example.weatherwidget.view.clock;
import android.util.Log;
/***
* It's open sourse
* Reference by https://gist.github.com/fronteer-kr/14d7f779d52a21ac2f16
*기능은 GPS 에서 얻을 세계 좌표가 한국 지도 좌표로 변견한다
*/
public class LatLngToXY {
public static int TO_GRID = 0;
public static int TO_GPS = 1;
public static LatXLngY tmp;
public void method(double x , double y) {
tmp = convertGRID_GPS(TO_GRID, x, y);
Log.i(">>", "x = " + tmp.x + ", y = " + tmp.y);
}
public LatXLngY convertGRID_GPS(int mode, double lat_X, double lng_Y )
{
double RE = 6371.00877; // 지구 반경(km)
double GRID = 5.0; // 격자 간격(km)
double SLAT1 = 30.0; // 투영 위도1(degree)
double SLAT2 = 60.0; // 투영 위도2(degree)
double OLON = 126.0; // 기준점 경도(degree)
double OLAT = 38.0; // 기준점 위도(degree)
double XO = 43; // 기준점 X좌표(GRID)
double YO = 136; // 기1준점 Y좌표(GRID)
//
// LCC DFS 좌표변환 ( code : "TO_GRID"(위경도->좌표, lat_X:위도, lng_Y:경도), "TO_GPS"(좌표->위경도, lat_X:x, lng_Y:y) )
//
double DEGRAD = Math.PI / 180.0;
double RADDEG = 180.0 / Math.PI;
double re = RE / GRID;
double slat1 = SLAT1 * DEGRAD;
double slat2 = SLAT2 * DEGRAD;
double olon = OLON * DEGRAD;
double olat = OLAT * DEGRAD;
double sn = Math.tan(Math.PI * 0.25 + slat2 * 0.5) / Math.tan(Math.PI * 0.25 + slat1 * 0.5);
sn = Math.log(Math.cos(slat1) / Math.cos(slat2)) / Math.log(sn);
double sf = Math.tan(Math.PI * 0.25 + slat1 * 0.5);
sf = Math.pow(sf, sn) * Math.cos(slat1) / sn;
double ro = Math.tan(Math.PI * 0.25 + olat * 0.5);
ro = re * sf / Math.pow(ro, sn);
LatXLngY rs = new LatXLngY();
if (mode == TO_GRID) {
rs.lat = lat_X;
rs.lng = lng_Y;
double ra = Math.tan(Math.PI * 0.25 + (lat_X) * DEGRAD * 0.5);
ra = re * sf / Math.pow(ra, sn);
double theta = lng_Y * DEGRAD - olon;
if (theta > Math.PI) theta -= 2.0 * Math.PI;
if (theta < -Math.PI) theta += 2.0 * Math.PI;
theta *= sn;
rs.x = Math.floor(ra * Math.sin(theta) + XO + 0.5);
rs.y = Math.floor(ro - ra * Math.cos(theta) + YO + 0.5);
}
else {
rs.x = lat_X;
rs.y = lng_Y;
double xn = lat_X - XO;
double yn = ro - lng_Y + YO;
double ra = Math.sqrt(xn * xn + yn * yn);
if (sn < 0.0) {
ra = -ra;
}
double alat = Math.pow((re * sf / ra), (1.0 / sn));
alat = 2.0 * Math.atan(alat) - Math.PI * 0.5;
double theta = 0.0;
if (Math.abs(xn) <= 0.0) {
theta = 0.0;
}
else {
if (Math.abs(yn) <= 0.0) {
theta = Math.PI * 0.5;
if (xn < 0.0) {
theta = -theta;
}
}
else theta = Math.atan2(xn, yn);
}
double alon = theta / sn + olon;
rs.lat = alat * RADDEG;
rs.lng = alon * RADDEG;
}
return rs;
}
class LatXLngY
{
public double lat;
public double lng;
public double x;
public double y;
}
}