Vanetza
Loading...
Searching...
No Matches
country_database.cpp
1#include <vanetza/geodesy/country_database.hpp>
2#include <vanetza/geodesy/country_data_reader.hpp>
3#include <boost/geometry/algorithms/within.hpp>
4#include <boost/units/quantity.hpp>
5#include <fstream>
6#include <iterator>
7#include <vector>
8
9#ifdef VANETZA_WITH_EMBEDDED_COUNTRY_DATA
10#include <vanetza/common/byte_view.hpp>
11namespace vanetza { namespace geodesy { namespace country { vanetza::byte_view_range embedded(); } } }
12#endif
13
14namespace vanetza
15{
16namespace geodesy
17{
18
20{
22#ifdef VANETZA_WITH_EMBEDDED_COUNTRY_DATA
23 auto view = country::embedded();
24 db.load(view.data(), view.size(), error);
25#else
26 if (error) {
27 *error = "embedded country data not available";
28 }
29#endif
30 return db;
31}
32
33bool CountryDatabase::load(const std::string& path, std::string* error)
34{
35 std::ifstream file(path, std::ios::binary);
36 if (!file) {
37 if (error) {
38 *error = "cannot open file: " + path;
39 }
40 return false;
41 }
42
43 std::vector<uint8_t> data { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() };
44 return load(data.data(), data.size(), error);
45}
46
47bool CountryDatabase::load(const uint8_t* data, std::size_t length, std::string* error)
48{
49 m_countries.clear();
50
51 auto result = read_country_data(data, length,
52 [this](M49Code code, CountryPolygon&& polygon) {
53 m_countries.emplace(code, std::move(polygon));
54 });
55
56 if (result.failed()) {
57 if (error) {
58 *error = result.message() + " at offset " + std::to_string(result.position());
59 }
60 m_countries.clear();
61 return false;
62 }
63
64 return true;
65}
66
67bool CountryDatabase::is_inside(M49Code country, const GeodeticPosition& position) const
68{
69 auto it = m_countries.find(country);
70 if (it == m_countries.end()) {
71 return false;
72 }
73
74 country::Point point(position.longitude / units::degree, position.latitude / units::degree);
75 return boost::geometry::within(point, it->second);
76}
77
79{
80 return m_countries.empty();
81}
82
83} // namespace geodesy
84} // namespace vanetza
bool is_inside(M49Code country, const GeodeticPosition &position) const
bool load(const std::string &path, std::string *error=nullptr)
bool load(const uint8_t *data, std::size_t length, std::string *error=nullptr)
static CountryDatabase embedded(std::string *error=nullptr)