-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaflet-amedas.js
174 lines (145 loc) · 3.75 KB
/
leaflet-amedas.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
/*
* @class L.Amedas
* @inherits L.Layer
* @author Yuta Tachibana
*
* for leaflet v1.0
*
* requirements:
*/
L.Amedas = L.Layer.extend({
options: {},
initialize: function (url, options){
L.setOptions(this, options);
if (!url){
var self = this;
this._loadAmedasJSON(function (data){
var data_url = "//s3-ap-northeast-1.amazonaws.com/amedas/wind/" + data.time + ".json";
self._loadJSON(data_url);
self._showTime(data.time);
});
}else{
this._loadJSON(url);
}
},
onAdd: function (map){
this.map = map;
},
onRemove: function (){
},
getEvents: function (){
return {
moveend: this._update,
};
},
_loadAmedasJSON: function (callback){
var url = "//s3-ap-northeast-1.amazonaws.com/amedas/amedas.json";
this._getJSON(url, function (data){
callback(data);
});
},
_loadJSON: function (url){
this._url = url;
var self = this;
this._getJSON(url, function (data){
console.log(data);
self.data = data;
self._init();
});
},
_showTime: function (time){
var time_str = time.substr(0, 4) + "/" + time.substr(4, 2) + "/" + time.substr(6, 2) +
" " + time.substr(8, 2) + ":" + time.substr(10, 2);
document.getElementById("time").innerHTML = time_str;
},
// substitute $.getJSON
_getJSON: function (url, callback){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if ((xhr.readyState === 4) && (xhr.status === 200)) {
var data = JSON.parse(xhr.responseText);
callback(data);
}
}
xhr.open("GET", url, true);
xhr.send(null);
},
_init: function (){
this._defaultMarkers = L.featureGroup();
for (id in this.data.data){
var point = this.data.data[id];
if (point.type == "observatory"){
var marker = this._createMarker(point);
this._defaultMarkers.addLayer(marker);
}
}
this._defaultMarkers.addTo(this.map);
console.log("default", this._defaultMarkers.getLayers().length);
// upscaled markers
this._upscaledMarkers = L.featureGroup();
this._showUpscaled = false;
},
_update: function (){
var zoom = this._map.getZoom(),
bounds = this._map.getBounds();
if (zoom > 7){
for (id in this.data.data){
var point = this.data.data[id];
if (point.type != "observatory"){
var contains = bounds.contains([point.lat, point.lon]);
if (!point.show && contains){
point.show = true;
var marker = this._createMarker(point);
this._upscaledMarkers.addLayer(marker);
point.layerId = this._upscaledMarkers.getLayerId(marker);
}else if (point.show && !contains){
point.show = false;
this._upscaledMarkers.removeLayer(point.layerId);
}
}
}
if (!this._showUpscaled){
this._showUpscaled = true;
this._upscaledMarkers.addTo(this.map);
}
}else{
if (this._showUpscaled){
this._showUpscaled = false;
this._upscaledMarkers.remove();
}
}
console.log("upscaled:", this._upscaledMarkers.getLayers().length);
},
_createMarker: function (point){
var deg = this._degrees[point.dir];
var icon = L.WindBarb.icon({
deg: deg,
speed: point.speed * 2, // 1 kt = 0.5 m/s
fillColor: this._color(point.speed),
pointRadius: 4,
forceDir: (deg !== undefined)
});
var marker = L.marker([point.lat, point.lon], {icon: icon});
marker.bindPopup(point.name + '<br>' + point.dir + ' ' + point.speed + 'm/s');
return marker;
},
_degrees: {
N: 0, NNE: 22.5, NE: 45, ENE: 67.5,
E: 90, ESE: 112.5, SE: 135, SSE: 157.5,
S: 180, SSW: 202.5, SW: 225, WSW: 247.5,
W: 270, WNW: 292.5, NW: 315, NNW: 337.5
},
_color: function (speed){
if (speed <= 2){
return "#2962AD";
}else if (speed <= 5){
return "#30A0C2";
}else if (speed <= 10){
return "#71CB50";
}else if (speed <= 20){
return "#FFEE55";
}else{
return "#DF6561";
}
},
});