MCPcopy Create free account
hub / github.com/Project-OSRM/osrm-backend / encodePolyline

Function encodePolyline

include/engine/polyline_compressor.hpp:22–49  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21template <unsigned POLYLINE_PRECISION = 100000>
22std::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

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 2

encodeFunction · 0.85
reserveMethod · 0.45

Tested by

no test coverage detected