forked from lzuniujp08/ol-d3-contours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ol-contours-gxV1.html
554 lines (501 loc) · 17.2 KB
/
ol-contours-gxV1.html
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>contour</title>
<link href="./lib/openlayer/ol.css" rel="stylesheet" />
<style>
html,
body {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
overflow: hidden;
}
#map {
height: 100%;
width: 100%;
}
#contour {
position: absolute;
z-index: 999;
top: 0px;
left: 0px;
opacity: 0.5;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
<script src="lib/jqure1.7.min.js"></script>
<script src="lib/d3.v4.min.js"></script>
<script src="lib/d3-contour.v1.min.js"></script>
<script src="lib/dat.gui.min.js"></script>
<script src="lib/openlayer/ol-debug.js"></script>
<script>
/**
* 这是将idw计算函数优化后的版本,全部数据1690,计算次数1290703700,耗时3.6秒
*/
var mode = {};
mode.dataLoad = false;
mode.mapType = 'contour';
mode.heatmap_line = true;
mode.contour_isfull = true;
mode.drawpoint = false;
mode.drawlable = false;
mode.referenceValue = 85;
mode.mapAlpha = 0.4;
mode.mapThresholds = 10;
// GUI
var gui = new dat.GUI();
// 下拉框形式选择文案
gui.add(mode, 'dataLoad').onChange(function () {
if (!originData) {
AjaxGET()
}
});
gui.add(mode, 'mapType', ['heatmap', 'contour']).onChange(function () {
});
gui.add(mode, 'heatmap_line').onChange(function () {
});
gui.add(mode, 'contour_isfull').onChange(function () {
});
gui.add(mode, 'drawpoint').onChange(function () {
});
gui.add(mode, 'drawlable').onChange(function () {
});
gui.add(mode, 'referenceValue', 70, 100).onChange(function () {
});
gui.add(mode, 'mapAlpha', 0.1, 1.0).onChange(function () {
});
gui.add(mode, 'mapThresholds', 5, 20).onChange(function () {
});
var originData;
function AjaxGET() {
$.ajax({
type: "get",
url: "../data/getGxTEM.json",
dataType: "json",
success: function (json) {
originData = json.data;
CanvasLayerInit(originData);
}
});
}
let baseLayer = new ol.layer.Tile({
title: "base",
source: new ol.source.OSM()
});
var map = new ol.Map({
target: 'map',
layers: [baseLayer],
view: new ol.View({
center: [108.22, 22.48],
projection: 'EPSG:4326',
zoom: 7
})
});
map.on('click', (evt) => {
const px = map.getPixelFromCoordinate(evt.coordinate);
})
// 定义裁剪边界
var Regioncoord = [
[112.018771, 24.742991],
[111.922317, 24.632471],
[112.052286, 24.389786],
[111.907721, 24.224724],
[111.935284, 23.99064],
[111.782283, 23.810257],
[111.481793, 23.629179],
[111.359452, 23.346328],
[111.353528, 22.891877],
[111.050612, 22.651229],
[110.745143, 22.560068],
[110.720781, 22.298481],
[110.499475, 22.145777],
[110.376498, 22.166857],
[110.386609, 21.952634],
[110.228064, 21.881519],
[109.941204, 21.847098],
[109.91003, 21.669518],
[109.745674, 21.591496],
[109.539086, 21.501962],
[109.15403, 21.403121],
[109.122942, 21.587646],
[108.792872, 21.635169],
[108.60476, 21.703956],
[108.393108, 21.553679],
[107.951241, 21.54183],
[107.856644, 21.654128],
[107.670284, 21.60483],
[107.384808, 21.598426],
[107.08438, 21.809196],
[107.06249, 21.897733],
[106.73693, 22.007554],
[106.660017, 22.334552],
[106.560223, 22.466608],
[106.70794, 22.579877],
[106.777117, 22.816929],
[106.605147, 22.927294],
[106.278289, 22.870475],
[106.202497, 22.985729],
[105.87158, 22.934157],
[105.541938, 23.193556],
[105.593011, 23.316427],
[105.864068, 23.534543],
[106.06837, 23.490039],
[106.193344, 23.839618],
[105.994917, 24.127046],
[105.838247, 24.033708],
[105.489794, 24.019978],
[105.22658, 24.168574],
[105.194475, 24.330613],
[105.047563, 24.444132],
[104.726227, 24.448855],
[104.519247, 24.538205],
[104.527261, 24.734426],
[104.62901, 24.663423],
[104.844471, 24.682999],
[105.030901, 24.790381],
[105.208045, 24.998223],
[105.443885, 24.919389],
[105.495992, 24.811707],
[105.762584, 24.723646],
[106.041005, 24.684321],
[106.167491, 24.761768],
[106.15812, 24.941302],
[106.434716, 25.021159],
[106.680148, 25.180254],
[106.894823, 25.194612],
[107.008447, 25.277277],
[106.959281, 25.440187],
[107.061434, 25.559848],
[107.314724, 25.506024],
[107.50047, 25.213561],
[107.75099, 25.241621],
[107.790364, 25.11902],
[108.135347, 25.238027],
[108.151225, 25.373298],
[108.327262, 25.538863],
[108.333191, 25.53823],
[108.41749, 25.443103],
[108.595029, 25.45789],
[108.757891, 25.640718],
[108.948369, 25.559414],
[108.946082, 25.734292],
[108.949757, 25.73391],
[108.954053, 25.734548],
[109.074594, 25.779999],
[109.292203, 25.717137],
[109.479632, 26.033639],
[109.781434, 25.992915],
[109.965848, 26.203308],
[110.059684, 26.045225],
[110.236062, 26.036341],
[110.546618, 26.236968],
[110.599353, 26.327283],
[110.794692, 26.27094],
[111.088237, 26.307829],
[111.277339, 26.27252],
[111.190019, 25.959266],
[111.378677, 25.884694],
[111.385872, 25.885442],
[111.302121, 25.720935],
[111.283031, 25.434527],
[111.097418, 25.285984],
[110.946744, 25.046674],
[110.987204, 24.960625],
[110.984792, 24.955197],
[110.989943, 24.93469],
[110.990291, 24.934372],
[110.990023, 24.933248],
[111.098564, 24.995761],
[111.098286, 24.99812],
[111.270769, 25.154386],
[111.468114, 25.015803],
[111.425673, 24.688552],
[111.567414, 24.648724],
[111.676845, 24.781188],
[112.018771, 24.742991]
]
// 色标卡
var colors = [
{ max: -30, color: '#020c64' },
{ min: -30, max: -28, color: '#071e78' },
{ min: -28, max: -26, color: '#11318b' },
{ min: -26, max: -24, color: '#1b449f' },
{ min: -24, max: -22, color: '#2657b3' },
{ min: -22, max: -20, color: '#306ac7' },
{ min: -20, max: -18, color: '#3b7edb' },
{ min: -20, max: -18, color: '#3b7edb' },
{ min: -18, max: -16, color: '#4e8add' },
{ min: -16, max: -14, color: '#6196e0' },
{ min: -14, max: -12, color: '#74a3e2' },
{ min: -12, max: -10, color: '#87afe5' },
{ min: -10, max: -8, color: '#9bbce8' },
{ min: -8, max: -6, color: '#9ac4dc' },
{ min: -6, max: -4, color: '#99cdd0' },
{ min: -4, max: -2, color: '#98d6c4' },
{ min: -2, max: 0, color: '#97e8ad' },
{ min: 0, max: 2, color: '#d7de7e' },
{ min: 2, max: 4, color: '#eadb70' },
{ min: 4, max: 6, color: '#f4d963' },
{ min: 6, max: 8, color: '#facc4f' },
{ min: 8, max: 10, color: '#f7b42d' },
{ min: 10, max: 12, color: '#f29b00' },
{ min: 12, max: 14, color: '#f19303' },
{ min: 14, max: 16, color: '#f0840a' },
{ min: 16, max: 18, color: '#ef7511' },
{ min: 18, max: 20, color: '#ee6618' },
{ min: 20, max: 22, color: '#ee581f' },
{ min: 22, max: 24, color: '#e74b1a' },
{ min: 24, max: 26, color: '#e03f16' },
{ min: 26, max: 28, color: '#d93312' },
{ min: 28, max: 30, color: '#d0240e' },
{ min: 30, max: 32, color: '#c20003' },
{ min: 32, max: 34, color: '#b50109' },
{ min: 34, max: 35, color: '#a90210' },
{ min: 35, max: 37, color: '#8a0519' },
{ min: 37, max: 40, color: '#6f0015' },
{ min: 40, color: '#6f0015' }
];
var contours, d3Path;
var context, contours, d3Path, cnavas;
function CanvasLayerInit(_data) {
var canvasLayer = new ol.layer.Image({
source: new ol.source.ImageCanvas({
canvasFunction: (extent, resolution, pixelRatio, size, projection) => {
const [width, height] = size;
const [left, bottom, right, top] = extent;
const xscale = width / (right - left);
const yscale = height / (top - bottom);
const pxdata = getPxData(_data, xscale, yscale, top, left);
const pxregion = getPxRegion(Regioncoord, xscale, yscale, top, left);
const cw = Math.ceil(pxregion.xmax)
const ch = Math.ceil(pxregion.ymax)
let canvas = document.createElement('canvas');
canvas.width = cw;
canvas.height = ch;
canvas.style.display = 'block';
// 设置canvas透明度
canvas.getContext('2d').globalAlpha = 0.1;
context = canvas.getContext('2d');
contours = d3.contours().size([cw, ch]); //等高线绘图实例
d3Path = d3.geoPath(null, context); //绘图笔
context.clearRect(0, 0, cw, ch);
// 绘制裁剪区
context.beginPath()
for (let i = 0; i < pxregion.data.length; i++) {
const rp = pxregion.data[i]
context.lineTo(rp.x, rp.y)
}
context.closePath()
context.stroke()
context.clip()
context.globalAlpha = 0.3; //设置透明度
context.lineWidth = 2; //线条宽度
const idwdata = olIDW(pxdata.data, cw, ch);
//绘图登高线图
contours
.thresholds(10)
(idwdata)
.forEach(fill);
return canvas;
},
projection: 'EPSG:4326',
ratio: 1
})
})
// 向map添加图层
map.addLayer(canvasLayer);
}
function getPxRegion(data, xscale, yscale, top, left) {
var _data = [];
var xmax = 0;
var ymax = 0;
var xmin = 9999;
var ymin = 9999;
for (var i = 0; i < data.length; i++) {
var p = coordToPoint(data[i][0], data[i][1], xscale, yscale, top, left)
if (i === 0) {
xmax = p[0];
ymax = p[1];
}
if (p[0] != 0 || p[1] != 0) {
xmax > p[0] ? null : xmax = p[0];
ymax > p[1] ? null : ymax = p[1];
xmin < p[0] ? null : xmin = p[0];
ymin < p[1] ? null : ymin = p[1];
_data.push({
x: p[0],
y: p[1]
})
}
// xmax > p[0] ? null : xmax = p[0];
// ymax > p[1] ? null : ymax = p[1];
// xmin < p[0] ? null : xmin = p[0];
// ymin < p[1] ? null : ymin = p[1];
// _data.push({
// x: p[0],
// y: p[1]
// })
}
return {
data: _data,
xmax: xmax,
ymax: ymax,
xmin: xmin,
ymin: ymin,
}
}
function getPxData(data, xscale, yscale, top, left) {
var _data = [];
var xmax = 0;
var ymax = 0;
var xmin = 9999;
var ymin = 9999;
for (var i = 0; i < data.length; i++) {
var p = coordToPoint(data[i].Lon, data[i].Lat, xscale, yscale, top, left)
if (i === 0) {
xmax = p[0];
ymax = p[1];
}
if (p[0] != 0 || p[1] != 0) {
xmax > p[0] ? null : xmax = p[0];
ymax > p[1] ? null : ymax = p[1];
xmin < p[0] ? null : xmin = p[0];
ymin < p[1] ? null : ymin = p[1];
_data.push({
x: p[0],
y: p[1],
value: Number(data[i].TEM)
})
}
// xmax > p[0] ? null : xmax = p[0];
// ymax > p[1] ? null : ymax = p[1];
// xmin < p[0] ? null : xmin = p[0];
// ymin < p[1] ? null : ymin = p[1];
// _data.push({
// x: p[0],
// y: p[1],
// value: Number(data[i].TEM)
// })
}
// value一定要确保是number类型,否则在插值计算的会先string转number再计算,影响效率
return {
data: _data,
xmax: xmax,
ymax: ymax,
xmin: xmin,
ymin: ymin,
}
}
function coordToPoint(x, y, xscale, yscale, top, left) {
// var px = Math.abs(left - x) * xscale;
// var py = Math.abs(top - y) * yscale;
// 目的是为了剔除范围外的数据
let px = (x - left) * xscale;
let py = (top - y) * yscale;
px > 0 ? null : px = 0;
py > 0 ? null : py = 0;
return [px, py]
}
function olIDW(data, width, height) {
var s = new Date().getTime();
var d = data;
//已有点初始二维数组
var dlen = d.length;
// var matrixData = new Array(width * height);
var matrixData = []
// for (var i = 0; i < dlen; i++) {
// var point = d[i];
// matrixData[Math.ceil(point.y * width + point.x)] = point.value;
// }
/**
* 插值矩阵数据,时间复杂度O(height*width*len)
*/
let idwcount = 0
for (let i = 0, k1 = 0; i < height; i++) {
for (let j = 0; j < width; j++ , k1++) {
// if (!matrixData[k1]) {
// var sum0 = 0, sum1 = 0;
// for (var k = 0; k < dlen; k++) {
// var dk = d[k];
// var dis = ((i - dk.y) * (i - dk.y) + (j - dk.x) * (j - dk.x));
// sum0 = sum0 + dk.value * 1 / dis;
// sum1 = sum1 + 1 / dis;
// idwcount++
// }
// if (sum1 != 0)
// //matrixData[k1] = sum0 / sum1 - referenceValue;
// matrixData[k1] = sum0 / sum1;
// else
// matrixData[k1] = 1;
// }
let sum0 = 0, sum1 = 0;
for (let k = 0; k < dlen; k++) {
const dk = d[k];
const dis = Math.pow((i - dk.y), 2) + Math.pow((j - dk.x), 2);
sum0 += dk.value / dis;
sum1 += 1 / dis;
idwcount++
}
if (sum1 != 0)
//matrixData[k1] = sum0 / sum1 - referenceValue;
matrixData[k1] = sum0 / sum1;
else
matrixData[k1] = 1;
}
}
console.log(idwcount)
var e = new Date().getTime();
console.log('插值:' + (e - s) / 1000 + '秒');
return matrixData;
}
//绘图一条等高线
function fill(geometry) {
context.beginPath();
d3Path(geometry);
console.log(geometry.value)
var acolor = getColor(colors, geometry.value);
//等高线梯度着色
context.fillStyle = acolor;
context.fill();
//等高线线条
context.strokeStyle = acolor;
context.stroke();
}
function colorRGB(color) {
let sColor = color.toLowerCase();
// 处理六位的颜色值
const sColorChange = [];
// 十六进制颜色值的正则表达式
const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
// 如果是16进制颜色
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
let sColorNew = '#';
for (let i = 1; i < 4; i += 1) {
sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
}
sColor = sColorNew;
}
for (let i = 1; i < 7; i += 2) {
sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)));
}
}
return sColorChange;
}
function getColor(_colors, value) {
var len = _colors.length;
for (var i = 0; i < len; i++) {
if (value > _colors[i].min && value <= _colors[i].max) {
const color = colorRGB(_colors[i].color);
return d3.rgb(color[0], color[1], color[2]);
}
}
}
</script>
</html>