my-openlayer 2.0.0 → 2.0.1

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.
@@ -1,68 +1,68 @@
1
- import Map from "ol/Map";
2
- import { MapJSONData } from "../types";
3
- import VectorLayer from "ol/layer/Vector";
4
- import VectorSource from "ol/source/Vector";
5
- import BaseLayer from "ol/layer/Base";
6
- import ImageLayer from "ol/layer/Image";
7
- import ImageSource from "ol/source/Image";
8
- /**
9
- * 地图工具类
10
- * 提供地图的基础操作功能
11
- */
12
- export default class MapTools {
13
- private readonly map;
14
- private errorHandler;
15
- constructor(map: Map);
16
- /**
17
- * 根据名称获取图层
18
- * @param layerName 图层名称
19
- * @returns 图层数组
20
- * @throws 当参数无效时抛出错误
21
- */
22
- getLayerByLayerName(layerName: string | string[]): (VectorLayer<VectorSource> | BaseLayer | ImageLayer<ImageSource>)[];
23
- /**
24
- * 根据图层名称获取图层
25
- * @param map 地图实例
26
- * @param layerName 图层名称
27
- * @returns 图层数组
28
- * @throws 当参数无效时抛出错误
29
- */
30
- static getLayerByLayerName(map: Map, layerName: string | string[]): (VectorLayer<VectorSource> | BaseLayer | ImageLayer<ImageSource>)[];
31
- /**
32
- * 设置地图裁剪
33
- */
34
- static setMapClip(baseLayer: any, data: MapJSONData): any;
35
- /**
36
- * 移除图层
37
- * @param layerName 图层名称
38
- * @throws 当参数无效时抛出错误
39
- */
40
- removeLayer(layerName: string | string[]): void;
41
- /**
42
- * 移除图层(静态方法,兼容性保留)
43
- * @param map 地图对象
44
- * @param layerName 图层名称
45
- * @throws 当参数无效时抛出错误
46
- */
47
- static removeLayer(map: Map, layerName: string | string[]): void;
48
- /**
49
- * 设置图层可见性
50
- * @param layerName 图层名称
51
- * @param visible 是否可见
52
- * @throws 当参数无效时抛出错误
53
- */
54
- setLayerVisible(layerName: string, visible: boolean): void;
55
- /**
56
- * 设置图层可见性
57
- * @param map 地图实例
58
- * @param layerName 图层名称
59
- * @param visible 是否可见
60
- * @throws 当参数无效时抛出错误
61
- */
62
- static setLayerVisible: (map: Map, layerName: string, visible: boolean) => void;
63
- /**
64
- * 获取地图实例
65
- * @returns 地图实例
66
- */
67
- getMap(): Map;
68
- }
1
+ import Map from "ol/Map";
2
+ import { MapJSONData } from "../types";
3
+ import VectorLayer from "ol/layer/Vector";
4
+ import VectorSource from "ol/source/Vector";
5
+ import BaseLayer from "ol/layer/Base";
6
+ import ImageLayer from "ol/layer/Image";
7
+ import ImageSource from "ol/source/Image";
8
+ /**
9
+ * 地图工具类
10
+ * 提供地图的基础操作功能
11
+ */
12
+ export default class MapTools {
13
+ private readonly map;
14
+ private errorHandler;
15
+ constructor(map: Map);
16
+ /**
17
+ * 根据名称获取图层
18
+ * @param layerName 图层名称
19
+ * @returns 图层数组
20
+ * @throws 当参数无效时抛出错误
21
+ */
22
+ getLayerByLayerName(layerName: string | string[]): (VectorLayer<VectorSource> | BaseLayer | ImageLayer<ImageSource>)[];
23
+ /**
24
+ * 根据图层名称获取图层
25
+ * @param map 地图实例
26
+ * @param layerName 图层名称
27
+ * @returns 图层数组
28
+ * @throws 当参数无效时抛出错误
29
+ */
30
+ static getLayerByLayerName(map: Map, layerName: string | string[]): (VectorLayer<VectorSource> | BaseLayer | ImageLayer<ImageSource>)[];
31
+ /**
32
+ * 设置地图裁剪
33
+ */
34
+ static setMapClip(baseLayer: any, data: MapJSONData): any;
35
+ /**
36
+ * 移除图层
37
+ * @param layerName 图层名称
38
+ * @throws 当参数无效时抛出错误
39
+ */
40
+ removeLayer(layerName: string | string[]): void;
41
+ /**
42
+ * 移除图层(静态方法,兼容性保留)
43
+ * @param map 地图对象
44
+ * @param layerName 图层名称
45
+ * @throws 当参数无效时抛出错误
46
+ */
47
+ static removeLayer(map: Map, layerName: string | string[]): void;
48
+ /**
49
+ * 设置图层可见性
50
+ * @param layerName 图层名称
51
+ * @param visible 是否可见
52
+ * @throws 当参数无效时抛出错误
53
+ */
54
+ setLayerVisible(layerName: string, visible: boolean): void;
55
+ /**
56
+ * 设置图层可见性
57
+ * @param map 地图实例
58
+ * @param layerName 图层名称
59
+ * @param visible 是否可见
60
+ * @throws 当参数无效时抛出错误
61
+ */
62
+ static setLayerVisible: (map: Map, layerName: string, visible: boolean) => void;
63
+ /**
64
+ * 获取地图实例
65
+ * @returns 地图实例
66
+ */
67
+ getMap(): Map;
68
+ }
package/core/MapTools.js CHANGED
@@ -1,201 +1,202 @@
1
- "use strict";
2
- import VectorLayer from "ol/layer/Vector";
3
- import VectorSource from "ol/source/Vector";
4
- import GeoJSON from "ol/format/GeoJSON";
5
- import { Fill, Style } from "ol/style";
6
- import { getVectorContext } from "ol/render";
7
- import { ErrorHandler, ErrorType } from "../utils/ErrorHandler";
8
- import { ValidationUtils } from "../utils/ValidationUtils";
9
- /**
10
- * 地图工具类
11
- * 提供地图的基础操作功能
12
- */
13
- export default class MapTools {
14
- constructor(map) {
15
- this.errorHandler = ErrorHandler.getInstance();
16
- try {
17
- ValidationUtils.validateMap(map);
18
- this.map = map;
19
- }
20
- catch (error) {
21
- this.errorHandler.createAndHandleError(`MapTools initialization failed: ${error}`, ErrorType.COMPONENT_ERROR, { map, error });
22
- throw error;
23
- }
24
- }
25
- /**
26
- * 根据名称获取图层
27
- * @param layerName 图层名称
28
- * @returns 图层数组
29
- * @throws 当参数无效时抛出错误
30
- */
31
- getLayerByLayerName(layerName) {
32
- if (!this.map) {
33
- throw new Error('Map instance is not available');
34
- }
35
- return MapTools.getLayerByLayerName(this.map, layerName);
36
- }
37
- /**
38
- * 根据图层名称获取图层
39
- * @param map 地图实例
40
- * @param layerName 图层名称
41
- * @returns 图层数组
42
- * @throws 当参数无效时抛出错误
43
- */
44
- static getLayerByLayerName(map, layerName) {
45
- ValidationUtils.validateMap(map);
46
- ValidationUtils.validateLayerNameParam(layerName);
47
- const targetLayer = [];
48
- try {
49
- const layers = map.getLayers().getArray();
50
- layers.forEach((layer) => {
51
- const _layerName = layer.get('layerName');
52
- if (typeof layerName === "string") {
53
- if (_layerName && _layerName === layerName) {
54
- targetLayer.push(layer);
55
- }
56
- }
57
- else {
58
- if (_layerName && layerName.includes(_layerName)) {
59
- targetLayer.push(layer);
60
- }
61
- }
62
- });
63
- }
64
- catch (error) {
65
- console.error('Error getting layers:', error);
66
- throw new Error('Failed to retrieve layers from map');
67
- }
68
- return targetLayer;
69
- }
70
- /**
71
- * 设置地图裁剪
72
- */
73
- static setMapClip(baseLayer, data) {
74
- const clipLayer = new VectorLayer({
75
- style: null,
76
- source: new VectorSource({
77
- features: new GeoJSON().readFeatures(data)
78
- })
79
- });
80
- const style = new Style({
81
- fill: new Fill({
82
- color: 'transparent'
83
- })
84
- });
85
- baseLayer.on("prerender", (event) => {
86
- const vectorContext = getVectorContext(event);
87
- event.context.globalCompositeOperation = 'source-over';
88
- const ctx = event.context;
89
- ctx.save();
90
- clipLayer.getSource()?.forEachFeature(function (feature) {
91
- vectorContext.drawFeature(feature, style);
92
- });
93
- ctx.clip();
94
- });
95
- baseLayer.on("postrender", (event) => {
96
- const ctx = event.context;
97
- ctx.restore();
98
- });
99
- clipLayer.getSource()?.on('addfeature', function () {
100
- baseLayer.setExtent(clipLayer.getSource()?.getExtent());
101
- });
102
- return baseLayer;
103
- }
104
- /**
105
- * 移除图层
106
- * @param layerName 图层名称
107
- * @throws 当参数无效时抛出错误
108
- */
109
- removeLayer(layerName) {
110
- if (!this.map) {
111
- throw new Error('Map instance is not available');
112
- }
113
- try {
114
- const layers = this.getLayerByLayerName(layerName);
115
- layers.forEach(layer => {
116
- this.map.removeLayer(layer);
117
- });
118
- }
119
- catch (error) {
120
- console.error('Error removing layers:', error);
121
- throw new Error('Failed to remove layers from map');
122
- }
123
- }
124
- /**
125
- * 移除图层(静态方法,兼容性保留)
126
- * @param map 地图对象
127
- * @param layerName 图层名称
128
- * @throws 当参数无效时抛出错误
129
- */
130
- static removeLayer(map, layerName) {
131
- if (!map) {
132
- throw new Error('Map instance is required');
133
- }
134
- try {
135
- const layers = MapTools.getLayerByLayerName(map, layerName);
136
- layers.forEach(layer => {
137
- map.removeLayer(layer);
138
- });
139
- }
140
- catch (error) {
141
- console.error('Error removing layers:', error);
142
- throw new Error('Failed to remove layers from map');
143
- }
144
- }
145
- /**
146
- * 设置图层可见性
147
- * @param layerName 图层名称
148
- * @param visible 是否可见
149
- * @throws 当参数无效时抛出错误
150
- */
151
- setLayerVisible(layerName, visible) {
152
- if (!this.map) {
153
- throw new Error('Map instance is not available');
154
- }
155
- try {
156
- const layers = this.getLayerByLayerName(layerName);
157
- layers.forEach(layer => {
158
- layer.setVisible(visible);
159
- });
160
- }
161
- catch (error) {
162
- console.error('Error setting layer visibility:', error);
163
- throw new Error('Failed to set layer visibility');
164
- }
165
- }
166
- /**
167
- * 获取地图实例
168
- * @returns 地图实例
169
- */
170
- getMap() {
171
- return this.map;
172
- }
173
- }
174
- /**
175
- * 设置图层可见性
176
- * @param map 地图实例
177
- * @param layerName 图层名称
178
- * @param visible 是否可见
179
- * @throws 当参数无效时抛出错误
180
- */
181
- MapTools.setLayerVisible = (map, layerName, visible) => {
182
- if (!map) {
183
- throw new Error('Map instance is required');
184
- }
185
- if (typeof layerName !== 'string') {
186
- throw new Error('Layer name must be a string');
187
- }
188
- if (typeof visible !== 'boolean') {
189
- throw new Error('Visible parameter must be a boolean');
190
- }
191
- try {
192
- const layers = MapTools.getLayerByLayerName(map, layerName);
193
- layers.forEach(layer => {
194
- layer.setVisible(visible);
195
- });
196
- }
197
- catch (error) {
198
- console.error('Error setting layer visibility:', error);
199
- throw new Error('Failed to set layer visibility');
200
- }
201
- };
1
+ "use strict";
2
+ import VectorLayer from "ol/layer/Vector";
3
+ import VectorSource from "ol/source/Vector";
4
+ import GeoJSON from "ol/format/GeoJSON";
5
+ import { Fill, Style } from "ol/style";
6
+ import { getVectorContext } from "ol/render";
7
+ import { ErrorHandler, ErrorType } from "../utils/ErrorHandler";
8
+ import { ValidationUtils } from "../utils/ValidationUtils";
9
+ /**
10
+ * 地图工具类
11
+ * 提供地图的基础操作功能
12
+ */
13
+ class MapTools {
14
+ constructor(map) {
15
+ this.errorHandler = ErrorHandler.getInstance();
16
+ try {
17
+ ValidationUtils.validateMap(map);
18
+ this.map = map;
19
+ }
20
+ catch (error) {
21
+ this.errorHandler.createAndHandleError(`MapTools initialization failed: ${error}`, ErrorType.COMPONENT_ERROR, { map, error });
22
+ throw error;
23
+ }
24
+ }
25
+ /**
26
+ * 根据名称获取图层
27
+ * @param layerName 图层名称
28
+ * @returns 图层数组
29
+ * @throws 当参数无效时抛出错误
30
+ */
31
+ getLayerByLayerName(layerName) {
32
+ if (!this.map) {
33
+ throw new Error('Map instance is not available');
34
+ }
35
+ return MapTools.getLayerByLayerName(this.map, layerName);
36
+ }
37
+ /**
38
+ * 根据图层名称获取图层
39
+ * @param map 地图实例
40
+ * @param layerName 图层名称
41
+ * @returns 图层数组
42
+ * @throws 当参数无效时抛出错误
43
+ */
44
+ static getLayerByLayerName(map, layerName) {
45
+ ValidationUtils.validateMap(map);
46
+ ValidationUtils.validateLayerNameParam(layerName);
47
+ const targetLayer = [];
48
+ try {
49
+ const layers = map.getLayers().getArray();
50
+ layers.forEach((layer) => {
51
+ const _layerName = layer.get('layerName');
52
+ if (typeof layerName === "string") {
53
+ if (_layerName && _layerName === layerName) {
54
+ targetLayer.push(layer);
55
+ }
56
+ }
57
+ else {
58
+ if (_layerName && layerName.includes(_layerName)) {
59
+ targetLayer.push(layer);
60
+ }
61
+ }
62
+ });
63
+ }
64
+ catch (error) {
65
+ console.error('Error getting layers:', error);
66
+ throw new Error('Failed to retrieve layers from map');
67
+ }
68
+ return targetLayer;
69
+ }
70
+ /**
71
+ * 设置地图裁剪
72
+ */
73
+ static setMapClip(baseLayer, data) {
74
+ const clipLayer = new VectorLayer({
75
+ style: null,
76
+ source: new VectorSource({
77
+ features: new GeoJSON().readFeatures(data)
78
+ })
79
+ });
80
+ const style = new Style({
81
+ fill: new Fill({
82
+ color: 'transparent'
83
+ })
84
+ });
85
+ baseLayer.on("prerender", (event) => {
86
+ const vectorContext = getVectorContext(event);
87
+ event.context.globalCompositeOperation = 'source-over';
88
+ const ctx = event.context;
89
+ ctx.save();
90
+ clipLayer.getSource()?.forEachFeature(function (feature) {
91
+ vectorContext.drawFeature(feature, style);
92
+ });
93
+ ctx.clip();
94
+ });
95
+ baseLayer.on("postrender", (event) => {
96
+ const ctx = event.context;
97
+ ctx.restore();
98
+ });
99
+ clipLayer.getSource()?.on('addfeature', function () {
100
+ baseLayer.setExtent(clipLayer.getSource()?.getExtent());
101
+ });
102
+ return baseLayer;
103
+ }
104
+ /**
105
+ * 移除图层
106
+ * @param layerName 图层名称
107
+ * @throws 当参数无效时抛出错误
108
+ */
109
+ removeLayer(layerName) {
110
+ if (!this.map) {
111
+ throw new Error('Map instance is not available');
112
+ }
113
+ try {
114
+ const layers = this.getLayerByLayerName(layerName);
115
+ layers.forEach(layer => {
116
+ this.map.removeLayer(layer);
117
+ });
118
+ }
119
+ catch (error) {
120
+ console.error('Error removing layers:', error);
121
+ throw new Error('Failed to remove layers from map');
122
+ }
123
+ }
124
+ /**
125
+ * 移除图层(静态方法,兼容性保留)
126
+ * @param map 地图对象
127
+ * @param layerName 图层名称
128
+ * @throws 当参数无效时抛出错误
129
+ */
130
+ static removeLayer(map, layerName) {
131
+ if (!map) {
132
+ throw new Error('Map instance is required');
133
+ }
134
+ try {
135
+ const layers = MapTools.getLayerByLayerName(map, layerName);
136
+ layers.forEach(layer => {
137
+ map.removeLayer(layer);
138
+ });
139
+ }
140
+ catch (error) {
141
+ console.error('Error removing layers:', error);
142
+ throw new Error('Failed to remove layers from map');
143
+ }
144
+ }
145
+ /**
146
+ * 设置图层可见性
147
+ * @param layerName 图层名称
148
+ * @param visible 是否可见
149
+ * @throws 当参数无效时抛出错误
150
+ */
151
+ setLayerVisible(layerName, visible) {
152
+ if (!this.map) {
153
+ throw new Error('Map instance is not available');
154
+ }
155
+ try {
156
+ const layers = this.getLayerByLayerName(layerName);
157
+ layers.forEach(layer => {
158
+ layer.setVisible(visible);
159
+ });
160
+ }
161
+ catch (error) {
162
+ console.error('Error setting layer visibility:', error);
163
+ throw new Error('Failed to set layer visibility');
164
+ }
165
+ }
166
+ /**
167
+ * 获取地图实例
168
+ * @returns 地图实例
169
+ */
170
+ getMap() {
171
+ return this.map;
172
+ }
173
+ }
174
+ /**
175
+ * 设置图层可见性
176
+ * @param map 地图实例
177
+ * @param layerName 图层名称
178
+ * @param visible 是否可见
179
+ * @throws 当参数无效时抛出错误
180
+ */
181
+ MapTools.setLayerVisible = (map, layerName, visible) => {
182
+ if (!map) {
183
+ throw new Error('Map instance is required');
184
+ }
185
+ if (typeof layerName !== 'string') {
186
+ throw new Error('Layer name must be a string');
187
+ }
188
+ if (typeof visible !== 'boolean') {
189
+ throw new Error('Visible parameter must be a boolean');
190
+ }
191
+ try {
192
+ const layers = MapTools.getLayerByLayerName(map, layerName);
193
+ layers.forEach(layer => {
194
+ layer.setVisible(visible);
195
+ });
196
+ }
197
+ catch (error) {
198
+ console.error('Error setting layer visibility:', error);
199
+ throw new Error('Failed to set layer visibility');
200
+ }
201
+ };
202
+ export default MapTools;
@@ -1,65 +1,65 @@
1
- import Map from "ol/Map";
2
- import { MeasureHandlerType } from "../types";
3
- /**
4
- * 测量工具处理类
5
- * 提供距离和面积测量功能
6
- */
7
- export default class MeasureHandler {
8
- private readonly source;
9
- private readonly vector;
10
- private sketch;
11
- private helpTooltipElement;
12
- private helpTooltip;
13
- private readonly _map;
14
- private measureTooltipElement;
15
- private measureTooltip;
16
- private readonly continuePolygonMsg;
17
- private readonly continueLineMsg;
18
- private _tipsCollection;
19
- private _mouseListener;
20
- private _draw;
21
- /**
22
- * 构造函数
23
- * @param map OpenLayers地图实例
24
- * @throws 当地图实例无效时抛出错误
25
- */
26
- constructor(map: Map);
27
- /**
28
- * destory the object
29
- */
30
- destory(): void;
31
- /**
32
- * Format length output.
33
- * @param {LineString} line The line.
34
- * @return {string} The formatted length.
35
- */
36
- formatLength(line: any): string;
37
- /**
38
- * Format area output.
39
- * @param {Polygon} polygon The polygon.
40
- * @return {string} Formatted area.
41
- */
42
- formatArea(polygon: any): string;
43
- /**
44
- * 开始测量
45
- * @param type 测量类型
46
- * @throws 当测量类型无效时抛出错误
47
- */
48
- start(type: MeasureHandlerType): void;
49
- /**
50
- * end the measure drawing
51
- */
52
- end(): void;
53
- /**
54
- * Creates a new help tooltip
55
- */
56
- createHelpTooltip(): void;
57
- /**
58
- * Creates a new measure tooltip
59
- */
60
- createMeasureTooltip(): void;
61
- /**
62
- * clean the all result of measure
63
- */
64
- clean(): void;
65
- }
1
+ import Map from "ol/Map";
2
+ import { MeasureHandlerType } from "../types";
3
+ /**
4
+ * 测量工具处理类
5
+ * 提供距离和面积测量功能
6
+ */
7
+ export default class MeasureHandler {
8
+ private readonly source;
9
+ private readonly vector;
10
+ private sketch;
11
+ private helpTooltipElement;
12
+ private helpTooltip;
13
+ private readonly _map;
14
+ private measureTooltipElement;
15
+ private measureTooltip;
16
+ private readonly continuePolygonMsg;
17
+ private readonly continueLineMsg;
18
+ private _tipsCollection;
19
+ private _mouseListener;
20
+ private _draw;
21
+ /**
22
+ * 构造函数
23
+ * @param map OpenLayers地图实例
24
+ * @throws 当地图实例无效时抛出错误
25
+ */
26
+ constructor(map: Map);
27
+ /**
28
+ * destory the object
29
+ */
30
+ destory(): void;
31
+ /**
32
+ * Format length output.
33
+ * @param {LineString} line The line.
34
+ * @return {string} The formatted length.
35
+ */
36
+ formatLength(line: any): string;
37
+ /**
38
+ * Format area output.
39
+ * @param {Polygon} polygon The polygon.
40
+ * @return {string} Formatted area.
41
+ */
42
+ formatArea(polygon: any): string;
43
+ /**
44
+ * 开始测量
45
+ * @param type 测量类型
46
+ * @throws 当测量类型无效时抛出错误
47
+ */
48
+ start(type: MeasureHandlerType): void;
49
+ /**
50
+ * end the measure drawing
51
+ */
52
+ end(): void;
53
+ /**
54
+ * Creates a new help tooltip
55
+ */
56
+ createHelpTooltip(): void;
57
+ /**
58
+ * Creates a new measure tooltip
59
+ */
60
+ createMeasureTooltip(): void;
61
+ /**
62
+ * clean the all result of measure
63
+ */
64
+ clean(): void;
65
+ }