File indexing completed on 2024-05-05 03:49:16

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
0004 // SPDX-FileCopyrightText: 2021 Torsten Rahn
0005 //
0006 
0007 #include "Coordinate.h"
0008 
0009 #include "MarbleGlobal.h"
0010 
0011 using Marble::GeoDataCoordinates;
0012 using Marble::EARTH_RADIUS;
0013 using Marble::DEG2RAD;
0014 
0015 Coordinate::Coordinate( qreal lon, qreal lat, qreal alt, QObject *parent ) :
0016     QObject( parent )
0017 {
0018     setLongitude( lon );
0019     setLatitude( lat );
0020     setAltitude( alt );
0021 }
0022 
0023 Coordinate::Coordinate(const Marble::GeoDataCoordinates &coordinates)
0024 {
0025     setCoordinates(coordinates);
0026 }
0027 
0028 qreal Coordinate::longitude() const
0029 {
0030     return m_coordinate.longitude( GeoDataCoordinates::Degree );
0031 }
0032 
0033 void Coordinate::setLongitude( qreal lon )
0034 {
0035     m_coordinate.setLongitude( lon, GeoDataCoordinates::Degree );
0036     emit longitudeChanged();
0037 }
0038 
0039 qreal Coordinate::latitude() const
0040 {
0041     return m_coordinate.latitude( GeoDataCoordinates::Degree );
0042 }
0043 
0044 void Coordinate::setLatitude( qreal lat )
0045 {
0046     m_coordinate.setLatitude( lat, GeoDataCoordinates::Degree );
0047     emit latitudeChanged();
0048 }
0049 
0050 qreal Coordinate::altitude() const
0051 {
0052     return m_coordinate.altitude();
0053 }
0054 
0055 void Coordinate::setAltitude( qreal alt )
0056 {
0057     m_coordinate.setAltitude( alt );
0058     emit altitudeChanged();
0059 }
0060 
0061 GeoDataCoordinates Coordinate::coordinates() const
0062 {
0063     return m_coordinate;
0064 }
0065 
0066 void Coordinate::setCoordinates( const GeoDataCoordinates &coordinates )
0067 {
0068     m_coordinate = coordinates;
0069 }
0070 
0071 QString Coordinate::toGeoString(Coordinate::Notation notation, int precision) const
0072 {
0073     return m_coordinate.toString(static_cast<GeoDataCoordinates::Notation>(notation), precision);
0074 }
0075 
0076 qreal Coordinate::distance( qreal longitude, qreal latitude ) const
0077 {
0078     GeoDataCoordinates::Unit deg = GeoDataCoordinates::Degree;
0079     GeoDataCoordinates other( longitude, latitude, 0, deg );
0080     return EARTH_RADIUS * coordinates().sphericalDistanceTo(other);
0081 }
0082 
0083 qreal Coordinate::bearing( qreal longitude, qreal latitude ) const
0084 {
0085     qreal deltaLon = longitude * DEG2RAD - m_coordinate.longitude();
0086     qreal y = sin( deltaLon ) * cos( latitude * DEG2RAD );
0087     qreal x = cos( m_coordinate.latitude() ) * sin( latitude * DEG2RAD ) -
0088               sin( m_coordinate.latitude() ) * cos( latitude * DEG2RAD ) * cos( deltaLon );
0089     return Marble::RAD2DEG * atan2( y, x );
0090 }
0091 
0092 bool Coordinate::operator == ( const Coordinate &other ) const
0093 {
0094     return m_coordinate == other.m_coordinate;
0095 }
0096 
0097 bool Coordinate::operator != ( const Coordinate &other ) const
0098 {
0099     return !operator == ( other );
0100 }
0101 
0102 Coordinate::Notation Coordinate::defaultNotation()
0103 {
0104     return static_cast<Coordinate::Notation>(GeoDataCoordinates::defaultNotation());
0105 }
0106 
0107 void Coordinate::setDefaultNotation(Coordinate::Notation defaultNotation)
0108 {
0109     if (GeoDataCoordinates::defaultNotation() == static_cast<GeoDataCoordinates::Notation>(defaultNotation))
0110         return;
0111     GeoDataCoordinates::setDefaultNotation(static_cast<GeoDataCoordinates::Notation>(defaultNotation));
0112     emit defaultNotationChanged(defaultNotation);
0113 }
0114 
0115 #include "moc_Coordinate.cpp"