geostat/geostat.cpp

397 wiersze
13 KiB
C++
Czysty Zwykły widok Historia

2019-09-08 16:42:10 +00:00
#include "okapi.h"
#include "gpx.h"
#include "cache.h"
#include "debug.h"
#include "heat.h"
#include "ocdb.h"
2019-09-08 16:42:10 +00:00
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <unistd.h>
#include <map>
const int HIST_MAX = 20;
2019-11-04 20:34:29 +00:00
void show_usage() {
std::cout << "Usage: [-p] [-g file] [-qn] [-MDHLTOYh] [-s size] [-m map]\n";
std::cout << "Generate stats from Opencaching data or GPX files.\n\n";
std::cout << " * Data source:\n";
std::cout << "\t-p\tuse Opencaching.pl\n";
std::cout << "\t-u user\tuser for Opencaching\n";
std::cout << "\t-g file\tuse specified gpx file\n";
std::cout << "\t-q\tuse local SQLite file\n";
std::cout << "\t-n\tget only unfound caches (works only with SQLite)\n";
std::cout << " * Output:\n";
std::cout << "\t-M\tprint geographically extreme caches\n";
std::cout << "\t-D\tprint furthest and closest caches\n";
std::cout << "\t-H file\trender a heat map to a file\n";
std::cout << "\t-s n\tstamp size for a heat map (default = 15)\n";
std::cout << "\t-e\tuse exponential to flatten the heat map\n";
std::cout << "\t-m map\tchosen map: Poland, Poland_relief, Poland_big, Europe or a name of voivodeship (default = Poland)\n";
std::cout << "\t-L\tprint all caches\n";
std::cout << "\t-T\tprint D/T matrix\n";
std::cout << "\t-O\tprint owner count for found caches\n";
std::cout << "\t-Y\tprint types count for found caches\n";
std::cout << "\t-W\toutput all data in HTML format\n";
std::cout << "\t-h\tdisplay this help screen\n";
std::exit(EXIT_FAILURE);
}
void show_histogram(Caches* cc, std::string Cache::*ptr, std::string caption, bool html = 0, bool sort_by_val = 1) {
std::map<std::string, int> histogram;
std::vector<std::pair<std::string, int>> pairs;
for (auto el : *cc)
histogram[el.*ptr]++;
for (auto el : histogram)
pairs.push_back(el);
if (sort_by_val)
2019-11-07 17:00:05 +00:00
sort(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second > b.second; });
else
2019-11-07 17:00:05 +00:00
sort(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.first < b.first; });
if (html) {
int max;
if (sort_by_val)
max = pairs[0].second;
else
2019-11-07 17:00:05 +00:00
max = std::max_element(pairs.begin(), pairs.end(), [&](std::pair<std::string, int>& a, std::pair<std::string, int>& b) { return a.second < b.second; })->second;
int i = 0;
std::cout << "<h2>" << caption << "</h2>\n";
std::cout << "<dl class=\"histogram\">\n";
for (auto own : pairs) {
if (own.first.empty()) own.first = "[unknown]";
if (i < HIST_MAX)
2019-11-09 10:32:41 +00:00
std::cout << "<dd class=\"bar\" style=\"--percent: " << 100 * own.second / max << "%;\"><span class=\"text\">" << own.first << ": " << own.second << "</span></dd>\n";
else if (i == HIST_MAX) {
std::cout << "</dl>\n";
std::cout << "<div class=\"histogram_others\">Other: " << own.first;
}
if (i > HIST_MAX)
std::cout << ", " << own.first;
i++;
}
if (i < HIST_MAX)
std::cout << "</dl>\n";
else
std::cout << "</div>\n";
} else {
std::cout << caption << '\n';
for (auto own : pairs)
std::cout << own.first << ": " << own.second << '\n';
}
}
2019-09-08 16:42:10 +00:00
int main(int argc, char** argv) {
bool show_minmax = 0;
bool show_dist = 0;
bool show_list = 0;
bool show_dt = 0;
bool show_owners = 0;
bool show_types = 0;
bool show_calendar = 0;
bool show_html = 0;
2019-09-08 16:42:10 +00:00
std::string heat_file;
int heat_stamp_size = 15;
2019-10-06 11:08:09 +00:00
std::string heat_map = "Poland";
2019-09-12 12:07:46 +00:00
bool heat_exp = 0;
2019-09-08 16:42:10 +00:00
bool use_ocpl = 0;
bool use_ocpl_db = 0;
bool get_not_found = 0;
2019-09-08 16:42:10 +00:00
std::string ocpl_user;
std::string ocpl_url = "https://opencaching.pl/okapi/";
const std::string Database = "ocpl.sqlite";
2019-09-08 16:42:10 +00:00
std::string gpx_file;
#include "config_user.h"
2019-11-04 20:34:29 +00:00
if (argc == 1) show_usage();
2019-09-08 16:42:10 +00:00
int o;
while ((o = getopt(argc, argv, "qng:d:pu:MDOH:s:m:eLTYWh?")) != -1)
2019-09-08 16:42:10 +00:00
switch (o) {
case 'd':
try {
if (std::stoi(optarg) > 0) {
// Debug(1) << "Setting debug level to " << optarg;
Debug::set_debug_level(std::stoi(optarg));
}
}
catch (...) {
std::cout << "Option \"-d\" requires a valid number as an argument\n";
std::exit(EXIT_FAILURE);
}
break;
case 'g':
gpx_file = optarg;
break;
case 'p':
use_ocpl = 1;
break;
case 'u':
ocpl_user = optarg;
break;
case 'q':
use_ocpl_db = 1;
break;
case 'n':
get_not_found = 1;
break;
2019-09-08 16:42:10 +00:00
case 'M':
show_minmax = 1;
break;
case 'D':
show_dist = 1;
break;
case 'O':
show_owners = 1;
break;
case 'Y':
show_types = 1;
break;
case 'W':
show_html = 1;
break;
2019-09-08 16:42:10 +00:00
case 'H':
heat_file = optarg;
break;
case 's':
try {
if (std::stoi(optarg) > 0) {
heat_stamp_size = std::stoi(optarg);
}
}
catch (...) {
std::cout << "Option \"-s\" requires a valid number as an argument\n";
std::exit(EXIT_FAILURE);
}
break;
case 'm':
heat_map = optarg;
2019-09-08 16:42:10 +00:00
break;
2019-09-12 12:07:46 +00:00
case 'e':
heat_exp = 1;
break;
2019-09-08 16:42:10 +00:00
case 'L':
show_list = 1;
break;
case 'T':
show_dt = 1;
break;
case 'h':
case '?':
default:
2019-11-04 20:34:29 +00:00
show_usage();
2019-09-08 16:42:10 +00:00
}
if (show_html) {
show_dist = 0;
show_list = 0;
show_dt = 0;
show_owners = 0;
show_types = 0;
}
2019-09-08 16:42:10 +00:00
Caches cc;
if (use_ocpl) {
Okapi OCpl(ocpl_url, ocpl_key);
if (!ocpl_user.empty()) {
ocpl_user_uuid = OCpl.get_uuid(ocpl_user);
}
cc.merge(OCpl.get_user_caches(ocpl_user_uuid, 0));
}
if (!gpx_file.empty()) {
GPX gpxfile(gpx_file);
cc.merge(gpxfile.get_user_caches());
}
if (use_ocpl_db) {
OCdb db(Database);
Okapi OCpl(ocpl_url, ocpl_key);
if (!ocpl_user.empty()) {
ocpl_user_uuid = OCpl.get_uuid(ocpl_user);
}
if (get_not_found) {
cc.merge(db.get_user_caches_not_found(ocpl_user_uuid));
} else {
cc.merge(db.get_user_caches(ocpl_user_uuid, 0));
}
}
2019-09-08 16:42:10 +00:00
// TODO: some cache deduplication is needed
Debug(2) << "Caches read: " << cc.size() << '\n';
if (cc.size() == 0) {
std::cout << "No caches found, aborting.\n";
std::exit(EXIT_FAILURE);
}
2019-09-08 16:42:10 +00:00
if (show_html) {
std::cout << "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n";
std::cout << "<html>\n";
std::cout << " <head>\n";
std::cout << " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=\"utf-8\">\n";
std::cout << " <link rel=\"stylesheet\" type=\"text/css\" href=\"geo.css\">\n";
std::cout << " <title>Geocaching stats</title>\n";
std::cout << "</head>\n";
std::cout << "<body>\n";
std::cout << "<h1>Geocaching stats for user " << ocpl_user << "<br>Powered by <a href=\"https://gitlab.com/tomaszg/geostat\">GeoStat</a></h1>\n";
2019-11-10 20:03:11 +00:00
}
if (!heat_file.empty()) {
const Map* chosen_map;
if (maps.count(heat_map) > 0)
chosen_map = &maps.at(heat_map);
else {
std::cout << "Map " << heat_map << " not found.\n";
std::exit(EXIT_FAILURE);
}
Heat hmap(&cc, chosen_map);
hmap.generate(heat_file, heat_stamp_size, (heat_exp == 1 ? "exp" : "soft"));
if (show_html)
std::cout << "<img class=\"heatmap\" src=\"" << heat_file << "\" alt=\"heat map\">\n";
}
if (show_html && !get_not_found) {
2019-11-10 19:59:21 +00:00
std::cout << "<div class=\"basic_stats\">\n";
std::cout << "Number of caches found: " << cc.size() << "<br>\n";
std::map<std::string, int> dates;
std::multimap<std::time_t, const Cache*> sorted_caches;
std::tm tmp;
for (auto& i : cc) {
dates[i.date]++;
if (i.type != "Moving" && i.type != "Own") {
tmp = i.date_tm;
sorted_caches.insert({std::mktime(&tmp), &i});
}
}
auto best_day = std::max_element(dates.begin(), dates.end(), [&](auto& a, auto& b) { return a.second < b.second; });
std::cout << "Number of caching days: " << dates.size() << "<br>\n";
std::cout << "Average caches per caching day: " << std::setprecision(3) << (1.0 * cc.size()) / dates.size() << "<br>\n";
std::cout << "Best caching day: " << best_day->first << ", found " << best_day->second << " caches<br>\n";
// for (auto& i : dates)
// std::cout << i.first << ", found " << i.second << " caches<br>\n";
int tot_dist = 0;
for (auto i = sorted_caches.begin(); i != std::prev(sorted_caches.end()); i++) {
// std::cout << "Distance between " << i->second->name << " and " << std::next(i)->second->name << " is " << cache_distance(*i->second, *std::next(i)->second) << "<br>";
tot_dist += cache_distance(*i->second, *std::next(i)->second);
}
std::cout << "Total distance between caches: " << tot_dist << " km (equivalent to " << tot_dist/(2 * Pi * Earth_radius) << "x trips around Earth)<br>\n";
2019-11-10 20:03:11 +00:00
std::cout << "</div>\n";
2019-09-08 16:42:10 +00:00
}
if ((show_minmax || show_html) && !get_not_found) {
2019-11-07 17:00:05 +00:00
auto N = std::max_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lat < b.pos.lat; });
auto S = std::min_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lat < b.pos.lat; });
auto E = std::max_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lon < b.pos.lon; });
auto W = std::min_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.pos.lon < b.pos.lon; });
2019-09-08 16:42:10 +00:00
if (show_minmax) {
std::cout << "Most N:\n";
N->show();
std::cout << "Most S:\n";
S->show();
std::cout << "Most E:\n";
E->show();
std::cout << "Most W:\n";
W->show();
} else {
std::cout << "<h2>Geographically extreme caches</h2>\n";
std::cout << "<table class=\"minmax\">\n";
std::cout << "<tr>\n";
std::cout << " <td>North:</td><td><a href=\"" << N->link() << "\"</a>" << N->name << " (" << N->code << ")</a></td><td>" << N->pos.lat << "</td>\n";
std::cout << "</tr><tr>\n";
std::cout << " <td>South:</td><td><a href=\"" << S->link() << "\"</a>" << S->name << " (" << S->code << ")</a></td><td>" << S->pos.lat << "</td>\n";
std::cout << "</tr><tr>\n";
std::cout << " <td>East:</td> <td><a href=\"" << E->link() << "\"</a>" << E->name << " (" << E->code << ")</a></td><td>" << E->pos.lon << "</td>\n";
std::cout << "</tr><tr>\n";
std::cout << " <td>West:</td> <td><a href=\"" << W->link() << "\"</a>" << W->name << " (" << W->code << ")</a></td><td>" << W->pos.lon << "</td>\n";
std::cout << "</tr>\n";
std::cout << "</table>\n";
}
2019-09-08 16:42:10 +00:00
}
if (show_dist && !get_not_found) {
2019-11-07 17:00:05 +00:00
auto far = std::max_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.distance() < b.distance(); });
auto near = std::min_element(cc.begin(), cc.end(), [&](const Cache& a, const Cache& b) { return a.distance() < b.distance(); });
2019-09-08 16:42:10 +00:00
std::cout << "Nearest cache: " << near->distance() << " km\n";
near->show();
std::cout << "Furthest cache: " << far->distance() << " km\n";
far->show();
}
if (show_list) {
for (auto el : cc)
el.show();
}
if ((show_owners || show_html) && !get_not_found) {
show_histogram(&cc, &Cache::owner, "Cache owners", show_html);
}
if ((show_types || show_html) && !get_not_found) {
show_histogram(&cc, &Cache::type, "Cache types", show_html);
2019-11-08 21:50:21 +00:00
show_histogram(&cc, &Cache::size, "Cache sizes", show_html);
if (!show_html) show_histogram(&cc, &Cache::origin, "Services", 0);
show_histogram(&cc, &Cache::region, "Regions", show_html);
}
if ((show_calendar || show_html) && !get_not_found) {
2019-11-07 17:07:22 +00:00
show_histogram(&cc, &Cache::mon, "Months", show_html, 0);
show_histogram(&cc, &Cache::year, "Years", show_html, 0);
}
if (show_dt && !get_not_found) {
2019-09-08 16:42:10 +00:00
short dt_table[11][11];
std::cout << std::setw(5) << "D\\T";
for (int j = 2; j <= 10; j++) { // print table terr headers
std::cout << std::setw(5) << j / 2.0;
}
std::cout << '\n';
for (int i = 2; i <= 10; i++) { // i -> diff in rows
std::cout << std::setw(5) << i / 2.0;
for (int j = 2; j <= 10; j++) { // j -> terr in cols
dt_table[i][j] = std::count_if(cc.begin(), cc.end(), [i, j](Cache c) { return (c.diff == i / 2.0 && c.terr == j / 2.0); });
std::cout << std::setw(5) << dt_table[i][j];
}
std::cout << '\n';
}
}
if (show_html && !get_not_found) {
short dt_table[11][11];
std::cout << "<h2>Difficulty / terrain matrix</h2>\n";
std::cout << "<table class=\"dt\">\n";
std::cout << "<tr class=\"dt_head\"><td></td>";
for (int j = 2; j <= 10; j++) { // print table terr headers
std::cout << "<td>" << j / 2.0 << "</td>";
}
std::cout << "</tr>\n";
for (int i = 2; i <= 10; i++) { // i -> diff in rows
std::cout << "<tr><td class=\"dt_head\">" << i / 2.0 << "</td> ";
for (int j = 2; j <= 10; j++) { // j -> terr in cols
dt_table[i][j] = std::count_if(cc.begin(), cc.end(), [i, j](Cache c) { return (c.diff == i / 2.0 && c.terr == j / 2.0); });
if (dt_table[i][j] == 0)
std::cout << "<td class=\"dt_zero\">" << dt_table[i][j] << "</td>";
else
std::cout << "<td>" << dt_table[i][j] << "</td>";
}
std::cout << "</tr>\n";
}
std::cout << "</table>\n";
}
if (show_html) {
std::cout << "</body>\n</html>\n";
}
2019-09-08 16:42:10 +00:00
}