my-openlayer 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -94,7 +94,8 @@ point.addPoint([
94
94
  lttd: 29.969,
95
95
  name: '测试点位'
96
96
  }
97
- ], 'test-point', {
97
+ ], {
98
+ type: 'test-point',
98
99
  nameKey: 'name', // 名称字段
99
100
  img: 'marker.png', // 图标路径
100
101
  hasImg: true, // 是否显示图标
package/dist/core/Line.js CHANGED
@@ -26,7 +26,7 @@ export default class Line {
26
26
  }),
27
27
  });
28
28
  },
29
- zIndex: options.zIndex ?? 3
29
+ zIndex: options.zIndex ?? 15
30
30
  });
31
31
  this[options.type + 'Layer'] = layer;
32
32
  this.map.addLayer(layer);
@@ -64,7 +64,7 @@ export default class Line {
64
64
  })
65
65
  });
66
66
  },
67
- zIndex: options.zIndex ?? 6
67
+ zIndex: options.zIndex ?? 15
68
68
  });
69
69
  riverLayer.setVisible(false);
70
70
  this.riverLayerList.push(riverLayer);
@@ -102,7 +102,7 @@ export default class Line {
102
102
  features: (new GeoJSON()).readFeatures(arr)
103
103
  }),
104
104
  style: (feature) => this.setFeatureAttr(feature),
105
- zIndex: 3
105
+ zIndex: 15
106
106
  });
107
107
  this.map.addLayer(riverLayer);
108
108
  }
@@ -30,7 +30,7 @@ export default class MapBaseLayers {
30
30
  }
31
31
  if (this.layers && Object.keys(this.layers).length !== 0) {
32
32
  this.addMapLayer();
33
- // this.switchBaseLayer(<string>Object.keys(this.layers)[0])
33
+ this.switchBaseLayer(Object.keys(this.layers)[0]);
34
34
  }
35
35
  }
36
36
  }
@@ -42,13 +42,13 @@ export default class MapBaseLayers {
42
42
  for (const key in this.layers) {
43
43
  this.layers[key]?.forEach((layer) => {
44
44
  layer.setVisible(key === type);
45
- console.log(layer);
45
+ // console.log(layer)
46
46
  });
47
47
  }
48
48
  }
49
49
  initLayer() {
50
50
  if (this.layers && this.options.token) {
51
- const { token, zIndex = 10 } = this.options;
51
+ const { token, zIndex = 9 } = this.options;
52
52
  this.layers.vec_c = [MapBaseLayers.getTiandiTuLayer({ type: 'vec_c', token, zIndex, visible: false })];
53
53
  this.layers.img_c = [MapBaseLayers.getTiandiTuLayer({ type: 'img_c', token, zIndex, visible: false })];
54
54
  this.layers.ter_c = [MapBaseLayers.getTiandiTuLayer({ type: 'ter_c', token, zIndex, visible: false })];
@@ -68,7 +68,7 @@ export default class Point {
68
68
  source: new VectorSource({
69
69
  features: pointFeatureList
70
70
  }),
71
- zIndex: options.zIndex || 4,
71
+ zIndex: options.zIndex || 21,
72
72
  });
73
73
  PointVectorLayer.setVisible(options.visible === undefined ? true : options.visible);
74
74
  this.map.addLayer(PointVectorLayer);
@@ -122,7 +122,7 @@ export default class Point {
122
122
  }
123
123
  return new Style(style);
124
124
  },
125
- zIndex: options.zIndex || 4,
125
+ zIndex: options.zIndex || 21,
126
126
  });
127
127
  clusterLayer.setVisible(options.visible === undefined ? true : options.visible);
128
128
  this.map.addLayer(clusterLayer);
@@ -177,7 +177,7 @@ export default class Point {
177
177
  })
178
178
  });
179
179
  },
180
- zIndex: 6
180
+ zIndex: 21
181
181
  });
182
182
  this.map.addLayer(basinLayer);
