forked from Ironholds/olctools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanipulate.cpp
More file actions
164 lines (127 loc) · 5.17 KB
/
manipulate.cpp
File metadata and controls
164 lines (127 loc) · 5.17 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
#include "manipulate.h"
std::string olc_manipulate::shorten_single(std::string olc, double latitude, double longitude){
if(!olc_check_full_single(olc)){
throw std::range_error("The Open Location Codes provided must be complete. Incomplete code: " + olc);
}
if(olc.find(padding) != std::string::npos){
throw std::range_error("The Open Location Codes provided cannot have padding characters. Padded code: " + olc);
}
for(unsigned int i = 0; i < olc.size(); i++){
olc[i] = toupper(olc[i]);
}
std::vector < double > decoded_code = olc_decode_single(olc);
if(decoded_code[6] < min_trim_length){
throw std::range_error("Open Location Codes must be >6 in length to be shortened. Offending code: " + olc);
}
longitude = clip_longitude(longitude);
latitude = clip_lat(latitude);
double range = std::max(std::abs((double) decoded_code[5] - longitude), std::abs((double) decoded_code[4] - latitude));
for(unsigned int i = resolution_levels.size() - 2; i >= 1; i--){
if(range < (resolution_levels[i] * 0.3)){
return olc.substr((i+1)*2);
}
}
return olc;
}
std::string olc_manipulate::recover_single(std::string olc, double latitude, double longitude){
if(!olc_check_short_single(olc)){
if(olc_check_full_single(olc)){
return olc;
}
throw std::range_error("codes provided to recover_olc must be valid short Open Location Codes. Offending code: " + olc);
}
double ref_longitude = clip_longitude(longitude);
double ref_latitude = clip_lat(latitude);
for(unsigned int i = 0; i < olc.size(); i++){
olc[i] = toupper(olc[i]);
}
int padding_length = (separator_position - olc.find(separator));
double resolution = pow(20.0, (2.0 - (padding_length / 2.0)));
double area_to_edge = resolution / 2.0;
std::vector < double > code_area = olc_decode_single(
olc_encode_single(ref_latitude, ref_longitude, max_pair_length).substr(0, padding_length) + olc
);
double degrees_difference = (code_area[4] - ref_latitude);
if(degrees_difference > area_to_edge){
code_area[4] -= resolution;
} else if(degrees_difference < -area_to_edge){
code_area[4] += resolution;
}
degrees_difference = (code_area[5] - ref_longitude);
if(degrees_difference > area_to_edge){
code_area[5] -= resolution;
} else if(degrees_difference < -area_to_edge){
code_area[5] += resolution;
}
return(olc_encode_single(code_area[4], code_area[5], code_area[6]));
}
CharacterVector olc_manipulate::shorten_vector(CharacterVector olc, NumericVector latitude, NumericVector longitude){
if(latitude.size() != longitude.size()){
throw std::range_error("There must be as many longitude values as latitude values");
}
unsigned int input_size = olc.size();
CharacterVector output(input_size);
if(latitude.size() == 1){
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
if(CharacterVector::is_na(olc[i]) || NumericVector::is_na(latitude[0]) || NumericVector::is_na(longitude[0])){
output[i] = NA_STRING;
} else {
output[i] = shorten_single(Rcpp::as<std::string>(olc[i]), latitude[0], longitude[0]);
}
}
} else if(latitude.size() == input_size){
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
if(CharacterVector::is_na(olc[i]) || NumericVector::is_na(latitude[i]) || NumericVector::is_na(longitude[i])){
output[i] = NA_STRING;
} else {
output[i] = shorten_single(Rcpp::as<std::string>(olc[i]), latitude[i], longitude[i]);
}
}
} else {
throw std::range_error("the latitude and longitude parameters must contain either one value, or one value for each OLC");
}
return output;
}
CharacterVector olc_manipulate::recover_vector(CharacterVector olc, NumericVector latitude,
NumericVector longitude){
if(latitude.size() != longitude.size()){
throw std::range_error("There must be as many longitude values as latitude values");
}
unsigned int input_size = olc.size();
CharacterVector output(input_size);
if(latitude.size() == 1){
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
if(CharacterVector::is_na(olc[i]) || NumericVector::is_na(latitude[0]) || NumericVector::is_na(longitude[0])){
output[i] = NA_STRING;
} else {
output[i] = recover_single(Rcpp::as<std::string>(olc[i]), latitude[0], longitude[0]);
}
}
} else if(latitude.size() == input_size){
for(unsigned int i = 0; i < input_size; i++){
if((i % 10000) == 0){
Rcpp::checkUserInterrupt();
}
if(CharacterVector::is_na(olc[i]) || NumericVector::is_na(latitude[i]) || NumericVector::is_na(longitude[i])){
output[i] = NA_STRING;
} else {
output[i] = recover_single(Rcpp::as<std::string>(olc[i]), latitude[i], longitude[i]);
}
}
} else {
throw std::range_error("the latitude and longitude parameters must contain either one value, or one value for each OLC");
}
return output;
}
olc_manipulate::olc_manipulate(){
min_trim_length = 6;
}