Skip to content

Commit

Permalink
Make PolylineCompresser's encode and decode free standing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-j-h committed Jan 8, 2016
1 parent 545d06e commit e46237d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
21 changes: 7 additions & 14 deletions include/engine/polyline_compressor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define POLYLINECOMPRESSOR_H_

#include "osrm/coordinate.hpp"
#include "engine/segment_information.hpp"

#include <string>
#include <vector>
Expand All @@ -10,21 +11,13 @@ namespace osrm
{
namespace engine
{
// Encodes geometry into polyline format.
// See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
std::string polylineEncode(const std::vector<SegmentInformation> &geometry);

struct SegmentInformation;

class PolylineCompressor
{
private:
std::string encode_vector(std::vector<int> &numbers) const;

std::string encode_number(const int number_to_encode) const;

public:
std::string get_encoded_string(const std::vector<SegmentInformation> &polyline) const;

std::vector<util::FixedPointCoordinate> decode_string(const std::string &geometry_string) const;
};
// Decodes geometry from polyline format
// See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
std::vector<util::FixedPointCoordinate> polylineDecode(const std::string &polyline);
}
}

Expand Down
48 changes: 24 additions & 24 deletions src/engine/polyline_compressor.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
#include "engine/polyline_compressor.hpp"
#include "engine/segment_information.hpp"

#include "osrm/coordinate.hpp"
#include <cstddef>

namespace osrm
{
namespace engine
{
namespace /*detail*/ // anonymous to keep TU local
{

std::string encode(int number_to_encode)
{
std::string output;
while (number_to_encode >= 0x20)
{
const int next_value = (0x20 | (number_to_encode & 0x1f)) + 63;
output += static_cast<char>(next_value);
number_to_encode >>= 5;
}

number_to_encode += 63;
output += static_cast<char>(number_to_encode);
return output;
}

std::string PolylineCompressor::encode_vector(std::vector<int> &numbers) const
std::string encode(std::vector<int> &numbers)
{
std::string output;
const auto end = numbers.size();
Expand All @@ -22,28 +38,13 @@ std::string PolylineCompressor::encode_vector(std::vector<int> &numbers) const
}
for (const int number : numbers)
{
output += encode_number(number);
output += encode(number);
}
return output;
}
} // anonymous ns

std::string PolylineCompressor::encode_number(int number_to_encode) const
{
std::string output;
while (number_to_encode >= 0x20)
{
const int next_value = (0x20 | (number_to_encode & 0x1f)) + 63;
output += static_cast<char>(next_value);
number_to_encode >>= 5;
}

number_to_encode += 63;
output += static_cast<char>(number_to_encode);
return output;
}

std::string
PolylineCompressor::get_encoded_string(const std::vector<SegmentInformation> &polyline) const
std::string polylineEncode(const std::vector<SegmentInformation> &polyline)
{
if (polyline.empty())
{
Expand All @@ -64,11 +65,10 @@ PolylineCompressor::get_encoded_string(const std::vector<SegmentInformation> &po
previous_coordinate = segment.location;
}
}
return encode_vector(delta_numbers);
return encode(delta_numbers);
}

std::vector<util::FixedPointCoordinate>
PolylineCompressor::decode_string(const std::string &geometry_string) const
std::vector<util::FixedPointCoordinate> polylineDecode(const std::string &geometry_string)
{
std::vector<util::FixedPointCoordinate> new_coordinates;
int index = 0, len = geometry_string.size();
Expand Down
4 changes: 3 additions & 1 deletion src/engine/polyline_formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "osrm/coordinate.hpp"

#include <vector>

namespace osrm
{
namespace engine
Expand All @@ -13,7 +15,7 @@ namespace engine
util::json::String
PolylineFormatter::printEncodedString(const std::vector<SegmentInformation> &polyline) const
{
return util::json::String(PolylineCompressor().get_encoded_string(polyline));
return util::json::String(polylineEncode(polyline));
}

util::json::Array
Expand Down
6 changes: 4 additions & 2 deletions src/engine/route_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

#include "engine/polyline_compressor.hpp"

#include <string>
#include <utility>

namespace osrm
{
namespace engine
Expand Down Expand Up @@ -153,8 +156,7 @@ void RouteParameters::AddSource(const boost::fusion::vector<double, double> &rec

void RouteParameters::SetCoordinatesFromGeometry(const std::string &geometry_string)
{
PolylineCompressor pc;
coordinates = pc.decode_string(geometry_string);
coordinates = polylineDecode(geometry_string);
}
}
}
3 changes: 1 addition & 2 deletions unit_tests/engine/geometry_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ BOOST_AUTO_TEST_CASE(decode)
{
// Polyline string for the 5 coordinates
const std::string polyline = "_gjaR_gjaR_pR_ibE_pR_ibE_pR_ibE_pR_ibE";
PolylineCompressor pc;
std::vector<util::FixedPointCoordinate> coords = pc.decode_string(polyline);
const auto coords = polylineDecode(polyline);

// Test coordinates; these would be the coordinates we give the loc parameter,
// e.g. loc=10.00,10.0&loc=10.01,10.1...
Expand Down

0 comments on commit e46237d

Please sign in to comment.