Files
MicrOBU/microbu-esp32c5/external/vanetza-idf/vanetza/geodesy/country_database.cpp
T
Ashin Walpola 0e9525162d Keep the colleague's microbu-esp32c5 tree in this repository
obu-firmware builds against vanetza-idf from microbu-esp32c5/external, but
that tree was gitignored, so a clone of this repository could not build the
firmware it ships. It is now committed here as ordinary files in its own
folder, microbu-esp32c5/: the colleague's commit cf4b99f plus the V2X2MAP
bridge's signature verification (--trust) used on the bench. Nothing is
fetched from or pushed to the colleague's repository; this repository and
its remotes carry everything. The folder's own .gitignore keeps build output,
downloaded components and private key material out, as it did there; the
committed file set is identical to that repository's tracked files.

The ESP32-C5 is still flashed from obu-firmware/, which only takes
vanetza-idf from microbu-esp32c5/, so the two stay separate folders.
FLASHING.md says how to take a newer version of the colleague's tree (copy
it over the folder, rebuild, test, commit).
2026-09-23 17:46:40 +02:00

85 lines
2.1 KiB
C++

#include <vanetza/geodesy/country_database.hpp>
#include <vanetza/geodesy/country_data_reader.hpp>
#include <boost/geometry/algorithms/within.hpp>
#include <boost/units/quantity.hpp>
#include <fstream>
#include <iterator>
#include <vector>
#ifdef VANETZA_WITH_EMBEDDED_COUNTRY_DATA
#include <vanetza/common/byte_view.hpp>
namespace vanetza { namespace geodesy { namespace country { vanetza::byte_view_range embedded(); } } }
#endif
namespace vanetza
{
namespace geodesy
{
CountryDatabase CountryDatabase::embedded(std::string* error)
{
CountryDatabase db;
#ifdef VANETZA_WITH_EMBEDDED_COUNTRY_DATA
auto view = country::embedded();
db.load(view.data(), view.size(), error);
#else
if (error) {
*error = "embedded country data not available";
}
#endif
return db;
}
bool CountryDatabase::load(const std::string& path, std::string* error)
{
std::ifstream file(path, std::ios::binary);
if (!file) {
if (error) {
*error = "cannot open file: " + path;
}
return false;
}
std::vector<uint8_t> data { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() };
return load(data.data(), data.size(), error);
}
bool CountryDatabase::load(const uint8_t* data, std::size_t length, std::string* error)
{
m_countries.clear();
auto result = read_country_data(data, length,
[this](M49Code code, CountryPolygon&& polygon) {
m_countries.emplace(code, std::move(polygon));
});
if (result.failed()) {
if (error) {
*error = result.message() + " at offset " + std::to_string(result.position());
}
m_countries.clear();
return false;
}
return true;
}
bool CountryDatabase::is_inside(M49Code country, const GeodeticPosition& position) const
{
auto it = m_countries.find(country);
if (it == m_countries.end()) {
return false;
}
country::Point point(position.longitude / units::degree, position.latitude / units::degree);
return boost::geometry::within(point, it->second);
}
bool CountryDatabase::empty() const
{
return m_countries.empty();
}
} // namespace geodesy
} // namespace vanetza