-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
271 lines (197 loc) · 6.75 KB
/
index.html
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
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<style>
body {
background-color: #92B4CA;
}
.vcells{
fill: none;
pointer-events: all;
stroke: #000;
stroke-opacity: .2;
}
.vcells:hover {
fill: orange;
fill-opacity:0.4;
stroke: red;
stroke-opacity: 1;
}
line {
stroke: #3558B8;
stroke-width: 2px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<div></div>
<script>
////////////////////////map//////////////////////////
var width = 700;
var height = 500;
var projection = d3.geo.azimuthalEqualArea()
.rotate([100, -45])
.center([23, 6])
.scale(5000)
.translate([width/2, height/2]);
var path = d3.geo.path().projection(projection);
var voronoi = d3.geom.voronoi()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; });
// .clipExtent([[0, 0], [width, height]]);
var map = d3.select("body")
.append("svg")
.attr({
width: width,
height: height,
overflow: "hidden"
});
var colour = d3.scale.category20c();
var tooltip = d3.select('div')
.append('span')
.style({
postion: 'absolute',
padding: '0 10px',
opacity: 0,
font: '15px arial, sans-serif'
});
////////// windmap stuff/////////////////////
var lines = [];
function lineAnimate(selection) {
selection
.attr({
x2: function(d) {return d.x0},
y2: function(d) {return d.y0}
})
.style('opacity', 0)
.transition()
.ease('linear')
.duration(function(d) {return d.duration;})
.delay(function(d) {return d.delay;})
.attr({
x2: function(d) {return d.x1},
y2: function(d) {return d.y1}
})
.style('opacity', 0.8)
.transition()
.duration(1000)
.style('opacity', 0.1)
.each('end', function() {d3.select(this).call(lineAnimate)});
}
//// MATH FUNCTIONS
function toRad(deg) {return deg * Math.PI / 180;}
function toDeg(rad) {return rad * 180 / Math.PI;}
function lonLatFromLonLatDistanceAndBearing(lonLat, d, brng) {
// Formulae from http://www.movable-type.co.uk/scripts/latlong.html
// brg in radians, d in km
var R = 6371; // Earth's radius in km
var lon1 = toRad(lonLat[0]), lat1 = toRad(lonLat[1]);
var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
return [toDeg(lon2), toDeg(lat2)];
}
var cardinalToBearing = {};
queue()
.defer(d3.json,"nb.geojson")
.defer(d3.csv, "weather.csv")
.await(ready);
function ready(error,nb,stations){
var i, cardinalPoints = ['S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE'];
// Calculate cardinal point to bearing mapping (wind direction is where the wind is coming *from*!)
for(i = 0; i < cardinalPoints.length; i++){
cardinalToBearing[cardinalPoints[i]] = i * Math.PI / 8;}
// Prepare line co-ordinates
var windData = stations
for(i = 0; i < windData.length; i++) {
var d = windData[i];
var speed = d.speed;
console.log(speed);
var lonLat0 = [d.latitude, d.longitude];
console.log(lonLat0);
// Scale line length proportionally to speed
var lonLat1 = lonLatFromLonLatDistanceAndBearing(lonLat0, 1.2 * speed, cardinalToBearing[d.direction]);
var x0y0 = projection(lonLat0);
var x1y1 = projection(lonLat1);
var line = {
x0: x0y0[0],
y0: x0y0[1],
x1: x1y1[0],
y1: x1y1[1],
s: speed,
// f: feelsLikeTemperature,
duration: 8000 / speed, /* pre-compute duration */
delay: Math.random() * 1000 /* pre-compute delay */
};
// console.log(line);
lines.push(line);
}
stations = stations.filter(function(d){
d[1] = d.longitude;
d[0] = d.latitude;
var position = projection(d);
d.x = position[0];
d.y = position[1];
return true;
});
voronoi(stations).forEach(function(d) { d.point.cell = d; });
var group = map.selectAll("svg")
.data(nb.features)
.enter();
group.append("path")
.attr("d", path)
.attr("class", "area")
.attr("id",function(d){ return d.properties.NAME;})
.attr("fill", "#CED8B6");
var buildPathFromPoint = function(point) {
return "M" + point.cell.join("L") + "Z"; };
var points = group.append("g")
.attr("class","stations")
.selectAll("g")
.data(stations)
.enter()
.append("g")
.attr("class","station")
.attr("id",function(d){ return d.name;});
points.append("circle")
.attr("cx",function(d){
return projection([d.latitude,d.longitude])[0];
})
.attr("cy",function(d){
return projection([d.latitude,d.longitude])[1];
})
.attr("r",5)
.attr("stroke","black")
.style("fill", "orange")
.style("opacity", 1);
points.append("path")
.attr("class","vcells")
.attr("d",buildPathFromPoint)
.on('mouseover', function(d){
tooltip.transition()
.style('opacity',0.9)
tooltip.html('<strong>Station: </strong>' + d['name'] +'<strong> Wind Speed: </strong>' + d['direction'] + ' ' + d['speed'] + ' ' + 'km/h')
.style('left', (d3.event.pageX) + 'px') // x position in relationship to the page
.style('top', (d3.event.pageY)+ 'px')
})
.on('mouseout', function(d){
d3.select(this) //'this keyword selects current selection'
.style('opacity',1)
});
// console.log(stations.length);
// console.log(stations[1].direction);
// Draw the lines
map.selectAll('line')
.data(lines)
.enter()
.append("line")
.attr({
x1: function(d) {return d.x0},
y1: function(d) {return d.y0}
})
.call(lineAnimate);
}
</script>
</body>
</html>