-
Notifications
You must be signed in to change notification settings - Fork 0
/
L.Graticule.js
104 lines (90 loc) · 2.6 KB
/
L.Graticule.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
/*
Graticule plugin for Leaflet powered maps.
*/
L.Graticule = L.GeoJSON.extend({
options: {
style: {
color: '#333',
weight: 1
},
interval: 20
},
initialize: function (options) {
L.Util.setOptions(this, options);
this._layers = {};
if (this.options.sphere) {
this.addData(this._getFrame());
} else {
this.addData(this._getGraticule());
}
},
_getFrame: function() {
return { "type": "Polygon",
"coordinates": [
this._getMeridian(-180).concat(this._getMeridian(180).reverse())
]
};
},
_getGraticule: function () {
var features = [], interval = this.options.interval;
// Meridians
for (var lng = 0; lng <= 180; lng = lng + interval) {
features.push(this._getFeature(this._getMeridian(lng), {
"name": (lng) ? lng.toString() + "° E" : "Prime meridian"
}));
if (lng !== 0) {
features.push(this._getFeature(this._getMeridian(-lng), {
"name": lng.toString() + "° W"
}));
}
}
// Parallels
for (var lat = 0; lat < 90; lat = lat + interval) {
features.push(this._getFeature(this._getParallel(lat), {
"name": (lat) ? lat.toString() + "° N" : "Equator"
}));
if (lat !== 0) {
features.push(this._getFeature(this._getParallel(-lat), {
"name": lat.toString() + "° S"
}));
}
}
return {
"type": "FeatureCollection",
"features": features
};
},
_getMeridian: function (lng) {
lng = this._lngFix(lng);
var coords = [];
for (var lat = -90; lat <= 90; lat++) {
coords.push([lng, lat]);
}
return coords;
},
_getParallel: function (lat) {
var coords = [];
for (var lng = -180; lng <= 180; lng++) {
coords.push([this._lngFix(lng), lat]);
}
return coords;
},
_getFeature: function (coords, prop) {
return {
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": coords
},
"properties": prop
};
},
_lngFix: function (lng) {
if (lng >= 180) return 179.999999;
if (lng <= -180) return -179.999999;
return lng;
}
});
L.graticule = function (options) {
return new L.Graticule(options);
};