-
Notifications
You must be signed in to change notification settings - Fork 16
/
L.Grid.js
240 lines (204 loc) · 4.79 KB
/
L.Grid.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
/*
* L.Grid displays a grid of lat/lng lines on the map.
*/
L.Grid = L.LayerGroup.extend({
options: {
xticks: 8,
yticks: 5,
// 'decimal' or one of the templates below
coordStyle: 'MinDec',
coordTemplates: {
'MinDec': '{degAbs}° {minDec}\'{dir}',
'DMS': '{degAbs}{dir}{min}\'{sec}"'
},
// Path style for the grid lines
lineStyle: {
stroke: true,
color: '#111',
opacity: 0.6,
weight: 1
},
// Redraw on move or moveend
redraw: 'move'
},
initialize: function (options) {
L.LayerGroup.prototype.initialize.call(this);
L.Util.setOptions(this, options);
},
onAdd: function (map) {
this._map = map;
var grid = this.redraw();
this._map.on('viewreset '+ this.options.redraw, function () {
grid.redraw();
});
this.eachLayer(map.addLayer, map);
},
onRemove: function (map) {
// remove layer listeners and elements
map.off('viewreset '+ this.options.redraw, this.map);
this.eachLayer(this.removeLayer, this);
},
redraw: function () {
// pad the bounds to make sure we draw the lines a little longer
this._bounds = this._map.getBounds().pad(0.5);
var grid = [];
var i;
var latLines = this._latLines();
for (i in latLines) {
if (Math.abs(latLines[i]) > 90) {
continue;
}
grid.push(this._horizontalLine(latLines[i]));
grid.push(this._label('lat', latLines[i]));
}
var lngLines = this._lngLines();
for (i in lngLines) {
grid.push(this._verticalLine(lngLines[i]));
grid.push(this._label('lng', lngLines[i]));
}
this.eachLayer(this.removeLayer, this);
for (i in grid) {
this.addLayer(grid[i]);
}
return this;
},
_latLines: function () {
return this._lines(
this._bounds.getSouth(),
this._bounds.getNorth(),
this.options.yticks * 2,
this._containsEquator()
);
},
_lngLines: function () {
return this._lines(
this._bounds.getWest(),
this._bounds.getEast(),
this.options.xticks * 2,
this._containsIRM()
);
},
_lines: function (low, high, ticks, containsZero) {
var delta = low - high,
tick = this._round(delta / ticks, delta);
if (containsZero) {
low = Math.floor(low / tick) * tick;
} else {
low = this._snap(low, tick);
}
var lines = [];
for (var i = -1; i <= ticks; i++) {
lines.push(low - (i * tick));
}
return lines;
},
_containsEquator: function () {
var bounds = this._map.getBounds();
return bounds.getSouth() < 0 && bounds.getNorth() > 0;
},
_containsIRM: function () {
var bounds = this._map.getBounds();
return bounds.getWest() < 0 && bounds.getEast() > 0;
},
_verticalLine: function (lng) {
return new L.Polyline([
[this._bounds.getNorth(), lng],
[this._bounds.getSouth(), lng]
], this.options.lineStyle);
},
_horizontalLine: function (lat) {
return new L.Polyline([
[lat, this._bounds.getWest()],
[lat, this._bounds.getEast()]
], this.options.lineStyle);
},
_snap: function (num, gridSize) {
return Math.floor(num / gridSize) * gridSize;
},
_round: function (num, delta) {
var ret;
delta = Math.abs(delta);
if (delta >= 1) {
if (Math.abs(num) > 1) {
ret = Math.round(num);
} else {
ret = (num < 0) ? Math.floor(num) : Math.ceil(num);
}
} else {
var dms = this._dec2dms(delta);
if (dms.min >= 1) {
ret = Math.ceil(dms.min) * 60;
} else {
ret = Math.ceil(dms.minDec * 60);
}
}
return ret;
},
_label: function (axis, num) {
var latlng;
var bounds = this._map.getBounds().pad(-0.005);
if (axis == 'lng') {
latlng = L.latLng(bounds.getNorth(), num);
} else {
latlng = L.latLng(num, bounds.getWest());
}
return L.marker(latlng, {
icon: L.divIcon({
iconSize: [0, 0],
className: 'leaflet-grid-label',
html: '<div class="' + axis + '">' + this.formatCoord(num, axis) + '</div>'
})
});
},
_dec2dms: function (num) {
var deg = Math.floor(num);
var min = ((num - deg) * 60);
var sec = Math.floor((min - Math.floor(min)) * 60);
return {
deg: deg,
degAbs: Math.abs(deg),
min: Math.floor(min),
minDec: min,
sec: sec
};
},
formatCoord: function (num, axis, style) {
if (!style) {
style = this.options.coordStyle;
}
if (style == 'decimal') {
var digits;
if (num >= 10) {
digits = 2;
} else if (num >= 1) {
digits = 3;
} else {
digits = 4;
}
return num.toFixed(digits);
} else {
// Calculate some values to allow flexible templating
var dms = this._dec2dms(num);
var dir;
if (dms.deg === 0) {
dir = ' ';
} else {
if (axis == 'lat') {
dir = (dms.deg > 0 ? 'N' : 'S');
} else {
dir = (dms.deg > 0 ? 'E' : 'W');
}
}
return L.Util.template(
this.options.coordTemplates[style],
L.Util.extend(dms, {
dir: dir,
minDec: Math.round(dms.minDec, 2)
})
);
}
}
});
L.grid = function (options) {
return new L.Grid(options);
};