Skip to content

Util (Utility Functions)

Common utility functions and helper methods for the ge3d library.

Overview

Util is a collection of static utility functions that provide common functionality for data processing, type checking, formatting, and other helper operations throughout the ge3d library.

Usage

ts
import { Util } from 'ge3d';

Type Checking Functions

FunctionParametersReturn ValueDescription
isNumber(obj: any)booleanCheck if object is a Number type
isString(obj: any)booleanCheck if object is a String type
isBoolean(obj: any)booleanCheck if object is a Boolean type
isObject(obj: any)booleanCheck if object is an Object type
isArray(obj: any)booleanCheck if object is an Array type
isSimpleType(value: any)booleanCheck if object is a simple type (String, Boolean, Number, Array)
isArrayOfType(arr: any[], type: any)booleanCheck if all elements in array are of specific type
hasMethod(obj: any, methodName: string)booleanCheck if object has a specific method

String and Number Functions

FunctionParametersReturn ValueDescription
formatNum(num: number, digits?: number)numberFormat number to specified decimal places
getDecimalPlaces(number: number)numberGet number of decimal places
trim(str: string)stringRemove leading and trailing whitespace
splitWords(str: string)string[]Split string by spaces and return array
padZero(number: number)stringPad number with leading zero if less than 10

Object and Array Functions

FunctionParametersReturn ValueDescription
clone(obj: any, removeKeys?: string[], level?: number)anyDeep clone object with optional key removal
merge(dest: object, ...sources: any[])objectMerge objects deeply
removeArrayItem(arr: any[], val: any)booleanRemove item from array
removeArrayItemReturnArray(arr: any[], val: any)any[]Remove item from array and return array
getArrayRandomOne(arr: any[])anyGet random element from array
sortBy(property: string, isOrder?: boolean)functionCreate sort function for array

File and URL Functions

FunctionParametersReturn ValueDescription
getFiletype(fname: string)stringGet file type from filename (image, txt, excel, word, pdf, etc.)
getUrlResource(resource: any, templateValues?: any)Cesium.ResourceCreate Cesium Resource with proxy and headers
serialize(obj: any)stringSerialize object to query string

Cesium Integration Functions

FunctionParametersReturn ValueDescription
formatPosition(position: Cesium.Cartesian3)objectFormat Cesium position to readable format
getCesiumValue(obj: any, ClasName?: any, time?: any)anyGet final value from Cesium object
getCesiumColor(color: string, defval?: Cesium.Color)Cesium.ColorConvert string to Cesium Color
getColorByStyle(style: object, defval?: Cesium.Color)Cesium.ColorGet Cesium color from style configuration

Template and HTML Functions

FunctionParametersReturn ValueDescription
template(str: string, data: object, toEmpty?: boolean)stringTemplate string replacement with data
getTemplateHtml(options: object)stringGenerate HTML template for popup/tooltip
getAttrVal(attr: any, options?: object)objectExtract simple key-value pairs from Cesium attributes

GeoJSON Functions

FunctionParametersReturn ValueDescription
getGeoJsonFeatures(geojson: object)object[]Extract features array from GeoJSON
geoJsonToGraphics(geojson: any, options?: object)object[]Convert GeoJSON to graphic parameters
featureToGraphic(feature: any, options?: object)object[]Convert single GeoJSON feature to graphic

Map and Coordinate Functions

FunctionParametersReturn ValueDescription
heightToZoom(altitude: number)numberConvert altitude to map zoom level
getHeightByLevel(level: number)numberConvert zoom level to altitude
pointsSimplify(points: any[], options?: object)LatLngPoint[]Simplify point array using Turf.js

UI and Visual Functions

FunctionParametersReturn ValueDescription
getCircleImage(count: number, options?: object)stringGenerate circle image for clustering
getSymbolStyle(symbol: object, attr: object, mergeStyle?: object)objectGet style from symbol configuration
getCustomShader(type: number, options?: object)Cesium.CustomShaderGet custom shader by type

Image Processing Functions

FunctionParametersReturn ValueDescription
createImageFromBlob(blob: Blob)Promise of ImageConvert Blob to Image
createImageBitmapFromBlob(blob: ImageBitmapSource)Promise of HTMLCanvasElementConvert Blob to ImageBitmap

Performance Functions

FunctionParametersReturn ValueDescription
throttle(func: Function, wait: number)FunctionThrottle function execution
debounce(func: Function, wait: number)FunctionDebounce function execution

Utility Functions

FunctionParametersReturn ValueDescription
uuid(prefix?: string)stringGenerate unique identifier
getLangText(key: string, langType?: LangType)stringGet localized text
handleEval(fn: string)anySafe eval alternative
handAbnormalData(scene: any, samples: any[], minDeviation?: number, maxDeviation?: number)any[]Handle abnormal elevation data
isPCBroswer()booleanCheck if running on PC browser
isPCBroswer1()booleanCheck if running on PC browser (cached)

Usage Examples

Type Checking

javascript
// Check data types
console.log(ge3d.Util.isNumber(123)); // true
console.log(ge3d.Util.isString("hello")); // true
console.log(ge3d.Util.isArray([1, 2, 3])); // true

String and Number Formatting

javascript
// Format numbers
const formatted = ge3d.Util.formatNum(3.14159, 2); // 3.14

// Template string replacement
const html = ge3d.Util.template(
  "Name: {name}, Date: {date}", 
  { name: "Ge3D", date: "2024-01-01" }
);

Object Manipulation

javascript
// Deep clone object
const cloned = ge3d.Util.clone(originalObject, ['internalId']);

// Merge objects
const merged = ge3d.Util.merge(target, source1, source2);

// Remove array item
const removed = ge3d.Util.removeArrayItem(array, item);

Cesium Integration

javascript
// Format Cesium position
const position = Cesium.Cartesian3.fromDegrees(116.3975, 39.9087, 1000);
const formatted = ge3d.Util.formatPosition(position);
// { x: 116.3975, y: 39.9087, z: 1000 }

// Get Cesium color
const color = ge3d.Util.getCesiumColor("#ff0000");

GeoJSON Processing

javascript
// Convert GeoJSON to graphics
const graphics = ge3d.Util.geoJsonToGraphics(geojsonData, {
  type: 'point',
  style: { color: '#ff0000' }
});

// Add graphics to layer
graphics.forEach(graphic => {
  layer.addGraphic(graphic);
});

Performance Optimization

javascript
// Throttle function
const throttledFunction = ge3d.Util.throttle(expensiveFunction, 100);

// Debounce function
const debouncedFunction = ge3d.Util.debounce(searchFunction, 300);

Notes

  1. All Util functions are static and can be called directly without instantiation
  2. Many functions are designed to work seamlessly with Cesium objects
  3. Type checking functions provide robust validation for different data types
  4. Template functions support complex HTML generation for popups and tooltips
  5. GeoJSON functions handle coordinate system transformations automatically
  6. Performance functions help optimize frequently called operations
  7. File type detection supports common image, document, and archive formats