summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2015-05-08 00:38:37 +0200
committerMatěj Cepl <mcepl@cepl.eu>2015-05-08 00:38:37 +0200
commitaa6dd049071cb2e0b4520881125bcb23ad1e3b13 (patch)
treee9775f6c61d9d627870084cbee4c0bfb48e89e92
parent8bf1b5b2c999d25f5d8a61d8ce03cde46b112b32 (diff)
downloadISS_Above-aa6dd049071cb2e0b4520881125bcb23ad1e3b13.tar.gz
Finally I can calculate distance from ISS.
-rwxr-xr-xiss_get_data.js71
1 files changed, 53 insertions, 18 deletions
diff --git a/iss_get_data.js b/iss_get_data.js
index 9ba7069..0db03c8 100755
--- a/iss_get_data.js
+++ b/iss_get_data.js
@@ -70,7 +70,6 @@ const ISS_Above = new GObject.Class({
'org.freedesktop.GeoClue2', '/org/freedesktop/GeoClue2/Manager');
[this.clientAddr] = this.managerProxy.GetClientSync();
- print('this.clientAddr = ' + this.clientAddr);
this.current_location = {};
this.iss_coords = null;
@@ -96,7 +95,7 @@ const ISS_Above = new GObject.Class({
let iss_api = Gio.file_new_for_uri(API_URL);
iss_api.load_contents_async(null, function(iss_api, result) {
self.iss_coords = JSON.parse(iss_api.load_contents_finish(result)[1]);
- print(self.iss_coords.toSource());
+ // print(self.iss_coords.toSource());
self.emit('processed_data');
});
},
@@ -105,31 +104,67 @@ const ISS_Above = new GObject.Class({
let geoclueLocation = new LocationProxy(Gio.DBus.system,
"org.freedesktop.GeoClue2",
newPath);
- print('location:');
- print('\tlatitude = ' + geoclueLocation.latitude);
- print('\tlongitude = ' + geoclueLocation.longitude);
- print('\taccuracy = ' + geoclueLocation.accuracy);
- print('\tdescription = ' + geoclueLocation.description);
- // this.current_location.latitude = geoclueLocation.latitude;
- // this.current_location.longitude = geoclueLocation.longitude;
- // this.current_location.accuracy = geoclueLocation.accuracy;
- // this.current_location.description = geoclueLocation.description;
+ this.current_location.Latitude = geoclueLocation.Latitude;
+ this.current_location.Longitude = geoclueLocation.Longitude;
+ this.current_location.Accuracy = geoclueLocation.Accuracy;
+ this.current_location.Description = geoclueLocation.Description;
this.emit('processed_data');
},
+ /**
+ * Calculate distance in km of two geographical points
+ *
+ * @param lat1 Number latitude of the first point
+ * @param long1 Number longitude of the first point
+ * @param lat2 Number latitude of the second point
+ * @param long2 Number longitude of the second point
+ * @return Number distance in km
+ *
+ * Described on https://en.wikipedia.org/wiki/Haversine_formula
+ */
+ get_distance: function(lat1, long1, lat2, long2) {
+ // Mean radius of the Earth in km
+ const EARTH_R = 6371;
+ const PI_180 = Math.PI / 180;
+
+ function haversin(fi) {
+ return (1 - Math.cos(fi)) / 2;
+ }
+
+ lat1 = lat1 * PI_180;
+ long1 = long1 * PI_180;
+ lat2 = lat2 * PI_180;
+ long2 = long2 * PI_180;
+
+ let dist = 2 * EARTH_R *
+ Math.asin(Math.sqrt(haversin(lat2 - lat1) +
+ Math.cos(lat1) * Math.cos(lat2) * haversin(long2 - long1)));
+
+ let test_haversin = haversin(dist / EARTH_R);
+ if (test_haversin < 1) {
+ return dist.toFixed(3);
+ }
+ else {
+ throw new Error('haversine(d/r) cannot be over 1, but it is ' +
+ test_haversin);
+ }
+ },
+
processResult: function() {
this.task_queue--;
- print('this.task_queue = ' + this.task_queue);
if (this.task_queue === 0) {
- print('location:');
- print('\tlatitude = ' + this.current_location.latitude);
- print('\tlongitude = ' + this.current_location.longitude);
- print('\taccuracy = ' + this.current_location.accuracy);
- print('\tdescription = ' + this.current_location.description);
- print('\n\niss coordinates: ' + this.iss_coords.toSource());
+ // print('our location: ' + this.current_location.toSource());
+ // print('iss coordinates: ' + this.iss_coords.toSource());
+
+ let dist = this.get_distance(this.current_location.Latitude,
+ this.current_location.Longitude,
+ this.iss_coords.iss_position.latitude,
+ this.iss_coords.iss_position.longitude)
+
+ print('ISS is ' + dist + ' km away.');
Mainloop.quit();
}