-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordtransform.cc
More file actions
86 lines (66 loc) · 2.35 KB
/
coordtransform.cc
File metadata and controls
86 lines (66 loc) · 2.35 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
#include <nan.h>
#include <string>
#include <math.h>
namespace __transform__ {
using v8::Local;
using v8::Object;
using v8::String;
const double pi = 3.14159265358979324;
const double a = 6378245.0;
const double ee = 0.00669342162296594323;
const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
bool outOfChina(double lon,double lat){
return !(lon>72.004&&lon<137.8347&&lat>0.8292&&lat<55.8272);
}
NAN_METHOD(WGS84ToGoogle)
{
if(!info.Length()) return;
auto input = info[0]->ToObject();
auto output = Nan::New<Object>();
auto x_prop = Nan::New("x").ToLocalChecked();
auto y_prop = Nan::New("y").ToLocalChecked();
auto lon_prop = Nan::New("lon").ToLocalChecked();
auto lat_prop = Nan::New("lat").ToLocalChecked();
double x = Nan::Get(input, x_prop).ToLocalChecked()->NumberValue();
double y = Nan::Get(input, y_prop).ToLocalChecked()->NumberValue();
if(outOfChina(x,y)){
Nan::Set(output, lon_prop, Nan::New(x));
Nan::Set(output, lat_prop, Nan::New(y));
}else{
double dx=x;
double dy=y;
x-=105.0;
y-=35.0;
double xy=x*y;
double absX=sqrt(abs(x));
double xPi=x*pi;
double yPi=y*pi;
double d = 20.0 * sin(6.0 * xPi) + 20.0 * sin(2.0 * xPi);
double lat = d;
double lng = d;
lat += 20.0 * sin(yPi) + 40.0 * sin(yPi / 3.0);
lng += 20.0 * sin(xPi) + 40.0 * sin(xPi / 3.0);
lat += 160.0 * sin(yPi / 12.0) + 320 * sin(yPi / 30.0);
lng += 150.0 * sin(xPi / 12.0) + 300.0 * sin(xPi / 30.0);
lat *= 2.0 / 3.0;
lng *= 2.0 / 3.0;
lat += -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * xy + 0.2 * absX;
lng += 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * xy + 0.1 * absX;
double radLat = dy / 180.0 * pi;
double magic = sin(radLat);
magic = 1 - ee * magic * magic;
double sqrtMagic = sqrt(magic);
lat = (lat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
lng = (lng * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
Nan::Set(output, lon_prop, Nan::New(dx+lng));
Nan::Set(output, lat_prop, Nan::New(dy+lat));
}
info.GetReturnValue().Set(output);
}
NAN_MODULE_INIT(Init)
{
Nan::Export(target, "WGS84ToGoogle", WGS84ToGoogle);
}
NODE_MODULE(reverse, Init)
}