forked from Ironholds/olctools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoders.cpp
More file actions
300 lines (245 loc) · 8.94 KB
/
coders.cpp
File metadata and controls
300 lines (245 loc) · 8.94 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "coders.h"
double olc_coders::clip_lat(double lat){
if(lat < -90){
return -90;
}
if(lat > 90){
return 90;
}
return lat;
}
double olc_coders::clip_longitude(double longitude){
while(longitude < -180){
longitude = longitude + 360;
}
while(longitude >= 180){
longitude = longitude - 360;
}
return longitude;
}
double olc_coders::lat_precision(unsigned int length){
if(length < max_pair_length){
return pow(20.0, floor(-(length/2.0) + 2.0));
}
return pow(20.0, -3.0)/pow( (double) grid_rows, (double) (length - max_pair_length));
}
std::string olc_coders::olc_encode_single(double lat, double longitude, unsigned int output_length){
if(output_length < 2 || (output_length < separator_position && output_length % 2 == 1)){
throw std::range_error("The length value you have provided is not valid; see the documentation");
}
std::string output;
lat = clip_lat(lat);
longitude = clip_longitude(longitude);
if(lat == 90){
lat = lat_precision(output_length);
}
unsigned int to_encode_length = fmin(output_length, max_pair_length);
double adjusted_lat = lat + max_latitude;
double adjusted_long = longitude + max_longitude;
int digit_value = 0;
unsigned int digit_count = 0;
double place_value;
while(digit_count < to_encode_length){
//Lat first
place_value = resolution_levels[floor(digit_count/2.0)];
digit_value = floor(adjusted_lat/place_value);
adjusted_lat -= digit_value * place_value;
output += character_set[digit_value];
digit_count++;
//Long
digit_value = floor(adjusted_long/place_value);
adjusted_long -= digit_value * place_value;
output += character_set[digit_value];
digit_count++;
if(digit_count == separator_position && digit_count < to_encode_length) {
output += "+";
}
}
if((unsigned int) output.size() < separator_position){
while((unsigned int) output.size() < separator_position){
output += padding;
}
}
if((unsigned int) output.size() == separator_position){
output += separator;
}
if(output_length > max_pair_length){
int additional_length = output_length - max_pair_length;
double lat_values = grid_degrees;
double long_values = grid_degrees;
double adjusted_latitude = fmod((lat + max_latitude), lat_values);
double adjusted_longitude = fmod((longitude + max_longitude), long_values);
for(signed int i = 0; i < additional_length; i++){
int row = floor(adjusted_latitude/(lat_values/grid_rows));
int col = floor(adjusted_longitude/(long_values/grid_cols));
lat_values = (lat_values/grid_rows);
long_values = (long_values/grid_cols);
adjusted_latitude -= row * lat_values;
adjusted_longitude -= col * long_values;
output += character_set[row * grid_cols + col];
}
}
return output;
}
std::vector < double > olc_coders::olc_decode_pair(std::string code, int offset){
unsigned int inval = 0;
unsigned int input_length = code.size();
double output_value = 0;
std::vector < double > output(2);
while((inval * 2 + offset) < input_length){
output_value += (character_set.find(code[inval * 2 + offset]) * resolution_levels[inval]);
inval++;
}
output[0] = output_value;
output[1] = (output_value + resolution_levels[(inval - 1)]);
return output;
}
std::vector < double > olc_coders::olc_decode_grid(std::string code){
double latitude_low = 0.0;
double longitude_low = 0.0;
double lat_place_value = grid_degrees;
double long_place_value = grid_degrees;
int row = 0;
int col = 0;
int code_index = 0.0;
std::vector < double > output(4);
for(unsigned int i = 0; i < code.size(); i++){
code_index = character_set.find(code[i]);
row = floor((double) code_index/ (double) grid_cols);
col = code_index % grid_cols;
lat_place_value = (lat_place_value / grid_rows);
long_place_value = (long_place_value / grid_cols);
latitude_low += (row * lat_place_value);
longitude_low += (col * long_place_value);
}
output.push_back(latitude_low);
output.push_back(latitude_low + lat_place_value);
output.push_back(longitude_low);
output.push_back(longitude_low + long_place_value);
return output;
}
std::vector < double > olc_coders::olc_decode_single(std::string olc){
if(!olc_check_full_single(olc)){
throw std::range_error("The Open Location Codes provided must be complete. Incomplete code: " + olc);
}
//Remove separator and padding character, upper-case
std::string validated_olc;
for(unsigned int i = 0; i < olc.size(); i++){
if(olc[i] != padding[0] && olc[i] != separator[0]){
validated_olc.push_back(toupper(olc[i]));
}
}
//Decode the pairs
std::vector < double > output;
std::vector < double > holding = olc_decode_pair(validated_olc.substr(0, max_pair_length), 0);
holding[0] -= max_latitude;
holding[1] -= max_latitude;
output.insert(output.end(), holding.begin(), holding.end());
holding = olc_decode_pair(validated_olc.substr(0, max_pair_length), 1);
holding[0] -= max_longitude;
holding[1] -= max_longitude;
output.insert(output.end(), holding.begin(), holding.end());
if(validated_olc.size() > max_pair_length){
std::vector < double > grid_decode_results = olc_decode_grid(validated_olc.substr(max_pair_length));
for(unsigned int i = 0; i < 4; i++){
output[i] += grid_decode_results[i];
}
}
output.push_back(std::min((output[0] + (output[1] - output[0])/2), (double) max_latitude));
output.push_back(std::min((output[2] + (output[3] - output[2])/2), (double) max_longitude));
output.push_back(validated_olc.size());
return output;
}
CharacterVector olc_coders::olc_encode_vector(NumericVector latitude, NumericVector longitude,
IntegerVector code_length){
if(latitude.size() != longitude.size()){
throw std::range_error("There must be as many longitude values as latitude values");
}
unsigned int input_size = latitude.size();
CharacterVector output(input_size);
if(code_length.size() == 1){
if(IntegerVector::is_na(code_length[0])){
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
output[i] = NA_STRING;
}
} else {
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
if(NumericVector::is_na(latitude[i]) || NumericVector::is_na(longitude[i])){
output[i] = NA_STRING;
} else {
output[i] = olc_encode_single(latitude[i], longitude[i], code_length[0]);
}
}
}
} else if(code_length.size() == input_size){
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
if(NumericVector::is_na(latitude[i]) || NumericVector::is_na(longitude[i]) || IntegerVector::is_na(code_length[i])){
output[i] = NA_STRING;
} else {
output[i] = olc_encode_single(latitude[i], longitude[i], code_length[i]);
}
}
} else {
throw std::range_error("the vector code_length must contain either one value, or one value for each input latitude and longitude");
}
return output;
}
DataFrame olc_coders::olc_decode_vector(CharacterVector olcs){
unsigned int input_size = olcs.size();
NumericVector low_lats(input_size);
NumericVector low_longs(input_size);
NumericVector high_lats(input_size);
NumericVector high_longs(input_size);
NumericVector center_lats(input_size);
NumericVector center_longs(input_size);
IntegerVector code_lengths(input_size);
NumericVector holding(7);
for(unsigned int i = 0; i < input_size; i++){
if(olcs[i] == NA_STRING){
low_lats[i] = NA_REAL;
high_lats[i] = NA_REAL;
low_longs[i] = NA_REAL;
high_longs[i] = NA_REAL;
center_lats[i] = NA_REAL;
center_longs[i] = NA_REAL;
code_lengths[i] = NA_INTEGER;
} else {
holding = olc_decode_single(Rcpp::as<std::string>(olcs[i]));
low_lats[i] = holding[0];
high_lats[i] = holding[1];
low_longs[i] = holding[2];
high_longs[i] = holding[3];
center_lats[i] = holding[4];
center_longs[i] = holding[5];
code_lengths[i] = holding[6];
}
}
return DataFrame::create(_["latitude_low"] = low_lats,
_["longitude_low"] = low_longs,
_["latitude_center"] = center_lats,
_["longitude_center"] = center_longs,
_["latitude_high"] = high_lats,
_["longitude_high"] = high_longs,
_["code_lengths"] = code_lengths,
_["stringsAsFactors"] = false);
}
olc_coders::olc_coders(){
grid_rows = 5;
grid_cols = 4;
grid_degrees = 0.000125;
max_pair_length = 10;
resolution_levels.push_back(20.0);
resolution_levels.push_back(1.0);
resolution_levels.push_back(.05);
resolution_levels.push_back(.0025);
resolution_levels.push_back(.000125);
}