57 lines
2.2 KiB
C++
57 lines
2.2 KiB
C++
// MIT License
|
|
// Copyright (c) 2026 by Peter Siegmund (mars3142)
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
// SOFTWARE.
|
|
|
|
#include "cartridge/lat_lng.h"
|
|
|
|
#include <cmath>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
|
|
namespace cartridge {
|
|
|
|
LatLng::LatLng(const double latitude, const double longitude)
|
|
: m_latitude(latitude), m_longitude(longitude) {}
|
|
|
|
std::string LatLng::latitude() const { return _format(m_latitude, "NS"); }
|
|
|
|
std::string LatLng::longitude() const { return _format(m_longitude, "EW"); }
|
|
|
|
std::string LatLng::toString() const {
|
|
const std::string lat = latitude();
|
|
const std::string lon = longitude();
|
|
if (!lat.empty() && !lon.empty()) {
|
|
return lat + " " + lon;
|
|
}
|
|
return lat + lon;
|
|
}
|
|
|
|
std::string LatLng::_format(double value, const std::string &suffix) {
|
|
const bool isNegative = value < 0;
|
|
value = std::abs(value);
|
|
const int degrees = static_cast<int>(std::floor(value));
|
|
if (degrees == 360) {
|
|
return "";
|
|
}
|
|
const double minutes = (value - degrees) * 60.0;
|
|
std::ostringstream oss;
|
|
oss << (isNegative ? suffix.substr(1, 1) : suffix.substr(0, 1)) << " "
|
|
<< degrees << "\u00B0 " << std::fixed << std::setprecision(3) << minutes;
|
|
return oss.str();
|
|
}
|
|
} // namespace cartridge
|