constellations.cpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 // SPDX-FileCopyrightText: Czech Technical University in Prague
3 
4 #include <string>
5 #include <utility>
6 
7 #include <gnsstk/SatID.hpp>
8 
12 #include <gnss_info_msgs/Enums.h>
13 #include <gnss_info_msgs/SatelliteInfo.h>
14 
15 namespace gnsstk_ros
16 {
17 
18 cras::optional<std::string> getRosConstellationFromPRN(const std::string& prn)
19 {
20  if (prn.empty())
21  return cras::nullopt;
22 
23  switch (prn[0])
24  {
25  case 'G':
26  return gnss_info_msgs::Enums::CONSTELLATION_GPS;
27  case 'R':
28  return gnss_info_msgs::Enums::CONSTELLATION_GLONASS;
29  case 'E':
30  return gnss_info_msgs::Enums::CONSTELLATION_GALILEO;
31  case 'C':
32  return gnss_info_msgs::Enums::CONSTELLATION_BEIDOU;
33  case 'J':
34  return gnss_info_msgs::Enums::CONSTELLATION_QZSS;
35  case 'I':
36  return gnss_info_msgs::Enums::CONSTELLATION_NAVIC;
37  default:
38  return cras::nullopt;
39  }
40 }
41 
42 cras::optional<std::string> getRosConstellationFromSVN(const std::string& svn)
43 {
44  // The implementation is actually the same.
45  return getRosConstellationFromPRN(svn);
46 }
47 
48 cras::optional<std::pair<int32_t, std::string>> prnStringToInt(const std::string& prn)
49 {
50  const auto constellation = getRosConstellationFromPRN(prn);
51  if (!constellation.has_value())
52  return cras::nullopt;
53 
54  auto prnString = prn.substr(1);
55 
56  // Get rid of leading zeros, they would trick parseInt32 into parsing as octal
57  while (!prnString.empty() && prnString[0] == '0')
58  prnString = prnString.substr(1);
59  if (prnString.empty())
60  return cras::nullopt;
61 
62  try
63  {
64  return std::make_pair(cras::parseInt32(prnString), *constellation);
65  }
66  catch (const std::invalid_argument&) // integer parsing failed
67  {
68  return cras::nullopt;
69  }
70 }
71 
72 cras::optional<std::string> prnIntToString(const int32_t prn, const std::string& constellation)
73 {
74  std::string prefix;
75 
76  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_GPS)
77  prefix = "G";
78  else if (constellation == gnss_info_msgs::Enums::CONSTELLATION_GLONASS)
79  prefix = "R";
80  else if (constellation == gnss_info_msgs::Enums::CONSTELLATION_GALILEO)
81  prefix = "E";
82  else if (constellation == gnss_info_msgs::Enums::CONSTELLATION_BEIDOU)
83  prefix = "C";
84  else if (constellation == gnss_info_msgs::Enums::CONSTELLATION_QZSS)
85  prefix = "J";
86  else if (constellation == gnss_info_msgs::Enums::CONSTELLATION_NAVIC)
87  prefix = "I";
88  else
89  return cras::nullopt;
90 
91  return prefix + std::to_string(prn);
92 }
93 
94 cras::optional<gnsstk::SatelliteSystem> rosConstellationToGnsstkSatelliteSystem(const std::string& constellation)
95 {
96  if (constellation.empty())
98  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_GPS)
100  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_GALILEO)
102  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_GLONASS)
104  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_BEIDOU)
106  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_NAVIC)
108  if (constellation == gnss_info_msgs::Enums::CONSTELLATION_QZSS)
111 }
112 
113 cras::optional<std::string> gnsstkSatelliteSystemRosConstellation(const gnsstk::SatelliteSystem& constellation)
114 {
115  switch (constellation)
116  {
118  return gnss_info_msgs::Enums::CONSTELLATION_GPS;
120  return gnss_info_msgs::Enums::CONSTELLATION_GALILEO;
122  return gnss_info_msgs::Enums::CONSTELLATION_GLONASS;
124  return gnss_info_msgs::Enums::CONSTELLATION_BEIDOU;
126  return gnss_info_msgs::Enums::CONSTELLATION_NAVIC;
128  return gnss_info_msgs::Enums::CONSTELLATION_QZSS;
129  default:
130  return cras::nullopt;
131  }
132 }
133 
134 cras::optional<gnsstk::SatID> satelliteInfoToSatID(const gnss_info_msgs::SatelliteInfo& info)
135 {
136  const auto maybeConstellationAndPrn = prnStringToInt(info.prn);
137  if (!maybeConstellationAndPrn.has_value())
138  return cras::nullopt;
139 
140  const auto& [prn, constellationStr] = *maybeConstellationAndPrn;
141 
142  auto maybeSatelliteSystem = rosConstellationToGnsstkSatelliteSystem(constellationStr);
143  if (!maybeSatelliteSystem.has_value())
144  maybeSatelliteSystem = rosConstellationToGnsstkSatelliteSystem(info.constellation);
145  if (!maybeSatelliteSystem.has_value())
146  return cras::nullopt;
147 
148  const auto system = *maybeSatelliteSystem;
149  return gnsstk::SatID(prn, system);
150 }
151 
152 }
optional.hpp
gnsstk::SatelliteSystem::IRNSS
@ IRNSS
gnsstk_ros
Definition: constellations.h:15
gnsstk_ros::satelliteInfoToSatID
cras::optional< gnsstk::SatID > satelliteInfoToSatID(const gnss_info_msgs::SatelliteInfo &info)
Convert the given satellite info to non-wildcard gnsstk SatID.
Definition: constellations.cpp:134
gnsstk::SatelliteSystem
SatelliteSystem
gnsstk::SatID
gnsstk_ros::prnStringToInt
cras::optional< std::pair< int32_t, std::string > > prnStringToInt(const std::string &prn)
Convert the given PRN string like E03 to integer like 3 and constellation name.
Definition: constellations.cpp:48
gnsstk_ros::gnsstkSatelliteSystemRosConstellation
cras::optional< std::string > gnsstkSatelliteSystemRosConstellation(const gnsstk::SatelliteSystem &constellation)
Convert the gnsstk constellation enum to gnss_info_msgs constellation string.
Definition: constellations.cpp:113
gnsstk::SatelliteSystem::GPS
@ GPS
string_utils.hpp
gnsstk::SatelliteSystem::Unknown
@ Unknown
constellations.h
gnsstk_ros::getRosConstellationFromPRN
cras::optional< std::string > getRosConstellationFromPRN(const std::string &prn)
Get constellation name from the given PRN string like E03.
Definition: constellations.cpp:18
gnsstk_ros::prnIntToString
cras::optional< std::string > prnIntToString(int32_t prn, const std::string &constellation)
Convert the given numeric PRN and constellation to string PRN like E03.
Definition: constellations.cpp:72
gnsstk_ros::rosConstellationToGnsstkSatelliteSystem
cras::optional< gnsstk::SatelliteSystem > rosConstellationToGnsstkSatelliteSystem(const std::string &constellation)
Convert the gnss_info_msgs constellation string to gnsstk constellation enum.
Definition: constellations.cpp:94
cras::parseInt32
int32_t parseInt32(const char *string)
gnsstk::SatelliteSystem::Glonass
@ Glonass
gnsstk::SatelliteSystem::BeiDou
@ BeiDou
gnsstk::SatelliteSystem::QZSS
@ QZSS
gnsstk::SatelliteSystem::Galileo
@ Galileo
gnsstk_ros::getRosConstellationFromSVN
cras::optional< std::string > getRosConstellationFromSVN(const std::string &svn)
Get constellation name from the given SVN string like E203.
Definition: constellations.cpp:42


gnsstk_ros
Author(s): Martin Pecka
autogenerated on Fri Nov 24 2023 03:50:23