forked from SymbolixAU/googleway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyline_decode.cpp
54 lines (46 loc) · 1.4 KB
/
polyline_decode.cpp
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
#include <Rcpp.h>
using namespace Rcpp;
using namespace std;
// ------------------------------------------------------
// C++ implementation of the google api polyline decoder
// https://developers.google.com/maps/documentation/utilities/polylineutility
//
// This code is adapted from an Open Frameworks implementation
// and used in this package with the permission of the author
// https://github.com/paulobarcelos/ofxGooglePolyline
//
// ------------------------------------------------------
// [[Rcpp::export]]
DataFrame rcpp_decode_pl( std::string encoded ) {
int len = encoded.size();
int index = 0;
float lat = 0;
float lng = 0;
NumericVector pointsLat;
NumericVector pointsLon;
while (index < len){
char b;
int shift = 0;
int result = 0;
do {
b = encoded.at(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
float dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.at(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
float dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
pointsLat.push_back(lat * (float)1e-5);
pointsLon.push_back(lng * (float)1e-5);
}
return DataFrame::create(Named("lat") = pointsLat,
Named("lon") = pointsLon);
}