Appearance
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
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| isNumber | (obj: any) | boolean | Check if object is a Number type |
| isString | (obj: any) | boolean | Check if object is a String type |
| isBoolean | (obj: any) | boolean | Check if object is a Boolean type |
| isObject | (obj: any) | boolean | Check if object is an Object type |
| isArray | (obj: any) | boolean | Check if object is an Array type |
| isSimpleType | (value: any) | boolean | Check if object is a simple type (String, Boolean, Number, Array) |
| isArrayOfType | (arr: any[], type: any) | boolean | Check if all elements in array are of specific type |
| hasMethod | (obj: any, methodName: string) | boolean | Check if object has a specific method |
String and Number Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| formatNum | (num: number, digits?: number) | number | Format number to specified decimal places |
| getDecimalPlaces | (number: number) | number | Get number of decimal places |
| trim | (str: string) | string | Remove leading and trailing whitespace |
| splitWords | (str: string) | string[] | Split string by spaces and return array |
| padZero | (number: number) | string | Pad number with leading zero if less than 10 |
Object and Array Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| clone | (obj: any, removeKeys?: string[], level?: number) | any | Deep clone object with optional key removal |
| merge | (dest: object, ...sources: any[]) | object | Merge objects deeply |
| removeArrayItem | (arr: any[], val: any) | boolean | Remove item from array |
| removeArrayItemReturnArray | (arr: any[], val: any) | any[] | Remove item from array and return array |
| getArrayRandomOne | (arr: any[]) | any | Get random element from array |
| sortBy | (property: string, isOrder?: boolean) | function | Create sort function for array |
File and URL Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| getFiletype | (fname: string) | string | Get file type from filename (image, txt, excel, word, pdf, etc.) |
| getUrlResource | (resource: any, templateValues?: any) | Cesium.Resource | Create Cesium Resource with proxy and headers |
| serialize | (obj: any) | string | Serialize object to query string |
Cesium Integration Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| formatPosition | (position: Cesium.Cartesian3) | object | Format Cesium position to readable format |
| getCesiumValue | (obj: any, ClasName?: any, time?: any) | any | Get final value from Cesium object |
| getCesiumColor | (color: string, defval?: Cesium.Color) | Cesium.Color | Convert string to Cesium Color |
| getColorByStyle | (style: object, defval?: Cesium.Color) | Cesium.Color | Get Cesium color from style configuration |
Template and HTML Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| template | (str: string, data: object, toEmpty?: boolean) | string | Template string replacement with data |
| getTemplateHtml | (options: object) | string | Generate HTML template for popup/tooltip |
| getAttrVal | (attr: any, options?: object) | object | Extract simple key-value pairs from Cesium attributes |
GeoJSON Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| 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
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| heightToZoom | (altitude: number) | number | Convert altitude to map zoom level |
| getHeightByLevel | (level: number) | number | Convert zoom level to altitude |
| pointsSimplify | (points: any[], options?: object) | LatLngPoint[] | Simplify point array using Turf.js |
UI and Visual Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| getCircleImage | (count: number, options?: object) | string | Generate circle image for clustering |
| getSymbolStyle | (symbol: object, attr: object, mergeStyle?: object) | object | Get style from symbol configuration |
| getCustomShader | (type: number, options?: object) | Cesium.CustomShader | Get custom shader by type |
Image Processing Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| createImageFromBlob | (blob: Blob) | Promise of Image | Convert Blob to Image |
| createImageBitmapFromBlob | (blob: ImageBitmapSource) | Promise of HTMLCanvasElement | Convert Blob to ImageBitmap |
Performance Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| throttle | (func: Function, wait: number) | Function | Throttle function execution |
| debounce | (func: Function, wait: number) | Function | Debounce function execution |
Utility Functions
| Function | Parameters | Return Value | Description |
|---|---|---|---|
| uuid | (prefix?: string) | string | Generate unique identifier |
| getLangText | (key: string, langType?: LangType) | string | Get localized text |
| handleEval | (fn: string) | any | Safe eval alternative |
| handAbnormalData | (scene: any, samples: any[], minDeviation?: number, maxDeviation?: number) | any[] | Handle abnormal elevation data |
| isPCBroswer | () | boolean | Check if running on PC browser |
| isPCBroswer1 | () | boolean | Check 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])); // trueString 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
- All Util functions are static and can be called directly without instantiation
- Many functions are designed to work seamlessly with Cesium objects
- Type checking functions provide robust validation for different data types
- Template functions support complex HTML generation for popups and tooltips
- GeoJSON functions handle coordinate system transformations automatically
- Performance functions help optimize frequently called operations
- File type detection supports common image, document, and archive formats