my-openlayer 1.0.1 → 1.0.3

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.
@@ -96,6 +96,31 @@ export default class Polygon {
96
96
  * @throws 当数据格式无效时抛出错误
97
97
  */
98
98
  addImageLayer(imageData: ImageLayerData, options?: PolygonOptions): ImageLayer<any>;
99
+ /**
100
+ * 尝试更新现有图层
101
+ * @private
102
+ */
103
+ private tryUpdateExistingImageLayer;
104
+ /**
105
+ * 创建新的图像图层
106
+ * @private
107
+ */
108
+ private createNewImageLayer;
109
+ /**
110
+ * 更新图层属性
111
+ * @private
112
+ */
113
+ private updateImageLayerProperties;
114
+ /**
115
+ * 配置图层基本属性
116
+ * @private
117
+ */
118
+ private configureImageLayer;
119
+ /**
120
+ * 添加图层到地图并应用裁剪
121
+ * @private
122
+ */
123
+ private addImageLayerToMap;
99
124
  /**
100
125
  * 添加热力图图层
101
126
  * @param pointData 点数据数组
@@ -341,7 +341,9 @@ export default class Polygon {
341
341
  * @throws 当数据格式无效时抛出错误
342
342
  */
343
343
  addImageLayer(imageData, options) {
344
- ValidationUtils.validateImageData(imageData);
344
+ // 检查是否允许空img(当存在layerName且存在同名图层时)
345
+ const allowEmptyImg = !imageData.img && !!options?.layerName;
346
+ ValidationUtils.validateImageData(imageData, allowEmptyImg);
345
347
  const mergedOptions = {
346
348
  opacity: 1,
347
349
  visible: true,
@@ -349,30 +351,100 @@ export default class Polygon {
349
351
  layerName: 'imageLayer',
350
352
  ...options
351
353
  };
352
- // 如果指定了图层名称,先移除同名图层
354
+ // 尝试更新现有图层
353
355
  if (mergedOptions.layerName) {
354
- MapTools.removeLayer(this.map, mergedOptions.layerName);
356
+ const existingLayer = this.tryUpdateExistingImageLayer(imageData, mergedOptions);
357
+ if (existingLayer) {
358
+ return existingLayer;
359
+ }
360
+ }
361
+ // 创建新图层
362
+ return this.createNewImageLayer(imageData, mergedOptions);
363
+ }
364
+ /**
365
+ * 尝试更新现有图层
366
+ * @private
367
+ */
368
+ tryUpdateExistingImageLayer(imageData, options) {
369
+ const existingLayers = MapTools.getLayerByLayerName(this.map, options.layerName);
370
+ if (existingLayers.length === 0) {
371
+ return null;
372
+ }
373
+ const existingLayer = existingLayers[0];
374
+ // 如果没有extent,直接设置source为undefined
375
+ if (!imageData.extent) {
376
+ existingLayer.setSource(undefined);
377
+ }
378
+ else {
379
+ // 创建新的source
380
+ const url = imageData.img || existingLayer.getSource()?.getUrl() || '';
381
+ const newSource = new ImageStatic({
382
+ url,
383
+ imageExtent: imageData.extent
384
+ });
385
+ existingLayer.setSource(newSource);
386
+ }
387
+ // 更新图层属性
388
+ this.updateImageLayerProperties(existingLayer, options);
389
+ return existingLayer;
390
+ }
391
+ /**
392
+ * 创建新的图像图层
393
+ * @private
394
+ */
395
+ createNewImageLayer(imageData, options) {
396
+ let source = undefined;
397
+ // 只有当extent存在时才创建ImageStatic source
398
+ if (imageData.extent) {
399
+ source = new ImageStatic({
400
+ url: imageData.img || '',
401
+ imageExtent: imageData.extent
402
+ });
355
403
  }
356
- const source = new ImageStatic({
357
- url: imageData.img,
358
- imageExtent: imageData.extent
359
- });
360
404
  const imageLayer = new ImageLayer({
361
405
  source,
362
- opacity: mergedOptions.opacity,
363
- visible: mergedOptions.visible
406
+ opacity: options.opacity,
407
+ visible: options.visible
364
408
  });
365
- imageLayer.set('name', mergedOptions.layerName);
366
- imageLayer.set('layerName', mergedOptions.layerName);
367
- imageLayer.setZIndex(mergedOptions.zIndex);
368
- // 应用地图裁剪
369
- if (mergedOptions.mapClip && mergedOptions.mapClipData) {
370
- const clippedLayer = MapTools.setMapClip(imageLayer, mergedOptions.mapClipData);
409
+ this.configureImageLayer(imageLayer, options);
410
+ return this.addImageLayerToMap(imageLayer, options);
411
+ }
412
+ /**
413
+ * 更新图层属性
414
+ * @private
415
+ */
416
+ updateImageLayerProperties(layer, options) {
417
+ if (options.opacity !== undefined) {
418
+ layer.setOpacity(options.opacity);
419
+ }
420
+ if (options.visible !== undefined) {
421
+ layer.setVisible(options.visible);
422
+ }
423
+ if (options.zIndex !== undefined) {
424
+ layer.setZIndex(options.zIndex);
425
+ }
426
+ }
427
+ /**
428
+ * 配置图层基本属性
429
+ * @private
430
+ */
431
+ configureImageLayer(layer, options) {
432
+ layer.set('name', options.layerName);
433
+ layer.set('layerName', options.layerName);
434
+ layer.setZIndex(options.zIndex);
435
+ }
436
+ /**
437
+ * 添加图层到地图并应用裁剪
438
+ * @private
439
+ */
440
+ addImageLayerToMap(layer, options) {
441
+ if (options.mapClip && options.mapClipData) {
442
+ const clippedLayer = MapTools.setMapClip(layer, options.mapClipData);
371
443
  this.map.addLayer(clippedLayer);
372
444
  return clippedLayer;
373
445
  }
374
- this.map.addLayer(imageLayer);
375
- return imageLayer;
446
+ this.map.addLayer(layer);
447
+ return layer;
376
448
  }
377
449
  /**
378
450
  * 添加热力图图层
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "my-openlayer",
3
3
  "private": false,
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/index.d.ts",
@@ -13,14 +13,17 @@
13
13
  "vue"
14
14
  ],
15
15
  "files": [
16
- "dist/*",
17
- "LICENSE",
18
- "README.md"
16
+ "**/*",
17
+ "!scripts",
18
+ "!temp-publish"
19
19
  ],
20
20
  "scripts": {
21
21
  "dev": "vite",
22
22
  "build": "tsc",
23
- "preview": "vite preview"
23
+ "preview": "vite preview",
24
+ "prepare-publish": "node scripts/prepare-publish.cjs",
25
+ "publish-flat": "npm run build && npm run prepare-publish && cd temp-publish && npm publish",
26
+ "publish-flat-dry": "npm run build && npm run prepare-publish && cd temp-publish && npm publish --dry-run"
24
27
  },
25
28
  "dependencies": {
26
29
  "@turf/turf": "^7.2.0",
@@ -39,4 +42,4 @@
39
42
  "peerDependencies": {
40
43
  "ol": "^6.15.1"
41
44
  }
42
- }
45
+ }
@@ -90,9 +90,10 @@ export declare class ValidationUtils {
90
90
  /**
91
91
  * 验证图像数据
92
92
  * @param imageData 图像数据
93
+ * @param allowEmptyImg 是否允许img为空
93
94
  * @throws 如果图像数据无效则抛出异常
94
95
  */
95
- static validateImageData(imageData: any): void;
96
+ static validateImageData(imageData: any, allowEmptyImg?: boolean): void;
96
97
  /**
97
98
  * 验证遮罩数据
98
99
  * @param data 遮罩数据
@@ -180,13 +180,17 @@ export class ValidationUtils {
180
180
  /**
181
181
  * 验证图像数据
182
182
  * @param imageData 图像数据
183
+ * @param allowEmptyImg 是否允许img为空
183
184
  * @throws 如果图像数据无效则抛出异常
184
185
  */
185
- static validateImageData(imageData) {
186
- if (!imageData || !imageData.img || !imageData.extent) {
187
- throw new Error('Invalid image data: img and extent are required');
186
+ static validateImageData(imageData, allowEmptyImg = false) {
187
+ if (!imageData) {
188
+ throw new Error('Invalid image data: imageData is required');
188
189
  }
189
- if (!Array.isArray(imageData.extent) || imageData.extent.length !== 4) {
190
+ if (!allowEmptyImg && !imageData.img) {
191
+ throw new Error('Invalid image data: img is required');
192
+ }
193
+ if (imageData.extent && (!Array.isArray(imageData.extent) || imageData.extent.length !== 4)) {
190
194
  throw new Error('Invalid extent: must be an array of 4 numbers [minX, minY, maxX, maxY]');
191
195
  }
192
196
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes