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