60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
#include <iostream>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
#include "s2/s2earth.h"
|
|
#include "s2/s2cell_id.h"
|
|
#include "s2/s1angle.h"
|
|
|
|
using namespace std;
|
|
|
|
uint64 latLonStringsToCellId(string latString, string lonString) {
|
|
S1Angle lat = S1Angle::Degrees(atof(latString.c_str()));
|
|
S1Angle lon = S1Angle::Degrees(atof(lonString.c_str()));
|
|
|
|
S2LatLng latLng = S2LatLng(lat, lon);
|
|
// Do not normalize since this doesn't actually normalize, it clamps/truncates
|
|
// .Normalized();
|
|
|
|
S2CellId s2CellId = S2CellId(latLng);
|
|
|
|
return s2CellId.id();
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
fstream file;
|
|
file.open("./00-sample-latlon.csv", fstream::in);
|
|
|
|
if (!file) {
|
|
cerr << "Could not open input file" << endl;
|
|
exit(1);
|
|
}
|
|
|
|
fstream ofile;
|
|
ofile.open("./01-sample-latlon-s2cpp-cellid.csv", fstream::out);
|
|
|
|
if (!ofile) {
|
|
cerr << "Could not open output file" << endl;
|
|
exit(1);
|
|
}
|
|
|
|
string latString = "";
|
|
string lonString = "";
|
|
|
|
string header;
|
|
getline(file, header, '\n');
|
|
|
|
ofile << header << ",cellid" << "\n";
|
|
|
|
while (getline(file, latString, ',')) {
|
|
getline(file, lonString, '\n') ;
|
|
uint64 generatedCellId = latLonStringsToCellId(latString, lonString);
|
|
|
|
ofile << latString << "," << lonString << "," << generatedCellId << "\n";
|
|
}
|
|
|
|
cout << "Done" << endl;
|
|
|
|
return 0;
|
|
}
|