-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgeo.h
More file actions
82 lines (75 loc) · 2.73 KB
/
Copy pathgeo.h
File metadata and controls
82 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef FGC_GEO_H__
#define FGC_GEO_H__
#include <stdexcept>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <string>
namespace minicore {
struct latlon_t: public std::pair<double, double> {
using super = std::pair<double, double>;
template<typename...Args>
latlon_t(Args &&...args): super(std::forward<Args>(args)...) {}
template<typename T>
latlon_t &operator=(const T &x) {
return super::operator=(x);
}
std::string to_string() const {return std::to_string(lon()) + ',' + std::to_string(lat());}
double lat() const {return this->first;}
double lon() const {return this->second;}
double &lat() {return this->first;}
double &lon() {return this->second;}
};
struct BoundingBoxData;
struct BoundingBoxData {
double latlo = 0., lathi = 0., lonlo = 0., lonhi = 0.;
double p_box = 0.99, p_nobox = 0.01;
bool valid() const {
return lathi >= latlo && lonhi >= lonlo
&& p_box <= 1. && p_box >= 0.
&& p_nobox <= 1. && p_nobox >= 0.;
}
std::string to_string() const {
char buf[256];
return std::string(buf, std::sprintf(buf, "lat (%0.12g->%0.12g), lon (%0.12g->%0.12g), probabilities: %0.12g/%0.12g", latlo, lathi, lonlo, lonhi, p_box, p_nobox));
}
void print(std::FILE *fp=stderr) const {
const std::string str = to_string();
if(std::fwrite(str.data(), 1, str.size(), fp) != str.size())
throw std::runtime_error("Failed to write to file");
}
bool contains(latlon_t pt) const {
return (pt.lat() <= lathi && pt.lat() >= latlo)
&& (pt.lon() <= lonhi && pt.lon() >= lonlo);
}
static BoundingBoxData parse_bbdata(const char *s) {
/*
* lon,lat,lon,lat
* %f,%f,%f,%f[,%f][,%f]
*/
std::fprintf(stderr, "parsing %s\n", s);
double llon, llat, ulon, ulat, highprob = 0.99, loprob=0.01;
llon = std::strtod(s, const_cast<char **>(&s));
llat = std::strtod(++s, const_cast<char **>(&s));
ulon = std::strtod(++s, const_cast<char **>(&s));
ulat = std::strtod(++s, const_cast<char **>(&s));
if(*s == ',') {
highprob = std::strtod(++s, const_cast<char **>(&s));
if(*s == ',') {
loprob = std::strtod(++s, const_cast<char **>(&s));
}
}
assert(loprob < highprob);
BoundingBoxData ret{llat, ulat, llon, ulon, highprob, loprob};
ret.print(stderr);
std::fputc('\n', stderr);
return ret;
}
BoundingBoxData &operator=(const char *s) {
assert(s);
return *this = parse_bbdata(s);
}
bool set() const {return latlo || lathi || lonlo || lonhi;}
};
} // namespace minicore
#endif /* FGC_GEO_H__ */