my-openlayer 3.0.1 → 3.1.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.
@@ -1,340 +0,0 @@
1
- import VectorSource from "ol/source/Vector";
2
- import GeoJSON from "ol/format/GeoJSON";
3
- import VectorLayer from "ol/layer/Vector";
4
- import { Stroke, Style } from "ol/style";
5
- import { Feature } from "ol";
6
- import ValidationUtils from "../../utils/ValidationUtils";
7
- import { ErrorHandler } from "../../utils/ErrorHandler";
8
- import { ConfigManager, EventManager, MapTools } from "../map";
9
- export default class RiverLayerManager {
10
- /**
11
- * 构造函数
12
- * @param map OpenLayers 地图实例
13
- * @param eventManager 可选事件管理器,未传入则内部创建
14
- */
15
- constructor(map, eventManager) {
16
- //************* 状态:分级河流图层列表与显示控制 *************
17
- this.riverLayerList = [];
18
- this.riverLayerShow = false;
19
- this.riverZoomOffset = 8;
20
- //************* 默认配置:按 level 映射线宽 *************
21
- this.defaultLevelWidthMap = ConfigManager.DEFAULT_RIVER_LEVEL_WIDTH_MAP;
22
- ValidationUtils.validateMapInstance(map);
23
- this.map = map;
24
- this.eventManager = eventManager ?? new EventManager(map);
25
- }
26
- addRiverLayersByZoom(fyRiverJson, options = {}) {
27
- ValidationUtils.validateGeoJSONData(fyRiverJson);
28
- const defaultOptions = {
29
- ...ConfigManager.DEFAULT_RIVER_LAYERS_BY_ZOOM_OPTIONS,
30
- removeExisting: options.removeExisting ?? false,
31
- levelWidthMap: this.defaultLevelWidthMap
32
- };
33
- const mergedOptions = { ...defaultOptions, ...options };
34
- this.riverZoomOffset = mergedOptions.zoomOffset;
35
- if (mergedOptions.removeExisting) {
36
- this.clearRiverLayers();
37
- }
38
- this.riverLayerShow = mergedOptions.visible;
39
- this.riverLayerList = [];
40
- for (let level = 1; level <= mergedOptions.levelCount; level++) {
41
- const vectorSource = new VectorSource({
42
- format: new GeoJSON(),
43
- loader: () => {
44
- const geojson = new GeoJSON();
45
- fyRiverJson.features.forEach((feature) => {
46
- if (feature.properties && feature.properties.level === level) {
47
- try {
48
- const olFeature = geojson.readFeature(feature);
49
- if (Array.isArray(olFeature)) {
50
- vectorSource.addFeatures(olFeature);
51
- }
52
- else {
53
- vectorSource.addFeature(olFeature);
54
- }
55
- }
56
- catch (error) {
57
- ErrorHandler.getInstance().warn(`Failed to load river feature at level ${level}:`, error);
58
- }
59
- }
60
- });
61
- }
62
- });
63
- const riverLayer = new VectorLayer({
64
- properties: {
65
- name: mergedOptions.layerName,
66
- layerName: mergedOptions.layerName,
67
- riverLevel: level
68
- },
69
- source: vectorSource,
70
- style: (feature) => {
71
- if (feature instanceof Feature) {
72
- feature.set("type", mergedOptions.layerName);
73
- feature.set("layerName", mergedOptions.layerName);
74
- }
75
- if (mergedOptions.style) {
76
- if (typeof mergedOptions.style === "function") {
77
- return mergedOptions.style(feature);
78
- }
79
- else {
80
- return mergedOptions.style;
81
- }
82
- }
83
- return new Style({
84
- stroke: new Stroke({
85
- color: mergedOptions.strokeColor,
86
- width: mergedOptions.strokeWidth
87
- })
88
- });
89
- },
90
- zIndex: mergedOptions.zIndex
91
- });
92
- riverLayer.setVisible(false);
93
- this.riverLayerList.push(riverLayer);
94
- this.map.addLayer(riverLayer);
95
- }
96
- this.eventManager.on("moveend", () => {
97
- this.showRiverLayerByZoom();
98
- });
99
- this.showRiverLayerByZoom();
100
- }
101
- //************* 分级河流:按缩放级别显示(URL 加载) *************
102
- /**
103
- * 从 URL 添加分级河流图层:根据缩放级别显示不同 level 的河流
104
- * @param url 河流数据 URL(GeoJSON)
105
- * @param options 河流图层配置选项
106
- */
107
- addRiverLayersByZoomByUrl(url, options = {}) {
108
- const defaultOptions = {
109
- ...ConfigManager.DEFAULT_RIVER_LAYERS_BY_ZOOM_OPTIONS,
110
- removeExisting: options.removeExisting ?? false,
111
- levelWidthMap: this.defaultLevelWidthMap
112
- };
113
- const mergedOptions = { ...defaultOptions, ...options };
114
- this.riverZoomOffset = mergedOptions.zoomOffset;
115
- if (mergedOptions.removeExisting) {
116
- this.clearRiverLayers();
117
- }
118
- this.riverLayerShow = mergedOptions.visible;
119
- this.riverLayerList = [];
120
- for (let level = 1; level <= mergedOptions.levelCount; level++) {
121
- const vectorSource = new VectorSource({
122
- url,
123
- format: new GeoJSON(),
124
- loader: function (extent, resolution, projection, success, failure) {
125
- fetch(url)
126
- .then(response => response.json())
127
- .then(data => {
128
- const geojson = new GeoJSON();
129
- data.features.forEach((feature) => {
130
- if (feature.properties && feature.properties.level === level) {
131
- try {
132
- const olFeature = geojson.readFeature(feature);
133
- if (Array.isArray(olFeature)) {
134
- vectorSource.addFeatures(olFeature);
135
- }
136
- else {
137
- vectorSource.addFeature(olFeature);
138
- }
139
- }
140
- catch (error) {
141
- ErrorHandler.getInstance().warn(`Failed to load river feature at level ${level}:`, error);
142
- }
143
- }
144
- });
145
- success?.(vectorSource.getFeatures());
146
- })
147
- .catch(error => {
148
- ErrorHandler.getInstance().error("Error loading river data:", error);
149
- failure?.();
150
- });
151
- }
152
- });
153
- const riverLayer = new VectorLayer({
154
- properties: {
155
- name: mergedOptions.layerName,
156
- layerName: mergedOptions.layerName,
157
- riverLevel: level
158
- },
159
- source: vectorSource,
160
- style: (feature) => {
161
- if (feature instanceof Feature) {
162
- feature.set("type", mergedOptions.layerName);
163
- feature.set("layerName", mergedOptions.layerName);
164
- }
165
- if (mergedOptions.style) {
166
- if (typeof mergedOptions.style === "function") {
167
- return mergedOptions.style(feature);
168
- }
169
- else {
170
- return mergedOptions.style;
171
- }
172
- }
173
- return new Style({
174
- stroke: new Stroke({
175
- color: mergedOptions.strokeColor,
176
- width: mergedOptions.strokeWidth
177
- })
178
- });
179
- },
180
- zIndex: mergedOptions.zIndex
181
- });
182
- riverLayer.setVisible(false);
183
- this.riverLayerList.push(riverLayer);
184
- this.map.addLayer(riverLayer);
185
- }
186
- this.eventManager.on("moveend", () => {
187
- this.showRiverLayerByZoom();
188
- });
189
- this.showRiverLayerByZoom();
190
- }
191
- //************* 分级河流:显示控制 *************
192
- /**
193
- * 设置分级河流图层是否显示
194
- * @param show 是否显示
195
- */
196
- showRiverLayer(show) {
197
- this.riverLayerShow = show;
198
- this.showRiverLayerByZoom();
199
- }
200
- /**
201
- * 根据当前 zoom 显示对应分级河流图层
202
- * 缩放越大,显示的 riverLevel 越多
203
- */
204
- showRiverLayerByZoom() {
205
- const zoom = this.map.getView().getZoom();
206
- if (!zoom) {
207
- return;
208
- }
209
- this.riverLayerList.forEach((layer, index) => {
210
- const displayThreshold = index + 1 + this.riverZoomOffset;
211
- if (zoom > displayThreshold) {
212
- layer.setVisible(this.riverLayerShow);
213
- }
214
- else {
215
- layer.setVisible(false);
216
- }
217
- });
218
- }
219
- addRiverWidthByLevel(data, options = {}) {
220
- ValidationUtils.validateGeoJSONData(data);
221
- const mergedOptions = {
222
- ...ConfigManager.DEFAULT_RIVER_WIDTH_BY_LEVEL_OPTIONS,
223
- ...options,
224
- removeExisting: options.removeExisting ?? ConfigManager.DEFAULT_RIVER_WIDTH_BY_LEVEL_OPTIONS.removeExisting,
225
- levelWidthMap: options.levelWidthMap ?? this.defaultLevelWidthMap,
226
- };
227
- if (mergedOptions.removeExisting && mergedOptions.layerName) {
228
- MapTools.removeLayer(this.map, mergedOptions.layerName);
229
- }
230
- const features = new GeoJSON().readFeatures(data, mergedOptions.projectionOptOptions);
231
- const riverLayer = new VectorLayer({
232
- properties: {
233
- name: mergedOptions.layerName,
234
- layerName: mergedOptions.layerName
235
- },
236
- source: new VectorSource({ features }),
237
- style: (feature) => {
238
- if (mergedOptions.style) {
239
- if (typeof mergedOptions.style === "function") {
240
- return mergedOptions.style(feature);
241
- }
242
- else {
243
- return mergedOptions.style;
244
- }
245
- }
246
- const level = feature.get("level");
247
- const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
248
- return new Style({
249
- stroke: new Stroke({
250
- color: mergedOptions.strokeColor,
251
- width: levelWidth
252
- })
253
- });
254
- },
255
- zIndex: mergedOptions.zIndex
256
- });
257
- riverLayer.setVisible(mergedOptions.visible);
258
- this.map.addLayer(riverLayer);
259
- return riverLayer;
260
- }
261
- //************* 河流线宽:按 level 设置不同宽度(URL 加载) *************
262
- /**
263
- * 从 URL 添加按级别显示不同宽度的河流图层
264
- * @param url 河流数据 URL(GeoJSON)
265
- * @param options 河流图层配置选项
266
- */
267
- addRiverWidthByLevelByUrl(url, options = {}) {
268
- const mergedOptions = {
269
- ...ConfigManager.DEFAULT_RIVER_WIDTH_BY_LEVEL_OPTIONS,
270
- ...options,
271
- removeExisting: options.removeExisting ?? ConfigManager.DEFAULT_RIVER_WIDTH_BY_LEVEL_OPTIONS.removeExisting,
272
- levelWidthMap: options.levelWidthMap ?? this.defaultLevelWidthMap,
273
- };
274
- if (mergedOptions.removeExisting && mergedOptions.layerName) {
275
- MapTools.removeLayer(this.map, mergedOptions.layerName);
276
- }
277
- const source = new VectorSource({
278
- url,
279
- format: new GeoJSON(options.projectionOptOptions)
280
- });
281
- const riverLayer = new VectorLayer({
282
- properties: {
283
- name: mergedOptions.layerName,
284
- layerName: mergedOptions.layerName
285
- },
286
- source,
287
- style: (feature) => {
288
- if (mergedOptions.style) {
289
- if (typeof mergedOptions.style === "function") {
290
- return mergedOptions.style(feature);
291
- }
292
- else {
293
- return mergedOptions.style;
294
- }
295
- }
296
- const level = feature.get("level");
297
- const levelWidth = mergedOptions.levelWidthMap[Number(level)] || 1;
298
- return new Style({
299
- stroke: new Stroke({
300
- color: mergedOptions.strokeColor,
301
- width: levelWidth
302
- })
303
- });
304
- },
305
- zIndex: mergedOptions.zIndex
306
- });
307
- riverLayer.setVisible(mergedOptions.visible);
308
- this.map.addLayer(riverLayer);
309
- return riverLayer;
310
- }
311
- //************* 清理与状态查询 *************
312
- /**
313
- * 清除本管理器创建的分级河流图层
314
- */
315
- clearRiverLayers() {
316
- this.riverLayerList.forEach(layer => {
317
- this.map.removeLayer(layer);
318
- });
319
- this.riverLayerList = [];
320
- this.riverLayerShow = false;
321
- }
322
- /**
323
- * 获取分级河流图层显示状态
324
- */
325
- getRiverLayerVisibility() {
326
- return this.riverLayerShow;
327
- }
328
- /**
329
- * 获取分级河流图层列表副本
330
- */
331
- getRiverLayers() {
332
- return [...this.riverLayerList];
333
- }
334
- /**
335
- * 销毁管理器,释放资源
336
- */
337
- destroy() {
338
- this.clearRiverLayers();
339
- }
340
- }
@@ -1,187 +0,0 @@
1
- # RiverLayerManager 河流图层管理类
2
-
3
- `RiverLayerManager` 专门用于处理河流数据的分级显示和样式管理。它支持基于地图缩放级别(Zoom-based)的动态分级显示,以及基于河流属性等级(Level-based)的固定线宽渲染。
4
-
5
- ## 构造函数
6
-
7
- ```typescript
8
- constructor(map: Map, eventManager?: EventManager)
9
- ```
10
-
11
- - **参数**:
12
- - `map` (Map): OpenLayers 地图实例。
13
- - `eventManager` (EventManager): 可选。事件管理器实例,如果未提供则内部创建。
14
-
15
- ## 接口定义
16
-
17
- ### RiverLayerOptions
18
-
19
- 河流图层配置选项,继承自 `LineOptions`。
20
-
21
- | 属性 | 类型 | 必填 | 默认值 | 描述 |
22
- | :--- | :--- | :--- | :--- | :--- |
23
- | levelCount | `number` | 否 | `5` | 河流等级总数。用于分级显示循环加载。 |
24
- | zoomOffset | `number` | 否 | `8` | 缩放级别偏移量。控制何时显示哪一级河流 (Zoom > Level + Offset)。 |
25
- | levelWidthMap | `RiverLevelWidthMap` | 否 | 默认映射 | 河流级别与线宽的映射对象。 |
26
- | removeExisting | `boolean` | 否 | `false` | 添加前是否删除由本管理器创建的旧图层。 |
27
- | layerName | `string` | 否 | - | 图层名称,用于标识和清理。 |
28
- | visible | `boolean` | 否 | `true` | 初始可见性。 |
29
- | strokeColor | `string` | 否 | - | 线条颜色。 |
30
- | strokeWidth | `number` | 否 | - | 基础线宽(如果未使用 levelWidthMap)。 |
31
- | zIndex | `number` | 否 | - | 图层 Z-Index。 |
32
- | projectionOptOptions | `any` | 否 | - | GeoJSON 读取时的投影选项。 |
33
-
34
- ### RiverLevelWidthMap
35
-
36
- 河流级别线宽映射接口。
37
-
38
- ```typescript
39
- interface RiverLevelWidthMap {
40
- [level: number]: number;
41
- }
42
- ```
43
-
44
- 默认映射 (`ConfigManager.DEFAULT_RIVER_LEVEL_WIDTH_MAP`) 通常为 `{1: 5, 2: 4, 3: 3, 4: 2, 5: 1}` 等类似结构。
45
-
46
- ## 方法
47
-
48
- ### 分级显示 (Zoom-based)
49
-
50
- 根据地图缩放级别自动显示不同等级的河流。通常用于实现“缩放越大约详细”的效果。
51
-
52
- #### addRiverLayersByZoom
53
-
54
- 直接传递 GeoJSON 数据添加分级河流图层。
55
-
56
- ```typescript
57
- addRiverLayersByZoom(fyRiverJson: MapJSONData, options?: RiverLayerOptions): void
58
- ```
59
-
60
- - **参数**:
61
- - `fyRiverJson`: 河流 GeoJSON 数据对象。
62
- - `options`: 配置选项。
63
-
64
- #### addRiverLayersByZoomByUrl
65
-
66
- 通过 URL 加载 GeoJSON 数据并添加分级河流图层。
67
-
68
- ```typescript
69
- addRiverLayersByZoomByUrl(url: string, options?: RiverLayerOptions): void
70
- ```
71
-
72
- #### showRiverLayer
73
-
74
- 控制分级河流图层的总开关。
75
-
76
- ```typescript
77
- showRiverLayer(show: boolean): void
78
- ```
79
-
80
- #### showRiverLayerByZoom
81
-
82
- 手动触发根据当前缩放级别更新图层可见性(通常不需要手动调用,已绑定 `moveend` 事件)。
83
-
84
- ```typescript
85
- showRiverLayerByZoom(): void
86
- ```
87
-
88
- ### 宽度分级 (Level-based Width)
89
-
90
- 根据河流数据中的 `level` 属性渲染不同宽度的线条,通常用于静态展示不同等级河流的粗细差异。
91
-
92
- #### addRiverWidthByLevel
93
-
94
- 直接传递 GeoJSON 数据添加按等级设定宽度的河流。
95
-
96
- ```typescript
97
- addRiverWidthByLevel(data: MapJSONData, options?: RiverLayerOptions): VectorLayer<VectorSource>
98
- ```
99
-
100
- - **返回值**: 创建的矢量图层。
101
-
102
- #### addRiverWidthByLevelByUrl
103
-
104
- 通过 URL 加载 GeoJSON 数据并添加按等级设定宽度的河流。
105
-
106
- ```typescript
107
- addRiverWidthByLevelByUrl(url: string, options?: RiverLayerOptions): VectorLayer<VectorSource>
108
- ```
109
-
110
- ### 管理与清理
111
-
112
- #### clearRiverLayers
113
-
114
- 清除所有由该管理器创建的分级河流图层。
115
-
116
- ```typescript
117
- clearRiverLayers(): void
118
- ```
119
-
120
- #### getRiverLayerVisibility
121
-
122
- 获取当前分级河流图层的总显示状态。
123
-
124
- ```typescript
125
- getRiverLayerVisibility(): boolean
126
- ```
127
-
128
- #### getRiverLayers
129
-
130
- 获取当前管理的所有分级河流图层列表。
131
-
132
- ```typescript
133
- getRiverLayers(): VectorLayer<VectorSource>[]
134
- ```
135
-
136
- #### destroy
137
-
138
- 销毁管理器,清理所有图层和事件监听。
139
-
140
- ```typescript
141
- destroy(): void
142
- ```
143
-
144
- ## 使用示例
145
-
146
- ```typescript
147
- import { MyOl, RiverLayerManager } from 'my-openlayer';
148
-
149
- const map = new MyOl('map-container');
150
- const riverManager = new RiverLayerManager(map.map);
151
-
152
- // 假设 riverGeoJSON 数据中每个 feature 都有 properties.level (1-5)
153
-
154
- // 示例 1: 添加分级河流(随地图缩放自动显隐不同级别的河流)
155
- // Level 1 在 Zoom > 9 (1+8) 显示
156
- // Level 2 在 Zoom > 10 (2+8) 显示
157
- // ...
158
- riverManager.addRiverLayersByZoom(riverGeoJSON, {
159
- layerName: 'dynamic-river',
160
- levelCount: 5,
161
- zoomOffset: 8,
162
- strokeColor: '#0071FF',
163
- strokeWidth: 2,
164
- removeExisting: true
165
- });
166
-
167
- // 控制分级河流的总开关
168
- riverManager.showRiverLayer(false); // 全部隐藏
169
- riverManager.showRiverLayer(true); // 恢复并根据当前 Zoom 显示对应级别
170
-
171
- // 示例 2: 添加静态河流,根据等级显示不同粗细
172
- riverManager.addRiverWidthByLevel(riverGeoJSON, {
173
- layerName: 'static-river',
174
- strokeColor: '#0071FF',
175
- levelWidthMap: {
176
- 1: 6, // 1级河流宽 6px
177
- 2: 5,
178
- 3: 4,
179
- 4: 3,
180
- 5: 1
181
- },
182
- zIndex: 10
183
- });
184
-
185
- // 清理资源
186
- // riverManager.destroy();
187
- ```