w-gis 1.0.12 → 1.0.13

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.
Files changed (36) hide show
  1. package/README.md +1 -1
  2. package/dist/w-gis.umd.js +2 -2
  3. package/dist/w-gis.umd.js.map +1 -1
  4. package/docs/-_class.mjs.html +1 -1
  5. package/docs/convertCoordinate.mjs.html +1 -1
  6. package/docs/examples/ex-getCenterOfMassMultiPolygon.html +1 -1
  7. package/docs/examples/ex-getCentroidMultiPolygon.html +1 -1
  8. package/docs/index.html +1 -1
  9. package/docs/w-gis.html +1 -1
  10. package/g-getCenterOfMassMultiPolygon.mjs +3 -3
  11. package/g-getCentroidMultiPolygon.mjs +3 -3
  12. package/package.json +2 -1
  13. package/src/WGis.mjs +18 -585
  14. package/src/{convertTurfCode.mjs → _convertTurfCode.mjs} +5 -5
  15. package/src/{importTurfBrowser.mjs → _importTurfBrowser.mjs} +1 -4
  16. package/src/{importTurfNode.mjs → _importTurfNode.mjs} +0 -0
  17. package/src/_turfCoreBrowser.mjs +1 -0
  18. package/src/calcContours.mjs +267 -0
  19. package/src/clipMultiPolygon.mjs +15 -0
  20. package/src/distilMultiPolygon.mjs +30 -0
  21. package/src/fixGeometryMultiPolygon.mjs +52 -0
  22. package/src/getArea.mjs +20 -0
  23. package/src/getCenterOfMassMultiPolygon.mjs +24 -0
  24. package/src/getCentroidMultiPolygon.mjs +24 -0
  25. package/src/getTurf.mjs +32 -0
  26. package/src/intersectMultiPolygon.mjs +21 -0
  27. package/src/invCoordMultiPolygon.mjs +22 -0
  28. package/src/invCoordPolygon.mjs +24 -0
  29. package/src/isPointInPolygon.mjs +21 -0
  30. package/src/maskMultiPolygon.mjs +21 -0
  31. package/src/parseGeometryCollection.mjs +27 -0
  32. package/src/simplifyMultiPolygon.mjs +41 -0
  33. package/src/splineMultiPolygon.mjs +38 -0
  34. package/src/toMultiPolygon.mjs +49 -0
  35. package/toolg/gPackageIndex.mjs +46 -0
  36. package/src/importTurfBrowser.json +0 -1
