this_algorithm/test-data/generator/generate.cpp

60 lines
1.4 KiB
C++
Raw Normal View History

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