-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (139 loc) · 4.92 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var timeFormat = d3.time.format("%H:%M:%S");
var dateFormat = d3.time.format("%Y-%m-%d");
function init(){
// hamburg
var currentLocation = {lng: 9.6476449, lat: 53.5582447};
// var now = new Date(2018, 6, 5);
// console.log(getLocationObject(now, currentLocation));
// return;
// amrum
// var currentLocation = {lng: 8.336389, lat: 54.651667};
// rio
// currentLocation = {lat: -22.908333,lng: -43.196389};
// pitcairn
// currentLocation = {lat: -24.362180,lng: -128.377562};
// nuuk
// currentLocation = {lat: 64.204170,lng: -51.278792};
// alaska
// currentLocation = {lat: 68.550344,lng: -66.576181};
// var currentLocation = {lat: -22.908333, lng: -43.196389};
// console.log(SunCalc.getTimes(new Date('2018-01-01 12:00'), currentLocation.lat, currentLocation.lng));
// console.log(SunCalc.getPosition(new Date('2018-01-01 12:00'), currentLocation.lat, currentLocation.lng));
// console.log(SunCalc.getTimes(new Date('2018-08-01 12:00'), currentLocation.lat, currentLocation.lng));
// console.log(SunCalc.getPosition(new Date('2018-08-01 12:00'), currentLocation.lat, currentLocation.lng));
// get list of the sun-time for the complete year
var times = getList(currentLocation);
var chart = initGraph();
drawGraph(times, chart);
var map = initMap(currentLocation);
map.on("moveend",function(e){
var c = map.getCenter().wrap();
updateGraph(c, chart);
});
return;
}
function updateGraph(center, chart) {
var times = getList(center);
drawGraph(times, chart);
}
/**
* Init the map.
* @param {[type]} center [description]
* @return {[type]} [description]
*/
function initMap(center){
var map = L.map('map', {
// maxBounds: L.latLngBounds([-90, -180], [90, 180])
}).setView([center.lat, center.lng], 5);
L.tileLayer('//maps.vesseltracker.com/vesseltracker/{z}/{x}/{y}.png', {
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>',
maxZoom: 18,
// noWrap: true
}).addTo(map);
return map;
}
/**
* Return the date at the start of the yearSet date to start of Year
* @param {[type]} date [description]
*/
function getFirstDayOfYear(date){
date.setMonth(0);
date.setDate(1);
date.setHours(12, 0, 0, 0);
return date;
}
/**
* Calcualte all times for a given date and location
* @param {[type]} date [description]
* @param {[type]} location [description]
* @return {[type]} [description]
*/
function getLocationObject(date, location) {
// calculate the last day's duration
var sunTimes = SunCalc.getTimes(date, location.lat, location.lng);
var duration = sunTimes.sunset.getTime() - sunTimes.sunrise.getTime();
var altitude = SunCalc.getPosition(sunTimes.solarNoon, location.lat, location.lng).altitude;
var noon = new Date(date)
noon.setUTCHours(12,0,0,0);
var sunriseTimeOfDay = 12 + (sunTimes.sunrise - noon)/1000/3600;
var sunsetTimeOfDay = 12 + (sunTimes.sunset - noon)/1000/3600;
if (sunTimes.sunset.toString() === "Invalid Date") {
console.log(noon, sunTimes.sunrise, sunTimes.sunset);
}
return {
sunrise: sunTimes.sunrise,
sunriseTimeOfDay: sunriseTimeOfDay,
noSunrise: sunTimes.sunrise.toString() === "Invalid Date",
sunset: sunTimes.sunset,
sunsetTimeOfDay: sunsetTimeOfDay,
noSunset: sunTimes.sunset.toString() === "Invalid Date",
noon: sunTimes.solarNoon,
date: new Date(date),
duration: duration,
altitude: altitude
}
}
/**
* Calculate the offset of the sunrise relative to the the previous sunrise
* @param {[type]} times [description]
* @return {[type]} [description]
*/
function calculateDeltas(times){
var previous = times.previous;
var delta = 0;
for (var i = 0; i < times.data.length; i++) {
var current = times.data[i];
delta += (current.sunrise - previous.sunrise);
delta -= 24 * 3600 * 1000; // subtract 24h
times.data[i].delta = delta;
previous = current;
}
return times;
}
/**
* Calculate the complete list of of sunsetsunrise objects for a given location
* @param {[type]} currentLocation [description]
* @return {[type]} [description]
*/
function getList(location){
var data = [];
var now = new Date();
var firstDayOfYear = getFirstDayOfYear(now);
var year = now.getFullYear();
// calculate the previous day's data
var prevDate = new Date(firstDayOfYear);
prevDate.setDate(prevDate.getDate() - 1);
var previousTimes = getLocationObject(prevDate, location);
var currentDate = new Date(firstDayOfYear);
while (currentDate.getFullYear() == year){
var currentTimes = getLocationObject(currentDate, location);
data.push(currentTimes);
currentDate.setDate(currentDate.getDate()+1);
}
var nextTimes = getLocationObject(currentDate, location);
return {
previous: previousTimes,
next: nextTimes,
data: data
};
}