refactor legacy code
This commit is contained in:
parent
30b2c1ad61
commit
c56a57c0ba
@ -32,10 +32,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
#include "../Util/StringUtil.h"
|
#include "../Util/StringUtil.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class PolylineCompressor {
|
class PolylineCompressor {
|
||||||
private:
|
private:
|
||||||
inline void encodeVectorSignedNumber(std::vector<int> & numbers, std::string & output) const {
|
inline void encodeVectorSignedNumber(
|
||||||
|
std::vector<int> & numbers,
|
||||||
|
std::string & output
|
||||||
|
) const {
|
||||||
for(unsigned i = 0; i < numbers.size(); ++i) {
|
for(unsigned i = 0; i < numbers.size(); ++i) {
|
||||||
numbers[i] <<= 1;
|
numbers[i] <<= 1;
|
||||||
if (numbers[i] < 0) {
|
if (numbers[i] < 0) {
|
||||||
@ -47,19 +51,21 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void encodeNumber(int numberToEncode, std::string & output) const {
|
inline void encodeNumber(int number_to_encode, std::string & output) const {
|
||||||
while (numberToEncode >= 0x20) {
|
while (number_to_encode >= 0x20) {
|
||||||
int nextValue = (0x20 | (numberToEncode & 0x1f)) + 63;
|
int nextValue = (0x20 | (number_to_encode & 0x1f)) + 63;
|
||||||
output += (static_cast<char> (nextValue));
|
output += static_cast<char>(nextValue);
|
||||||
if(92 == nextValue)
|
if(92 == nextValue) {
|
||||||
output += (static_cast<char> (nextValue));
|
output += static_cast<char>(nextValue);
|
||||||
numberToEncode >>= 5;
|
}
|
||||||
|
number_to_encode >>= 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
numberToEncode += 63;
|
number_to_encode += 63;
|
||||||
output += (static_cast<char> (numberToEncode));
|
output += static_cast<char>(number_to_encode);
|
||||||
if(92 == numberToEncode)
|
if(92 == number_to_encode) {
|
||||||
output += (static_cast<char> (numberToEncode));
|
output += static_cast<char>(number_to_encode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@ -74,8 +80,9 @@ public:
|
|||||||
deltaNumbers.push_back( lastCoordinate.lat );
|
deltaNumbers.push_back( lastCoordinate.lat );
|
||||||
deltaNumbers.push_back( lastCoordinate.lon );
|
deltaNumbers.push_back( lastCoordinate.lon );
|
||||||
for(unsigned i = 1; i < polyline.size(); ++i) {
|
for(unsigned i = 1; i < polyline.size(); ++i) {
|
||||||
if(!polyline[i].necessary)
|
if(!polyline[i].necessary) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
deltaNumbers.push_back(polyline[i].location.lat - lastCoordinate.lat);
|
deltaNumbers.push_back(polyline[i].location.lat - lastCoordinate.lat);
|
||||||
deltaNumbers.push_back(polyline[i].location.lon - lastCoordinate.lon);
|
deltaNumbers.push_back(polyline[i].location.lon - lastCoordinate.lon);
|
||||||
lastCoordinate = polyline[i].location;
|
lastCoordinate = polyline[i].location;
|
||||||
@ -86,7 +93,10 @@ public:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void printEncodedString(const std::vector<FixedPointCoordinate>& polyline, std::string &output) const {
|
inline void printEncodedString(
|
||||||
|
const std::vector<FixedPointCoordinate>& polyline,
|
||||||
|
std::string &output
|
||||||
|
) const {
|
||||||
std::vector<int> deltaNumbers(2*polyline.size());
|
std::vector<int> deltaNumbers(2*polyline.size());
|
||||||
output += "\"";
|
output += "\"";
|
||||||
if(!polyline.empty()) {
|
if(!polyline.empty()) {
|
||||||
@ -101,7 +111,10 @@ public:
|
|||||||
output += "\"";
|
output += "\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void printUnencodedString(std::vector<FixedPointCoordinate> & polyline, std::string & output) const {
|
inline void printUnencodedString(
|
||||||
|
const std::vector<FixedPointCoordinate> & polyline,
|
||||||
|
std::string & output
|
||||||
|
) const {
|
||||||
output += "[";
|
output += "[";
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
for(unsigned i = 0; i < polyline.size(); i++) {
|
for(unsigned i = 0; i < polyline.size(); i++) {
|
||||||
@ -119,12 +132,16 @@ public:
|
|||||||
output += "]";
|
output += "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void printUnencodedString(std::vector<SegmentInformation> & polyline, std::string & output) const {
|
inline void printUnencodedString(
|
||||||
|
const std::vector<SegmentInformation> & polyline,
|
||||||
|
std::string & output
|
||||||
|
) const {
|
||||||
output += "[";
|
output += "[";
|
||||||
std::string tmp;
|
std::string tmp;
|
||||||
for(unsigned i = 0; i < polyline.size(); i++) {
|
for(unsigned i = 0; i < polyline.size(); i++) {
|
||||||
if(!polyline[i].necessary)
|
if(!polyline[i].necessary) {
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
convertInternalLatLonToString(polyline[i].location.lat, tmp);
|
convertInternalLatLonToString(polyline[i].location.lat, tmp);
|
||||||
output += "[";
|
output += "[";
|
||||||
output += tmp;
|
output += tmp;
|
||||||
|
@ -45,31 +45,47 @@ double DescriptionFactory::GetBearing(
|
|||||||
) const {
|
) const {
|
||||||
double deltaLong = DegreeToRadian(B.lon/COORDINATE_PRECISION - A.lon/COORDINATE_PRECISION);
|
double deltaLong = DegreeToRadian(B.lon/COORDINATE_PRECISION - A.lon/COORDINATE_PRECISION);
|
||||||
|
|
||||||
double lat1 = DegreeToRadian(A.lat/COORDINATE_PRECISION);
|
const double lat1 = DegreeToRadian(A.lat/COORDINATE_PRECISION);
|
||||||
double lat2 = DegreeToRadian(B.lat/COORDINATE_PRECISION);
|
const double lat2 = DegreeToRadian(B.lat/COORDINATE_PRECISION);
|
||||||
|
|
||||||
double y = sin(deltaLong) * cos(lat2);
|
const double y = sin(deltaLong) * cos(lat2);
|
||||||
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLong);
|
const double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltaLong);
|
||||||
double result = RadianToDegree(atan2(y, x));
|
double result = RadianToDegree(atan2(y, x));
|
||||||
while(result <= 0.)
|
while(result < 0.) {
|
||||||
result += 360.;
|
result += 360.;
|
||||||
while(result >= 360.)
|
}
|
||||||
|
while(result >= 360.) {
|
||||||
result -= 360.;
|
result -= 360.;
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptionFactory::SetStartSegment(const PhantomNode & _startPhantom) {
|
void DescriptionFactory::SetStartSegment(const PhantomNode & sph) {
|
||||||
startPhantom = _startPhantom;
|
start_phantom = sph;
|
||||||
AppendSegment(_startPhantom.location, _PathData(0, _startPhantom.nodeBasedEdgeNameID, 10, _startPhantom.weight1));
|
AppendSegment(
|
||||||
|
sph.location,
|
||||||
|
_PathData(0, sph.nodeBasedEdgeNameID, 10, sph.weight1)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptionFactory::SetEndSegment(const PhantomNode & _targetPhantom) {
|
void DescriptionFactory::SetEndSegment(const PhantomNode & tph) {
|
||||||
targetPhantom = _targetPhantom;
|
target_phantom = tph;
|
||||||
pathDescription.push_back(SegmentInformation(_targetPhantom.location, _targetPhantom.nodeBasedEdgeNameID, 0, _targetPhantom.weight1, 0, true) );
|
pathDescription.push_back(
|
||||||
|
SegmentInformation(
|
||||||
|
tph.location,
|
||||||
|
tph.nodeBasedEdgeNameID,
|
||||||
|
0,
|
||||||
|
tph.weight1,
|
||||||
|
0,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptionFactory::AppendSegment(const FixedPointCoordinate & coordinate, const _PathData & data ) {
|
void DescriptionFactory::AppendSegment(
|
||||||
|
const FixedPointCoordinate & coordinate,
|
||||||
|
const _PathData & data
|
||||||
|
) {
|
||||||
if(1 == pathDescription.size() && pathDescription.back().location == coordinate) {
|
if(1 == pathDescription.size() && pathDescription.back().location == coordinate) {
|
||||||
pathDescription.back().nameID = data.nameID;
|
pathDescription.back().nameID = data.nameID;
|
||||||
} else {
|
} else {
|
||||||
@ -77,18 +93,22 @@ void DescriptionFactory::AppendSegment(const FixedPointCoordinate & coordinate,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptionFactory::AppendEncodedPolylineString(std::string & output, bool isEncoded) {
|
void DescriptionFactory::AppendEncodedPolylineString(
|
||||||
if(isEncoded)
|
const bool return_encoded,
|
||||||
|
std::string & output
|
||||||
|
) {
|
||||||
|
if(return_encoded) {
|
||||||
pc.printEncodedString(pathDescription, output);
|
pc.printEncodedString(pathDescription, output);
|
||||||
else
|
} else {
|
||||||
pc.printUnencodedString(pathDescription, output);
|
pc.printUnencodedString(pathDescription, output);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptionFactory::AppendEncodedPolylineString(std::string &output) {
|
void DescriptionFactory::AppendEncodedPolylineString(std::string &output) const {
|
||||||
pc.printEncodedString(pathDescription, output);
|
pc.printEncodedString(pathDescription, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DescriptionFactory::AppendUnencodedPolylineString(std::string &output) {
|
void DescriptionFactory::AppendUnencodedPolylineString(std::string &output) const {
|
||||||
pc.printUnencodedString(pathDescription, output);
|
pc.printUnencodedString(pathDescription, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,16 +194,16 @@ void DescriptionFactory::AppendUnencodedPolylineString(std::string &output) {
|
|||||||
|
|
||||||
// //Post-processing to remove empty or nearly empty path segments
|
// //Post-processing to remove empty or nearly empty path segments
|
||||||
// if(FLT_EPSILON > pathDescription.back().length) {
|
// if(FLT_EPSILON > pathDescription.back().length) {
|
||||||
// // SimpleLogger().Write() << "#segs: " << pathDescription.size() << ", last ratio: " << targetPhantom.ratio << ", length: " << pathDescription.back().length;
|
// // SimpleLogger().Write() << "#segs: " << pathDescription.size() << ", last ratio: " << target_phantom.ratio << ", length: " << pathDescription.back().length;
|
||||||
// if(pathDescription.size() > 2){
|
// if(pathDescription.size() > 2){
|
||||||
// pathDescription.pop_back();
|
// pathDescription.pop_back();
|
||||||
// pathDescription.back().necessary = true;
|
// pathDescription.back().necessary = true;
|
||||||
// pathDescription.back().turnInstruction = TurnInstructions.NoTurn;
|
// pathDescription.back().turnInstruction = TurnInstructions.NoTurn;
|
||||||
// targetPhantom.nodeBasedEdgeNameID = (pathDescription.end()-2)->nameID;
|
// target_phantom.nodeBasedEdgeNameID = (pathDescription.end()-2)->nameID;
|
||||||
// // SimpleLogger().Write() << "Deleting last turn instruction";
|
// // SimpleLogger().Write() << "Deleting last turn instruction";
|
||||||
// }
|
// }
|
||||||
// } else {
|
// } else {
|
||||||
// pathDescription[indexOfSegmentBegin].duration *= (1.-targetPhantom.ratio);
|
// pathDescription[indexOfSegmentBegin].duration *= (1.-target_phantom.ratio);
|
||||||
// }
|
// }
|
||||||
// if(FLT_EPSILON > pathDescription[0].length) {
|
// if(FLT_EPSILON > pathDescription[0].length) {
|
||||||
// //TODO: this is never called actually?
|
// //TODO: this is never called actually?
|
||||||
@ -191,11 +211,11 @@ void DescriptionFactory::AppendUnencodedPolylineString(std::string &output) {
|
|||||||
// pathDescription.erase(pathDescription.begin());
|
// pathDescription.erase(pathDescription.begin());
|
||||||
// pathDescription[0].turnInstruction = TurnInstructions.HeadOn;
|
// pathDescription[0].turnInstruction = TurnInstructions.HeadOn;
|
||||||
// pathDescription[0].necessary = true;
|
// pathDescription[0].necessary = true;
|
||||||
// startPhantom.nodeBasedEdgeNameID = pathDescription[0].nameID;
|
// start_phantom.nodeBasedEdgeNameID = pathDescription[0].nameID;
|
||||||
// // SimpleLogger().Write() << "Deleting first turn instruction, ratio: " << startPhantom.ratio << ", length: " << pathDescription[0].length;
|
// // SimpleLogger().Write() << "Deleting first turn instruction, ratio: " << start_phantom.ratio << ", length: " << pathDescription[0].length;
|
||||||
// }
|
// }
|
||||||
// } else {
|
// } else {
|
||||||
// pathDescription[0].duration *= startPhantom.ratio;
|
// pathDescription[0].duration *= start_phantom.ratio;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// //Generalize poly line
|
// //Generalize poly line
|
||||||
@ -213,8 +233,11 @@ void DescriptionFactory::AppendUnencodedPolylineString(std::string &output) {
|
|||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
void DescriptionFactory::BuildRouteSummary(const double distance, const unsigned time) {
|
void DescriptionFactory::BuildRouteSummary(
|
||||||
summary.startName = startPhantom.nodeBasedEdgeNameID;
|
const double distance,
|
||||||
summary.destName = targetPhantom.nodeBasedEdgeNameID;
|
const unsigned time
|
||||||
|
) {
|
||||||
|
summary.startName = start_phantom.nodeBasedEdgeNameID;
|
||||||
|
summary.destName = target_phantom.nodeBasedEdgeNameID;
|
||||||
summary.BuildDurationAndLengthStrings(distance, time);
|
summary.BuildDurationAndLengthStrings(distance, time);
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|||||||
class DescriptionFactory {
|
class DescriptionFactory {
|
||||||
DouglasPeucker<SegmentInformation> dp;
|
DouglasPeucker<SegmentInformation> dp;
|
||||||
PolylineCompressor pc;
|
PolylineCompressor pc;
|
||||||
PhantomNode startPhantom, targetPhantom;
|
PhantomNode start_phantom, target_phantom;
|
||||||
|
|
||||||
double DegreeToRadian(const double degree) const;
|
double DegreeToRadian(const double degree) const;
|
||||||
double RadianToDegree(const double degree) const;
|
double RadianToDegree(const double degree) const;
|
||||||
@ -64,7 +64,10 @@ public:
|
|||||||
destName(0)
|
destName(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void BuildDurationAndLengthStrings(const double distance, const unsigned time) {
|
void BuildDurationAndLengthStrings(
|
||||||
|
const double distance,
|
||||||
|
const unsigned time
|
||||||
|
) {
|
||||||
//compute distance/duration for route summary
|
//compute distance/duration for route summary
|
||||||
intToString(round(distance), lengthString);
|
intToString(round(distance), lengthString);
|
||||||
int travelTime = time/10 + 1;
|
int travelTime = time/10 + 1;
|
||||||
@ -79,13 +82,16 @@ public:
|
|||||||
DescriptionFactory();
|
DescriptionFactory();
|
||||||
virtual ~DescriptionFactory();
|
virtual ~DescriptionFactory();
|
||||||
double GetBearing(const FixedPointCoordinate& C, const FixedPointCoordinate& B) const;
|
double GetBearing(const FixedPointCoordinate& C, const FixedPointCoordinate& B) const;
|
||||||
void AppendEncodedPolylineString(std::string &output);
|
void AppendEncodedPolylineString(std::string &output) const;
|
||||||
void AppendUnencodedPolylineString(std::string &output);
|
void AppendUnencodedPolylineString(std::string &output) const;
|
||||||
void AppendSegment(const FixedPointCoordinate & coordinate, const _PathData & data);
|
void AppendSegment(const FixedPointCoordinate & coordinate, const _PathData & data);
|
||||||
void BuildRouteSummary(const double distance, const unsigned time);
|
void BuildRouteSummary(const double distance, const unsigned time);
|
||||||
void SetStartSegment(const PhantomNode & startPhantom);
|
void SetStartSegment(const PhantomNode & start_phantom);
|
||||||
void SetEndSegment(const PhantomNode & startPhantom);
|
void SetEndSegment(const PhantomNode & start_phantom);
|
||||||
void AppendEncodedPolylineString(std::string & output, bool isEncoded);
|
void AppendEncodedPolylineString(
|
||||||
|
const bool return_encoded,
|
||||||
|
std::string & output
|
||||||
|
);
|
||||||
|
|
||||||
template<class DataFacadeT>
|
template<class DataFacadeT>
|
||||||
void Run(const DataFacadeT * facade, const unsigned zoomLevel) {
|
void Run(const DataFacadeT * facade, const unsigned zoomLevel) {
|
||||||
@ -171,16 +177,16 @@ public:
|
|||||||
|
|
||||||
//Post-processing to remove empty or nearly empty path segments
|
//Post-processing to remove empty or nearly empty path segments
|
||||||
if(std::numeric_limits<double>::epsilon() > pathDescription.back().length) {
|
if(std::numeric_limits<double>::epsilon() > pathDescription.back().length) {
|
||||||
// SimpleLogger().Write() << "#segs: " << pathDescription.size() << ", last ratio: " << targetPhantom.ratio << ", length: " << pathDescription.back().length;
|
// SimpleLogger().Write() << "#segs: " << pathDescription.size() << ", last ratio: " << target_phantom.ratio << ", length: " << pathDescription.back().length;
|
||||||
if(pathDescription.size() > 2){
|
if(pathDescription.size() > 2){
|
||||||
pathDescription.pop_back();
|
pathDescription.pop_back();
|
||||||
pathDescription.back().necessary = true;
|
pathDescription.back().necessary = true;
|
||||||
pathDescription.back().turnInstruction = TurnInstructions.NoTurn;
|
pathDescription.back().turnInstruction = TurnInstructions.NoTurn;
|
||||||
targetPhantom.nodeBasedEdgeNameID = (pathDescription.end()-2)->nameID;
|
target_phantom.nodeBasedEdgeNameID = (pathDescription.end()-2)->nameID;
|
||||||
// SimpleLogger().Write() << "Deleting last turn instruction";
|
// SimpleLogger().Write() << "Deleting last turn instruction";
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pathDescription[indexOfSegmentBegin].duration *= (1.-targetPhantom.ratio);
|
pathDescription[indexOfSegmentBegin].duration *= (1.-target_phantom.ratio);
|
||||||
}
|
}
|
||||||
if(std::numeric_limits<double>::epsilon() > pathDescription[0].length) {
|
if(std::numeric_limits<double>::epsilon() > pathDescription[0].length) {
|
||||||
//TODO: this is never called actually?
|
//TODO: this is never called actually?
|
||||||
@ -188,11 +194,11 @@ public:
|
|||||||
pathDescription.erase(pathDescription.begin());
|
pathDescription.erase(pathDescription.begin());
|
||||||
pathDescription[0].turnInstruction = TurnInstructions.HeadOn;
|
pathDescription[0].turnInstruction = TurnInstructions.HeadOn;
|
||||||
pathDescription[0].necessary = true;
|
pathDescription[0].necessary = true;
|
||||||
startPhantom.nodeBasedEdgeNameID = pathDescription[0].nameID;
|
start_phantom.nodeBasedEdgeNameID = pathDescription[0].nameID;
|
||||||
// SimpleLogger().Write() << "Deleting first turn instruction, ratio: " << startPhantom.ratio << ", length: " << pathDescription[0].length;
|
// SimpleLogger().Write() << "Deleting first turn instruction, ratio: " << start_phantom.ratio << ", length: " << pathDescription[0].length;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pathDescription[0].duration *= startPhantom.ratio;
|
pathDescription[0].duration *= start_phantom.ratio;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Generalize poly line
|
//Generalize poly line
|
||||||
|
@ -111,8 +111,8 @@ public:
|
|||||||
reply.content += "\"route_geometry\": ";
|
reply.content += "\"route_geometry\": ";
|
||||||
if(config.geometry) {
|
if(config.geometry) {
|
||||||
description_factory.AppendEncodedPolylineString(
|
description_factory.AppendEncodedPolylineString(
|
||||||
reply.content,
|
config.encode_geometry,
|
||||||
config.encode_geometry
|
reply.content
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
reply.content += "[]";
|
reply.content += "[]";
|
||||||
@ -183,8 +183,8 @@ public:
|
|||||||
if(config.geometry && INT_MAX != raw_route_information.lengthOfAlternativePath) {
|
if(config.geometry && INT_MAX != raw_route_information.lengthOfAlternativePath) {
|
||||||
//Generate the linestrings for each alternative
|
//Generate the linestrings for each alternative
|
||||||
alternateDescriptionFactory.AppendEncodedPolylineString(
|
alternateDescriptionFactory.AppendEncodedPolylineString(
|
||||||
reply.content,
|
config.encode_geometry,
|
||||||
config.encode_geometry
|
reply.content
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
reply.content += "],";
|
reply.content += "],";
|
||||||
|
Loading…
Reference in New Issue
Block a user