Geographical position and calculation using latitude/longitude. Most of this comes from http://www.movable-type.co.uk/scripts/latlong.html

Dependents:   Senet NAMote

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers GeoPosition.cpp Source File

GeoPosition.cpp

00001 #include "GeoPosition.h"
00002 #include <math.h>
00003 
00004 // Earth's mean radius in meters
00005 #define EARTHRADIUS 6371000.0
00006 
00007 GeoPosition::GeoPosition(): _R(EARTHRADIUS), _latitude(0.0), _longitude(0.0)
00008 {
00009 }
00010 
00011 GeoPosition::GeoPosition(double latitude, double longitude): _R(EARTHRADIUS), _latitude(latitude), _longitude(longitude)
00012 {
00013 }
00014     
00015 /*
00016 double GeoPosition::easting()
00017 {
00018 }
00019 
00020 double GeoPosition::northing()
00021 {
00022 }
00023 */
00024 
00025 double GeoPosition::latitude()
00026 {
00027     return _latitude;
00028 }
00029 
00030 double GeoPosition::longitude()
00031 {
00032     return _longitude;
00033 }
00034 
00035 void GeoPosition::set(double latitude, double longitude)
00036 {
00037     _latitude = latitude;
00038     _longitude = longitude;
00039 }
00040 
00041 void GeoPosition::set(GeoPosition pos)
00042 {
00043     _latitude = pos.latitude();
00044     _longitude = pos.longitude();
00045 }
00046 
00047 /*
00048 void GeoPosition::set(UTM coord)
00049 {
00050 }
00051 */
00052 
00053 void GeoPosition::move(float course, float distance)
00054 {
00055     double d = distance / _R;
00056     double c = radians(course);
00057     double rlat1 = radians(_latitude);
00058     double rlon1 = radians(_longitude);
00059 
00060     double rlat2 = asin(sin(rlat1)*cos(d) + cos(rlat1)*sin(d)*cos(c));
00061     double rlon2 = rlon1 + atan2(sin(c)*sin(d)*cos(rlat1), cos(d)-sin(rlat1)*sin(rlat2));
00062 
00063     _latitude  = degrees(rlat2);
00064     _longitude = degrees(rlon2);
00065 
00066     // bring back within the range -180 to +180
00067     while (_longitude < -180.0) _longitude += 360.0;
00068     while (_longitude > 180.0) _longitude -= 360.0;
00069 }
00070 
00071 /*
00072 void GeoPosition::move(Direction toWhere)
00073 {
00074 }
00075 
00076 Direction GeoPosition::direction(GeoPosition startingPoint)
00077 {
00078 }
00079 */
00080 
00081 float GeoPosition::bearing(GeoPosition from)
00082 {
00083     double lat1 = radians(from.latitude());
00084     double lon1 = radians(from.longitude());
00085     double lat2 = radians(_latitude);
00086     double lon2 = radians(_longitude);
00087     double dLon = lon2 - lon1;
00088 
00089     double y = sin(dLon) * cos(lat2);
00090     double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
00091 
00092     return degrees(atan2(y, x)); 
00093 }
00094 
00095 /*
00096 float GeoPosition::bearing(LatLong startingPoint)
00097 {
00098 }
00099 
00100 float GeoPosition::bearing(UTM startingPoint)
00101 {
00102 }
00103 */
00104 
00105 float GeoPosition::distance(GeoPosition from)
00106 {
00107     double lat1 = radians(from.latitude());
00108     double lon1 = radians(from.longitude());
00109     double lat2 = radians(_latitude);
00110     double lon2 = radians(_longitude);
00111     double dLat = lat2 - lat1;
00112     double dLon = lon2 - lon1;
00113 
00114     double a = sin(dLat/2.0) * sin(dLat/2.0) + 
00115                cos(lat1) * cos(lat2) *
00116                sin(dLon/2.0) * sin(dLon/2.0);
00117     double c = 2.0 * atan2(sqrt(a), sqrt(1-a));
00118     
00119     return _R * c;
00120 }
00121 
00122 /*
00123 float GeoPosition::distance(LatLong startingPoint)
00124 {
00125 }
00126 
00127 float GeoPosition::distance(UTM startingPoint)
00128 {
00129 }
00130 */
00131 
00132 void GeoPosition::setTimestamp(int time) {
00133     _time = time;
00134 }
00135 
00136 int GeoPosition::getTimestamp(void) {
00137     return _time;
00138 }
00139 
00140 
00141