Decoding should not fail for incomplete polyline strings (#3404)
Possible fails in 1) correct lattitude, longitude is missing 2) no end-of-number (0 5th bit) marker in the last character
This commit is contained in:
committed by
Patrick Niklaus
parent
0e6863aec1
commit
4b1aae40af
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/assert.hpp>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
@@ -55,42 +56,50 @@ std::string encode(std::vector<int> &numbers)
|
||||
|
||||
std::vector<util::Coordinate> decodePolyline(const std::string &geometry_string)
|
||||
{
|
||||
std::vector<util::Coordinate> new_coordinates;
|
||||
int index = 0, len = geometry_string.size();
|
||||
int lat = 0, lng = 0;
|
||||
// https://developers.google.com/maps/documentation/utilities/polylinealgorithm
|
||||
auto decode_polyline_integer = [](auto &first, auto last) {
|
||||
// varint coding parameters
|
||||
const std::uint32_t bits_in_chunk = 5;
|
||||
const std::uint32_t continuation_bit = 1 << bits_in_chunk;
|
||||
const std::uint32_t chunk_mask = (1 << bits_in_chunk) - 1;
|
||||
|
||||
while (index < len)
|
||||
std::uint32_t result = 0;
|
||||
for (std::uint32_t value = continuation_bit, shift = 0;
|
||||
(value & continuation_bit) && (shift < CHAR_BIT * sizeof(result) - 1) && first != last;
|
||||
++first, shift += bits_in_chunk)
|
||||
{
|
||||
value = *first - 63; // convert ASCII coding [?..~] to an integer [0..63]
|
||||
result |= (value & chunk_mask) << shift;
|
||||
}
|
||||
|
||||
// change "zig-zag" sign coding to two's complement
|
||||
result = ((result & 1) == 1) ? ~(result >> 1) : (result >> 1);
|
||||
return static_cast<std::int32_t>(result);
|
||||
};
|
||||
|
||||
auto polyline_to_coordinate = [](auto value) {
|
||||
return static_cast<std::int32_t>(value * detail::POLYLINE_TO_COORDINATE);
|
||||
};
|
||||
|
||||
std::vector<util::Coordinate> coordinates;
|
||||
std::int32_t latitude = 0, longitude = 0;
|
||||
|
||||
std::string::const_iterator first = geometry_string.begin();
|
||||
const std::string::const_iterator last = geometry_string.end();
|
||||
while (first != last)
|
||||
{
|
||||
int b, shift = 0, result = 0;
|
||||
do
|
||||
{
|
||||
b = geometry_string.at(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20 && index < len);
|
||||
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
||||
lat += dlat;
|
||||
const auto dlat = decode_polyline_integer(first, last);
|
||||
const auto dlon = decode_polyline_integer(first, last);
|
||||
|
||||
shift = 0;
|
||||
result = 0;
|
||||
do
|
||||
{
|
||||
b = geometry_string.at(index++) - 63;
|
||||
result |= (b & 0x1f) << shift;
|
||||
shift += 5;
|
||||
} while (b >= 0x20 && index < len);
|
||||
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
||||
lng += dlng;
|
||||
latitude += dlat;
|
||||
longitude += dlon;
|
||||
|
||||
util::Coordinate p;
|
||||
p.lat =
|
||||
util::FixedLatitude{static_cast<std::int32_t>(lat * detail::POLYLINE_TO_COORDINATE)};
|
||||
p.lon =
|
||||
util::FixedLongitude{static_cast<std::int32_t>(lng * detail::POLYLINE_TO_COORDINATE)};
|
||||
new_coordinates.push_back(p);
|
||||
coordinates.emplace_back(
|
||||
util::Coordinate{util::FixedLongitude{polyline_to_coordinate(longitude)},
|
||||
util::FixedLatitude{polyline_to_coordinate(latitude)}});
|
||||
}
|
||||
|
||||
return new_coordinates;
|
||||
return coordinates;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user