#include "common/GeoJson.h" #include namespace visione::geojson { namespace { struct Point { double x; double y; }; } // namespace bool pointInPolygon(double latitude, double longitude, const Json::Value &geojson) { if (!geojson.isObject() || geojson["type"].asString() != "Polygon" || !geojson["coordinates"].isArray() || geojson["coordinates"].empty()) { return false; } const auto &ring = geojson["coordinates"][0]; if (!ring.isArray() || ring.size() < 4) { return false; } std::vector points; points.reserve(ring.size()); for (const auto &coordinate : ring) { if (!coordinate.isArray() || coordinate.size() < 2) { return false; } points.push_back({coordinate[0].asDouble(), coordinate[1].asDouble()}); } const auto x = longitude; const auto y = latitude; bool inside = false; for (size_t i = 0, j = points.size() - 1; i < points.size(); j = i++) { const auto &pi = points[i]; const auto &pj = points[j]; const bool intersects = ((pi.y > y) != (pj.y > y)) && (x < (pj.x - pi.x) * (y - pi.y) / (pj.y - pi.y) + pi.x); if (intersects) { inside = !inside; } } return inside; } } // namespace visione::geojson