package/src/WGis.mjs CHANGED
@@ -1,585 +1,18 @@
1
- import each from 'lodash/each'
2
- import map from 'lodash/map'
3
- import min from 'lodash/min'
4
- import max from 'lodash/max'
5
- import get from 'lodash/get'
6
- import filter from 'lodash/filter'
7
- import size from 'lodash/size'
8
- import isNumber from 'lodash/isNumber'
9
- import cloneDeep from 'lodash/cloneDeep'
10
- import isernot from 'wsemi/src/isernot.mjs'
11
- import isearr from 'wsemi/src/isearr.mjs'
12
- import isbol from 'wsemi/src/isbol.mjs'
13
- import isWindow from 'wsemi/src/isWindow.mjs'
14
- import { tricontour } from 'd3-tricontour'
15
- import turfBrowser from './importTurfBrowser.mjs'
16
- import turfNode from './importTurfNode.mjs'
17
- import convertCoordinate from './convertCoordinate.mjs'
18
-
19
-
20
- let turf = null
21
- if (isWindow()) {
22
- // console.log('use turfBrowser')
23
- turf = turfBrowser
24
- }
25
- else {
26
- // console.log('use turfNode')
27
- turf = turfNode
28
- }
29
- // console.log('turf', turf)
30
-
31
-
32
- //turf function
33
- let helpers = turf.helpers
34
- let { polygon, multiPolygon, lineString } = helpers
35
- let area = turf.area
36
- let centroid = turf.centroid
37
- let centerOfMass = turf.centerOfMass
38
- let intersect = turf.intersect
39
- let mask = turf.mask
40
- let difference = turf.difference
41
- let bezierSpline = turf.bezierSpline
42
- let buffer = turf.buffer
43
- let booleanPointInPolygon = turf.booleanPointInPolygon
44
-
45
-
46
- function toMultiPolygon(v) {
47
-
48
- function pointDepth(v) {
49
- let ip1 = size(get(v, '0', null))
50
- let ip2 = size(get(v, '0.0', null))
51
- let ip3 = size(get(v, '0.0.0', null))
52
- if (ip3 === 2) {
53
- return 3
54
- }
55
- if (ip2 === 2) {
56
- return 2
57
- }
58
- if (ip1 === 2) {
59
- return 1
60
- }
61
- if (ip1 === 0) {
62
- return 0
63
- }
64
- console.log('invalid point depth', v)
65
- return null
66
- }
67
-
68
- let d = pointDepth(v)
69
- if (d === 3) {
70
- return v
71
- }
72
- if (d === 2) {
73
- //return [v]
74
- return map(v, (vv) => {
75
- return [vv]
76
- })
77
- }
78
- if (d === 1) {
79
- return [[v]]
80
- }
81
- if (d === 0) {
82
- return []
83
- }
84
- return v
85
- }
86
-
87
-
88
- function getCentroidMultiPolygon(pgs) {
89
- if (!isearr(pgs)) {
90
- return null
91
- }
92
- let r = centroid(multiPolygon(toMultiPolygon(pgs)))
93
- let pt = r.geometry.coordinates
94
- return pt
95
- }
96
-
97
-
98
- function getCenterOfMassMultiPolygon(pgs) {
99
- if (!isearr(pgs)) {
100
- return null
101
- }
102
- let r = centerOfMass(multiPolygon(toMultiPolygon(pgs)))
103
- let pt = r.geometry.coordinates
104
- return pt
105
- }
106
-
107
-
108
- function getArea(pgs) {
109
- if (!isearr(pgs)) {
110
- return null
111
- }
112
- let r = area(multiPolygon(toMultiPolygon(pgs)))
113
- return r
114
- }
115
-
116
-
117
- function invCoordPolygon(pg) {
118
- //因為turf的point是先經再緯跟leaflet相反, 故需相反座標
119
- let r = map(pg, (v) => {
120
- return map(v, (vv) => {
121
- return [vv[1], vv[0]]
122
- })
123
- })
124
- return r
125
- }
126
-
127
-
128
- function invCoordMultiPolygon(pgs) {
129
- let r = map(pgs, (pg) => {
130
- return invCoordPolygon(pg)
131
- })
132
- return r
133
- }
134
-
135
-
136
- function isPointInPolygon(p, pgs) {
137
- let r = booleanPointInPolygon(p, multiPolygon(toMultiPolygon(pgs)))
138
- return r
139
- }
140
-
141
-
142
- function parseGeometryCollection(data) {
143
- let gs = get(data, 'geometry.geometries', [])
144
- let pgs = []
145
- each(gs, (v) => {
146
- if (v.type === 'Polygon') {
147
- pgs.push(v.coordinates)
148
- }
149
- else if (v.type === 'MultiPolygon') {
150
- each(v.coordinates, (vv) => {
151
- pgs.push(vv)
152
- })
153
- }
154
- })
155
- return pgs
156
- }
157
-
158
-
159
- function distilMultiPolygon(r) {
160
- //因turf計算後可能產生Polygon,MultiPolygon,LineString,GeometryCollection, 故將全部轉成MultiPolygon後回傳
161
- let type = get(r, 'geometry.type')
162
- let pgNew = get(r, 'geometry.coordinates')
163
- if (type === 'Polygon') {
164
- return [pgNew]
165
- }
166
- else if (type === 'MultiPolygon') {
167
- return pgNew
168
- }
169
- else if (type === 'GeometryCollection') {
170
- return parseGeometryCollection(r)
171
- }
172
- else {
173
- // console.log('type', type, r)
174
- return []
175
- }
176
- }
177
-
178
-
179
- function intersectMultiPolygon(pgs, pgsInter) {
180
-
181
- //pgNew
182
- let r = intersect(multiPolygon(toMultiPolygon(pgs)), multiPolygon(toMultiPolygon(pgsInter)))
183
-
184
- return distilMultiPolygon(r)
185
- }
186
-
187
-
188
- function maskMultiPolygon(pgs) {
189
-
190
- //pgNew
191
- let r = mask(multiPolygon(toMultiPolygon(pgs)))
192
-
193
- return distilMultiPolygon(r)
194
- }
195
-
196
-
197
- function clipMultiPolygon(pgs, pgsCut) {
198
-
199
- //difference
200
- let r = difference(multiPolygon(toMultiPolygon(pgs)), multiPolygon(toMultiPolygon(pgsCut)))
201
-
202
- return distilMultiPolygon(r)
203
- }
204
-
205
-
206
- function splineMultiPolygon(pgs, opt = {}) {
207
- //splineMultiPolygon(pgs, { resolution: 50000, sharpness: 0.05 })
208
-
209
- function core(pg, opt = {}) {
210
-
211
- //cloneDeep
212
- pg = cloneDeep(pg)
213
-
214
- //pgNew
215
- let pgNew = map(pg, (ps, k) => {
216
- let line = lineString(ps)
217
- let r = bezierSpline(line, opt)
218
- let psNew = get(r, 'geometry.coordinates')
219
- return psNew
220
- })
221
-
222
- return pgNew
223
- }
224
-
225
- //cloneDeep
226
- pgs = cloneDeep(pgs)
227
-
228
- //core
229
- pgs = map(pgs, (pg) => {
230
- return core(pg, opt)
231
- })
232
-
233
- return pgs
234
- }
235
-
236
-
237
- function simplifyMultiPolygon(pgs, opt = {}) {
238
-
239
- //check
240
- if (!isearr(pgs)) {
241
- return null
242
- }
243
-
244
- //tolerance
245
- let tolerance = get(opt, 'tolerance')
246
- if (!isNumber(tolerance)) {
247
- tolerance = 0.005
248
- }
249
-
250
- //highQuality
251
- let highQuality = get(opt, 'highQuality')
252
- if (!isbol(highQuality)) {
253
- highQuality = true
254
- }
255
-
256
- //simplify
257
- let r = turf.simplify(turf.multiPolygon(toMultiPolygon(pgs)), { tolerance, highQuality })
258
-
259
- //get pgs
260
- r = get(r, 'geometry.coordinates', [])
261
- // console.log('r',r)
262
-
263
- return r
264
- }
265
-
266
-
267
- function fixGeometryMultiPolygon(pgs) {
268
-
269
- function core(pg) {
270
-
271
- //invCoordPolygon, 因為buffer需要輸入正確經緯度才有辦法運算
272
- pg = invCoordPolygon(pg)
273
-
274
- //pgt
275
- let pgt = polygon(pg)
276
-
277
- //buffer
278
- let bf = buffer(pgt, 1, { units: 'kilometers' })
279
-
280
- //distilMultiPolygon
281
- let pgsNew = distilMultiPolygon(bf)
282
-
283
- //invCoordMultiPolygon
284
- pgsNew = invCoordMultiPolygon(pgsNew)
285
-
286
- return pgsNew
287
- }
288
-
289
- //cloneDeep
290
- pgs = cloneDeep(pgs)
291
-
292
- //core
293
- let pgsNew = []
294
- each(pgs, (pg) => {
295
- let r = core(pg) //回傳是MultiPolygon
296
- each(r, (v) => {
297
- pgsNew.push(v)
298
- })
299
- })
300
-
301
- //filter
302
- pgsNew = filter(pgsNew, isernot)
303
-
304
- return pgsNew
305
- }
306
-
307
-
308
- function calcContours(points, opt = {}) {
309
-
310
- function getContours(pts) {
311
-
312
- //tricontour
313
- let tric = tricontour()
314
-
315
- //calc
316
- let contours = tric(pts)
317
- // console.log('calcContours contours1', contours)
318
- // contours = [contours[0], contours[1], contours[2], contours[3]]
319
- // console.log('calcContours contours2', contours)
320
-
321
- return contours
322
- }
323
-
324
- //containInner
325
- let containInner = get(opt, 'containInner', null)
326
-
327
- //clipInner
328
- let clipInner = get(opt, 'clipInner', null)
329
-
330
- //clipOuter
331
- let clipOuter = get(opt, 'clipOuter', null)
332
-
333
- //valueMin, valueMax
334
- let valueMin = points[0][2]
335
- let valueMax = points[0][2]
336
- for (let i = 1; i < size(points); i++) {
337
- let value = points[i][2]
338
- if (valueMin > value) {
339
- valueMin = value
340
- }
341
- if (valueMax < value) {
342
- valueMax = value
343
- }
344
- }
345
- // console.log('valueMin', valueMin, 'valueMax', valueMax)
346
-
347
- //contours
348
- let contours = getContours(points)
349
- // console.log('contours', cloneDeep(contours))
350
-
351
- //check
352
- if (!isearr(contours)) {
353
- // console.log('can not calculate contours', contours)
354
- return []
355
- }
356
- // console.log('contours.length', contours.length)
357
-
358
- //polylines
359
- let polylines = map(contours, (v, k) => {
360
-
361
- return {
362
- latLngs: v.coordinates,
363
- level: v.value,
364
- effectArea: getArea(v.coordinates),
365
- effectAreaCentroid: getCentroidMultiPolygon(v.coordinates), //要先計算, 否則之後會被相減計算成真實等值區域, 就不是影響區域的中心了
366
- }
367
- })
368
- // console.log('polylines from contours', cloneDeep(polylines))
369
-
370
- //針對可能超出數據區添加polyline
371
- if (true) {
372
-
373
- //pmin, pmax
374
- let pmin = min(map(polylines, 'level'))
375
- let pmax = max(map(polylines, 'level'))
376
- // console.log('pmin', pmin, 'pmax', pmax)
377
-
378
- //實際數據有超出contours的最大值, 添加虛擬polyline
379
- if (pmax < valueMax) {
380
- polylines.push({
381
- mode: 'virtualEnd',
382
- latLngs: [],
383
- level: valueMax,
384
- effectArea: 0,
385
- })
386
- }
387
-
388
- //實際數據有低於contours的最小值, 添加虛擬polyline
389
- if (pmin > valueMin) {
390
- let pl = {
391
- mode: 'virtualStart',
392
- latLngs: [],
393
- level: valueMin,
394
- effectArea: 0,
395
- }
396
- polylines = [pl, ...polylines]
397
- }
398
-
399
- }
400
- // console.log('polylines for vartual level', cloneDeep(polylines))
401
-
402
- //若不是於超出數據區新增虛擬polyline, 因tricontour會給出繪圖間距而不是實際資料間距, 故會出現level值大於或小於原數據上下限
403
- if (true) {
404
- each(polylines, (v, k) => {
405
- v.level = Math.min(v.level, valueMax)
406
- v.level = Math.max(v.level, valueMin)
407
- })
408
- }
409
- // console.log('polylines for limit level', cloneDeep(polylines))
410
-
411
- //polygonSets, 剔除下1個多邊形區域, 為實際需繪製的等值區
412
- let polygonSets = []
413
- for (let i = 0; i <= polylines.length - 2; i++) {
414
-
415
- //p0,p1,range
416
- let p0 = polylines[i].latLngs
417
- let p1 = polylines[i + 1].latLngs
418
- let range = {
419
- text: `${polylines[i].level} - ${polylines[i + 1].level}`,
420
- low: polylines[i].level,
421
- up: polylines[i + 1].level,
422
- }
423
-
424
- //clipMultiPolygon
425
- let latLngs = []
426
- latLngs = clipMultiPolygon(p0, p1)
427
- if (get(polylines[i], 'mode' === 'virtualStart')) { //若為virtualStart, 則代表直接使用下1個polylines成為等值區域, 方能代表凹陷區
428
- latLngs = p1 //因為既有屬性都是取前者, 故若virtualStart係使用後者p1, 就代表全部polygonSet都會有真實effectArea
429
- }
430
- else {
431
- latLngs = clipMultiPolygon(p0, p1)
432
- }
433
-
434
- polygonSets.push({
435
- ...polylines[i],
436
- latLngs,
437
- range,
438
- })
439
- }
440
- // console.log('polygonSets from polylines', cloneDeep(polygonSets))
441
-
442
- //polygonsContainInner, 保留指定多polygon以內區域
443
- if (true) {
444
- let t = []
445
- each(polygonSets, (polygonSet, k) => {
446
-
447
- //latLngs
448
- let latLngs = null
449
- if (isearr(containInner)) {
450
-
451
- //intersectMultiPolygon
452
- latLngs = intersectMultiPolygon(polygonSet.latLngs, containInner)
453
-
454
- }
455
- else {
456
- latLngs = polygonSet.latLngs
457
- }
458
-
459
- //check
460
- if (size(latLngs) > 0) {
461
- t.push({
462
- ...polygonSet,
463
- latLngs,
464
- })
465
- }
466
-
467
- })
468
- polygonSets = t
469
- }
470
- // console.log('polygonSets for polygonsContainInner', cloneDeep(polygonSets))
471
-
472
- //polygonsClipInner, 剔除指定多polygon以內區域
473
- if (true) {
474
- let t = []
475
- each(polygonSets, (polygonSet, k) => {
476
-
477
- //latLngs
478
- let latLngs = null
479
- if (isearr(clipInner)) {
480
-
481
- //clipMultiPolygon
482
- latLngs = clipMultiPolygon(polygonSet.latLngs, clipInner)
483
-
484
- }
485
- else {
486
- latLngs = polygonSet.latLngs
487
- }
488
-
489
- //check
490
- if (size(latLngs) > 0) {
491
- t.push({
492
- ...polygonSet,
493
- latLngs,
494
- })
495
- }
496
-
497
- })
498
- polygonSets = t
499
- }
500
- // console.log('polygonSets for polygonsClipInner', cloneDeep(polygonSets))
501
-
502
- //polygonClipOuter, 剔除指定polygon以外區域
503
- if (true) {
504
- let t = []
505
- each(polygonSets, (polygonSet) => {
506
-
507
- //latLngs
508
- let latLngs = null
509
- if (isearr(clipOuter)) {
510
-
511
- //intersectMultiPolygon
512
- latLngs = intersectMultiPolygon(polygonSet.latLngs, clipOuter)
513
-
514
- }
515
- else {
516
- latLngs = polygonSet.latLngs
517
- }
518
-
519
- //check
520
- if (size(latLngs) > 0) {
521
- t.push({
522
- ...polygonSet,
523
- latLngs,
524
- })
525
- }
526
-
527
- })
528
- polygonSets = t
529
- }
530
- // console.log('polygonSets for polygonClipOuter', cloneDeep(polygonSets))
531
-
532
- //center
533
- if (true) {
534
- let areaMax = 0
535
- let areaInd = null
536
- let areaCentroid = null
537
- each(polygonSets, (polygonSet, k) => {
538
- if (areaMax < polygonSet.effectArea) {
539
- areaInd = k
540
- areaMax = polygonSet.effectArea
541
- areaCentroid = polygonSet.effectAreaCentroid
542
- }
543
- })
544
- if (areaInd === null) {
545
- // console.log('can not calculate centroid of contour', polygonSets)
546
- return []
547
- }
548
-
549
- //add center
550
- each(polygonSets, (polygonSet, k) => {
551
- polygonSets[k].center = areaCentroid
552
- })
553
-
554
- }
555
-
556
- return polygonSets
557
- }
558
-
559
-
560
- let WGis = {
561
-
562
- turf,
563
-
564
- toMultiPolygon,
565
- getCentroidMultiPolygon,
566
- getCenterOfMassMultiPolygon,
567
- getArea,
568
- isPointInPolygon,
569
- intersectMultiPolygon,
570
- clipMultiPolygon,
571
- maskMultiPolygon,
572
- splineMultiPolygon,
573
- simplifyMultiPolygon,
574
- fixGeometryMultiPolygon,
575
- calcContours,
576
-
577
- invCoordPolygon,
578
- invCoordMultiPolygon,
579
-
580
- convertCoordinate,
581
-
582
- }
583
-
584
-
585
- export default WGis
1
+ export { default as calcContours } from './calcContours.mjs'
2
+ export { default as clipMultiPolygon } from './clipMultiPolygon.mjs'
3
+ export { default as convertCoordinate } from './convertCoordinate.mjs'
4
+ export { default as distilMultiPolygon } from './distilMultiPolygon.mjs'
5
+ export { default as fixGeometryMultiPolygon } from './fixGeometryMultiPolygon.mjs'
6
+ export { default as getArea } from './getArea.mjs'
7
+ export { default as getCenterOfMassMultiPolygon } from './getCenterOfMassMultiPolygon.mjs'
8
+ export { default as getCentroidMultiPolygon } from './getCentroidMultiPolygon.mjs'
9
+ export { default as getTurf } from './getTurf.mjs'
10
+ export { default as intersectMultiPolygon } from './intersectMultiPolygon.mjs'
11
+ export { default as invCoordMultiPolygon } from './invCoordMultiPolygon.mjs'
12
+ export { default as invCoordPolygon } from './invCoordPolygon.mjs'
13
+ export { default as isPointInPolygon } from './isPointInPolygon.mjs'
14
+ export { default as maskMultiPolygon } from './maskMultiPolygon.mjs'
15
+ export { default as parseGeometryCollection } from './parseGeometryCollection.mjs'
16
+ export { default as simplifyMultiPolygon } from './simplifyMultiPolygon.mjs'
17
+ export { default as splineMultiPolygon } from './splineMultiPolygon.mjs'
18
+ export { default as toMultiPolygon } from './toMultiPolygon.mjs'
@@ -8,14 +8,14 @@ let b64 = w.str2b64(c)
8
8
 
