2011-06-27 18:08:53 -04:00
|
|
|
/*
|
|
|
|
open source routing machine
|
|
|
|
Copyright (C) Dennis Luxen, others 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU AFFERO General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
or see http://www.gnu.org/licenses/agpl.txt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef POLYLINECOMPRESSOR_H_
|
|
|
|
#define POLYLINECOMPRESSOR_H_
|
|
|
|
|
2011-07-29 12:41:32 -04:00
|
|
|
#include <string>
|
|
|
|
|
2011-06-27 18:08:53 -04:00
|
|
|
#include "ExtractorStructs.h"
|
|
|
|
|
|
|
|
class PolylineCompressor {
|
|
|
|
private:
|
2011-08-04 13:08:37 -04:00
|
|
|
inline string encodeSignedNumber(const int number) const {
|
2011-06-28 05:28:19 -04:00
|
|
|
int signedNumber = number << 1;
|
|
|
|
if (number < 0) {
|
|
|
|
signedNumber = ~(signedNumber);
|
|
|
|
}
|
|
|
|
return (encodeNumber(signedNumber));
|
|
|
|
}
|
2011-06-27 18:08:53 -04:00
|
|
|
|
2011-06-28 05:28:19 -04:00
|
|
|
inline string encodeNumber(int numberToEncode) const {
|
2011-07-29 12:41:32 -04:00
|
|
|
string encodeString;
|
2011-06-27 18:08:53 -04:00
|
|
|
|
2011-06-28 05:28:19 -04:00
|
|
|
while (numberToEncode >= 0x20) {
|
|
|
|
int nextValue = (0x20 | (numberToEncode & 0x1f)) + 63;
|
2011-07-29 12:41:32 -04:00
|
|
|
encodeString += (static_cast<char> (nextValue));
|
2011-06-28 05:28:19 -04:00
|
|
|
numberToEncode >>= 5;
|
|
|
|
}
|
2011-06-27 18:08:53 -04:00
|
|
|
|
2011-06-28 05:28:19 -04:00
|
|
|
numberToEncode += 63;
|
2011-07-29 12:41:32 -04:00
|
|
|
encodeString += (static_cast<char> (numberToEncode));
|
2011-06-27 18:08:53 -04:00
|
|
|
|
2011-07-29 12:41:32 -04:00
|
|
|
return encodeString;
|
2011-06-28 05:28:19 -04:00
|
|
|
}
|
2011-06-29 04:43:08 -04:00
|
|
|
|
|
|
|
inline void replaceBackslash(string & str) {
|
|
|
|
size_t found = 0;
|
|
|
|
do {
|
|
|
|
found = str.find("\\", found);
|
|
|
|
if(found ==string::npos)
|
|
|
|
break;
|
|
|
|
str.insert(found, "\\");
|
|
|
|
found+=2;
|
|
|
|
} while(true);
|
|
|
|
}
|
|
|
|
|
2011-06-27 18:08:53 -04:00
|
|
|
public:
|
2011-06-28 05:28:19 -04:00
|
|
|
inline void printEncodedString(vector<_Coordinate>& polyline, string &output) {
|
|
|
|
output += "\"";
|
2011-07-12 10:03:31 -04:00
|
|
|
if(!polyline.empty()) {
|
2011-06-28 05:28:19 -04:00
|
|
|
output += encodeSignedNumber(polyline[0].lat);
|
|
|
|
output += encodeSignedNumber(polyline[0].lon);
|
|
|
|
}
|
|
|
|
for(unsigned i = 1; i < polyline.size(); i++) {
|
|
|
|
output += encodeSignedNumber(polyline[i].lat - polyline[i-1].lat);
|
|
|
|
output += encodeSignedNumber(polyline[i].lon - polyline[i-1].lon);
|
|
|
|
}
|
|
|
|
output += "\"";
|
2011-06-29 04:43:08 -04:00
|
|
|
replaceBackslash(output);
|
2011-06-28 05:28:19 -04:00
|
|
|
}
|
2011-06-27 18:08:53 -04:00
|
|
|
|
2011-06-28 05:28:19 -04:00
|
|
|
inline void printUnencodedString(vector<_Coordinate> & polyline, string & output) {
|
|
|
|
output += "[";
|
|
|
|
string tmp;
|
|
|
|
for(unsigned i = 0; i < polyline.size(); i++) {
|
2011-07-07 04:01:14 -04:00
|
|
|
convertInternalLatLonToString(polyline[i].lat, tmp);
|
2011-06-28 05:28:19 -04:00
|
|
|
output += "[";
|
|
|
|
output += tmp;
|
2011-07-07 04:01:14 -04:00
|
|
|
convertInternalLatLonToString(polyline[i].lon, tmp);
|
2011-06-28 05:28:19 -04:00
|
|
|
output += ", ";
|
|
|
|
output += tmp;
|
|
|
|
output += "]";
|
|
|
|
if( i < polyline.size()-1 ) {
|
|
|
|
output += ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output += "]";
|
|
|
|
}
|
2011-06-27 18:08:53 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* POLYLINECOMPRESSOR_H_ */
|