my-openlayer 0.1.18 → 1.0.0

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.
@@ -9,7 +9,15 @@ import { fromExtent } from "ol/geom/Polygon";
9
9
  import Feature from "ol/Feature";
10
10
  import ImageStatic from "ol/source/ImageStatic";
11
11
  import MapTools from "./MapTools";
12
+ /**
13
+ * Polygon 类用于处理地图上的面要素操作
14
+ * 包括添加多边形、边框、图片图层、热力图等功能
15
+ */
12
16
  export default class Polygon {
17
+ /**
18
+ * 构造函数
19
+ * @param map OpenLayers 地图实例
20
+ */
13
21
  constructor(map) {
14
22
  this.colorMap = {
15
23
  '0': 'rgba(255, 0, 0, 0.6)',
@@ -17,123 +25,228 @@ export default class Polygon {
17
25
  '2': 'rgba(255, 238, 0, 0.6)',
18
26
  '3': 'rgba(1, 111, 255, 0.6)'
19
27
  };
28
+ if (!map) {
29
+ throw new Error('Map instance is required');
30
+ }
20
31
  this.map = map;
21
32
  }
22
33
  /**
23
34
  * 获取等级颜色
24
- * @param lev
35
+ * @param lev 等级值,支持字符串或数字
36
+ * @returns 对应等级的颜色值,如果等级不存在则返回默认颜色
25
37
  */
26
38
  getLevColor(lev) {
27
- return this.colorMap[lev.toString()];
39
+ const key = lev.toString();
40
+ return this.colorMap[key] || 'rgba(128, 128, 128, 0.6)';
28
41
  }
29
42
  /**
30
- * 添加 地图 边框 图层
31
- * @param data 图层数据
32
- * @param options 图层配置
43
+ * 添加地图边框图层
44
+ * @param data 图层数据,必须是有效的 GeoJSON 格式
45
+ * @param options 图层配置选项
46
+ * @returns 创建的图层实例
47
+ * @throws 当数据格式无效时抛出错误
33
48
  */
34
49
  addBorderPolygon(data, options) {
35
- options = options ?? {};
36
- options.layerName = options.layerName ?? 'border';
37
- options.fillColor = options.fillColor ?? 'rgba(255, 255, 255, 0)';
38
- this.addPolygon(data, options);
39
- if (options.mask)
50
+ if (!data || !data.features || !Array.isArray(data.features)) {
51
+ throw new Error('Invalid GeoJSON data: features array is required');
52
+ }
53
+ const mergedOptions = {
54
+ layerName: 'border',
55
+ fillColor: 'rgba(255, 255, 255, 0)',
56
+ ...options
57
+ };
58
+ const layer = this.addPolygon(data, mergedOptions);
59
+ if (mergedOptions.mask) {
40
60
  this.setOutLayer(data);
61
+ }
62
+ return layer;
41
63
  }
42
- // 添加分区
43
- //fyBasinJson中的id的key需要跟options中的nameKey一致
64
+ /**
65
+ * 添加多边形图层
66
+ * @param dataJSON GeoJSON 数据
67
+ * @param options 图层配置选项
68
+ * @returns 创建的矢量图层
69
+ * @throws 当数据格式无效时抛出错误
70
+ */
44
71
  addPolygon(dataJSON, options) {
45
- options = options ?? {};
46
- if (options.layerName != null) {
47
- MapTools.removeLayer(this.map, options.layerName);
72
+ if (!dataJSON || !dataJSON.features || !Array.isArray(dataJSON.features)) {
73
+ throw new Error('Invalid GeoJSON data: features array is required');
74
+ }
75
+ const mergedOptions = {
76
+ zIndex: 11,
77
+ visible: true,
78
+ strokeColor: '#EBEEF5',
79
+ strokeWidth: 2,
80
+ fillColor: 'rgba(255, 255, 255, 0.3)',
81
+ textFont: '14px Calibri,sans-serif',
82
+ textFillColor: '#FFF',
83
+ textStrokeColor: '#409EFF',
84
+ textStrokeWidth: 2,
85
+ ...options
86
+ };
87
+ // 如果指定了图层名称,先移除同名图层
88
+ if (mergedOptions.layerName) {
89
+ new MapTools(this.map).removeLayer(mergedOptions.layerName);
90
+ }
91
+ let features;
92
+ try {
93
+ features = new GeoJSON().readFeatures(dataJSON, mergedOptions.projectionOptOptions ?? {});
94
+ }
95
+ catch (error) {
96
+ throw new Error(`Failed to parse GeoJSON data: ${error}`);
48
97
  }
49
98
  const layer = new VectorLayer({
50
- name: options.layerName,
51
- layerName: options.layerName,
52
- source: new VectorSource({
53
- features: (new GeoJSON()).readFeatures(dataJSON, options.projectionOptOptions ?? {})
54
- }),
55
- zIndex: options.zIndex ?? 11
99
+ properties: {
100
+ name: mergedOptions.layerName,
101
+ layerName: mergedOptions.layerName
102
+ },
103
+ source: new VectorSource({ features }),
104
+ zIndex: mergedOptions.zIndex
56
105
  });
57
- const features = layer.getSource()?.getFeatures();
58
- features?.forEach(feature => {
59
- feature.set('type', options?.type);
60
- feature.set('layerName', options?.type);
61
- const fillColor = options?.fillColorCallBack ? options.fillColorCallBack(feature) : options?.fillColor;
106
+ // 设置要素样式
107
+ this.setFeatureStyles(features, mergedOptions);
108
+ layer.setVisible(mergedOptions.visible);
109
+ this.map.addLayer(layer);
110
+ // 如果需要适应视图
111
+ if (mergedOptions.fitView) {
112
+ this.fitViewToLayer(layer);
113
+ }
114
+ return layer;
115
+ }
116
+ /**
117
+ * 设置要素样式
118
+ * @param features 要素数组
119
+ * @param options 样式配置选项
120
+ */
121
+ setFeatureStyles(features, options) {
122
+ features.forEach(feature => {
123
+ feature.set('type', options.type || options.layerName);
124
+ feature.set('layerName', options.type || options.layerName);
125
+ const fillColor = options.fillColorCallBack
126
+ ? options.fillColorCallBack(feature)
127
+ : options.fillColor;
62
128
  const featureStyle = new Style({
63
129
  stroke: new Stroke({
64
- color: options?.strokeColor ?? '#EBEEF5',
65
- width: options?.strokeWidth ?? 2,
66
- lineDash: options?.lineDash,
67
- lineDashOffset: options?.lineDashOffset
130
+ color: options.strokeColor,
131
+ width: options.strokeWidth,
132
+ lineDash: options.lineDash,
133
+ lineDashOffset: options.lineDashOffset
68
134
  }),
69
- fill: new Fill({ color: fillColor ?? 'rgba(255, 255, 255, 0.3)' }),
135
+ fill: new Fill({ color: fillColor })
70
136
  });
71
- if (options?.textVisible) {
72
- const text = (options?.textCallBack ? options?.textCallBack(feature) : '') || (options.nameKey ? feature.get(options.nameKey) : "");
73
- featureStyle.setText(new Text({
74
- text: text,
75
- font: options?.textFont ?? '14px Calibri,sans-serif',
76
- fill: new Fill({ color: options?.textFillColor ?? '#FFF' }),
77
- stroke: new Stroke({
78
- color: options?.textStrokeColor ?? '#409EFF',
79
- width: options?.textStrokeWidth ?? 2
80
- })
81
- }));
137
+ // 添加文本样式
138
+ if (options.textVisible) {
139
+ const text = this.getFeatureText(feature, options);
140
+ if (text) {
141
+ featureStyle.setText(new Text({
142
+ text,
143
+ font: options.textFont,
144
+ fill: new Fill({ color: options.textFillColor }),
145
+ stroke: new Stroke({
146
+ color: options.textStrokeColor,
147
+ width: options.textStrokeWidth
148
+ })
149
+ }));
150
+ }
82
151
  }
83
152
  feature.setStyle(featureStyle);
84
153
  });
85
- layer.setVisible(options.visible === undefined ? true : options.visible);
86
- this.map.addLayer(layer);
87
- if (options.fitView) {
88
- // 获取面的范围
89
- const extent = layer.getSource()?.getExtent();
90
- // 适应这个范围
91
- if (extent)
92
- this.map.getView().fit(extent, { duration: 500 });
154
+ }
155
+ /**
156
+ * 获取要素文本
157
+ * @param feature 要素对象
158
+ * @param options 配置选项
159
+ * @returns 文本内容
160
+ */
161
+ getFeatureText(feature, options) {
162
+ if (options.textCallBack) {
163
+ return options.textCallBack(feature) || '';
164
+ }
165
+ if (options.nameKey) {
166
+ return feature.get(options.nameKey) || '';
167
+ }
168
+ return '';
169
+ }
170
+ /**
171
+ * 适应图层视图
172
+ * @param layer 图层对象
173
+ */
174
+ fitViewToLayer(layer) {
175
+ const extent = layer.getSource()?.getExtent();
176
+ if (extent) {
177
+ this.map.getView().fit(extent, { duration: 500 });
93
178
  }
94
- return layer;
95
179
  }
96
180
  /**
97
181
  * 根据数据数组更新某个面颜色
98
182
  * @param layerName 图层名称
99
- * @param colorObj 数据 不传或者传{}则重置所有颜色
100
- * colorObj:{
101
- * 对应geojson文件中的索引字段[propName]: 'rgba(255, 0, 0, 0.6)', // 颜色
102
- * ...
103
- * }
183
+ * @param colorObj 颜色映射对象,键为要素属性值,值为颜色字符串
104
184
  * @param options 配置项
185
+ * @throws 当图层不存在时抛出错误
105
186
  */
106
187
  updateFeatureColor(layerName, colorObj, options) {
107
- const layer = MapTools.getLayerByLayerName(this.map, layerName)[0];
108
- if (layer instanceof VectorLayer) {
109
- const features = layer.getSource()?.getFeatures();
110
- features?.forEach((feature) => {
111
- if (options?.nameKey || (!colorObj || Object.keys(colorObj).length === 0)) {
112
- const name = options?.nameKey ? feature.get(options.nameKey) : '';
113
- const newColor = colorObj?.[name];
114
- const featureStyle = new Style({
115
- stroke: new Stroke({
116
- color: options?.strokeColor ?? '#EBEEF5',
117
- width: options?.strokeWidth ?? 2
118
- }),
119
- fill: new Fill({ color: newColor || options?.fillColor || 'rgba(255, 255, 255, 0.3)' }),
120
- });
121
- if (options?.textVisible) {
122
- const text = (options?.textCallBack ? options?.textCallBack(feature) : '') || (options.nameKey ? feature.get(options.nameKey) : "");
123
- featureStyle.setText(new Text({
124
- text,
125
- font: options?.textFont ?? '14px Calibri,sans-serif',
126
- fill: new Fill({ color: options?.textFillColor || '#FFF' }),
127
- stroke: new Stroke({
128
- color: options?.textStrokeColor ?? '#409EFF',
129
- width: options?.textStrokeWidth ?? 2
130
- })
131
- }));
132
- }
133
- feature.setStyle(featureStyle);
134
- }
135
- });
188
+ if (!layerName) {
189
+ throw new Error('Layer name is required');
190
+ }
191
+ const layers = MapTools.getLayerByLayerName(this.map, layerName);
192
+ if (layers.length === 0) {
193
+ throw new Error(`Layer with name '${layerName}' not found`);
136
194
  }
195
+ const layer = layers[0];
196
+ if (!(layer instanceof VectorLayer)) {
197
+ throw new Error(`Layer '${layerName}' is not a vector layer`);
198
+ }
199
+ const mergedOptions = {
200
+ strokeColor: '#EBEEF5',
201
+ strokeWidth: 2,
202
+ fillColor: 'rgba(255, 255, 255, 0.3)',
203
+ textFont: '14px Calibri,sans-serif',
204
+ textFillColor: '#FFF',
205
+ textStrokeColor: '#409EFF',
206
+ textStrokeWidth: 2,
207
+ ...options
208
+ };
209
+ const features = layer.getSource()?.getFeatures();
210
+ if (!features) {
211
+ console.warn(`No features found in layer '${layerName}'`);
212
+ return;
213
+ }
214
+ features.forEach((feature) => {
215
+ this.updateSingleFeatureColor(feature, colorObj, mergedOptions);
216
+ });
217
+ }
218
+ /**
219
+ * 更新单个要素的颜色
220
+ * @param feature 要素对象
221
+ * @param colorObj 颜色映射对象
222
+ * @param options 配置选项
223
+ */
224
+ updateSingleFeatureColor(feature, colorObj, options) {
225
+ const name = options?.nameKey ? feature.get(options.nameKey) : '';
226
+ const newColor = colorObj?.[name] || options?.fillColor;
227
+ const featureStyle = new Style({
228
+ stroke: new Stroke({
229
+ color: options?.strokeColor,
230
+ width: options?.strokeWidth
231
+ }),
232
+ fill: new Fill({ color: newColor })
233
+ });
234
+ // 添加文本样式
235
+ if (options?.textVisible) {
236
+ const text = this.getFeatureText(feature, options);
237
+ if (text) {
238
+ featureStyle.setText(new Text({
239
+ text,
240
+ font: options.textFont,
241
+ fill: new Fill({ color: options.textFillColor }),
242
+ stroke: new Stroke({
243
+ color: options.textStrokeColor,
244
+ width: options.textStrokeWidth
245
+ })
246
+ }));
247
+ }
248
+ }
249
+ feature.setStyle(featureStyle);
137
250
  }
138
251
  /**
139
252
  * 设置外围蒙版图层
@@ -227,65 +340,133 @@ export default class Polygon {
227
340
  }
228
341
  /**
229
342
  * 添加图片图层
230
- * @param layerName 图层名称
231
- * @param img 图片地址
232
- * @param extent 图片范围(对角线坐标) [minx, miny, maxx, maxy]
233
- * @param options 图层配置
343
+ * @param imageData 图片数据,包含url和extent
344
+ * @param options 配置项
345
+ * @returns 创建的图片图层
346
+ * @throws 当数据格式无效时抛出错误
234
347
  */
235
- addImage(layerName, img, extent, options) {
236
- let imageLayer = MapTools.getLayerByLayerName(this.map, layerName)[0];
237
- if (img && extent) {
238
- const source = new ImageStatic({
239
- url: img,
240
- imageExtent: extent
241
- });
242
- if (imageLayer && imageLayer instanceof ImageLayer) {
243
- imageLayer.setSource(source);
244
- }
245
- else {
246
- imageLayer = new ImageLayer();
247
- imageLayer.set('name', layerName);
248
- imageLayer.set('layerName', layerName);
249
- if (imageLayer instanceof ImageLayer)
250
- imageLayer.setSource(source);
251
- imageLayer.setZIndex(options?.zIndex ?? 11);
252
- imageLayer.setOpacity(options?.opacity ?? 1);
253
- if (options?.visible !== undefined)
254
- imageLayer.setVisible(options?.visible);
255
- if (options?.mapClip && options?.mapClipData) {
256
- imageLayer = MapTools.setMapClip(imageLayer, options?.mapClipData);
257
- }
258
- this.map.addLayer(imageLayer);
259
- }
348
+ addImageLayer(imageData, options) {
349
+ if (!imageData || !imageData.img || !imageData.extent) {
350
+ throw new Error('Invalid image data: img and extent are required');
260
351
  }
261
- else {
262
- this.removePolygonLayer(layerName);
352
+ if (!Array.isArray(imageData.extent) || imageData.extent.length !== 4) {
353
+ throw new Error('Invalid extent: must be an array of 4 numbers [minX, minY, maxX, maxY]');
263
354
  }
355
+ const mergedOptions = {
356
+ opacity: 1,
357
+ visible: true,
358
+ zIndex: 11,
359
+ layerName: 'imageLayer',
360
+ ...options
361
+ };
362
+ // 如果指定了图层名称,先移除同名图层
363
+ if (mergedOptions.layerName) {
364
+ MapTools.removeLayer(this.map, mergedOptions.layerName);
365
+ }
366
+ const source = new ImageStatic({
367
+ url: imageData.img,
368
+ imageExtent: imageData.extent
369
+ });
370
+ const imageLayer = new ImageLayer({
371
+ source,
372
+ opacity: mergedOptions.opacity,
373
+ visible: mergedOptions.visible
374
+ });
375
+ imageLayer.set('name', mergedOptions.layerName);
376
+ imageLayer.set('layerName', mergedOptions.layerName);
377
+ imageLayer.setZIndex(mergedOptions.zIndex);
378
+ // 应用地图裁剪
379
+ if (mergedOptions.mapClip && mergedOptions.mapClipData) {
380
+ const clippedLayer = MapTools.setMapClip(imageLayer, mergedOptions.mapClipData);
381
+ this.map.addLayer(clippedLayer);
382
+ return clippedLayer;
383
+ }
384
+ this.map.addLayer(imageLayer);
264
385
  return imageLayer;
265
386
  }
266
- addHeatmap(layerName, pointData, options) {
387
+ /**
388
+ * 添加热力图图层
389
+ * @param pointData 点数据数组
390
+ * @param options 热力图配置
391
+ */
392
+ addHeatmap(pointData, options) {
393
+ // 只有在指定layerName时才移除已存在的同名图层
394
+ if (options?.layerName) {
395
+ new MapTools(this.map).removeLayer(options.layerName);
396
+ }
267
397
  const heatmapLayer = new Heatmap({
268
398
  source: new VectorSource(),
269
399
  weight: function (fea) {
270
400
  return fea.get('weight');
271
401
  },
272
- blur: options.blur ?? 15,
273
- radius: options.radius ?? 10,
274
- zIndex: options.zIndex ?? 11,
275
- opacity: options.opacity ?? 1,
402
+ blur: options?.blur ?? 15,
403
+ radius: options?.radius ?? 10,
404
+ zIndex: options?.zIndex ?? 11,
405
+ opacity: options?.opacity ?? 1,
276
406
  });
277
- heatmapLayer.set('layerName', layerName);
407
+ // 只有在指定layerName时才设置layerName
408
+ if (options?.layerName) {
409
+ heatmapLayer.set('layerName', options.layerName);
410
+ }
278
411
  this.map.addLayer(heatmapLayer);
279
- const max = Math.max(...pointData.map(item => item[options.valueKey]));
412
+ const valueKey = options?.valueKey || 'value';
413
+ const max = Math.max(...pointData.map(item => item[valueKey]));
280
414
  pointData.forEach((item) => {
281
415
  heatmapLayer?.getSource().addFeature(new Feature({
282
416
  geometry: new Point([item.lgtd, item.lttd]),
283
- weight: item[options.valueKey] / max //热力值范围是【0,1】;热力值计算 = 找出数据集中的最大值,然后用值除以最大值
417
+ weight: item[valueKey] / max //热力值范围是【0,1】;热力值计算 = 找出数据集中的最大值,然后用值除以最大值
284
418
  }));
285
419
  });
420
+ return heatmapLayer;
421
+ }
422
+ /**
423
+ * 添加遮罩图层
424
+ * @param data GeoJSON格式的遮罩数据
425
+ * @param options 配置项
426
+ * @returns 创建的遮罩图层
427
+ * @throws 当数据格式无效时抛出错误
428
+ */
429
+ addMaskLayer(data, options) {
430
+ if (!data) {
431
+ throw new Error('Mask data is required');
432
+ }
433
+ const mergedOptions = {
434
+ fillColor: 'rgba(0, 0, 0, 0.5)',
435
+ opacity: 1,
436
+ visible: true,
437
+ layerName: 'maskLayer',
438
+ ...options
439
+ };
440
+ let features;
441
+ try {
442
+ features = new GeoJSON().readFeatures(data);
443
+ }
444
+ catch (error) {
445
+ throw new Error(`Invalid GeoJSON data: ${error}`);
446
+ }
447
+ if (!features || features.length === 0) {
448
+ console.warn('No features found in mask data');
449
+ }
450
+ const maskLayer = new VectorLayer({
451
+ source: new VectorSource({ features }),
452
+ style: new Style({
453
+ fill: new Fill({
454
+ color: mergedOptions.fillColor
455
+ }),
456
+ stroke: mergedOptions.strokeColor ? new Stroke({
457
+ color: mergedOptions.strokeColor,
458
+ width: mergedOptions.strokeWidth || 1
459
+ }) : undefined
460
+ }),
461
+ opacity: mergedOptions.opacity,
462
+ visible: mergedOptions.visible
463
+ });
464
+ maskLayer.set('layerName', mergedOptions.layerName);
465
+ this.map.addLayer(maskLayer);
466
+ return maskLayer;
286
467
  }
287
468
  removePolygonLayer(layerName) {
288
- MapTools.removeLayer(this.map, layerName);
469
+ new MapTools(this.map).removeLayer(layerName);
289
470
  this[layerName] = null;
290
471
  }
291
472
  }
package/dist/index.d.ts CHANGED
@@ -1,10 +1,17 @@
1
- import MyOl from "./MyOl";
2
- import Line from "./core/Line";
3
- import Point from "./core/Point";
4
- import MapTools from "./core/MapTools";
5
- import Polygon from "./core/Polygon";
6
- import MapBaseLayers from "./core/MapBaseLayers";
7
- import DomPoint from "./core/DomPoint";
8
- import MeasureHandler from "./core/MeasureHandler";
9
- export { Line, Point, MapTools, Polygon, MapBaseLayers, DomPoint, MeasureHandler };
10
- export default MyOl;
1
+ export { default } from './MyOl';
2
+ export { default as MyOl } from './MyOl';
3
+ export { default as Point } from './core/Point';
4
+ export { default as Line } from './core/Line';
5
+ export { default as Polygon } from './core/Polygon';
6
+ export { default as MapBaseLayers } from './core/MapBaseLayers';
7
+ export { default as MapTools } from './core/MapTools';
8
+ export { default as MeasureHandler } from './core/MeasureHandler';
9
+ export { default as DomPoint } from './core/DomPoint';
10
+ export { ConfigManager } from './core/ConfigManager';
11
+ export { EventManager } from './core/EventManager';
12
+ export type { MapEventType, EventCallback, MapEventData } from './core/EventManager';
13
+ export { ErrorHandler, MyOpenLayersError, ErrorType } from './utils/ErrorHandler';
14
+ export type { BaseOptions, StyleOptions, TextOptions } from './types';
15
+ export type { PointOptions, LineOptions, PolygonOptions } from './types';
16
+ export type { OptionsType } from './types';
17
+ export type { MapInitType, MapLayersOptions, HeatMapOptions, ImageLayerData, MaskLayerOptions, ColorMap, FeatureColorUpdateOptions, PointData, LineData, ClusterOptions, MeasureHandlerType, EventType, DomPointOptions, MapJSONData, Feature, AnnotationType, TiandituType, MapLayers, AnnotationLayerOptions } from './types';
package/dist/index.js CHANGED
@@ -1,10 +1,15 @@
1
- import MyOl from "./MyOl";
2
- import Line from "./core/Line";
3
- import Point from "./core/Point";
4
- import MapTools from "./core/MapTools";
5
- import Polygon from "./core/Polygon";
6
- import MapBaseLayers from "./core/MapBaseLayers";
7
- import DomPoint from "./core/DomPoint";
8
- import MeasureHandler from "./core/MeasureHandler";
9
- export { Line, Point, MapTools, Polygon, MapBaseLayers, DomPoint, MeasureHandler };
10
- export default MyOl;
1
+ // 导出核心类 - 默认导出
2
+ export { default } from './MyOl';
3
+ export { default as MyOl } from './MyOl';
4
+ export { default as Point } from './core/Point';
5
+ export { default as Line } from './core/Line';
6
+ export { default as Polygon } from './core/Polygon';
7
+ export { default as MapBaseLayers } from './core/MapBaseLayers';
8
+ export { default as MapTools } from './core/MapTools';
9
+ export { default as MeasureHandler } from './core/MeasureHandler';
10
+ export { default as DomPoint } from './core/DomPoint';
11
+ // 新增工具类
12
+ export { ConfigManager } from './core/ConfigManager';
13
+ export { EventManager } from './core/EventManager';
14
+ // 错误处理
15
+ export { ErrorHandler, MyOpenLayersError, ErrorType } from './utils/ErrorHandler';