9
9
 
10
10
  //輸出json
11
- let j = `{"b64":"${b64}"}`
12
- fs.writeFileSync('./src/importTurfBrowser.json', j, 'utf8')
11
+ let j = `let b64='${b64}'; export default b64`
12
+ fs.writeFileSync('./src/_turfCoreBrowser.mjs', j, 'utf8')
13
13
 
14
14
 
15
15
  // //塞入程式碼於前端自動引入至window
16
- // let t = fs.readFileSync('./src/importTurfBrowser.tmp', 'utf8')
16
+ // let t = fs.readFileSync('./src/_importTurfBrowser.tmp', 'utf8')
17
17
  // t = t.replace('{code}', b64)
18
- // fs.writeFileSync('./src/importTurfBrowser.mjs', t, 'utf8')
18
+ // fs.writeFileSync('./src/_importTurfBrowser.mjs', t, 'utf8')
19
19
 
20
20
 
21
- //node --experimental-modules --es-module-specifier-resolution=node src/convertTurfCode.mjs
21
+ //node --experimental-modules --es-module-specifier-resolution=node src/_convertTurfCode.mjs
@@ -2,7 +2,7 @@ import b642str from 'wsemi/src/b642str.mjs'
2
2
  import haskey from 'wsemi/src/haskey.mjs'
3
3
  import isWindow from 'wsemi/src/isWindow.mjs'
4
4
  import getGlobal from 'wsemi/src/getGlobal.mjs'
5
- import j from './importTurfBrowser.json'
5
+ import b64 from './_turfCoreBrowser.mjs'
6
6
 
7
7
 
8
8
  function init() {
@@ -24,9 +24,6 @@ function init() {
24
24
  //ele
25
25
  let ele = g.document.createElement('script')
26
26
 
27
- //b64
28
- let b64 = j.b64
29
-
30
27
  //innerHTML
31
28
  ele.innerHTML = b642str(b64)
32
29
 
File without changes