|
| 1 | +// Copyright 2023 TIER IV, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <GeographicLib/Geoid.hpp> |
| 16 | +#include <autoware/geography_utils/height.hpp> |
| 17 | + |
| 18 | +#include <map> |
| 19 | +#include <stdexcept> |
| 20 | +#include <string> |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +namespace autoware::geography_utils |
| 24 | +{ |
| 25 | + |
| 26 | +double convert_wgs84_to_egm2008(const double height, const double latitude, const double longitude) |
| 27 | +{ |
| 28 | + GeographicLib::Geoid egm2008("egm2008-1"); |
| 29 | + // cSpell: ignore ELLIPSOIDTOGEOID |
| 30 | + return egm2008.ConvertHeight(latitude, longitude, height, GeographicLib::Geoid::ELLIPSOIDTOGEOID); |
| 31 | +} |
| 32 | + |
| 33 | +double convert_egm2008_to_wgs84(const double height, const double latitude, const double longitude) |
| 34 | +{ |
| 35 | + GeographicLib::Geoid egm2008("egm2008-1"); |
| 36 | + // cSpell: ignore GEOIDTOELLIPSOID |
| 37 | + return egm2008.ConvertHeight(latitude, longitude, height, GeographicLib::Geoid::GEOIDTOELLIPSOID); |
| 38 | +} |
| 39 | + |
| 40 | +double convert_height( |
| 41 | + const double height, const double latitude, const double longitude, |
| 42 | + const std::string & source_vertical_datum, const std::string & target_vertical_datum) |
| 43 | +{ |
| 44 | + if (source_vertical_datum == target_vertical_datum) { |
| 45 | + return height; |
| 46 | + } |
| 47 | + std::map<std::pair<std::string, std::string>, HeightConversionFunction> conversion_map; |
| 48 | + conversion_map[{"WGS84", "EGM2008"}] = convert_wgs84_to_egm2008; |
| 49 | + conversion_map[{"EGM2008", "WGS84"}] = convert_egm2008_to_wgs84; |
| 50 | + |
| 51 | + auto key = std::make_pair(source_vertical_datum, target_vertical_datum); |
| 52 | + if (conversion_map.find(key) != conversion_map.end()) { |
| 53 | + return conversion_map[key](height, latitude, longitude); |
| 54 | + } else { |
| 55 | + std::string error_message = |
| 56 | + "Invalid conversion types: " + std::string(source_vertical_datum.c_str()) + " to " + |
| 57 | + std::string(target_vertical_datum.c_str()); |
| 58 | + |
| 59 | + throw std::invalid_argument(error_message); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +} // namespace autoware::geography_utils |
0 commit comments