183
183
  this.setTwinkleLayer(twinkleList, className, key, (twinkleItem) => {
@@ -3,7 +3,7 @@ import VectorLayer from "ol/layer/Vector";
3
3
  import VectorSource from "ol/source/Vector";
4
4
  import { Image as ImageLayer } from "ol/layer";
5
5
  import { Geometry } from "ol/geom";
6
- import { OptionsType, MapJSONData } from '../types';
6
+ import { OptionsType, MapJSONData, PointData, HeatMapOptions } from '../types';
7
7
  export default class Polygon {
8
8
  map: Map;
9
9
  private colorMap;
@@ -19,21 +19,21 @@ export default class Polygon {
19
19
  * @param data 图层数据
20
20
  * @param options 图层配置
21
21
  */
22
- addBorderPolygon(data: MapJSONData, options: OptionsType): void;
22
+ addBorderPolygon(data: MapJSONData, options?: OptionsType): void;
23
23
  addPolygon(dataJSON: MapJSONData, options?: OptionsType): VectorLayer<VectorSource<Geometry>>;
24
24
  /**
25
25
  * 根据数据数组更新某个面颜色
26
26
  * @param layerName 图层名称
27
- * @param colorObj 数据
27
+ * @param colorObj 数据 不传或者传{}则重置所有颜色
28
28
  * colorObj:{
29
29
  * 对应geojson文件中的索引字段[propName]: 'rgba(255, 0, 0, 0.6)', // 颜色
30
30
  * ...
31
31
  * }
32
32
  * @param options 配置项
33
33
  */
34
- updateFeatureColors(layerName: string, colorObj: {
34
+ updateFeatureColors(layerName: string, colorObj?: {
35
35
  [propName: string]: string;
36
- }, options: OptionsType): void;
36
+ }, options?: OptionsType): void;
37
37
  /**
38
38
  * 设置外围蒙版图层
39
39
  *
@@ -57,5 +57,6 @@ export default class Polygon {
57
57
  * @param options 图层配置
58
58
  */
59
59
  addImage(layerName: string, img?: string, extent?: number[], options?: OptionsType): import("ol/layer/Base").default | VectorLayer<VectorSource<Geometry>> | ImageLayer<import("ol/source").Image>;
60
+ addHeatmap(layerName: string, pointData: PointData[], options: HeatMapOptions): void;
60
61
  removePolygonLayer(layerName: string): void;
61
62
  }
@@ -3,8 +3,8 @@ import VectorLayer from "ol/layer/Vector";
3
3
  import VectorSource from "ol/source/Vector";
4
4
  import GeoJSON from "ol/format/GeoJSON";
5
5
  import { Fill, Stroke, Style, Text } from "ol/style";
6
- import { Image as ImageLayer } from "ol/layer";
7
- import { Geometry, LinearRing } from "ol/geom";
6
+ import { Image as ImageLayer, Heatmap } from "ol/layer";
7
+ import { Geometry, LinearRing, Point } from "ol/geom";
8
8
  import { fromExtent } from "ol/geom/Polygon";
9
9
  import Feature from "ol/Feature";
10
10
  import ImageStatic from "ol/source/ImageStatic";
@@ -32,6 +32,7 @@ export default class Polygon {
32
32
  * @param options 图层配置
33
33
  */
34
34
  addBorderPolygon(data, options) {
35
+ options = options ?? {};
35
36
  options.type = options.type ?? 'border';
36
37
  options.fillColor = options.fillColor ?? 'rgba(255, 255, 255, 0)';
37
38
  this.addPolygon(data, options);
@@ -40,7 +41,8 @@ export default class Polygon {
40
41
  }
41
42
  // 添加分区
42
43
  //fyBasinJson中的id的key需要跟options中的nameKey一致
43
- addPolygon(dataJSON, options = {}) {
44
+ addPolygon(dataJSON, options) {
45
+ options = options ?? {};
44
46
  if (options.type != null) {
45
47
  MapTools.removeLayer(this.map, options.type);
46
48
  }
@@ -50,29 +52,52 @@ export default class Polygon {
50
52
  source: new VectorSource({
51
53
  features: (new GeoJSON()).readFeatures(dataJSON, options.projectionOptOptions ?? {})
52
54
  }),
53
- style: function (feature) {
54
- feature.set('type', options.type);
55
- feature.set('layerName', options.type);
56
- return new Style({
55
+ // style: function (feature: any) {
56
+ // feature.set('type', options?.type)
57
+ // feature.set('layerName', options?.type)
58
+ // return new Style({
59
+ // stroke: new Stroke({
60
+ // color: options?.strokeColor ?? '#EBEEF5',
61
+ // width: options?.strokeWidth ?? 2,
62
+ // lineDash: options?.lineDash,
63
+ // lineDashOffset: options?.lineDashOffset
64
+ // }),
65
+ // fill: new Fill({ color: options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
66
+ // text: new Text({
67
+ // text: options?.textVisible && options.nameKey ? feature.values_[options.nameKey] : "",
68
+ // font: options?.textFont ?? '14px Calibri,sans-serif',
69
+ // fill: new Fill({ color: options?.textFillColor ?? '#FFF' }),
70
+ // stroke: new Stroke({
71
+ // color: options?.textStrokeColor ?? '#409EFF',
72
+ // width: options?.textStrokeWidth ?? 2
73
+ // })
74
+ // })
75
+ // })
76
+ // },
77
+ zIndex: options.zIndex ?? 11
78
+ });
79
+ const features = layer.getSource()?.getFeatures();
80
+ features?.forEach(feature => {
81
+ feature.set('type', options?.type);
82
+ feature.set('layerName', options?.type);
83
+ feature.setStyle(new Style({
84
+ stroke: new Stroke({
85
+ color: options?.strokeColor ?? '#EBEEF5',
86
+ width: options?.strokeWidth ?? 2,
87
+ lineDash: options?.lineDash,
88
+ lineDashOffset: options?.lineDashOffset
89
+ }),
90
+ fill: new Fill({ color: options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
91
+ text: new Text({
92
+ text: options?.textVisible && options.nameKey ? feature.get(options.nameKey) : "",
93
+ font: options?.textFont ?? '14px Calibri,sans-serif',
94
+ fill: new Fill({ color: options?.textFillColor ?? '#FFF' }),
57
95
  stroke: new Stroke({
58
- color: options.strokeColor ?? '#EBEEF5',
59
- width: options.strokeWidth ?? 2,
60
- lineDash: options.lineDash,
61
- lineDashOffset: options.lineDashOffset
62
- }),
63
- fill: new Fill({ color: options.fillColor || 'rgba(255, 255, 255, 0.3)' }),
64
- text: new Text({
65
- text: options.textVisible && options.nameKey ? feature.values_[options.nameKey] : "",
66
- font: options.textFont ?? '14px Calibri,sans-serif',
67
- fill: new Fill({ color: options.textFillColor ?? '#FFF' }),
68
- stroke: new Stroke({
69
- color: options.textStrokeColor ?? '#409EFF',
70
- width: options.textStrokeWidth ?? 2
71
- })
96
+ color: options?.textStrokeColor ?? '#409EFF',
97
+ width: options?.textStrokeWidth ?? 2
72
98
  })
73
- });
74
- },
75
- zIndex: options.zIndex ?? 2
99
+ })
100
+ }));
76
101
  });
77
102
  layer.setVisible(options.visible === undefined ? true : options.visible);
78
103
  this.map.addLayer(layer);
@@ -88,7 +113,7 @@ export default class Polygon {
88
113
  /**
89
114
  * 根据数据数组更新某个面颜色
90
115
  * @param layerName 图层名称
91
- * @param colorObj 数据
116
+ * @param colorObj 数据 不传或者传{}则重置所有颜色
92
117
  * colorObj:{
93
118
  * 对应geojson文件中的索引字段[propName]: 'rgba(255, 0, 0, 0.6)', // 颜色
94
119
  * ...
@@ -98,30 +123,31 @@ export default class Polygon {
98
123
  updateFeatureColors(layerName, colorObj, options) {
99
124
  const layer = MapTools.getLayerByLayerName(this.map, layerName)[0];
100
125
  if (layer instanceof VectorLayer) {
101
- const source = layer.getSource();
102
- const features = source.getFeatures();
126
+ const features = layer.getSource()?.getFeatures();
103
127
  features.forEach((feature) => {
104
- if (options.nameKey) {
105
- const name = feature['values_'][options.nameKey];
106
- const newColor = colorObj[name];
107
- if (newColor) {
108
- feature.setStyle(new Style({
128
+ if (options?.nameKey || (!colorObj || Object.keys(colorObj).length === 0)) {
129
+ const name = options?.nameKey ? feature.get(options.nameKey) : '';
130
+ const newColor = colorObj?.[name];
131
+ // const oldStyle = feature.getStyle()
132
+ // console.log(oldStyle)
133
+ // if (newColor) {
134
+ feature.setStyle(new Style({
135
+ stroke: new Stroke({
136
+ color: options?.strokeColor ?? '#EBEEF5',
137
+ width: options?.strokeWidth ?? 2
138
+ }),
139
+ fill: new Fill({ color: newColor || options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
140
+ text: new Text({
141
+ text: options?.textVisible === false ? "" : name,
142
+ font: options?.textFont ?? '14px Calibri,sans-serif',
143
+ fill: new Fill({ color: options?.textFillColor || '#FFF' }),
109
144
  stroke: new Stroke({
110
- color: options.strokeColor ?? '#EBEEF5',
111
- width: options.strokeWidth ?? 2
112
- }),
113
- fill: new Fill({ color: newColor }),
114
- text: new Text({
115
- text: options.textVisible === false ? "" : name,
116
- font: options.textFont ?? '14px Calibri,sans-serif',
117
- fill: new Fill({ color: options.textFillColor || '#FFF' }),
118
- stroke: new Stroke({
119
- color: options.textStrokeColor ?? '#409EFF',
120
- width: options.textStrokeWidth ?? 2
121
- })
145
+ color: options?.textStrokeColor ?? '#409EFF',
146
+ width: options?.textStrokeWidth ?? 2
122
147
  })
123
- }));
124
- }
148
+ })
149
+ }));
150
+ // }
125
151
  }
126
152
  });
127
153
  }
@@ -203,7 +229,7 @@ export default class Polygon {
203
229
  const vtLayer = new VectorLayer({
204
230
  source: vtSource,
205
231
  style: shadeStyle,
206
- zIndex: options?.zIndex ?? 99
232
+ zIndex: options?.zIndex ?? 12
207
233
  });
208
234
  this.map.addLayer(vtLayer);
209
235
  const features = new GeoJSON().readFeatures(data);
@@ -223,7 +249,7 @@ export default class Polygon {
223
249
  * @param extent 图片范围(对角线坐标) [minx, miny, maxx, maxy]
224
250
  * @param options 图层配置
225
251
  */
226
- addImage(layerName, img, extent, options = { zIndex: 3 }) {
252
+ addImage(layerName, img, extent, options) {
227
253
  let imageLayer = MapTools.getLayerByLayerName(this.map, layerName)[0];
228
254
  if (img && extent) {
229
255
  const source = new ImageStatic({
@@ -239,12 +265,12 @@ export default class Polygon {
239
265
  imageLayer.set('layerName', layerName);
240
266
  if (imageLayer instanceof ImageLayer)
241
267
  imageLayer.setSource(source);
242
- imageLayer.setZIndex(options.zIndex ?? 3);
243
- imageLayer.setOpacity(options.opacity ?? 1);
244
- if (options.visible !== undefined)
245
- imageLayer.setVisible(options.visible);
246
- if (options.mapClip && options.mapClipData) {
247
- imageLayer = MapTools.setMapClip(imageLayer, options.mapClipData);
268
+ imageLayer.setZIndex(options?.zIndex ?? 11);
269
+ imageLayer.setOpacity(options?.opacity ?? 1);
270
+ if (options?.visible !== undefined)
271
+ imageLayer.setVisible(options?.visible);
272
+ if (options?.mapClip && options?.mapClipData) {
273
+ imageLayer = MapTools.setMapClip(imageLayer, options?.mapClipData);
248
274
  }
249
275
  this.map.addLayer(imageLayer);
250
276
  }
@@ -254,6 +280,27 @@ export default class Polygon {
254
280
  }
255
281
  return imageLayer;
256
282
  }
283
+ addHeatmap(layerName, pointData, options) {
284
+ const heatmapLayer = new Heatmap({
285
+ source: new VectorSource(),
286
+ weight: function (fea) {
287
+ return fea.get('weight');
288
+ },
289
+ blur: options.blur ?? 15,
290
+ radius: options.radius ?? 10,
291
+ zIndex: options.zIndex ?? 11,
292
+ opacity: options.opacity ?? 1,
293
+ });
294
+ heatmapLayer.set('layerName', layerName);
295
+ this.map.addLayer(heatmapLayer);
296
+ const max = Math.max(...pointData.map(item => item[options.valueKey]));
297
+ pointData.forEach((item) => {
298
+ heatmapLayer?.getSource().addFeature(new Feature({
299
+ geometry: new Point([item.lgtd, item.lttd]),
300
+ weight: item[options.valueKey] / max //热力值范围是【0,1】;热力值计算 = 找出数据集中的最大值,然后用值除以最大值
301
+ }));
302
+ });
303
+ }
257
304
  removePolygonLayer(layerName) {
258
305
  MapTools.removeLayer(this.map, layerName);
259
306
  this[layerName] = null;
package/dist/types.d.ts CHANGED
@@ -48,6 +48,15 @@ export interface AnnotationLayerOptions {
48
48
  zIndex?: number;
49
49
  visible?: boolean;
50
50
  }
51
+ export interface HeatMapOptions {
52
+ radius?: number;
53
+ blur?: number;
54
+ gradient?: string[];
55
+ opacity?: number;
56
+ visible?: boolean;
57
+ zIndex?: number;
58
+ valueKey: string;
59
+ }
51
60
  export interface OptionsType {
52
61
  type?: string;
53
62
  nameKey?: string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "0.1.4",
4
+ "version": "0.1.5",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",