This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addon.cc
132 lines (101 loc) · 3.59 KB
/
addon.cc
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <assert.h>
#include "napi.h"
#include "src/dglib.h"
struct DgParam
{
long double pole_lon_deg;
long double pole_lat_deg;
long double azimuth_deg;
unsigned int aperture;
int res;
std::string topology = "cheese"; //"HEXAGON", "DIAMOND", "TRIANGLE"
std::string projection; //ISEA/FULLER
};
DgParam dp;
/*
* Construct a DGGS
*/
void dgconstruct(string projection = "ISEA", int aperture = 3,
string topology = "HEXAGON",
double azimuth_deg = 0,
double res = 10,
double pole_lon_deg = 11.25, double pole_lat_deg = 58.28252559) {
std::cout << "** executing DGGRID version " << DGGRID_VERSION << " **\n";
dp.aperture = aperture;
dp.azimuth_deg = azimuth_deg;
dp.pole_lat_deg = pole_lat_deg;
dp.pole_lon_deg = pole_lon_deg;
dp.projection = projection;
dp.topology = topology;
dp.res = res;
}
Napi::Value DGGSConstruct(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 7) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Null();
}
// apearture. must be 3,4 or 7
if (!info[1].IsNumber() || !info[3].IsNumber() || !info[4].IsNumber() || !info[5].IsNumber() || !info[6].IsNumber()) {
throw Napi::Error::New(env, "Number expected for arg apearture, res, azimuth,pole lat and lon");
}
if(!info[0].IsString() || !info[2].IsString()){
throw Napi::Error::New(env, "String expected");
}
dgconstruct(info[0].As<Napi::String>(),
info[1].As<Napi::Number>().Int32Value(),
info[2].As<Napi::String>(),
info[3].As<Napi::Number>().DoubleValue(),
info[4].As<Napi::Number>().Int32Value(),
info[5].As<Napi::Number>().DoubleValue(),
info[6].As<Napi::Number>().DoubleValue()
);
Napi::Boolean state = Napi::Boolean::New(env, true);
return Napi::Boolean::New(env, true);
}
/*
* Helper function to check if an item is in a vector or not
*/
bool in_array(const std::string &value, const std::vector<string> &array) {
return std::find(array.begin(), array.end(), value) != array.end();
}
/*
* checks if DgParams are valid or not
* FIXME: only topology is checked here
*/
bool isvalid(napi_env env) {
std::vector<std::string> topologies{"HEXAGON", "DIAMOND", "TRIANGLE"};
if (in_array(dp.topology, topologies)) {
return true;
} else {
std::cout << "dgconstruct(): Topology must be set! call dgconstruct() first and configure DGGS parametes.";
}
return false;
}
void GeoToSeqnum(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (!info[0].IsNumber() || !info[1].IsNumber()) {
throw Napi::Error::New(env, "Number expected");
}
Napi::Function fn = info[2].As<Napi::Function>();
double lon = info[0].As<Napi::Number>().DoubleValue();
double lat = info[1].As<Napi::Number>().DoubleValue();
// if (!isvalid(env)) {
// dgconstruct();
// }
dglib::Transformer dgt( 58.28252559,11.25,0,3,10,"HEXAGON","ISEA");
std::cout << lon;
std::cout << lat;
auto in = dgt.inGEO(lon, lat);
uint64_t out_seqnum;
dgt.outSEQNUM(in, out_seqnum);
// std::cout << out_seqnum;
fn.Call(env.Global(),{ Napi::Number::New(env, out_seqnum) });
}
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports.Set(Napi::String::New(env, "GeoToSeqnum"), Napi::Function::New(env, GeoToSeqnum));
exports.Set(Napi::String::New(env, "DGGSConstruct"), Napi::Function::New(env, DGGSConstruct));
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init)