Skip to content

Commit

Permalink
feat(utils): updated utils
Browse files Browse the repository at this point in the history
  • Loading branch information
zouyaoji committed Mar 11, 2022
1 parent 8b652bf commit 91abff6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
35 changes: 32 additions & 3 deletions packages/utils/cesium-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ import type {
CesiumMaterialProperty,
CesiumMaterial,
VcBoundingRectangle,
CesiumBoundingRectangle
CesiumBoundingRectangle,
VcHeadingPitchRoll,
VcHeadingPitchRange
} from './types'
import { hasOwn, isFunction, isArray, isString, isPlainObject, isEmptyObj, getObjClassName, isUndefined } from './util'

Expand Down Expand Up @@ -949,14 +951,41 @@ export function makeJulianDate(val: string | Date | Cesium.JulianDate): Cesium.J
if (val instanceof JulianDate) {
return val
} else if (isString(val)) {
return Cesium.JulianDate.fromIso8601(val)
return Cesium.JulianDate.fromDate(new Date(val))
} else if (val instanceof Date) {
return Cesium.JulianDate.fromDate(val)
}

return Cesium.JulianDate.now()
}

export function makeHeadingPitchRoll(val: VcHeadingPitchRoll): Cesium.HeadingPitchRoll {
const { HeadingPitchRoll, Math: CesiumMath } = Cesium
if (val instanceof Cesium.HeadingPitchRoll) {
return val
} else if (Array.isArray(val)) {
// 认为是经纬度数组
return new HeadingPitchRoll(CesiumMath.toRadians(val[0]) || 0, CesiumMath.toRadians(val[1]) || 0, CesiumMath.toRadians(val[2]) || 0)
} else if (isPlainObject(val)) {
return new HeadingPitchRoll(val.heading || 0, val.pitch || 0, val.roll || 0)
}

return new HeadingPitchRoll()
}

export function makeHeadingPitchRang(val: VcHeadingPitchRange): Cesium.HeadingPitchRange {
const { HeadingPitchRange, Math: CesiumMath } = Cesium
if (val instanceof Cesium.HeadingPitchRange) {
return val
} else if (Array.isArray(val)) {
return new HeadingPitchRange(CesiumMath.toRadians(val[0]) || 0, CesiumMath.toRadians(val[1]) || 0, val[2] || 0)
} else if (isPlainObject(val)) {
return new HeadingPitchRange(val.heading || 0, val.pitch || 0, val.range || 0)
}

return new HeadingPitchRange()
}

export function getPolylineSegmentHeading(start: Cesium.Cartesian3, end: Cesium.Cartesian3) {
const { Cartesian3, Matrix4, Transforms, Math: CesiumMath } = Cesium
const cartesian3Scratch = new Cartesian3()
Expand Down Expand Up @@ -997,8 +1026,8 @@ export function getFirstIntersection(
return undefined
}

// 粗略计算
export function heightToLevel(altitude: number) {
// 粗略计算
const A = 40487.57
const B = 0.00007096758
const C = 91610.74
Expand Down
11 changes: 9 additions & 2 deletions packages/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ export function getDefaultOptionByProps<T>(props, ignores: Array<string> = []) {
return defaultOptions
}

const addCustomProperty = (obj, options) => {
const addCustomProperty = (obj, options, ignores: Array<string> = []) => {
for (const prop in options) {
if (!obj[prop]) {
if (!obj[prop] && ignores.indexOf(prop) === -1) {
obj[prop] = options[prop]
}
}
Expand All @@ -155,6 +155,13 @@ export const merge = <T extends Record<string, any>>(a: T, b: T) => {
return obj
}

export function isArrayLike(obj) {
if (Array.isArray(obj)) return true
if (typeof obj !== 'object' || !obj) return false
const length = obj.length
return typeof length === 'number' && length >= 0
}

// reexport from lodash & vue shared
export {
hasOwn,
Expand Down

0 comments on commit 91abff6

Please sign in to comment.