-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
316 lines (269 loc) · 8.03 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
click to show list of launches from that location
*/
d3.selection.prototype.moveToFront = function() {
return this.each(function() {
this.parentNode.appendChild(this);
});
};
var countries = [];
//initialize this once so we don't have to build it every time
var dates = [];
//this is to remove periods, which we need to do for IDs
var noPer = /[\s\.]/g;
function init(){
var buildDatePromise = buildDateList();
buildDatePromise.done(
function(){
drawMapAndPoints();
}
);
}
function datePickerChanged(){
var dateIn = new Date($('input[id=datepicker]').val());
//getMonth is zero based, life is not
var month = dateIn.getMonth() + 1;
month = month.toString().length == 1 ? '0' + month.toString() : month.toString();
var day = dateIn.getDate().toString().length == 1 ? '0' + dateIn.getDate().toString() : dateIn.getDate().toString();
var mathDate = Number(dateIn.getFullYear().toString() + month + day);
var launchBirthdayPromise = returnLaunchInfoByBirthday(mathDate);
launchBirthdayPromise.done(
function(result){
writeSpaceInfoOut(mathDate, result);
}
);
}
function writeSpaceInfoOut(date, data){
var container = d3.selectAll("#birthdayInfo");
container.selectAll("*").remove();
if (data.difference === 0){
container.append("h3")
.text("You have a birthday spacecraft!");
}
else if (data.difference < 365) {
var priority = "before";
if (date < data.date){
priority = "after";
}
var plural = data.difference > 1 ? "s " : " ";
container.append("h3")
.text("You don't have a birthday spacecraft, but the closest was launched " + data.difference + " day" + plural + priority + " your birthday!");
}
if (data.difference < 365) {
for (var i = 0; i < data.launchInfo.length; i++) {
//print info
var launchDate = data.launchInfo[i].LaunchDate;
var dateStr = launchDate.substring(5, 7) + "/" + launchDate.substring(8, 10) + "/" + launchDate.substring(0, 4);
container.append("p")
.text(
data.launchInfo[i].Name + " (" + data.launchInfo[i].NSSDC + ") was launched from " +
data.launchInfo[i].LaunchLoc + ", " + data.launchInfo[i].LaunchCountry +
" on " + dateStr + " "
).append("a")
.attr({
"href" : "http://nssdc.gsfc.nasa.gov" + data.launchInfo[i].URL
})
.text("(More Info)");
//highlight launch site
var map = d3.select("#map");
map.selectAll("circle")
.attr({
"fill" : "#3D89C4"
});
var match = _.where(countries, {location: data.launchInfo[i].LaunchLoc});
if (match[0].latitude !== null){
var circleMatch = d3.select("#x" + match[0].latitude.toString().replace(noPer, "x") + "x" + match[0].longitude.toString().replace(noPer, "x"))
.moveToFront();
circleMatch.transition()
.duration(1000)
.ease("linear")
.attr({
fill : "yellow"
});
}
}
}
}
//birthday needs to be YYYYMMDD
//this quick searches through the loop for a match, finds the closest
//will return launch data
function returnLaunchInfoByBirthday(birthday){
var def = $.Deferred();
var minDate = Math.min.apply(null, dates);
var maxDate = Math.max.apply(null, dates);
var closestDate;
var result = {date: 0, difference: 365};
if (birthday < minDate){
result.date = minDate;
result.difference = Math.abs(minDate - birthday);
}
else if (birthday > maxDate){
result.date = maxDate;
result.difference = Math.abs(maxDate - birthday);
}
else {
for (var i = 0; i < dates.length; i++) {
var diff = Math.abs(dates[i] - birthday);
if (diff < result.difference){
result.date = dates[i];
result.difference = diff;
if (diff===0){ //if you find an exact match, stop looking
break;
}
}
}
}
//we don't want to do this search unless the difference isn't ridiculous
if (result.difference < 365){
//date is setup with diff above, get whatever the closest was
var launchByDatePromise = returnLaunchInfoByDate(result.date);
launchByDatePromise.done(
function(resultIn){
result.launchInfo = resultIn;
def.resolve(result);
}
);
}
else {
def.resolve(result);
}
return def.promise();
}
//dateIn needs to be YYYYMMDD
//this assumes that the dateIn will have launch info
function returnLaunchInfoByDate(dateIn){
var def = $.Deferred();
var launchInfoOut = [];
var spaceCraft = new Miso.Dataset({
url: "data/spacecraft.csv",
delimiter: ","
});
_.when(spaceCraft.fetch({
success: function(){
this.each(function(row){
var dateNum = Number(row.LaunchDate.replace(/-/g, ""));
if( dateNum == dateIn){
launchInfoOut.push(row);
}
});
}
})).then(function(){
def.resolve(launchInfoOut);
});
return def.promise();
}
function getLocationFromCoord(long, lat){
var match = _.where(countries, {longitude: long, latitude: lat});
var locOut = match[0].location;
locOut = locOut + " (" + _.pluck(match, 'country').toString() + ")";
return locOut;
}
function buildDateList(){
var def = $.Deferred();
var spaceCraft = new Miso.Dataset({
url: "data/spacecraft.csv",
delimiter: ","
});
_.when(spaceCraft.fetch({
success: function(){
this.each(function(row){
if (row.LaunchDate != '0000-00-00'){
dates.push((row.LaunchDate).toString());
}
});
}
})).then(function(){
//make comparisons easier with a purely numeric date
dates = _.uniq(dates.map(function(d){return Number(d.replace(/-/g, ""));}));
def.resolve();
});
return def.promise();
}
//just drawing the map with geojson
function drawMap(map, path){
var def = $.Deferred();
d3.json("data/map.geojson", function(collection) {
map.selectAll("path")
.data(collection.features)
.enter()
.append("path")
.attr({
"d" : path,
fill : "#C8C8C8",
stroke : "#000000",
id : function(d) {return d.id;},
"title" : function(d) {return (d.properties.name);}
});
def.resolve();
});
return def.promise();
}
//includes drawing the map and the points on top, with shading for countries with launch sites
function drawMapAndPoints(){
var mapWidth = 960;
var mapHeight = 600;
var proj = d3.geo.winkel3().scale(182).translate([mapWidth / 2, mapHeight / 2]);
var path = d3.geo.path().projection(proj);
// Create the states variable
var map = d3.select("#map")
.attr({
width : mapWidth,
height: mapHeight,
style : 'display:block; margin:auto;'
})
.append("g");
var drawPromise = drawMap(map, path);
drawPromise.done(
function(){
var countriesDS = new Miso.Dataset({
url: "data/locations.csv",
delimiter: ","
});
_.when(countriesDS.fetch({
success: function(){
this.each(function(row){
var countryInfo = {};
countryInfo.modernCountryCode = row.modernCountryCode;
countryInfo.longitude = row.longitude;
countryInfo.latitude = row.latitude;
countryInfo.location = row.location;
countryInfo.country = row.country;
countries.push(countryInfo);
});
}
})).then(function(){
//the shorthand doesn't work
var uniqCC = _.uniq(_.pluck(countries, 'modernCountryCode'));
//Get just the longitude and latitude from the countries, then return them uniquely by concatinating them together
var uniqLongLat = _.uniq(countries.map(function(d){return [d.longitude, d.latitude];}), function(d){return d[0]+" " + d[1];});
for (var i = 0; i < uniqCC.length; i++) {
d3.select("#" + uniqCC[i])
.transition()
.duration(1000)
.ease("linear")
.attr({
fill: '#666666'
});
}
var circlesAppend = map.selectAll("circle")
.data(uniqLongLat)
.enter();
circlesAppend.append("svg:circle")
.filter(function(d){ return d[1] !== null; })
.attr({
"transform" : function(d) {return "translate(" + proj([d[1],d[0]]) + ")";},
"r" : function(d) { return 4; },
fill : '#3D89C4',
stroke : "#000000",
//ids cannot start with a number, and cannot have periods
"id" : function(d) {
if (d[1] !== null && d[0] !== null){
return "x" + d[1].toString().replace(noPer, "x") + "x" + d[0].toString().replace(noPer, "x") ;
}
},
"title" : function(d) { return getLocationFromCoord(d[0],d[1]) ; }
});
});
}
);
}