[traffxml] Parse and store distance for location points

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-05-17 19:25:29 +03:00
parent 2ed300ca08
commit c0c8d5da58
3 changed files with 24 additions and 2 deletions

View File

@@ -550,7 +550,7 @@ std::string DebugPrint(Point point)
std::ostringstream os; std::ostringstream os;
os << "Point { "; os << "Point { ";
os << "coordinates: " << DebugPrint(point.m_coordinates) << ", "; os << "coordinates: " << DebugPrint(point.m_coordinates) << ", ";
// TODO optional float m_distance; (not in struct yet) os << "distance: " << (point.m_distance ? std::to_string(point.m_distance.value()) : "nullopt") << ", ";
os << "junctionName: " << point.m_junctionName.value_or("nullopt") << ", "; os << "junctionName: " << point.m_junctionName.value_or("nullopt") << ", ";
os << "junctionRef: " << point.m_junctionRef.value_or("nullopt"); os << "junctionRef: " << point.m_junctionRef.value_or("nullopt");
os << " }"; os << " }";

View File

@@ -275,7 +275,7 @@ struct Point
// TODO role? // TODO role?
ms::LatLon m_coordinates = ms::LatLon::Zero(); ms::LatLon m_coordinates = ms::LatLon::Zero();
// TODO optional float m_distance; std::optional<float> m_distance;
std::optional<std::string> m_junctionName; std::optional<std::string> m_junctionName;
std::optional<std::string> m_junctionRef; std::optional<std::string> m_junctionRef;
}; };

View File

@@ -128,6 +128,27 @@ std::optional<uint8_t> OptionalIntegerFromXml(pugi::xml_attribute attribute)
} }
} }
/**
* @brief Retrieves a float value from an attribute.
*
* @param attribute The XML attribute to retrieve.
* @return `true` on success, `false` if the attribute is not set or does not contain a float value.
*/
std::optional<float> OptionalFloatFromXml(pugi::xml_attribute attribute)
{
if (attribute.empty())
return std::nullopt;
try
{
float result = std::stof(attribute.as_string());
return result;
}
catch (std::invalid_argument const& ex)
{
return std::nullopt;
}
}
/** /**
* @brief Retrieves a string from an attribute. * @brief Retrieves a string from an attribute.
* *
@@ -405,6 +426,7 @@ std::optional<Point> OptionalPointFromXml(pugi::xml_node node)
result.m_junctionName = OptionalStringFromXml(node.attribute("junction_name")); result.m_junctionName = OptionalStringFromXml(node.attribute("junction_name"));
result.m_junctionRef = OptionalStringFromXml(node.attribute("junction_ref")); result.m_junctionRef = OptionalStringFromXml(node.attribute("junction_ref"));
result.m_distance = OptionalFloatFromXml(node.attribute("distance"));
return result; return result;
} }