|
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+import townshipGeoJson from '@/assets/mapJson/map.json'
|
|
|
2
|
+
|
|
|
3
|
+export const TOWNSHIP_SOURCE_ID = 'baqing-township'
|
|
|
4
|
+export const TOWNSHIP_FILL_LAYER_ID = 'baqing-township-fill'
|
|
|
5
|
+export const TOWNSHIP_LINE_GLOW_LAYER_ID = 'baqing-township-line-glow'
|
|
|
6
|
+export const TOWNSHIP_LINE_LAYER_ID = 'baqing-township-line'
|
|
|
7
|
+
|
|
|
8
|
+/** 乡镇名称标注手动微调(经度, 纬度) */
|
|
|
9
|
+const TOWNSHIP_LABEL_OVERRIDES = {
|
|
|
10
|
+ 拉西镇: [94.003062, 32.041406],
|
|
|
11
|
+ 玛如乡: [93.862433, 32.341736]
|
|
|
12
|
+}
|
|
|
13
|
+
|
|
|
14
|
+/** 乡镇点击下钻中心手动微调(经度, 纬度) */
|
|
|
15
|
+const TOWNSHIP_DRILL_CENTER_OVERRIDES = {
|
|
|
16
|
+ 拉西镇: [94.054094, 31.920244]
|
|
|
17
|
+}
|
|
|
18
|
+
|
|
|
19
|
+function getTownshipDrillCenter(feature) {
|
|
|
20
|
+ if (!feature) {
|
|
|
21
|
+ return null
|
|
|
22
|
+ }
|
|
|
23
|
+ const name = String(feature.properties?.name || '')
|
|
|
24
|
+ const override = TOWNSHIP_DRILL_CENTER_OVERRIDES[name]
|
|
|
25
|
+ if (Array.isArray(override) && override.length >= 2) {
|
|
|
26
|
+ const lng = Number(override[0])
|
|
|
27
|
+ const lat = Number(override[1])
|
|
|
28
|
+ if (Number.isFinite(lng) && Number.isFinite(lat)) {
|
|
|
29
|
+ return [lng, lat]
|
|
|
30
|
+ }
|
|
|
31
|
+ }
|
|
|
32
|
+
|
|
|
33
|
+ const labelPoint = getTownshipLabelPoints().find((item) => item.name === name)?.lngLat
|
|
|
34
|
+ if (Array.isArray(labelPoint) && labelPoint.length >= 2) {
|
|
|
35
|
+ return [...labelPoint]
|
|
|
36
|
+ }
|
|
|
37
|
+
|
|
|
38
|
+ const center = feature.properties?.center
|
|
|
39
|
+ if (Array.isArray(center) && center.length >= 2) {
|
|
|
40
|
+ return [Number(center[0]), Number(center[1])]
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ return null
|
|
|
44
|
+}
|
|
|
45
|
+
|
|
|
46
|
+export function getTownshipGeoJson() {
|
|
|
47
|
+ return townshipGeoJson
|
|
|
48
|
+}
|
|
|
49
|
+
|
|
|
50
|
+export function getTownshipFeatures(geojson = townshipGeoJson) {
|
|
|
51
|
+ return (geojson?.features || []).filter((feature) => feature?.geometry?.type === 'Polygon')
|
|
|
52
|
+}
|
|
|
53
|
+
|
|
|
54
|
+function getPolygonOuterRing(feature) {
|
|
|
55
|
+ return feature?.geometry?.coordinates?.[0] || null
|
|
|
56
|
+}
|
|
|
57
|
+
|
|
|
58
|
+function pointInPolygon(point, ring) {
|
|
|
59
|
+ const x = point[0]
|
|
|
60
|
+ const y = point[1]
|
|
|
61
|
+ let inside = false
|
|
|
62
|
+ for (let i = 0, j = ring.length - 1; i < ring.length; j = i, i += 1) {
|
|
|
63
|
+ const xi = ring[i][0]
|
|
|
64
|
+ const yi = ring[i][1]
|
|
|
65
|
+ const xj = ring[j][0]
|
|
|
66
|
+ const yj = ring[j][1]
|
|
|
67
|
+ const intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
|
|
|
68
|
+ if (intersect) {
|
|
|
69
|
+ inside = !inside
|
|
|
70
|
+ }
|
|
|
71
|
+ }
|
|
|
72
|
+ return inside
|
|
|
73
|
+}
|
|
|
74
|
+
|
|
|
75
|
+/** 在多边形内部求视觉中心(拉西镇等 concave 面 properties.centroid 可能在面外) */
|
|
|
76
|
+function computePolygonLabelPoint(ring, precision = 0.001) {
|
|
|
77
|
+ if (!Array.isArray(ring) || ring.length < 4) {
|
|
|
78
|
+ return null
|
|
|
79
|
+ }
|
|
|
80
|
+
|
|
|
81
|
+ let minLng = Infinity
|
|
|
82
|
+ let maxLng = -Infinity
|
|
|
83
|
+ let minLat = Infinity
|
|
|
84
|
+ let maxLat = -Infinity
|
|
|
85
|
+ ring.forEach(([lng, lat]) => {
|
|
|
86
|
+ minLng = Math.min(minLng, lng)
|
|
|
87
|
+ maxLng = Math.max(maxLng, lng)
|
|
|
88
|
+ minLat = Math.min(minLat, lat)
|
|
|
89
|
+ maxLat = Math.max(maxLat, lat)
|
|
|
90
|
+ })
|
|
|
91
|
+
|
|
|
92
|
+ const width = maxLng - minLng
|
|
|
93
|
+ const height = maxLat - minLat
|
|
|
94
|
+ if (!Number.isFinite(width) || !Number.isFinite(height) || width <= 0 || height <= 0) {
|
|
|
95
|
+ return null
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ const cellSize = Math.min(width, height)
|
|
|
99
|
+ const half = cellSize / 2
|
|
|
100
|
+ const cells = []
|
|
|
101
|
+
|
|
|
102
|
+ const cellMaxDistance = (lng, lat, inside) => {
|
|
|
103
|
+ if (!inside) {
|
|
|
104
|
+ return -1
|
|
|
105
|
+ }
|
|
|
106
|
+ return Math.min(lng - minLng, maxLng - lng, lat - minLat, maxLat - lat)
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ for (let lng = minLng; lng < maxLng; lng += cellSize) {
|
|
|
110
|
+ for (let lat = minLat; lat < maxLat; lat += cellSize) {
|
|
|
111
|
+ const cx = lng + half
|
|
|
112
|
+ const cy = lat + half
|
|
|
113
|
+ const inside = pointInPolygon([cx, cy], ring)
|
|
|
114
|
+ cells.push({
|
|
|
115
|
+ x: cx,
|
|
|
116
|
+ y: cy,
|
|
|
117
|
+ h: half,
|
|
|
118
|
+ max: cellMaxDistance(cx, cy, inside) + half * Math.SQRT2
|
|
|
119
|
+ })
|
|
|
120
|
+ }
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ cells.sort((a, b) => b.max - a.max)
|
|
|
124
|
+ let best = cells[0]
|
|
|
125
|
+ if (!best) {
|
|
|
126
|
+ return null
|
|
|
127
|
+ }
|
|
|
128
|
+
|
|
|
129
|
+ while (best.h > precision) {
|
|
|
130
|
+ const nextHalf = best.h / 2
|
|
|
131
|
+ let nextBest = null
|
|
|
132
|
+ ;[
|
|
|
133
|
+ [-nextHalf, -nextHalf],
|
|
|
134
|
+ [nextHalf, -nextHalf],
|
|
|
135
|
+ [-nextHalf, nextHalf],
|
|
|
136
|
+ [nextHalf, nextHalf]
|
|
|
137
|
+ ].forEach(([dx, dy]) => {
|
|
|
138
|
+ const cx = best.x + dx
|
|
|
139
|
+ const cy = best.y + dy
|
|
|
140
|
+ const inside = pointInPolygon([cx, cy], ring)
|
|
|
141
|
+ const cell = {
|
|
|
142
|
+ x: cx,
|
|
|
143
|
+ y: cy,
|
|
|
144
|
+ h: nextHalf,
|
|
|
145
|
+ max: cellMaxDistance(cx, cy, inside) + nextHalf * Math.SQRT2
|
|
|
146
|
+ }
|
|
|
147
|
+ if (!nextBest || cell.max > nextBest.max) {
|
|
|
148
|
+ nextBest = cell
|
|
|
149
|
+ }
|
|
|
150
|
+ })
|
|
|
151
|
+ best = nextBest || best
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ return [best.x, best.y]
|
|
|
155
|
+}
|
|
|
156
|
+
|
|
|
157
|
+/** 从多边形几何计算乡镇标注点(手动微调 > 面内视觉中心 > properties.centroid) */
|
|
|
158
|
+export function getTownshipLabelPoints(geojson = townshipGeoJson) {
|
|
|
159
|
+ return getTownshipFeatures(geojson)
|
|
|
160
|
+ .map((feature) => {
|
|
|
161
|
+ const props = feature.properties || {}
|
|
|
162
|
+ const name = String(props.name || '')
|
|
|
163
|
+ const override = TOWNSHIP_LABEL_OVERRIDES[name]
|
|
|
164
|
+ let lngLat = null
|
|
|
165
|
+
|
|
|
166
|
+ if (Array.isArray(override) && override.length >= 2) {
|
|
|
167
|
+ const lng = Number(override[0])
|
|
|
168
|
+ const lat = Number(override[1])
|
|
|
169
|
+ if (Number.isFinite(lng) && Number.isFinite(lat)) {
|
|
|
170
|
+ lngLat = [lng, lat]
|
|
|
171
|
+ }
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ if (!lngLat) {
|
|
|
175
|
+ const ring = getPolygonOuterRing(feature)
|
|
|
176
|
+ lngLat = computePolygonLabelPoint(ring)
|
|
|
177
|
+ }
|
|
|
178
|
+
|
|
|
179
|
+ if (!lngLat) {
|
|
|
180
|
+ const center = props.centroid || props.center
|
|
|
181
|
+ if (!Array.isArray(center) || center.length < 2) {
|
|
|
182
|
+ return null
|
|
|
183
|
+ }
|
|
|
184
|
+ const lng = Number(center[0])
|
|
|
185
|
+ const lat = Number(center[1])
|
|
|
186
|
+ if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
|
|
|
187
|
+ return null
|
|
|
188
|
+ }
|
|
|
189
|
+ lngLat = [lng, lat]
|
|
|
190
|
+ }
|
|
|
191
|
+
|
|
|
192
|
+ return {
|
|
|
193
|
+ name,
|
|
|
194
|
+ adcode: props.adcode,
|
|
|
195
|
+ lngLat
|
|
|
196
|
+ }
|
|
|
197
|
+ })
|
|
|
198
|
+ .filter(Boolean)
|
|
|
199
|
+}
|
|
|
200
|
+
|
|
|
201
|
+export function getTownshipBounds(feature) {
|
|
|
202
|
+ const ring = getPolygonOuterRing(feature)
|
|
|
203
|
+ if (!ring?.length) {
|
|
|
204
|
+ return null
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+ let minLng = Infinity
|
|
|
208
|
+ let maxLng = -Infinity
|
|
|
209
|
+ let minLat = Infinity
|
|
|
210
|
+ let maxLat = -Infinity
|
|
|
211
|
+ ring.forEach(([lng, lat]) => {
|
|
|
212
|
+ minLng = Math.min(minLng, lng)
|
|
|
213
|
+ maxLng = Math.max(maxLng, lng)
|
|
|
214
|
+ minLat = Math.min(minLat, lat)
|
|
|
215
|
+ maxLat = Math.max(maxLat, lat)
|
|
|
216
|
+ })
|
|
|
217
|
+
|
|
|
218
|
+ if (!Number.isFinite(minLng) || !Number.isFinite(maxLng) || !Number.isFinite(minLat) || !Number.isFinite(maxLat)) {
|
|
|
219
|
+ return null
|
|
|
220
|
+ }
|
|
|
221
|
+
|
|
|
222
|
+ return [
|
|
|
223
|
+ [minLng, minLat],
|
|
|
224
|
+ [maxLng, maxLat]
|
|
|
225
|
+ ]
|
|
|
226
|
+}
|
|
|
227
|
+
|
|
|
228
|
+/** 根据点击坐标命中乡镇面 */
|
|
|
229
|
+export function findTownshipFeatureByLngLat(lngLat, geojson = townshipGeoJson) {
|
|
|
230
|
+ const lng = Number(lngLat?.lng ?? lngLat?.[0])
|
|
|
231
|
+ const lat = Number(lngLat?.lat ?? lngLat?.[1])
|
|
|
232
|
+ if (!Number.isFinite(lng) || !Number.isFinite(lat)) {
|
|
|
233
|
+ return null
|
|
|
234
|
+ }
|
|
|
235
|
+
|
|
|
236
|
+ return (
|
|
|
237
|
+ getTownshipFeatures(geojson).find((feature) => {
|
|
|
238
|
+ const ring = getPolygonOuterRing(feature)
|
|
|
239
|
+ return ring && pointInPolygon([lng, lat], ring)
|
|
|
240
|
+ }) || null
|
|
|
241
|
+ )
|
|
|
242
|
+}
|
|
|
243
|
+
|
|
|
244
|
+export function buildTownshipInfo(feature) {
|
|
|
245
|
+ if (!feature) {
|
|
|
246
|
+ return null
|
|
|
247
|
+ }
|
|
|
248
|
+ const props = feature.properties || {}
|
|
|
249
|
+ const name = String(props.name || '')
|
|
|
250
|
+ const labelPoint = getTownshipLabelPoints().find((item) => item.name === name)?.lngLat || null
|
|
|
251
|
+ const drillCenter = getTownshipDrillCenter(feature)
|
|
|
252
|
+
|
|
|
253
|
+ return {
|
|
|
254
|
+ name,
|
|
|
255
|
+ adcode: props.adcode,
|
|
|
256
|
+ center: Array.isArray(props.center) ? [...props.center] : null,
|
|
|
257
|
+ centroid: Array.isArray(props.centroid) ? [...props.centroid] : null,
|
|
|
258
|
+ labelPoint: labelPoint ? [...labelPoint] : null,
|
|
|
259
|
+ drillCenter: drillCenter ? [...drillCenter] : null,
|
|
|
260
|
+ bounds: getTownshipBounds(feature)
|
|
|
261
|
+ }
|
|
|
262
|
+}
|
|
|
263
|
+
|
|
|
264
|
+/** 点击乡镇后缩放到指定级别(默认 zoom 19,中心取标注点/中心点) */
|
|
|
265
|
+export function flyToTownship(map, feature, options = {}) {
|
|
|
266
|
+ if (!map || !feature) {
|
|
|
267
|
+ return false
|
|
|
268
|
+ }
|
|
|
269
|
+
|
|
|
270
|
+ const duration = Number(options.duration) || 900
|
|
|
271
|
+ const zoom = Number(options.zoom ?? options.maxZoom) || 19
|
|
|
272
|
+ const center = getTownshipDrillCenter(feature)
|
|
|
273
|
+
|
|
|
274
|
+ if (Array.isArray(center) && center.length >= 2 && typeof map.easeTo === 'function') {
|
|
|
275
|
+ map.easeTo({
|
|
|
276
|
+ center,
|
|
|
277
|
+ zoom,
|
|
|
278
|
+ duration,
|
|
|
279
|
+ essential: true
|
|
|
280
|
+ })
|
|
|
281
|
+ return true
|
|
|
282
|
+ }
|
|
|
283
|
+
|
|
|
284
|
+ return false
|
|
|
285
|
+}
|
|
|
286
|
+
|
|
|
287
|
+export function addTownshipBoundaryLayers(map, geojson = townshipGeoJson) {
|
|
|
288
|
+ if (!map || typeof map.addSource !== 'function') {
|
|
|
289
|
+ return false
|
|
|
290
|
+ }
|
|
|
291
|
+ if (map.getSource(TOWNSHIP_SOURCE_ID)) {
|
|
|
292
|
+ return true
|
|
|
293
|
+ }
|
|
|
294
|
+
|
|
|
295
|
+ map.addSource(TOWNSHIP_SOURCE_ID, {
|
|
|
296
|
+ type: 'geojson',
|
|
|
297
|
+ data: geojson
|
|
|
298
|
+ })
|
|
|
299
|
+
|
|
|
300
|
+ map.addLayer({
|
|
|
301
|
+ id: TOWNSHIP_FILL_LAYER_ID,
|
|
|
302
|
+ type: 'fill',
|
|
|
303
|
+ source: TOWNSHIP_SOURCE_ID,
|
|
|
304
|
+ paint: {
|
|
|
305
|
+ 'fill-color': 'rgba(0, 0, 0, 0)',
|
|
|
306
|
+ 'fill-outline-color': 'transparent'
|
|
|
307
|
+ }
|
|
|
308
|
+ })
|
|
|
309
|
+
|
|
|
310
|
+ map.addLayer({
|
|
|
311
|
+ id: TOWNSHIP_LINE_GLOW_LAYER_ID,
|
|
|
312
|
+ type: 'line',
|
|
|
313
|
+ source: TOWNSHIP_SOURCE_ID,
|
|
|
314
|
+ paint: {
|
|
|
315
|
+ 'line-color': 'rgba(0, 229, 204, 0.5)',
|
|
|
316
|
+ 'line-width': 3.5,
|
|
|
317
|
+ 'line-blur': 2
|
|
|
318
|
+ }
|
|
|
319
|
+ })
|
|
|
320
|
+
|
|
|
321
|
+ map.addLayer({
|
|
|
322
|
+ id: TOWNSHIP_LINE_LAYER_ID,
|
|
|
323
|
+ type: 'line',
|
|
|
324
|
+ source: TOWNSHIP_SOURCE_ID,
|
|
|
325
|
+ paint: {
|
|
|
326
|
+ 'line-color': '#b8fff2',
|
|
|
327
|
+ 'line-width': 1.2
|
|
|
328
|
+ }
|
|
|
329
|
+ })
|
|
|
330
|
+
|
|
|
331
|
+ return true
|
|
|
332
|
+}
|
|
|
333
|
+
|
|
|
334
|
+export function removeTownshipBoundaryLayers(map) {
|
|
|
335
|
+ if (!map) {
|
|
|
336
|
+ return
|
|
|
337
|
+ }
|
|
|
338
|
+ ;[TOWNSHIP_LINE_LAYER_ID, TOWNSHIP_LINE_GLOW_LAYER_ID, TOWNSHIP_FILL_LAYER_ID].forEach((id) => {
|
|
|
339
|
+ if (typeof map.getLayer === 'function' && map.getLayer(id)) {
|
|
|
340
|
+ map.removeLayer(id)
|
|
|
341
|
+ }
|
|
|
342
|
+ })
|
|
|
343
|
+ if (typeof map.getSource === 'function' && map.getSource(TOWNSHIP_SOURCE_ID)) {
|
|
|
344
|
+ map.removeSource(TOWNSHIP_SOURCE_ID)
|
|
|
345
|
+ }
|
|
|
346
|
+}
|
|
|
347
|
+
|
|
|
348
|
+/** 点击乡镇面触发下钻 */
|
|
|
349
|
+export function bindTownshipBoundaryClick(map, onSelect) {
|
|
|
350
|
+ if (!map || typeof map.on !== 'function' || typeof onSelect !== 'function') {
|
|
|
351
|
+ return () => {}
|
|
|
352
|
+ }
|
|
|
353
|
+
|
|
|
354
|
+ const resolveFeature = (eventFeature) => {
|
|
|
355
|
+ const adcode = eventFeature?.properties?.adcode
|
|
|
356
|
+ if (adcode == null) {
|
|
|
357
|
+ return eventFeature
|
|
|
358
|
+ }
|
|
|
359
|
+ return getTownshipFeatures().find((feature) => feature.properties?.adcode === adcode) || eventFeature
|
|
|
360
|
+ }
|
|
|
361
|
+
|
|
|
362
|
+ const onClick = (e) => {
|
|
|
363
|
+ if (!e.features?.length) {
|
|
|
364
|
+ return
|
|
|
365
|
+ }
|
|
|
366
|
+ const feature = resolveFeature(e.features[0])
|
|
|
367
|
+ onSelect(feature, e)
|
|
|
368
|
+ }
|
|
|
369
|
+
|
|
|
370
|
+ const onEnter = () => {
|
|
|
371
|
+ if (typeof map.getCanvas === 'function') {
|
|
|
372
|
+ map.getCanvas().style.cursor = 'pointer'
|
|
|
373
|
+ }
|
|
|
374
|
+ }
|
|
|
375
|
+
|
|
|
376
|
+ const onLeave = () => {
|
|
|
377
|
+ if (typeof map.getCanvas === 'function') {
|
|
|
378
|
+ map.getCanvas().style.cursor = ''
|
|
|
379
|
+ }
|
|
|
380
|
+ }
|
|
|
381
|
+
|
|
|
382
|
+ map.on('click', TOWNSHIP_FILL_LAYER_ID, onClick)
|
|
|
383
|
+ map.on('mouseenter', TOWNSHIP_FILL_LAYER_ID, onEnter)
|
|
|
384
|
+ map.on('mouseleave', TOWNSHIP_FILL_LAYER_ID, onLeave)
|
|
|
385
|
+
|
|
|
386
|
+ return () => {
|
|
|
387
|
+ map.off('click', TOWNSHIP_FILL_LAYER_ID, onClick)
|
|
|
388
|
+ map.off('mouseenter', TOWNSHIP_FILL_LAYER_ID, onEnter)
|
|
|
389
|
+ map.off('mouseleave', TOWNSHIP_FILL_LAYER_ID, onLeave)
|
|
|
390
|
+ onLeave()
|
|
|
391
|
+ }
|
|
|
392
|
+}
|