diff --git a/seerep_hdf5/seerep_hdf5_core/include/seerep_hdf5_core/hdf5_core_general.h b/seerep_hdf5/seerep_hdf5_core/include/seerep_hdf5_core/hdf5_core_general.h index 683bd52cc..1424f5b59 100644 --- a/seerep_hdf5/seerep_hdf5_core/include/seerep_hdf5_core/hdf5_core_general.h +++ b/seerep_hdf5/seerep_hdf5_core/include/seerep_hdf5_core/hdf5_core_general.h @@ -195,19 +195,21 @@ class Hdf5CoreGeneral void writeVersion(const std::string& version); const std::optional readVersion(); - // ################ - // Geodetic Coordinates - // ################ /** - * @brief Writes Geodetic location of the project to its HDF5 file. + * @brief Writes a geodetic coordinate to an HDF5 file. + * + * Note: The data is written to the root group of the HDF5 file, since the geo-reference applies to complete project. * - * @param geocoords seerep_core_msgs Geodetic Coordinates object. + * @param geo_coordinates The geodetic coordinate to write to the file. */ - void writeGeodeticLocation(const seerep_core_msgs::GeodeticCoordinates geocoords); + void writeGeodeticLocation(const seerep_core_msgs::GeodeticCoordinates& geo_coordinates); + /** - * @brief Read Geodetic location of the project from its HDF5 file. + * @brief Reads a geodetic coodrinate from HDF5 file. + * + * Note: The data is expected to be stored in the root group of the HDF5 file. * - * @return std::optional Optional return of Geodetic Coordinates + * @return std::optional Geodetic coordinate if available, empty optional otherwise. */ std::optional readGeodeticLocation(); diff --git a/seerep_hdf5/seerep_hdf5_core/src/hdf5_core_general.cpp b/seerep_hdf5/seerep_hdf5_core/src/hdf5_core_general.cpp index da214f953..8276c2dd0 100644 --- a/seerep_hdf5/seerep_hdf5_core/src/hdf5_core_general.cpp +++ b/seerep_hdf5/seerep_hdf5_core/src/hdf5_core_general.cpp @@ -393,45 +393,30 @@ std::shared_ptr Hdf5CoreGeneral::getHdf5Group(const std::string return nullptr; } -void Hdf5CoreGeneral::writeGeodeticLocation(const seerep_core_msgs::GeodeticCoordinates geocoords) +void Hdf5CoreGeneral::writeGeodeticLocation(const seerep_core_msgs::GeodeticCoordinates& geo_coordinates) { - const std::scoped_lock lock(*m_write_mtx); - - if (!m_file->hasAttribute(GEODETICLOCATION_COORDINATESYSTEM) || !m_file->hasAttribute(GEODETICLOCATION_ELLIPSOID) || - !m_file->hasAttribute(GEODETICLOCATION_ALTITUDE) || !m_file->hasAttribute(GEODETICLOCATION_LATITUDE) || - !m_file->hasAttribute(GEODETICLOCATION_LONGITUDE)) - { - m_file->createAttribute(GEODETICLOCATION_COORDINATESYSTEM, geocoords.coordinateSystem); - m_file->createAttribute(GEODETICLOCATION_ALTITUDE, geocoords.altitude); - m_file->createAttribute(GEODETICLOCATION_LATITUDE, geocoords.latitude); - m_file->createAttribute(GEODETICLOCATION_LONGITUDE, geocoords.longitude); - } - else - { - m_file->getAttribute(GEODETICLOCATION_COORDINATESYSTEM).write(geocoords.coordinateSystem); - m_file->getAttribute(GEODETICLOCATION_ALTITUDE).write(geocoords.altitude); - m_file->getAttribute(GEODETICLOCATION_LATITUDE).write(geocoords.latitude); - m_file->getAttribute(GEODETICLOCATION_LONGITUDE).write(geocoords.longitude); - } + writeAttributeToHdf5(*m_file, GEODETICLOCATION_COORDINATESYSTEM, geo_coordinates.coordinateSystem); + writeAttributeToHdf5(*m_file, GEODETICLOCATION_ALTITUDE, geo_coordinates.altitude); + writeAttributeToHdf5(*m_file, GEODETICLOCATION_LATITUDE, geo_coordinates.latitude); + writeAttributeToHdf5(*m_file, GEODETICLOCATION_LONGITUDE, geo_coordinates.longitude); m_file->flush(); } std::optional Hdf5CoreGeneral::readGeodeticLocation() { - const std::scoped_lock lock(*m_write_mtx); - seerep_core_msgs::GeodeticCoordinates geocoords; try { - m_file->getAttribute(GEODETICLOCATION_COORDINATESYSTEM).read(geocoords.coordinateSystem); - m_file->getAttribute(GEODETICLOCATION_ALTITUDE).read(geocoords.altitude); - m_file->getAttribute(GEODETICLOCATION_LATITUDE).read(geocoords.latitude); - m_file->getAttribute(GEODETICLOCATION_LONGITUDE).read(geocoords.longitude); + geocoords.coordinateSystem = + readAttributeFromHdf5(*m_file, GEODETICLOCATION_COORDINATESYSTEM, m_file->getName()); + geocoords.altitude = readAttributeFromHdf5(*m_file, GEODETICLOCATION_ALTITUDE, m_file->getName()); + geocoords.latitude = readAttributeFromHdf5(*m_file, GEODETICLOCATION_LATITUDE, m_file->getName()); + geocoords.longitude = readAttributeFromHdf5(*m_file, GEODETICLOCATION_LONGITUDE, m_file->getName()); } - catch (...) + catch (const std::invalid_argument& e) { BOOST_LOG_SEV(m_logger, boost::log::trivial::severity_level::warning) - << "geographic coordinates of the project " << m_file->getName() << " could not be read!"; + << "Geographic coordinates of the project " << m_file->getName() << " could not be read!"; return std::nullopt; } return geocoords;