Skip to content

Commit

Permalink
refactor read and write of geo-reference in hdf5_core
Browse files Browse the repository at this point in the history
  • Loading branch information
jarkenau authored and Mark-Niemeyer committed Dec 19, 2023
1 parent 18e1c3d commit 759a812
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,21 @@ class Hdf5CoreGeneral
void writeVersion(const std::string& version);
const std::optional<std::string> 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<seerep_core_msgs::GeodeticCoordinates> Optional return of Geodetic Coordinates
* @return std::optional<seerep_core_msgs::GeodeticCoordinates> Geodetic coordinate if available, empty optional otherwise.
*/
std::optional<seerep_core_msgs::GeodeticCoordinates> readGeodeticLocation();

Expand Down
39 changes: 12 additions & 27 deletions seerep_hdf5/seerep_hdf5_core/src/hdf5_core_general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,45 +393,30 @@ std::shared_ptr<HighFive::Group> 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<std::string>(GEODETICLOCATION_COORDINATESYSTEM, geocoords.coordinateSystem);
m_file->createAttribute<double>(GEODETICLOCATION_ALTITUDE, geocoords.altitude);
m_file->createAttribute<double>(GEODETICLOCATION_LATITUDE, geocoords.latitude);
m_file->createAttribute<double>(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<std::string>(*m_file, GEODETICLOCATION_COORDINATESYSTEM, geo_coordinates.coordinateSystem);
writeAttributeToHdf5<double>(*m_file, GEODETICLOCATION_ALTITUDE, geo_coordinates.altitude);
writeAttributeToHdf5<double>(*m_file, GEODETICLOCATION_LATITUDE, geo_coordinates.latitude);
writeAttributeToHdf5<double>(*m_file, GEODETICLOCATION_LONGITUDE, geo_coordinates.longitude);
m_file->flush();
}

std::optional<seerep_core_msgs::GeodeticCoordinates> 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<std::string>(*m_file, GEODETICLOCATION_COORDINATESYSTEM, m_file->getName());
geocoords.altitude = readAttributeFromHdf5<double>(*m_file, GEODETICLOCATION_ALTITUDE, m_file->getName());
geocoords.latitude = readAttributeFromHdf5<double>(*m_file, GEODETICLOCATION_LATITUDE, m_file->getName());
geocoords.longitude = readAttributeFromHdf5<double>(*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;
Expand Down

0 comments on commit 759a812

Please sign in to comment.