Make PolylineCompresser's encode and decode free standing functions
This commit is contained in:
		
							parent
							
								
									c590596dbe
								
							
						
					
					
						commit
						c65dd16460
					
				| @ -2,6 +2,7 @@ | ||||
| #define POLYLINECOMPRESSOR_H_ | ||||
| 
 | ||||
| #include "osrm/coordinate.hpp" | ||||
| #include "engine/segment_information.hpp" | ||||
| 
 | ||||
| #include <string> | ||||
| #include <vector> | ||||
| @ -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); | ||||
| } | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -1,33 +1,15 @@ | ||||
| #include "engine/polyline_compressor.hpp" | ||||
| #include "engine/segment_information.hpp" | ||||
| 
 | ||||
| #include "osrm/coordinate.hpp" | ||||
| #include <cstddef> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| { | ||||
| 
 | ||||
| std::string PolylineCompressor::encode_vector(std::vector<int> &numbers) const | ||||
| namespace /*detail*/ // anonymous to keep TU local
 | ||||
| { | ||||
|     std::string output; | ||||
|     const auto end = numbers.size(); | ||||
|     for (std::size_t i = 0; i < end; ++i) | ||||
|     { | ||||
|         numbers[i] <<= 1; | ||||
|         if (numbers[i] < 0) | ||||
|         { | ||||
|             numbers[i] = ~(numbers[i]); | ||||
|         } | ||||
|     } | ||||
|     for (const int number : numbers) | ||||
|     { | ||||
|         output += encode_number(number); | ||||
|     } | ||||
|     return output; | ||||
| } | ||||
| 
 | ||||
| std::string PolylineCompressor::encode_number(int number_to_encode) const | ||||
| std::string encode(int number_to_encode) | ||||
| { | ||||
|     std::string output; | ||||
|     while (number_to_encode >= 0x20) | ||||
| @ -42,8 +24,27 @@ std::string PolylineCompressor::encode_number(int number_to_encode) const | ||||
|     return output; | ||||
| } | ||||
| 
 | ||||
| std::string | ||||
| PolylineCompressor::get_encoded_string(const std::vector<SegmentInformation> &polyline) const | ||||
| std::string encode(std::vector<int> &numbers) | ||||
| { | ||||
|     std::string output; | ||||
|     const auto end = numbers.size(); | ||||
|     for (std::size_t i = 0; i < end; ++i) | ||||
|     { | ||||
|         numbers[i] <<= 1; | ||||
|         if (numbers[i] < 0) | ||||
|         { | ||||
|             numbers[i] = ~(numbers[i]); | ||||
|         } | ||||
|     } | ||||
|     for (const int number : numbers) | ||||
|     { | ||||
|         output += encode(number); | ||||
|     } | ||||
|     return output; | ||||
| } | ||||
| } // anonymous ns
 | ||||
| 
 | ||||
| std::string polylineEncode(const std::vector<SegmentInformation> &polyline) | ||||
| { | ||||
|     if (polyline.empty()) | ||||
|     { | ||||
| @ -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(); | ||||
|  | ||||
| @ -5,6 +5,8 @@ | ||||
| 
 | ||||
| #include "osrm/coordinate.hpp" | ||||
| 
 | ||||
| #include <vector> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| @ -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 | ||||
|  | ||||
| @ -7,6 +7,9 @@ | ||||
| 
 | ||||
| #include "engine/polyline_compressor.hpp" | ||||
| 
 | ||||
| #include <string> | ||||
| #include <utility> | ||||
| 
 | ||||
| namespace osrm | ||||
| { | ||||
| namespace engine | ||||
| @ -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); | ||||
| } | ||||
| } | ||||
| } | ||||
|  | ||||
| @ -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...
 | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user