-
Notifications
You must be signed in to change notification settings - Fork 461
/
Graticule.js
278 lines (250 loc) · 9.62 KB
/
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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Copyright (c) 2017 Jean-Marc VIGLINO,
released under the CeCILL-B license (French BSD license)
(http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt).
*/
import ol_ext_inherits from '../util/ext'
import ol_proj_Projection from 'ol/proj/Projection'
import ol_style_Style from 'ol/style/Style'
import ol_style_Stroke from 'ol/style/Stroke'
import ol_style_Fill from 'ol/style/Fill'
import ol_style_Text from 'ol/style/Text'
import {transform as ol_proj_transform} from 'ol/proj'
import {get as ol_proj_get} from 'ol/proj'
import ol_control_CanvasBase from './CanvasBase'
/**
* Draw a graticule on the map.
*
* @constructor
* @extends {ol_control_CanvasBase}
* @param {Object=} _ol_control_ options.
* @param {ol.projectionLike} options.projection projection to use for the graticule, default EPSG:4326
* @param {number} options.maxResolution max resolution to display the graticule
* @param {ol_style_Style} options.style Style to use for drawing the graticule, default black.
* @param {number} options.step step beetween lines (in proj units), default 1
* @param {number} options.stepCoord show a coord every stepCoord, default 1
* @param {number} options.spacing spacing beetween lines (in px), default 40px
* @param {number} options.borderWidthwidth of the border (in px), default 5px
* @param {number} options.marginmargin of the border (in px), default 0px
*/
var ol_control_Graticule = function(options) {
if (!options) options = {};
// Initialize parent
var elt = document.createElement("div");
elt.className = "ol-graticule ol-unselectable ol-hidden";
ol_control_CanvasBase.call(this, { element: elt });
this.set('projection', options.projection || 'EPSG:4326');
// Use to limit calculation
var p = new ol_proj_Projection({code:this.get('projection')});
var m = p.getMetersPerUnit();
this.fac = 1;
while (m/this.fac>10) {
this.fac *= 10;
}
this.fac = 10000/this.fac;
this.set('maxResolution', options.maxResolution || Infinity);
this.set('step', options.step || 0.1);
this.set('stepCoord', options.stepCoord || 1);
this.set('spacing', options.spacing || 40);
this.set('margin', options.margin || 0);
this.set('borderWidth', options.borderWidth || 5);
this.set('stroke', options.stroke!==false);
this.formatCoord = options.formatCoord || function(c){return c;};
if (options.style instanceof ol_style_Style) {
this.setStyle(options.style);
}
else {
this.setStyle(new ol_style_Style({
stroke: new ol_style_Stroke({ color:"#000", width:1 }),
fill: new ol_style_Fill({ color: "#fff" }),
text: new ol_style_Text({
stroke: new ol_style_Stroke({ color:"#fff", width:2 }),
fill: new ol_style_Fill({ color:"#000" }),
})
}));
}
};
ol_ext_inherits(ol_control_Graticule, ol_control_CanvasBase);
ol_control_Graticule.prototype.setStyle = function (style) {
this._style = style;
};
ol_control_Graticule.prototype._draw = function (e) {
if (this.get('maxResolution')<e.frameState.viewState.resolution) return;
var ctx = this.getContext(e);
var canvas = ctx.canvas;
var ratio = e.frameState.pixelRatio;
var w = canvas.width/ratio;
var h = canvas.height/ratio;
var proj = this.get('projection');
var map = this.getMap();
var bbox =
[ map.getCoordinateFromPixel([0,0]),
map.getCoordinateFromPixel([w,0]),
map.getCoordinateFromPixel([w,h]),
map.getCoordinateFromPixel([0,h])
];
var xmax = -Infinity;
var xmin = Infinity;
var ymax = -Infinity;
var ymin = Infinity;
for (var i=0, c; c=bbox[i]; i++)
{ bbox[i] = ol_proj_transform (c, map.getView().getProjection(), proj);
xmax = Math.max (xmax, bbox[i][0]);
xmin = Math.min (xmin, bbox[i][0]);
ymax = Math.max (ymax, bbox[i][1]);
ymin = Math.min (ymin, bbox[i][1]);
}
var spacing = this.get('spacing');
var step = this.get('step');
var step2 = this.get('stepCoord');
var borderWidth = this.get('borderWidth');
var margin = this.get('margin');
// Limit max line draw
var ds = (xmax-xmin)/step*spacing;
if (ds>w)
{ var dt = Math.round((xmax-xmin)/w*spacing /step);
step *= dt;
if (step>this.fac) step = Math.round(step/this.fac)*this.fac;
}
xmin = (Math.floor(xmin/step))*step -step;
ymin = (Math.floor(ymin/step))*step -step;
xmax = (Math.floor(xmax/step))*step +2*step;
ymax = (Math.floor(ymax/step))*step +2*step;
var extent = ol_proj_get(proj).getExtent();
if (extent)
{ if (xmin < extent[0]) xmin = extent[0];
if (ymin < extent[1]) ymin = extent[1];
if (xmax > extent[2]) xmax = extent[2]+step;
if (ymax > extent[3]) ymax = extent[3]+step;
}
var hasLines = this.getStyle().getStroke() && this.get("stroke");
var hasText = this.getStyle().getText();
var hasBorder = this.getStyle().getFill();
ctx.save();
ctx.scale(ratio,ratio);
ctx.beginPath();
ctx.rect(margin, margin, w-2*margin, h-2*margin);
ctx.clip();
ctx.beginPath();
var txt = {top:[],left:[],bottom:[], right:[]};
var x, y, p, p0, p1;
for (x=xmin; x<xmax; x += step)
{ p0 = ol_proj_transform ([x, ymin], proj, map.getView().getProjection());
p0 = map.getPixelFromCoordinate(p0);
if (hasLines) ctx.moveTo(p0[0], p0[1]);
p = p0;
for (y=ymin+step; y<=ymax; y+=step)
{ p1 = ol_proj_transform ([x, y], proj, map.getView().getProjection());
p1 = map.getPixelFromCoordinate(p1);
if (hasLines) ctx.lineTo(p1[0], p1[1]);
if (p[1]>0 && p1[1]<0) txt.top.push([x, p]);
if (p[1]>h && p1[1]<h) txt.bottom.push([x,p]);
p = p1;
}
}
for (y=ymin; y<ymax; y += step)
{ p0 = ol_proj_transform ([xmin, y], proj, map.getView().getProjection());
p0 = map.getPixelFromCoordinate(p0);
if (hasLines) ctx.moveTo(p0[0], p0[1]);
p = p0;
for (x=xmin+step; x<=xmax; x+=step)
{ p1 = ol_proj_transform ([x, y], proj, map.getView().getProjection());
p1 = map.getPixelFromCoordinate(p1);
if (hasLines) ctx.lineTo(p1[0], p1[1]);
if (p[0]<0 && p1[0]>0) txt.left.push([y,p]);
if (p[0]<w && p1[0]>w) txt.right.push([y,p]);
p = p1;
}
}
if (hasLines)
{ ctx.strokeStyle = this.getStyle().getStroke().getColor();
ctx.lineWidth = this.getStyle().getStroke().getWidth();
ctx.stroke();
}
// Draw text
if (hasText)
{
ctx.fillStyle = this.getStyle().getText().getFill().getColor();
ctx.strokeStyle = this.getStyle().getText().getStroke().getColor();
ctx.lineWidth = this.getStyle().getText().getStroke().getWidth();
ctx.textAlign = 'center';
ctx.textBaseline = 'hanging';
var t, tf;
var offset = (hasBorder ? borderWidth : 0) + margin + 2;
for (i=0; t = txt.top[i]; i++) if (!(Math.round(t[0]/this.get('step'))%step2))
{ tf = this.formatCoord(t[0]);
ctx.strokeText(tf, t[1][0], offset);
ctx.fillText(tf, t[1][0], offset);
}
ctx.textBaseline = 'alphabetic';
for (i=0; t = txt.bottom[i]; i++) if (!(Math.round(t[0]/this.get('step'))%step2))
{ tf = this.formatCoord(t[0]);
ctx.strokeText(tf, t[1][0], h-offset);
ctx.fillText(tf, t[1][0], h-offset);
}
ctx.textBaseline = 'middle';
ctx.textAlign = 'left';
for (i=0; t = txt.left[i]; i++) if (!(Math.round(t[0]/this.get('step'))%step2))
{ tf = this.formatCoord(t[0]);
ctx.strokeText(tf, offset, t[1][1]);
ctx.fillText(tf, offset, t[1][1]);
}
ctx.textAlign = 'right';
for (i=0; t = txt.right[i]; i++) if (!(Math.round(t[0]/this.get('step'))%step2))
{ tf = this.formatCoord(t[0]);
ctx.strokeText(tf, w-offset, t[1][1]);
ctx.fillText(tf, w-offset, t[1][1]);
}
}
// Draw border
if (hasBorder)
{ var fillColor = this.getStyle().getFill().getColor();
var color, stroke;
if (stroke = this.getStyle().getStroke())
{ color = this.getStyle().getStroke().getColor();
}
else
{ color = fillColor;
fillColor = "#fff";
}
ctx.strokeStyle = color;
ctx.lineWidth = stroke ? stroke.getWidth() : 1;
//
for (i=1; i<txt.top.length; i++)
{ ctx.beginPath();
ctx.rect(txt.top[i-1][1][0], margin, txt.top[i][1][0]-txt.top[i-1][1][0], borderWidth);
ctx.fillStyle = Math.round(txt.top[i][0]/step)%2 ? color: fillColor;
ctx.fill();
ctx.stroke();
}
for (i=1; i<txt.bottom.length; i++)
{ ctx.beginPath();
ctx.rect(txt.bottom[i-1][1][0], h-borderWidth-margin, txt.bottom[i][1][0]-txt.bottom[i-1][1][0], borderWidth);
ctx.fillStyle = Math.round(txt.bottom[i][0]/step)%2 ? color: fillColor;
ctx.fill();
ctx.stroke();
}
for (i=1; i<txt.left.length; i++)
{ ctx.beginPath();
ctx.rect(margin, txt.left[i-1][1][1], borderWidth, txt.left[i][1][1]-txt.left[i-1][1][1]);
ctx.fillStyle = Math.round(txt.left[i][0]/step)%2 ? color: fillColor;
ctx.fill();
ctx.stroke();
}
for (i=1; i<txt.right.length; i++)
{ ctx.beginPath();
ctx.rect(w-borderWidth-margin, txt.right[i-1][1][1], borderWidth, txt.right[i][1][1]-txt.right[i-1][1][1]);
ctx.fillStyle = Math.round(txt.right[i][0]/step)%2 ? color: fillColor;
ctx.fill();
ctx.stroke();
}
ctx.beginPath();
ctx.fillStyle = color;
ctx.rect(margin,margin, borderWidth, borderWidth);
ctx.rect(margin,h-borderWidth-margin, borderWidth,borderWidth);
ctx.rect(w-borderWidth-margin,margin, borderWidth, borderWidth);
ctx.rect(w-borderWidth-margin,h-borderWidth-margin, borderWidth,borderWidth);
ctx.fill();
}
ctx.restore();
};
export default ol_control_Graticule