forked from lzuniujp08/ol-d3-contours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maptalks-contoursV2.html
300 lines (269 loc) · 15.1 KB
/
maptalks-contoursV2.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
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Layer - CanvasLayer: an example</title>
<style type="text/css">
html,
body {
margin: 0px;
height: 100%;
width: 100%
}
.container {
width: 100%;
height: 100%
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/maptalks/dist/maptalks.css">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/maptalks/dist/maptalks.min.js"></script>
<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="js/contour.js"></script>
<body>
<div id="map" class="container"></div>
<script>
function AjaxGET() {
$.ajax({
type: "get",
url: "../data/getLcAQI.json",
dataType: "json",
success: function (json) {
var originData = json.data;
console.log(originData)
MapInit(originData)
}
});
}
AjaxGET();
var canvasLayer, layer;
var contours, d3Path;
function MapInit(_data) {
var map = new maptalks.Map('map', {
center: [115.96481323242188, 36.43890380859375],
zoom: 8,
baseLayer: new maptalks.TileLayer('geoq', {
style: 'normal',
urlTemplate: 'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}'
})
});
//定义裁剪边界
var coord = [[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 polygon = new maptalks.Polygon(coord);
var poly = new maptalks.Polygon(coord);
// var layer = new maptalks.VectorLayer('v',poly).addTo(map);
canvasLayer = new maptalks.CanvasLayer('c', {
'forceRenderOnMoving': true,
'forceRenderOnZooming': true
}).addTo(map);
canvasLayer.setMask(polygon)
canvasLayer.prepareToDraw = function (/* context */) {
return [];
};
//draw when map is interacting
canvasLayer.drawOnInteracting = function (context, view) {
this.draw(context, view);
};
// param1 and param2 are prepareToDraw's return values.
canvasLayer.draw = function (context, view) {
var size = map.getSize();
// get mask ContainerExtent
var CE = canvasLayer.getMask().getContainerExtent()
CE.pxmax = Math.ceil(CE.pxmax);
CE.pxmin = Math.ceil(CE.pxmin);
CE.pymax = Math.ceil(CE.pymax);
CE.pymin = Math.ceil(CE.pymin);
// var CE = poly.getContainerExtent()
// var width = Math.ceil(CE.pxmax - CE.pxmin);
// var height = Math.ceil(CE.pymax - CE.pymin);
var width = size.width;
var height = size.height;
// coordinate to containpoint and manage data
var pxData = []
for (var i = 0; i < _data.length; i++) {
if (_data[i].lon && _data[i].lat) {
var p = map.coordinateToContainerPoint(new maptalks.Coordinate([_data[i].lon, _data[i].lat]));
pxData.push({
x: Math.ceil(p.x),
y: Math.ceil(p.y),
value: _data[i].AQI
})
}
}
var _canvas = document.createElement('canvas');
_canvas.width = CE.pxmax;
_canvas.height = CE.pymax;
var _contenxt = _canvas.getContext('2d');
_contenxt.lineWidth = 2; //线条宽度
// Math.ceil(CE.pxmax - CE.pxmin), Math.ceil(CE.pymax - CE.pymin)
contours = d3.contours().size([CE.pxmax, CE.pymax]); //等高线绘图实例
d3Path = d3.geoPath(null, _contenxt); //绘图笔
var mdata = maptalksIDW(pxData, CE)
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] },
]
//绘图登高线图
contours
.thresholds(10)
(mdata)
.forEach(fill);
//绘图一条等高线
function fill(geometry) {
_contenxt.beginPath();
d3Path(geometry);
console.log(geometry.value)
var acolor = getColor(colors, geometry.value);
//等高线梯度着色
_contenxt.fillStyle = acolor;
_contenxt.fill();
//等高线线条
_contenxt.strokeStyle = acolor;
_contenxt.stroke();
}
context.clearRect(0, 0, width, height);
context.globalAlpha = 0.3; //设置透明度
context.drawImage(_canvas, 0, 0, width, height);
this.completeRender();
};
}
function maptalksIDW(data, extext) {
var s = new Date().getTime();
var d = data;
//已有点初始二维数组
var width = Math.ceil(extext.pxmax - extext.pxmin);
var height = Math.ceil(extext.pymax - extext.pymin);
var dlen = d.length;
var colx = extext.pxmax;
var coly = extext.pymax;
var matrixDataLen = colx * coly;
var matrixData = new Array(matrixDataLen);
var _s = new Date().getTime();
for (var i = 0; i < dlen; i++) {
var point = d[i];
matrixData[point.y * width + point.x] = point.value;
}
var _e = new Date().getTime();
console.log('测试' + (_e - _s) / 1000 + '秒');
var idwcount = 0;
// 从左上原点计算到mask的xmax,ymax
for (var i = 0, k1 = 0; i < extext.pymax; i++) {
for (var j = 0; j < extext.pxmax; 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;
// }
if (i < extext.pymin || j < extext.pxmin) {//不在区域不做计算
matrixData[k1] = 1
} else {
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 + '秒');
/**
* 插值矩阵数据,时间复杂度O(height*width*len)
*/
// var idwcount = 0
// for (var i = extext.pymin, k1 = 0; i < extext.pymax; i++) {
// for (var j = extext.pxmin; j < extext.pxmax; j++ , k1++) {
// if (!matrixData[i * j + 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[i * j + k1] = sum0 / sum1;
// else
// matrixData[i * j + k1] = 1;
// }
// }
// }
// console.log(idwcount)
// var e = new Date().getTime();
// console.log('插值:' + (e - s) / 1000 + '秒');
return matrixData;
}
// function maptalksIDW(data, width, height) {
// var s = new Date().getTime();
// // this.imapData = this.idw(this.mapData, this.width, this.height);
// // this.newData[i] = this.imapData[i] - this.mode.referenceValue;
// var d = data;
// //已有点初始二维数组
// var dlen = d.length;
// var matrixData = new Array(width * height);
// /**
// * 插值矩阵数据,时间复杂度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>
</body>
</html>