forked from turban/Leaflet.Graticule
-
Notifications
You must be signed in to change notification settings - Fork 1
/
L.Graticule.js
146 lines (127 loc) · 4.51 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
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
import 'leaflet-textpath' ;
/*
Graticule plugin for Leaflet powered maps.
*/
L.Graticule = L.GeoJSON.extend({
options: {
style: {
color: '#333',
weight: 1
},
intervalLat: 10 ,
intervalLng: 10 ,
latBounds:[-90,90],
lngBounds:[-180,180] ,
centerLatLabels: true ,
centerLonLabels: false ,
zIndex: 99999,
onEachFeature: function (feature, layer) {
if (feature.properties.name.match('W')) {
layer.setText(
feature.properties.name,
{offset:-3, orientation:0, center: layer.options.centerLonLabels, attributes:{class:'grat-labels'}}
);
} else if (feature.properties.name.match('E')) {
layer.setText(
feature.properties.name,
{offset:-3, orientation:180, center: layer.options.centerLonLabels, attributes:{class:'grat-labels'}}
);
} else if (feature.properties.name.match('S')) {
layer.setText(
feature.properties.name,
{offset:-3, center: layer.options.centerLatLabels, attributes:{class:'grat-labels'}}
);
} else if (feature.properties.name.match('N')) {
layer.setText(
feature.properties.name,
{offset:-3, center: layer.options.centerLatLabels, attributes:{class:'grat-labels'}}
);
}
}
},
initialize: function (options) {
L.Util.setOptions(this, options);
this.options.style={...this.options.style, interactive:false}
this._layers = {};
this.addData(this._getGraticule());
},
_getGraticule: function () {
var features = [];
//round the starting point to a multiple of the lng interval
let lng = this.options.intervalLng*Math.round(this.options.lngBounds[0]/this.options.intervalLng)
// Meridians
for (lng ; lng < this.options.lngBounds[1]; lng += this.options.intervalLng) {
if (lng >=0) {
features.push(this._getFeature(this._getMeridian(lng), {
"name": (lng) ? lng.toString() + "° E" : "0° E"
}));
}
else if (lng < 0) {
features.push(this._getFeature(this._getMeridian(lng), {
"name": (-lng).toString() + "° W"
}));
}
}
//round the starting point to a multiple of the lat interval
let lat = this.options.intervalLat*Math.round(this.options.latBounds[0]/this.options.intervalLat)
// Parallels
for (lat; lat < this.options.latBounds[1]; lat = lat + this.options.intervalLat) {
if (lat>=0) {
features.push(this._getFeature(this._getParallel(lat), {
"name": (lat) ? lat.toString() + "° N" : "0° N"
}));
}
else 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 convenience with labelling, we are going to draw lines towards the poles
if (this.options.latBounds[0]<-this.options.latBounds[1]) {
// lines start at max and go towards the min (e.g.. from -30 towards -90)
for (var lat = this.options.latBounds[1]; lat >= this.options.latBounds[0]; lat--) {
coords.push([lng, lat]);
}
} else {
// lines start at min and go towards max (e.g. from 0 towards 90)
for (var lat = this.options.latBounds[0]; lat <= this.options.latBounds[1]; lat++) {
coords.push([lng, lat]);
}
}
return coords;
},
_getParallel: function (lat) {
var coords = [];
for (var lng = this.options.lngBounds[0]; lng <= this.options.lngBounds[1]; 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);
};