forked from mkyral/POI-Importer.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL.Grid.js
230 lines (198 loc) · 6.57 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
/*
* L.Grid displays a grid of lat/lng lines on the map.
*
*/
L.Grid = L.GeoJSON.extend({
options: {
tickRes: 300, // how many pixels between ticks, good range is between 30 and 200, I like 80
// '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.GeoJSON.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();
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 () { // makes a simple list of latitudes from which to make lines
return this._lines(
this._bounds.getSouth(),
this._bounds.getNorth(),
this._map.getSize().y/this.options.tickRes, // used to calculate ticks according to map pixel size
this._containsEquator()
);
},
_lngLines: function () {
return this._lines(
this._bounds.getWest(),
this._bounds.getEast(),
this._map.getSize().x/this.options.tickRes, // used to caclulate ticks according to map pixel size
this._containsIRM()
);
},
_lines: function (low, high, ticks, containsZero) {
var delta = high - low;
var interval;
if (Math.abs(1/3600*ticks - delta) < Math.abs(1/1800*ticks - delta)) { interval = 1/3600; } // 1"
else if (Math.abs(1/1800*ticks - delta) < Math.abs(1/720*ticks - delta)) { interval = 1/1800; } // 2"
else if (Math.abs(1/720*ticks - delta) < Math.abs(1/360*ticks - delta)) { interval = 1/720; } // 5"
else if (Math.abs(1/360*ticks - delta) < Math.abs(1/180*ticks - delta)) { interval = 1/360; } // 10"
else if (Math.abs(1/180*ticks - delta) < Math.abs(1/120*ticks - delta)) { interval = 1/180; } // 20"
else if (Math.abs(1/120*ticks - delta) < Math.abs(1/60*ticks - delta)) { interval = 1/120; } // 30"
else if (Math.abs(1/60*ticks - delta) < Math.abs(1/30*ticks - delta)) { interval = 1/60; } // 1'
else if (Math.abs(1/30*ticks - delta) < Math.abs(1/12*ticks - delta)) { interval = 1/30; } // 2'
else if (Math.abs(1/12*ticks - delta) < Math.abs(1/6*ticks - delta)) { interval = 1/12; } // 5'
else if (Math.abs(1/6*ticks - delta) < Math.abs(1/3*ticks - delta)) { interval = 1/6; } // 10'
else if (Math.abs(1/3*ticks - delta) < Math.abs(1/2*ticks - delta)) { interval = 1/3; } // 20'
else if (Math.abs(1/2*ticks - delta) < Math.abs(ticks - delta)) { interval = 1/2; } // 30'
else if (Math.abs(ticks - delta) < Math.abs(2*ticks - delta)) { interval = 1; } // 1 deg
else if (Math.abs(2*ticks - delta) < Math.abs(5*ticks - delta)) { interval = 2; }
else if (Math.abs(5*ticks - delta) < Math.abs(10*ticks - delta)) { interval = 5; }
else if (Math.abs(10*ticks - delta) < Math.abs(20*ticks - delta)) { interval = 10; }
else { interval = 20; }
var tick = interval;
// next we need to round 'low' to be evenly divisable by 'tick' aka 'interval'
low = Math.floor((low / tick) - (10)) * tick; // draw 10 extract graticules off the map, for overlap
ticks = delta/interval + 20; // draw extract graticules, 10 before, 10 after the map bounds
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([
[90, lng], // this creates overlap, helps when printing.
[-90, lng]
], this.options.lineStyle);
},
_horizontalLine: function (lat) { // this makes a standard geojson LineString, I think
return new L.Polyline([
[lat, this._bounds.getWest()-180],
[lat, this._bounds.getEast()+180]
], this.options.lineStyle);
},
_label: function (axis, num) { // axis is either the string 'lat' or 'lng',
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 = Math.round((num - deg) * 60 * 100)/100;
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(Math.abs(num) + 0.000014); // this rounds it up so you get something like '60' instead of 59.999999
var dir;
if (dms.deg === 0) {
dir = ' ';
} else {
if (axis == 'lat') {
dir = (num > 0 ? 'N' : 'S');
} else {
dir = (num > 0 ? 'E' : 'W');
}
}
return L.Util.template(
this.options.coordTemplates[style],
L.Util.extend(dms, {
dir: dir,
sec: Math.round(dms.sec)
})
);
}
}
});
L.grid = function (options) {
return new L.Grid(options);
};