[traffic] Calculate filter list for active MWMs

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-05-17 17:31:21 +03:00
parent e3f5dd3ca8
commit a7897e2347
5 changed files with 72 additions and 3 deletions

View File

@@ -266,12 +266,21 @@ void TrafficManager::UpdateViewport(ScreenBase const & screen)
UpdateActiveMwms(screen.ClipRect(), m_lastDrapeMwmsByRect, m_activeDrapeMwms);
}
std::string TrafficManager::GetMwmFilters(std::set<MwmSet::MwmId> & mwms)
{
std::vector<m2::RectD> rects;
for (auto mwmId : mwms)
rects.push_back(mwmId.GetInfo()->m_bordersRect);
return traffxml::FiltersToXml(rects);
}
// TODO make this work with multiple sources (e.g. Android)
bool TrafficManager::Subscribe(std::set<MwmSet::MwmId> & mwms)
{
// TODO what if were subscribed already?
std::string filterList = GetMwmFilters(mwms);
// TODO
LOG(LINFO, ("Would subscribe to", mwms));
LOG(LINFO, ("Would subscribe to:\n", filterList));
m_subscriptionId = "placeholder_subscription_id";
m_isPollNeeded = true; // would be false if we got a feed here
return true;
@@ -280,9 +289,11 @@ bool TrafficManager::Subscribe(std::set<MwmSet::MwmId> & mwms)
// TODO make this work with multiple sources (e.g. Android)
bool TrafficManager::ChangeSubscription(std::set<MwmSet::MwmId> & mwms)
{
// TODO what if were not subscribed yet?
if (!IsSubscribed())
return false;
std::string filterList = GetMwmFilters(mwms);
// TODO
LOG(LINFO, ("Would change subscription", m_subscriptionId, "to", mwms));
LOG(LINFO, ("Would change subscription", m_subscriptionId, "to:\n", filterList));
m_isPollNeeded = true; // would be false if we got a feed here
return true;
}
@@ -290,6 +301,7 @@ bool TrafficManager::ChangeSubscription(std::set<MwmSet::MwmId> & mwms)
bool TrafficManager::SetSubscriptionArea()
{
std::set<MwmSet::MwmId> activeMwms;
if (!IsSubscribed())
{
{

View File

@@ -183,6 +183,14 @@ private:
traffic::TrafficInfo::Availability m_lastAvailability;
};
/**
* @brief Returns a TraFF filter list for a set of MWMs.
*
* @param mwms The MWMs for which a filter list is to be created.
* @return A `filter_list` in XML format.
*/
std::string GetMwmFilters(std::set<MwmSet::MwmId> & mwms);
/**
* @brief Subscribes to a traffic service.
*

View File

@@ -395,6 +395,22 @@ struct TraffMessage
using TraffFeed = std::vector<TraffMessage>;
// TODO Capabilities
/*
* Filter: currently not implemented.
* We only use bbox, for which we have a suitable data type.
* min_road_class is not needed as we do not filter by road class.
*/
/*
* TraffSubscription: currently not implemented.
* We just store the ID as a string.
* Filters are only by bbox, not by min_road_class. The list is auto-generated from the list of
* active MWMs and changes exactly when the active MWM set changes, eliminating the need to store
* the full filter list.
*/
/**
* @brief Guess the distance to the next point.
*

View File

@@ -643,4 +643,16 @@ bool ParseTraff(pugi::xml_document const & document, TraffFeed & feed)
}
return result;
}
std::string FiltersToXml(std::vector<m2::RectD> & bboxRects)
{
std::ostringstream os;
for (auto rect : bboxRects)
os << std::format("<filter bbox=\"{} {} {} {}\"/>\n",
mercator::YToLat(rect.minY()),
mercator::XToLon(rect.minX()),
mercator::YToLat(rect.maxY()),
mercator::XToLon(rect.maxX()));
return os.str();
}
} // namespace openlr

View File

@@ -2,6 +2,11 @@
#include "traffxml/traff_model.hpp"
#include "geometry/rect2d.hpp"
#include <string>
#include <vector>
namespace pugi
{
class xml_document;
@@ -11,4 +16,20 @@ class xml_node;
namespace traffxml
{
bool ParseTraff(pugi::xml_document const & document, TraffFeed & feed);
/**
* @brief Generates a list of XML `filter` elements from a vector of rects representing bboxes.
*
* The resulting string can be placed inside a TraFF XML `filter_list` element or can be passed as
* an extra to an Android intent.
*
* It will have one `filter` element for each element in `bboxRects`, although simplification may
* be applied to reduce the number of rects (currently not implemented).
*
* The `min_road_class` attribute is not used.
*
* @param bboxRects The rectangles, each of which represents a bounding box.
* @return A string of XML `filter` elements.
*/
std::string FiltersToXml(std::vector<m2::RectD> & bboxRects);
} // namespace traffxml