gis-common 3.1.1 → 3.1.2

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.
@@ -0,0 +1,94 @@
1
+ function trim(str) {
2
+ return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '')
3
+ }
4
+ function splitWords(str) {
5
+ return trim(str).split(/\s+/)
6
+ }
7
+ export default {
8
+ getStyle(el, style) {
9
+ let value = el.style[style] || (el.currentStyle && el.currentStyle[style])
10
+
11
+ if ((!value || value === 'auto') && document.defaultView) {
12
+ const css = document.defaultView.getComputedStyle(el, null)
13
+ value = css ? css[style] : null
14
+ }
15
+ return value === 'auto' ? null : value
16
+ },
17
+ create(tagName, className, container) {
18
+ const el = document.createElement(tagName)
19
+ el.className = className || ''
20
+
21
+ if (container) {
22
+ container.appendChild(el)
23
+ }
24
+ return el
25
+ },
26
+ remove(el) {
27
+ const parent = el.parentNode
28
+ if (parent) {
29
+ parent.removeChild(el)
30
+ }
31
+ },
32
+ empty(el) {
33
+ while (el.firstChild) {
34
+ el.removeChild(el.firstChild)
35
+ }
36
+ },
37
+ toFront(el) {
38
+ const parent = el.parentNode
39
+ if (parent && parent.lastChild !== el) {
40
+ parent.appendChild(el)
41
+ }
42
+ },
43
+ toBack(el) {
44
+ const parent = el.parentNode
45
+ if (parent && parent.firstChild !== el) {
46
+ parent.insertBefore(el, parent.firstChild)
47
+ }
48
+ },
49
+ getClass(el) {
50
+ if (el.correspondingElement) {
51
+ el = el.correspondingElement
52
+ }
53
+ return el.className.baseVal === undefined ? el.className : el.className.baseVal
54
+ },
55
+ hasClass(el, name) {
56
+ if (el.classList !== undefined) {
57
+ return el.classList.contains(name)
58
+ }
59
+ const className = this.getClass(el)
60
+ return className.length > 0 && new RegExp('(^|\\s)' + name + '(\\s|$)').test(className)
61
+ },
62
+ addClass(el, name) {
63
+ if (el.classList !== undefined) {
64
+ const classes = splitWords(name)
65
+ for (let i = 0, len = classes.length; i < len; i++) {
66
+ el.classList.add(classes[i])
67
+ }
68
+ } else if (!this.hasClass(el, name)) {
69
+ const className = this.getClass(el)
70
+ this.setClass(el, (className ? className + ' ' : '') + name)
71
+ }
72
+ },
73
+ removeClass(el, name) {
74
+ if (el.classList !== undefined) {
75
+ const classes = splitWords(name)
76
+ classes.forEach((name) => el.classList.remove(name))
77
+ } else {
78
+ this.setClass(el, trim((' ' + this.getClass(el) + ' ').replace(' ' + name + ' ', ' ')))
79
+ }
80
+ },
81
+ setClass(el, name) {
82
+ if (el.className.baseVal === undefined) {
83
+ el.className = name
84
+ } else {
85
+ // in case of SVG element
86
+ el.className.baseVal = name
87
+ }
88
+ },
89
+ parseFromString(str) {
90
+ const parser = new DOMParser()
91
+ const doc = parser.parseFromString(str, 'text/xml')
92
+ return doc.children[0]
93
+ },
94
+ }
@@ -0,0 +1,104 @@
1
+ export default {
2
+ /**
3
+ * 将base64类型编码的图片转换成blob类型
4
+ *
5
+ * @param {*} data
6
+ * @returns {*}
7
+ */
8
+ convertBase64ToBlob(data) {
9
+ var mimeString = data.split(',')[0].split(':')[1].split(';')[0]
10
+ var byteString = window.atob(data.split(',')[1])
11
+ var ab = new ArrayBuffer(byteString.length)
12
+ var ia = new Uint8Array(ab)
13
+
14
+ for (var i = 0; i < byteString.length; i++) {
15
+ ia[i] = byteString.charCodeAt(i)
16
+ }
17
+ var bb = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder
18
+
19
+ if (bb) {
20
+ bb = new (window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder)()
21
+ bb.append(ab)
22
+ return bb.getBlob(mimeString)
23
+ } else {
24
+ bb = new Blob([ab], {
25
+ type: mimeString,
26
+ })
27
+ return bb
28
+ }
29
+ },
30
+ /**
31
+ * 将url图片转换成base64编码的图片,也可用于图片压缩
32
+ *
33
+ * @param {*} url 需要转换的图片url
34
+ * @param {*} width 指定宽高,如未指定则使用默认宽度
35
+ * @param {*} height
36
+ * @returns {*}
37
+ */
38
+ convertUrlToBase64(url, width, height) {
39
+ return new Promise(function (resolve, reject) {
40
+ var img = new Image()
41
+ img.crossOrigin = 'Anonymous'
42
+ img.src = url
43
+ img.onload = function () {
44
+ var canvas = document.createElement('canvas')
45
+ canvas.width = width || img.width
46
+ canvas.height = height || img.height
47
+ var ctx = canvas.getContext('2d')
48
+ ctx.drawImage(img, 0, 0, img.width, img.height)
49
+ var ext = img.src.substring(img.src.lastIndexOf('.') + 1).toLowerCase()
50
+ var dataURL = canvas.toDataURL('image/' + ext)
51
+ var base64 = {
52
+ dataURL: dataURL,
53
+ type: 'image/' + ext,
54
+ ext: ext,
55
+ }
56
+ resolve(base64)
57
+ }
58
+ })
59
+ },
60
+ /**
61
+ * 将base64类型编码的图片转换成file类型
62
+ *
63
+ * @param {*} dataurl
64
+ * @param {*} filename
65
+ * @returns {*}
66
+ */
67
+ convertBase64ToFile(dataurl, filename) {
68
+ var arr = dataurl.split(','),
69
+ mime = arr[0].match(/:(.*?);/)[1],
70
+ bstr = atob(arr[1]),
71
+ n = bstr.length,
72
+ u8arr = new Uint8Array(n)
73
+ while (n--) {
74
+ u8arr[n] = bstr.charCodeAt(n)
75
+ }
76
+ let file = new File([u8arr], filename, { type: mime })
77
+ return file
78
+ },
79
+ /**
80
+ * 直接下载文件,支持blob类型和url类型
81
+ *
82
+ * @param {*} data
83
+ * @param {*} saveName
84
+ */
85
+ downloadFromFile(data, saveName) {
86
+ if (typeof data == 'object') {
87
+ if (data instanceof Blob) {
88
+ data = URL.createObjectURL(data) // 创建blob地址
89
+ } else {
90
+ const str = JSON.stringify(data)
91
+ const blob = new Blob([str], { type: 'text/json' }) // json对象
92
+ data = window.URL.createObjectURL(blob)
93
+ }
94
+ } else if (typeof data == 'string' && data.indexOf('http') === -1) {
95
+ const blob = new Blob([data], { type: 'text/json' }) //json文本
96
+ data = window.URL.createObjectURL(blob)
97
+ }
98
+ var link = document.createElement('a')
99
+ link.href = data
100
+ link.download = saveName || '' // HTML5新增的属性,指定保存文件名,可以不要后缀,注意,file:///模式下不会生效
101
+ link.click()
102
+ window.URL.revokeObjectURL(link.href)
103
+ },
104
+ }
@@ -0,0 +1,253 @@
1
+ import MathUtils from './MathUtils'
2
+ export default {
3
+ toRadian: Math.PI / 180,
4
+ R: 6371393,
5
+ /**
6
+ * 验证经纬度坐标
7
+ *
8
+ * @param {*} lng
9
+ * @param {*} lat
10
+ * @returns {*} {boolean}
11
+ */
12
+ isLnglat(lng, lat) {
13
+ return lng && lat && !!(+lat > -90 && +lat < 90 && +lng > -180 && +lng < 180)
14
+ },
15
+ /**
16
+ * 格式化经纬度(转度分秒)
17
+ *
18
+ * @param {*} lng
19
+ * @param {*} lat
20
+ * @returns {*}
21
+ */
22
+ formatLnglat(lng, lat) {
23
+ let res = ''
24
+ function format(value) {
25
+ const a = parseFloat(value)
26
+ const degree = parseInt(a)
27
+ let min = parseInt((a - degree) * 60)
28
+ let sec = (a - degree) * 3600 - min * 60
29
+ return degree + '°' + min + '′' + sec.toFixed(2) + '″'
30
+ }
31
+ if (lng && lat) {
32
+ res = format(lng) + ',' + format(lat)
33
+ }
34
+ return res
35
+ },
36
+ /**
37
+ * 度分秒转十进制
38
+ *
39
+ * @param {*} lng
40
+ * @param {*} lat
41
+ * @returns {*}
42
+ */
43
+ transformLnglat(lng, lat) {
44
+ function dms2deg(s) {
45
+ var sw = /[sw]/i.test(s)
46
+ var f = sw ? -1 : 1
47
+ var bits = s.match(/[\d.]+/g)
48
+ var result = 0
49
+ for (var i = 0, iLen = bits.length; i < iLen; i++) {
50
+ result += bits[i] / f
51
+ f *= 60
52
+ }
53
+ return result
54
+ }
55
+ if (lng && lat) {
56
+ return {
57
+ lng: dms2deg(lng),
58
+ lat: dms2deg(lat),
59
+ }
60
+ }
61
+ },
62
+ /**
63
+ * 判断点是否在多边形内
64
+ *
65
+ * @param {*} p
66
+ * @param {*} poly
67
+ * @returns {*}
68
+ */
69
+ rayCasting(p, poly) {
70
+ var px = p.x,
71
+ py = p.y,
72
+ flag = false
73
+ for (var i = 0, l = poly.length, j = l - 1; i < l; j = i, i++) {
74
+ var sx = poly[i].x,
75
+ sy = poly[i].y,
76
+ tx = poly[j].x,
77
+ ty = poly[j].y
78
+ // 点与多边形顶点重合
79
+ if ((sx === px && sy === py) || (tx === px && ty === py)) {
80
+ return 'on'
81
+ }
82
+ // 判断线段两端点是否在射线两侧
83
+ if ((sy < py && ty >= py) || (sy >= py && ty < py)) {
84
+ // 线段上与射线 Y 坐标相同的点的 X 坐标
85
+ var x = sx + ((py - sy) * (tx - sx)) / (ty - sy)
86
+ // 点在多边形的边上
87
+ if (x === px) {
88
+ return 'on'
89
+ }
90
+ // 射线穿过多边形的边界
91
+ if (x > px) {
92
+ flag = !flag
93
+ }
94
+ }
95
+ }
96
+ // 射线穿过多边形边界的次数为奇数时点在多边形内
97
+ return flag ? 'in' : 'out'
98
+ },
99
+ /**
100
+ * 计算两点距离
101
+ *
102
+ * @param {*} fromPoint
103
+ * @param {*} toPoint
104
+ * @returns {*}
105
+ */
106
+ wgs84PointsDistance(fromPoint, toPoint) {
107
+ var PI = Math.PI
108
+ function getRad(d) {
109
+ return (d * PI) / 180.0
110
+ }
111
+ if (arguments.length != 2) {
112
+ return 0
113
+ }
114
+ lon1 = fromPoint.x
115
+ lat1 = fromPoint.y
116
+ lon2 = toPoint.x
117
+ lat2 = toPoint.y
118
+ var a = 6378137,
119
+ b = 6356752.3142,
120
+ f = 1 / 298.257223563
121
+ var L = getRad(lon2 - lon1)
122
+ var U1 = Math.atan((1 - f) * Math.tan(getRad(lat1)))
123
+ var U2 = Math.atan((1 - f) * Math.tan(getRad(lat2)))
124
+ var sinU1 = Math.sin(U1),
125
+ cosU1 = Math.cos(U1)
126
+ var sinU2 = Math.sin(U2),
127
+ cosU2 = Math.cos(U2)
128
+ var lambda = L,
129
+ lambdaP,
130
+ iterLimit = 100
131
+ do {
132
+ var sinLambda = Math.sin(lambda),
133
+ cosLambda = Math.cos(lambda)
134
+ var sinSigma = Math.sqrt(
135
+ cosU2 * sinLambda * (cosU2 * sinLambda) + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda)
136
+ )
137
+ if (sinSigma == 0) return 0
138
+ var cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda
139
+ var sigma = Math.atan2(sinSigma, cosSigma)
140
+ var sinAlpha = (cosU1 * cosU2 * sinLambda) / sinSigma
141
+ var cosSqAlpha = 1 - sinAlpha * sinAlpha
142
+ var cos2SigmaM = cosSigma - (2 * sinU1 * sinU2) / cosSqAlpha
143
+ if (isNaN(cos2SigmaM)) cos2SigmaM = 0
144
+ var C = (f / 16) * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha))
145
+ lambdaP = lambda
146
+ lambda = L + (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)))
147
+ } while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0)
148
+ if (iterLimit == 0) {
149
+ return NaN
150
+ }
151
+ var uSq = (cosSqAlpha * (a * a - b * b)) / (b * b)
152
+ var A = 1 + (uSq / 16384) * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)))
153
+ var B = (uSq / 1024) * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)))
154
+ var deltaSigma =
155
+ B *
156
+ sinSigma *
157
+ (cos2SigmaM +
158
+ (B / 4) *
159
+ (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) -
160
+ (B / 6) * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)))
161
+ var s = b * A * (sigma - deltaSigma)
162
+ var fwdAz = Math.atan2(cosU2 * sinLambda, cosU1 * sinU2 - sinU1 * cosU2 * cosLambda)
163
+ var revAz = Math.atan2(cosU1 * sinLambda, -sinU1 * cosU2 + cosU1 * sinU2 * cosLambda)
164
+ return s
165
+ },
166
+ /**
167
+ * P1绕一个坐标点P2旋转θ角度后,新的坐标的计算公式
168
+ *
169
+ * @param {*} p1
170
+ * @param {*} p2
171
+ * @param {*} θ 旋转角度,正:表示顺时针,负:表示逆时针
172
+ */
173
+ rotatePoint(p1, p2, θ) {
174
+ const x = (p1.x - p2.x) * Math.cos((Math.PI / 180) * -θ) - (p1.y - p2.y) * Math.sin((Math.PI / 180) * -θ) + p2.x
175
+ const y = (p1.x - p2.x) * Math.sin((Math.PI / 180) * -θ) + (p1.y - p2.y) * Math.cos((Math.PI / 180) * -θ) + p2.y
176
+ return { x, y }
177
+ },
178
+ calcBearAndDis(p1, p2) {
179
+ const { x: x1, y: y1 } = p1
180
+ const { x: x2, y: y2 } = p2
181
+ const dx = x2 - x1
182
+ const dy = y2 - y1
183
+ const distance = Math.sqrt(dx * dx + dy * dy)
184
+ const angleInRadians = Math.atan2(dy, dx)
185
+ const angle = (angleInRadians * (180 / Math.PI) + 360 + 90) % 360
186
+ return { angle, distance }
187
+ },
188
+ /**
189
+ * 计算latlng2相对于latlng1的方位角和距离
190
+ *
191
+ * @param {*} latlng1
192
+ * @param {*} latlng2
193
+ * @returns {*} {boolean}
194
+ */
195
+ calcBearAndDisByPoints(latlng1, latlng2) {
196
+ var f1 = parseFloat(latlng1.lat),
197
+ l1 = parseFloat(latlng1.lng),
198
+ f2 = parseFloat(latlng2.lat),
199
+ l2 = parseFloat(latlng2.lng)
200
+ var y = Math.sin((l2 - l1) * this.toRadian) * Math.cos(f2 * this.toRadian)
201
+ var x =
202
+ Math.cos(f1 * this.toRadian) * Math.sin(f2 * this.toRadian) -
203
+ Math.sin(f1 * this.toRadian) * Math.cos(f2 * this.toRadian) * Math.cos((l2 - l1) * this.toRadian)
204
+ var angle = Math.atan2(y, x) * (180 / Math.PI)
205
+ var deltaF = (f2 - f1) * this.toRadian
206
+ var deltaL = (l2 - l1) * this.toRadian
207
+ var a =
208
+ Math.sin(deltaF / 2) * Math.sin(deltaF / 2) +
209
+ Math.cos(f1 * this.toRadian) * Math.cos(f2 * this.toRadian) * Math.sin(deltaL / 2) * Math.sin(deltaL / 2)
210
+ var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
211
+ var distance = this.R * c
212
+ return {
213
+ angle,
214
+ distance,
215
+ }
216
+ },
217
+
218
+ /**
219
+ * 根据方位角和距离生成新的坐标
220
+ *
221
+ * @param {*} latlng
222
+ * @param {*} angle
223
+ * @param {*} distance
224
+ * @returns {*}
225
+ */
226
+ calcPointByBearAndDis(latlng, angle, distance) {
227
+ const sLat = MathUtils.toRadians(parseFloat(latlng.lat))
228
+ const sLng = MathUtils.toRadians(parseFloat(latlng.lng))
229
+ angle = parseFloat(angle)
230
+ distance = parseFloat(distance)
231
+ const d = distance / this.R
232
+ angle = MathUtils.toRadians(angle)
233
+ const lat = Math.asin(Math.sin(sLat) * Math.cos(d) + Math.cos(sLat) * Math.sin(d) * Math.cos(angle))
234
+ const lon = sLng + Math.atan2(Math.sin(angle) * Math.sin(d) * Math.cos(sLat), Math.cos(d) - Math.sin(sLat) * Math.sin(lat))
235
+ return {
236
+ lat: MathUtils.toDegrees(lat),
237
+ lng: MathUtils.toDegrees(lon),
238
+ }
239
+ },
240
+ mercatorTolonlat(x, y) {
241
+ const lng = (x / 20037508.34) * 180
242
+ var mmy = (y / 20037508.34) * 180
243
+ const lat = (180 / Math.PI) * (2 * Math.atan(Math.exp((mmy * Math.PI) / 180)) - Math.PI / 2)
244
+ return { lng, lat }
245
+ },
246
+ lonlatToMercator(lng, lat) {
247
+ var earthRad = 6378137.0
248
+ const x = ((lng * Math.PI) / 180) * earthRad
249
+ var a = (lat * Math.PI) / 180
250
+ const y = (earthRad / 2) * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)))
251
+ return { x, y }
252
+ },
253
+ }
@@ -0,0 +1,49 @@
1
+ export default {
2
+ DEG2RAD: Math.PI / 180,
3
+ RAD2DEG: 180 / Math.PI,
4
+ randInt(low, high) {
5
+ return low + Math.floor(Math.random() * (high - low + 1))
6
+ },
7
+ randFloat(low, high) {
8
+ return low + Math.random() * (high - low)
9
+ },
10
+ /**
11
+ * 角度转弧度
12
+ *
13
+ * @param {*} degrees
14
+ * @returns {*}
15
+ */
16
+ degreesToRadians(degrees) {
17
+ return degrees * this.DEG2RAD
18
+ },
19
+ /**
20
+ * 角度转弧度
21
+ *
22
+ * @param {*} degrees
23
+ * @returns {*}
24
+ */
25
+ toRadians(degrees) {
26
+ return degrees * this.DEG2RAD
27
+ },
28
+ /**
29
+ * 弧度转角度
30
+ *
31
+ * @param {*} radians
32
+ * @returns {*}
33
+ */
34
+ radiansToDegrees(radians) {
35
+ return radians * this.RAD2DEG
36
+ },
37
+ /**
38
+ * 弧度转角度
39
+ *
40
+ * @param {*} radians
41
+ * @returns {*}
42
+ */
43
+ toDegrees(radians) {
44
+ return radians * this.RAD2DEG
45
+ },
46
+ formatFloat(value, n = 2) {
47
+ return Math.round(value * Math.pow(10, n)) / Math.pow(10, n)
48
+ },
49
+ }
@@ -0,0 +1,94 @@
1
+ export default {
2
+ /**
3
+ * @desc 在事件被触发n秒后再执行回调函数,如果在这n秒内又被触发,则重新计时
4
+ * @desc 防止onresize,scroll,mousemove ,mousehover等,会被频繁触发影响性能
5
+ * @param func 需要执行的函数
6
+ * @param wait 延迟执行时间(毫秒)
7
+ * @param immediate---true 表立即执行,false 表非立即执行
8
+ * eg: window.addEventListener("resize",debounce(handle,1000));
9
+ * @returns {*}
10
+ */
11
+ debounce(func, wait, immediate = true) {
12
+ let timeout, args, context, timestamp, result
13
+
14
+ const later = function () {
15
+ // 据上一次触发时间间隔
16
+ const last = +new Date() - timestamp
17
+
18
+ // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
19
+ if (last < wait && last > 0) {
20
+ timeout = setTimeout(later, wait - last)
21
+ } else {
22
+ timeout = null
23
+ // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
24
+ if (!immediate) {
25
+ result = func.apply(context, args)
26
+ if (!timeout) context = args = null
27
+ }
28
+ }
29
+ }
30
+
31
+ return function (...args) {
32
+ context = this
33
+ timestamp = +new Date()
34
+ const callNow = immediate && !timeout
35
+ // 如果延时不存在,重新设定延时
36
+ if (!timeout) timeout = setTimeout(later, wait)
37
+ if (callNow) {
38
+ result = func.apply(context, args)
39
+ context = args = null
40
+ }
41
+
42
+ return result
43
+ }
44
+ },
45
+ /**
46
+ * 规定一个单位时间,在这个单位时间内,只能有一次触发事件的回调函数执行,如果在同一个单位时间内某事件被触发多次,只有一次能生效
47
+ *
48
+ * @param {*} func
49
+ * @param {*} wait
50
+ * @param {*} type 1 表时间戳版,2 表定时器版
51
+ * @returns {*}
52
+ */
53
+ throttle(func, wait, type) {
54
+ if (type === 1) {
55
+ var previous = 0
56
+ } else if (type === 2) {
57
+ var timeout
58
+ }
59
+ return function () {
60
+ let context = this
61
+ let args = arguments
62
+ if (type === 1) {
63
+ let now = Date.now()
64
+ if (now - previous > wait) {
65
+ func.apply(context, args)
66
+ previous = now
67
+ }
68
+ } else if (type === 2) {
69
+ if (!timeout) {
70
+ timeout = setTimeout(() => {
71
+ timeout = null
72
+ func.apply(context, args)
73
+ }, wait)
74
+ }
75
+ }
76
+ }
77
+ },
78
+ /**
79
+ * 延时递归
80
+ *
81
+ * @param {Number} frequency 间隔时间ms
82
+ * @param {Number} duration 持续时间ms
83
+ */
84
+ recurve(fun, frequency = 500, duration = 5000) {
85
+ let timer = 0
86
+ setTimeout(function fn() {
87
+ timer++
88
+ if (timer < Math.floor(duration / frequency)) {
89
+ fun.call(this)
90
+ setTimeout(fn, frequency)
91
+ }
92
+ })
93
+ },
94
+ }