-
Notifications
You must be signed in to change notification settings - Fork 2
/
ol-contours.html
350 lines (305 loc) · 14 KB
/
ol-contours.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
<!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>
/**
这是比较完整的ol-d3-contour版本
TODO
1.zoom大时,PXregion异常 | ok 左上角尚有小问题
2.等值线添加label
3.引入webworker 多线程计算 | 效果不明显,可能是方法的问题
4.idw插值算法优化的思路
(1)画布以(0,0)为起点 xmax => width, ymax => height | 这条好像行不通
1)直接在region的extent里计算
2)直接在region里插值计算(需要判断空间关系,范围内计算,范围外不计算)
(2)画布以(xmin, ymin) 为起点 xmax - xmin => width, ymax - ymin => height | 已通
3)region的extent(xmax - xmin, ymax - ymin)计算插值,然后canvas偏移至(xmin, ymin)
(3)基于idw算法函数优化 | Math函数优化,循环优化
*/
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/getLcAQI.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: [115.79, 36.142],
projection: 'EPSG:4326',
zoom: 8
})
});
map.on('click', (evt) => {
const px = map.getPixelFromCoordinate(evt.coordinate);
})
// 定义裁剪边界
var Regioncoord = [[116.05883026123047, 36.88069534301758], [116.12857055664074, 36.88832473754883], [116.16789245605469, 36.936771392822266], [116.20486450195324, 36.9648323059082], [116.23371124267578, 37.02350616455084], [116.30213165283214, 36.98831558227545], [116.32343292236328, 36.970138549804744], [116.43280029296886, 36.96689605712896], [116.45199584960938, 36.956863403320256], [116.45880126953125, 36.934246063232536], [116.42424011230469, 36.897548675537166], [116.4191665649414, 36.87055206298834], [116.38722229003918, 36.796733856201286], [116.39975738525402, 36.76072311401367], [116.38756561279308, 36.731239318847656], [116.40126800537132, 36.70432662963873], [116.40677642822266, 36.6568336486817], [116.39505004882824, 36.52444839477545], [116.44647979736351, 36.493713378906364], [116.45575714111328, 36.44969940185541], [116.49484252929688, 36.44376754760748], [116.54493713378918, 36.40079116821289], [116.50951385498047, 36.377944946289176], [116.47891998291027, 36.33679962158203], [116.44329071044933, 36.336921691894645], [116.32552337646484, 36.290496826171875], [116.27454376220703, 36.2218589782716], [116.20741271972656, 36.169773101806754], [116.16371154785156, 36.1711540222168], [116.15852355957043, 36.14592742919922], [116.1110076904298, 36.12730026245117], [116.09490203857422, 36.1111297607423], [116.05631256103516, 36.104995727539176], [116.01735687255871, 36.06333923339844], [115.92543792724621, 36.02585220336914], [115.89026641845703, 36.0249404907226], [115.85330963134777, 36.0066299438476], [115.80311584472656, 36.01005172729498], [115.74849700927734, 35.97412109375006], [115.68595886230469, 35.96349334716808], [115.65953826904308, 35.93789672851568], [115.64204406738281, 35.92935180664074], [115.58512115478538, 35.92505645751953], [115.5428695678711, 35.89764404296881], [115.50706481933594, 35.89407730102545], [115.44414520263672, 35.86289215087896], [115.39887237548828, 35.81025314331066], [115.35540771484375, 35.784446716308594], [115.33479309082031, 35.80162811279297], [115.33245086669933, 35.86394882202154], [115.36087036132824, 35.90924835205084], [115.34718322753906, 35.94689178466808], [115.35207366943371, 35.97102355957031], [115.43162536621105, 36.00341796875], [115.44813537597656, 36.075164794921875], [115.48091888427734, 36.126602172851506], [115.47286987304688, 36.16936111450207], [115.47711181640625, 36.2026824951173], [115.46441650390636, 36.257820129394474], [115.4229507446289, 36.28858184814453], [115.40888214111351, 36.32630920410156], [115.36056518554688, 36.308712005615234], [115.36262512207031, 36.342380523681754], [115.34253692626964, 36.38442230224621], [115.29388427734386, 36.41213226318354], [115.30603790283203, 36.43793106079113], [115.28143310546875, 36.47678756713873], [115.29291534423851, 36.520103454589844], [115.32230377197277, 36.54581069946295], [115.33660125732433, 36.61305999755865], [115.36998748779308, 36.63021469116222], [115.41259002685547, 36.683006286621094], [115.4307861328125, 36.68991088867193], [115.4688491821289, 36.74204254150402], [115.48047637939464, 36.76197052001953], [115.58241271972656, 36.779426574707145], [115.68394470214855, 36.81945800781256], [115.7004165649414, 36.87666702270502], [115.75374603271496, 36.915134429931754], [115.77561950683594, 36.909465789794865], [115.8946304321289, 36.905590057373104], [115.91487121582031, 36.89275741577154], [116.05883026123047, 36.88069534301758]];
// 色标卡
var colors = [
{ min: 0, max: 50, color: [0, 229, 0] },
{ min: 50, max: 100, color: [255, 255, 0] },
{ min: 100, max: 150, color: [255, 126, 0] },
{ min: 150, max: 200, color: [254, 0, 0] },
{ min: 200, max: 250, color: [152, 0, 75] },
{ min: 250, max: 300, color: [126, 1, 35] },
]
var contours, d3Path;
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)
const idwdata = olIDW(pxdata.data, cw, ch);
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; //线条宽度
//绘图登高线图
contours
.thresholds(10)
(idwdata)
.forEach(fill);
//绘图一条等高线
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();
}
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]
})
}
}
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].AQI)
})
}
}
// 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);
// for (var i = 0; i < dlen; i++) {
// var point = d[i];
// matrixData[point.y * width + point.x] = point.value;
// }
/**
* 插值矩阵数据,时间复杂度O(height*width*len)
*/
var idwcount = 0
for (var i = 0, k1 = 0; i < height; i++) {
for (var 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;
}
}
}
console.log(idwcount)
var e = new Date().getTime();
console.log('插值:' + (e - s) / 1000 + '秒');
return matrixData;
}
function getColor(colors, value) {
var len = colors.length;
for (var i = 0; i < len; i++) {
if (value > colors[i].min && value <= colors[i].max) return d3.rgb(colors[i].color[0], colors[i].color[1], colors[i].color[2]);
}
}
</script>
</html>