| 20 | |
| 21 | template <unsigned POLYLINE_PRECISION = 100000> |
| 22 | std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter end) |
| 23 | { |
| 24 | double coordinate_to_polyline = POLYLINE_PRECISION / COORDINATE_PRECISION; |
| 25 | auto size = std::distance(begin, end); |
| 26 | if (size == 0) |
| 27 | { |
| 28 | return {}; |
| 29 | } |
| 30 | |
| 31 | std::string output; |
| 32 | // just a guess that we will need ~4 bytes per coordinate to avoid reallocations |
| 33 | output.reserve(size * 4); |
| 34 | |
| 35 | int current_lat = 0; |
| 36 | int current_lon = 0; |
| 37 | for (auto it = begin; it != end; ++it) |
| 38 | { |
| 39 | const int lat_diff = |
| 40 | std::round(static_cast<int>(it->lat) * coordinate_to_polyline) - current_lat; |
| 41 | const int lon_diff = |
| 42 | std::round(static_cast<int>(it->lon) * coordinate_to_polyline) - current_lon; |
| 43 | detail::encode(lat_diff, output); |
| 44 | detail::encode(lon_diff, output); |
| 45 | current_lat += lat_diff; |
| 46 | current_lon += lon_diff; |
| 47 | } |
| 48 | return output; |
| 49 | } |
| 50 | |
| 51 | // Decodes geometry from polyline format |
| 52 | // See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm |
no test coverage detected