@eliorar/angular-cesium
Version:
Angular library for working with Cesium.
1,209 lines (1,190 loc) • 670 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, Optional, EventEmitter, DOCUMENT, Inject, Input, Component, ChangeDetectionStrategy, ViewContainerRef, ViewChild, Directive, Output, forwardRef, Pipe, NgModule, InjectionToken, TemplateRef, ContentChild, ContentChildren, HostListener } from '@angular/core';
import * as i5 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as Cesium$1 from 'cesium';
import { Viewer, Math as Math$1, Cartesian3, JulianDate, Cartographic, SceneTransforms, Cartesian2, PrimitiveCollection, Ellipsoid, PolylineCollection, Material, CustomDataSource, CallbackProperty, CzmlDataSource, Color, Entity, SceneMode as SceneMode$1, GeometryInstance, Primitive, CircleGeometry, PolylineGeometry, PolygonGeometry, EllipseGeometry, LabelCollection, BillboardCollection, PointPrimitiveCollection, buildModuleUrl, Cesium3DTileStyle, DistanceDisplayCondition, AssociativeArray, defined, ShadowMode, ColorMaterialProperty, ColorGeometryInstanceAttribute, ShowGeometryInstanceAttribute, DistanceDisplayConditionGeometryInstanceAttribute, VerticalOrigin, LabelStyle, HorizontalOrigin, HeightReference, PolygonHierarchy, ClassificationType, sampleTerrain, Rectangle } from 'cesium';
import * as geodesy from 'geodesy';
import { LatLonEllipsoidal, Utm } from 'geodesy';
import { tap, filter, publish, mergeMap, delay, takeUntil, map, switchMap, merge as merge$1 } from 'rxjs/operators';
import { Observable, merge, of, Subject, from, BehaviorSubject, fromEvent } from 'rxjs';
import { EllipsePrimitive } from 'primitive-primitives';
import * as i1 from '@auscope/angular2parse';
import { PIPES_CONFIG, Angular2ParseModule } from '@auscope/angular2parse';
import { JsonStringMapper } from 'json-string-mapper';
import { get } from 'lodash';
class ViewerFactory {
/**
* Creates a viewer with default or custom options
* @param mapContainer - container to initialize the viewer on
* @param options - Options to create the viewer with - Optional
*
* @returns new viewer
*/
createViewer(mapContainer, options) {
let viewer = null;
if (options) {
viewer = new Viewer(mapContainer, {
contextOptions: {
webgl: { preserveDrawingBuffer: true }
},
...options
});
}
else {
viewer = new Viewer(mapContainer, {
contextOptions: {
webgl: { preserveDrawingBuffer: true }
},
});
}
return viewer;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ViewerFactory, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ViewerFactory }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ViewerFactory, decorators: [{
type: Injectable
}] });
/**
* Service for setting cesium viewer map options.
* defaulty angular-cesium doesnt provide this service and viewer is created with default options.
* In order set specific options you must set this service as provider in your component and
* set the wanted options.
* ```typescript
* constructor(viewerConf :ViewerConfiguration ) {
* viewerConf.viewerOptions = { timeline: false };
* }
* ```
* notice this configuration will be for all <ac-maps> in your component.
*/
class ViewerConfiguration {
constructor() {
this.nextViewerOptionsIndex = 0;
this.nextViewerModifierIndex = 0;
}
get viewerOptions() {
return this._viewerOptions;
}
getNextViewerOptions() {
if (this._viewerOptions instanceof Array) {
return this._viewerOptions[this.nextViewerOptionsIndex++];
}
else {
return this._viewerOptions;
}
}
/**
* Can be used to set initial map viewer options.
* If there is more than one map you can give the function an array of options.
* The map initialized first will be set with the first option object in the options array and so on.
*/
set viewerOptions(value) {
this._viewerOptions = value;
}
get viewerModifier() {
return this._viewerModifier;
}
getNextViewerModifier() {
if (this._viewerModifier instanceof Array) {
return this._viewerModifier[this.nextViewerModifierIndex++];
}
else {
return this._viewerModifier;
}
}
/**
* Can be used to set map viewer options after the map has been initialized.
* If there is more than one map you can give the function an array of functions.
* The map initialized first will be set with the first option object in the options array and so on.
*/
set viewerModifier(value) {
this._viewerModifier = value;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ViewerConfiguration, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ViewerConfiguration }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ViewerConfiguration, decorators: [{
type: Injectable
}] });
/**
* Service that initialize cesium viewer and expose cesium viewer and scene.
*/
class CesiumService {
constructor(ngZone, viewerFactory, viewerConfiguration) {
this.ngZone = ngZone;
this.viewerFactory = viewerFactory;
this.viewerConfiguration = viewerConfiguration;
}
init(mapContainer) {
this.mapContainer = mapContainer;
this.ngZone.runOutsideAngular(() => {
const options = this.viewerConfiguration ? this.viewerConfiguration.getNextViewerOptions() : undefined;
this.cesiumViewer = this.viewerFactory.createViewer(mapContainer, options);
const viewerModifier = this.viewerConfiguration && this.viewerConfiguration.getNextViewerModifier();
if (typeof viewerModifier === 'function') {
viewerModifier(this.cesiumViewer);
}
});
}
/**
* For more information see https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewe
* @returns cesiumViewer
*/
getViewer() {
return this.cesiumViewer;
}
/**
* For more information see https://cesiumjs.org/Cesium/Build/Documentation/Scene.html?classFilter=scene
* @returns cesium scene
*/
getScene() {
return this.cesiumViewer.scene;
}
/**
* For more information see https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API
* @returns cesium canvas
*/
getCanvas() {
return this.cesiumViewer.canvas;
}
getMapContainer() {
return this.mapContainer;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CesiumService, deps: [{ token: i0.NgZone }, { token: ViewerFactory }, { token: ViewerConfiguration, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CesiumService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CesiumService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: i0.NgZone }, { type: ViewerFactory }, { type: ViewerConfiguration, decorators: [{
type: Optional
}] }] });
/**
* Cesium scene modes
*/
var SceneMode;
(function (SceneMode) {
SceneMode[SceneMode["SCENE3D"] = 0] = "SCENE3D";
SceneMode[SceneMode["COLUMBUS_VIEW"] = 1] = "COLUMBUS_VIEW";
SceneMode[SceneMode["SCENE2D"] = 2] = "SCENE2D";
SceneMode[SceneMode["PERFORMANCE_SCENE2D"] = 3] = "PERFORMANCE_SCENE2D";
})(SceneMode || (SceneMode = {}));
/**
* The service exposes the scene's camera and screenSpaceCameraController
* SceneMode.PERFORMANCE_SCENE2D - is a 3D scene mode that acts like Cesium 2D mode,
* but is more efficient performance wise.
*/
class CameraService {
static { this.PERFORMANCE_2D_ALTITUDE = 25000000; }
constructor() {
this.isSceneModePerformance2D = false;
}
init(cesiumService) {
this.viewer = cesiumService.getViewer();
this.scene = cesiumService.getScene();
this.screenSpaceCameraController = this.scene.screenSpaceCameraController;
this.camera = this.scene.camera;
this.lastRotate = this.screenSpaceCameraController.enableRotate;
this.lastTilt = this.screenSpaceCameraController.enableTilt;
this.lastLook = this.screenSpaceCameraController.enableLook;
}
_listenToSceneModeMorph(callback) {
this.morphListenerCancelFn = this.scene.morphStart.addEventListener(callback);
}
_revertCameraProperties() {
this.isSceneModePerformance2D = false;
this.enableTilt(this.lastTilt);
this.enableRotate(this.lastRotate);
this.enableLook(this.lastLook);
}
/**
* Gets the scene's camera
*/
getCamera() {
return this.camera;
}
/**
* Gets the scene's screenSpaceCameraController
*/
getScreenSpaceCameraController() {
return this.screenSpaceCameraController;
}
/**
* Gets the minimum zoom value in meters
*/
getMinimumZoom() {
return this.screenSpaceCameraController.minimumZoomDistance;
}
/**
* Sets the minimum zoom value in meters
* @param zoom amount
*/
setMinimumZoom(amount) {
this.screenSpaceCameraController.minimumZoomDistance = amount;
}
/**
* Gets the maximum zoom value in meters
*/
getMaximumZoom() {
return this.screenSpaceCameraController.maximumZoomDistance;
}
/**
* Sets the maximum zoom value in meters
* @param zoom amount
*/
setMaximumZoom(amount) {
this.screenSpaceCameraController.maximumZoomDistance = amount;
}
/**
* Sets if the camera is able to tilt
*/
enableTilt(tilt) {
this.screenSpaceCameraController.enableTilt = tilt;
}
/**
* Sets if the camera is able to rotate
*/
enableRotate(rotate) {
this.screenSpaceCameraController.enableRotate = rotate;
}
/**
* Sets if the camera is able to free-look
*/
enableLook(lock) {
this.screenSpaceCameraController.enableLook = lock;
}
/**
* Sets if the camera is able to translate
*/
enableTranslate(translate) {
this.screenSpaceCameraController.enableTranslate = translate;
}
/**
* Sets if the camera is able to zoom
*/
enableZoom(zoom) {
this.screenSpaceCameraController.enableZoom = zoom;
}
/**
* Sets if the camera receives inputs
*/
enableInputs(inputs) {
this.screenSpaceCameraController.enableInputs = inputs;
}
/**
* Sets the map's SceneMode
* @param sceneMode - The SceneMode to morph the scene into.
* @param duration - The duration of scene morph animations, in seconds
*/
setSceneMode(sceneMode, duration) {
switch (sceneMode) {
case SceneMode.SCENE3D: {
if (this.isSceneModePerformance2D) {
this._revertCameraProperties();
}
this.scene.morphTo3D(duration);
break;
}
case SceneMode.COLUMBUS_VIEW: {
if (this.isSceneModePerformance2D) {
this._revertCameraProperties();
}
this.scene.morphToColumbusView(duration);
break;
}
case SceneMode.SCENE2D: {
if (this.isSceneModePerformance2D) {
this._revertCameraProperties();
}
this.scene.morphTo2D(duration);
break;
}
case SceneMode.PERFORMANCE_SCENE2D: {
this.isSceneModePerformance2D = true;
this.lastLook = this.screenSpaceCameraController.enableLook;
this.lastTilt = this.screenSpaceCameraController.enableTilt;
this.lastRotate = this.screenSpaceCameraController.enableRotate;
this.screenSpaceCameraController.enableTilt = false;
this.screenSpaceCameraController.enableRotate = false;
this.screenSpaceCameraController.enableLook = false;
if (this.morphListenerCancelFn) {
this.morphListenerCancelFn();
}
this.scene.morphToColumbusView(duration);
const morphCompleteEventListener = this.scene.morphComplete.addEventListener(() => {
this.camera.setView({
destination: Cartesian3.fromDegrees(0.0, 0.0, Math.min(CameraService.PERFORMANCE_2D_ALTITUDE, this.getMaximumZoom())),
orientation: {
pitch: Math$1.toRadians(-90)
}
});
morphCompleteEventListener();
this._listenToSceneModeMorph(this._revertCameraProperties.bind(this));
});
break;
}
}
}
/**
* Flies the camera to a destination
* API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=cam#flyTo
*/
cameraFlyTo(options) {
return this.camera.flyTo(options);
}
/**
* Flies the camera to a target
* API: https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewer#flyTo
* @returns Promise<boolean>
*/
flyTo(target, options) {
return this.viewer.flyTo(target, options);
}
/**
* Zooms amount along the camera's view vector.
* API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html#zoomIn
*/
zoomIn(amount) {
return this.camera.zoomIn(amount || this.camera.defaultZoomAmount);
}
/**
* Zooms amount along the opposite direction of the camera's view vector.
* API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html#zoomOut
*/
zoomOut(amount) {
return this.camera.zoomOut(amount || this.camera.defaultZoomAmount);
}
/**
* Zoom the camera to a target
* API: https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewer#zoomTo
* @returns Promise<boolean>
*/
zoomTo(target, offset) {
return this.viewer.zoomTo(target, offset);
}
/**
* Flies the camera to a destination
* API: https://cesiumjs.org/Cesium/Build/Documentation/Camera.html?classFilter=camera#setView
* @param options viewer options
*/
setView(options) {
this.camera.setView(options);
}
/**
* Set camera's rotation
*/
setRotation(degreesInRadians) {
this.setView({ orientation: { heading: degreesInRadians } });
}
/**
* Locks or unlocks camera rotation
*/
lockRotation(lock) {
this.scene.screenSpaceCameraController.enableRotate = !lock;
}
/**
* Make the camera track a specific entity
* API: https://cesiumjs.org/Cesium/Build/Documentation/Viewer.html?classFilter=viewer#trackedEntity
* @param cesiumEntity - cesium entity( billboard, polygon...) to track
* @param options - track entity options
*/
trackEntity(cesiumEntity, options) {
const flyTo = (options && options.flyTo) || false;
this.viewer.trackedEntity = undefined;
return new Promise(resolve => {
if (flyTo) {
const flyToDuration = (options && options.flyToDuration) || 1;
const altitude = (options && options.altitude) || 10000;
// Calc entity flyTo position and wanted altitude
const entPosCar3 = cesiumEntity.position.getValue(JulianDate.now());
const entPosCart = Cartographic.fromCartesian(entPosCar3);
const zoomAmount = altitude - entPosCart.height;
entPosCart.height = altitude;
const flyToPosition = Cartesian3.fromRadians(entPosCart.longitude, entPosCart.latitude, entPosCart.height);
this.cameraFlyTo({
duration: flyToDuration,
destination: flyToPosition,
complete: () => {
this.viewer.trackedEntity = cesiumEntity;
setTimeout(() => {
if (zoomAmount > 0) {
this.camera.zoomOut(zoomAmount);
}
else {
this.camera.zoomIn(zoomAmount);
}
}, 0);
resolve();
}
});
}
else {
this.viewer.trackedEntity = cesiumEntity;
resolve();
}
});
}
untrackEntity() {
this.trackEntity();
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CameraService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CameraService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CameraService, decorators: [{
type: Injectable
}], ctorParameters: () => [] });
/**
* Event options for registration on map-event-manager.
*/
var CesiumEvent;
(function (CesiumEvent) {
CesiumEvent[CesiumEvent["MOUSE_MOVE"] = 15] = "MOUSE_MOVE";
CesiumEvent[CesiumEvent["LEFT_CLICK"] = 2] = "LEFT_CLICK";
CesiumEvent[CesiumEvent["LEFT_DOUBLE_CLICK"] = 3] = "LEFT_DOUBLE_CLICK";
CesiumEvent[CesiumEvent["LEFT_DOWN"] = 0] = "LEFT_DOWN";
CesiumEvent[CesiumEvent["LEFT_UP"] = 1] = "LEFT_UP";
CesiumEvent[CesiumEvent["MIDDLE_CLICK"] = 12] = "MIDDLE_CLICK";
// MIDDLE_DOUBLE_CLICK = ScreenSpaceEventType.MIDDLE_DOUBLE_CLICK,
CesiumEvent[CesiumEvent["MIDDLE_DOWN"] = 10] = "MIDDLE_DOWN";
CesiumEvent[CesiumEvent["MIDDLE_UP"] = 11] = "MIDDLE_UP";
CesiumEvent[CesiumEvent["PINCH_START"] = 17] = "PINCH_START";
CesiumEvent[CesiumEvent["PINCH_END"] = 18] = "PINCH_END";
CesiumEvent[CesiumEvent["PINCH_MOVE"] = 19] = "PINCH_MOVE";
CesiumEvent[CesiumEvent["RIGHT_CLICK"] = 7] = "RIGHT_CLICK";
// RIGHT_DOUBLE_CLICK = ScreenSpaceEventType.RIGHT_DOUBLE_CLICK,
CesiumEvent[CesiumEvent["RIGHT_DOWN"] = 5] = "RIGHT_DOWN";
CesiumEvent[CesiumEvent["RIGHT_UP"] = 6] = "RIGHT_UP";
CesiumEvent[CesiumEvent["WHEEL"] = 16] = "WHEEL";
CesiumEvent[CesiumEvent["LONG_LEFT_PRESS"] = 110] = "LONG_LEFT_PRESS";
CesiumEvent[CesiumEvent["LONG_RIGHT_PRESS"] = 111] = "LONG_RIGHT_PRESS";
CesiumEvent[CesiumEvent["LONG_MIDDLE_PRESS"] = 112] = "LONG_MIDDLE_PRESS";
CesiumEvent[CesiumEvent["LEFT_CLICK_DRAG"] = 113] = "LEFT_CLICK_DRAG";
CesiumEvent[CesiumEvent["RIGHT_CLICK_DRAG"] = 114] = "RIGHT_CLICK_DRAG";
CesiumEvent[CesiumEvent["MIDDLE_CLICK_DRAG"] = 115] = "MIDDLE_CLICK_DRAG";
})(CesiumEvent || (CesiumEvent = {}));
/**
* NO_PICK, - will not pick entities
* PICK_FIRST - first entity will be picked . use Cesium.scene.pick()
* PICK_ONE - in case a few entities are picked plonter is resolved . use Cesium.scene.drillPick()
* PICK_ALL - all entities are picked. use Cesium.scene.drillPick()
*/
var PickOptions;
(function (PickOptions) {
PickOptions[PickOptions["NO_PICK"] = 0] = "NO_PICK";
PickOptions[PickOptions["PICK_FIRST"] = 1] = "PICK_FIRST";
PickOptions[PickOptions["PICK_ONE"] = 2] = "PICK_ONE";
PickOptions[PickOptions["PICK_ALL"] = 3] = "PICK_ALL";
})(PickOptions || (PickOptions = {}));
/**
* The Service manages a singleton context menu over the map. It should be initialized with MapEventsManager.
* The Service allows opening and closing of the context menu and passing data to the context menu inner component.
*
* notice, `data` will be injected to your custom menu component into the `data` field in the component.
* __Usage :__
* ```
* ngOnInit() {
* this.clickEvent$ = this.eventsManager.register({ event: CesiumEvent.RIGHT_CLICK, pick: PickOptions.PICK_ONE });
* this.clickEvent$.subscribe(result => {
* if (result.entities) {
* const pickedMarker = result.entities[0];
* this.contextMenuService.open(MapContextmenuComponent, pickedMarker.position, {
* data: {
* myData: data,
* onDelete: () => this.delete(pickedMarker.id)
* }
* });
* }
* });
* }
*
*
* private delete(id) {
* this.mapMenu.close();
* this.detailedSiteService.removeMarker(id);
* }
* ```
*/
class ContextMenuService {
constructor() {
this._showContextMenu = false;
this._contextMenuChangeNotifier = new EventEmitter();
this._onOpen = new EventEmitter();
this._onClose = new EventEmitter();
this._defaultContextMenuOptions = {
closeOnLeftCLick: true,
closeOnLeftClickPriority: 10,
};
}
get contextMenuChangeNotifier() {
return this._contextMenuChangeNotifier;
}
get showContextMenu() {
return this._showContextMenu;
}
get options() {
return this._options;
}
get position() {
return this._position;
}
get content() {
return this._content;
}
get onOpen() {
return this._onOpen;
}
get onClose() {
return this._onClose;
}
init(mapEventsManager) {
this.mapEventsManager = mapEventsManager;
}
open(contentComponent, position, options = {}) {
this.close();
this._content = contentComponent;
this._position = position;
this._options = Object.assign({}, this._defaultContextMenuOptions, options);
this._showContextMenu = true;
if (this.mapEventsManager && this._options.closeOnLeftCLick) {
this.leftClickRegistration = this.mapEventsManager.register({
event: CesiumEvent.LEFT_CLICK,
pick: PickOptions.NO_PICK,
priority: this._options.closeOnLeftClickPriority,
});
this.leftClickSubscription = this.leftClickRegistration.subscribe(() => {
this.leftClickSubscription.unsubscribe();
this.close();
});
}
this._contextMenuChangeNotifier.emit();
this._onOpen.emit();
}
close() {
this._content = undefined;
this._position = undefined;
this._options = undefined;
this._showContextMenu = false;
if (this.leftClickRegistration) {
this.leftClickRegistration.dispose();
this.leftClickRegistration = undefined;
}
if (this.leftClickSubscription) {
this.leftClickSubscription.unsubscribe();
this.leftClickSubscription = undefined;
}
this._contextMenuChangeNotifier.emit();
this._onClose.emit();
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ContextMenuService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ContextMenuService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ContextMenuService, decorators: [{
type: Injectable
}] });
const LatLonVectors = geodesy['LatLonVectors']; // doesnt exists on typings
window['geodesy'] = geodesy;
/**
* Given different types of coordinates, we provide you a service converting those types to the most common other types.
* We are using the geodesy implementation of UTM conversion. see: https://github.com/chrisveness/geodesy.
*
* @example
* import { Component, OnInit } from '@angular/core';
* import { CoordinateConverter } from 'angular2-cesium';
*
* @Component({
* selector:'my-component',
* template:'<div>{{showCartographic}}</div>',
* providers:[CoordinateConverter]
* })
* export class MyComponent implements OnInit {
* showCartographic;
*
* constructor(private coordinateConverter:CoordinateConverter){
* }
*
* ngOnInit(){
* this.showCartographic = this.coordinateConverter.degreesToCartographic(5, 5, 5);
* }
* }
*
*/
class CoordinateConverter {
constructor(cesiumService) {
this.cesiumService = cesiumService;
}
static cartesian3ToLatLon(cartesian3, ellipsoid) {
const cart = Cartographic.fromCartesian(cartesian3, ellipsoid);
return {
lon: Math$1.toDegrees(cart.longitude),
lat: Math$1.toDegrees(cart.latitude),
height: cart.height
};
}
screenToCartesian3(screenPos, addMapCanvasBoundsToPos) {
if (!this.cesiumService) {
throw new Error('ANGULAR2-CESIUM - Cesium service should be provided in order' +
' to do screen position calculations');
}
else {
const screenPosition = { ...screenPos };
if (addMapCanvasBoundsToPos) {
const mapBounds = this.cesiumService.getViewer().canvas.getBoundingClientRect();
screenPosition.x += mapBounds.left;
screenPosition.y += mapBounds.top;
}
const camera = this.cesiumService.getViewer().camera;
return camera.pickEllipsoid(screenPosition);
}
}
screenToCartographic(screenPos, ellipsoid) {
return this.cartesian3ToCartographic(this.screenToCartesian3(screenPos), ellipsoid);
}
cartesian3ToCartographic(cartesian, ellipsoid) {
return Cartographic.fromCartesian(cartesian, ellipsoid);
}
degreesToCartographic(longitude, latitude, height) {
return Cartographic.fromDegrees(longitude, latitude, height);
}
radiansToCartographic(longitude, latitude, height) {
return Cartographic.fromRadians(longitude, latitude, height);
}
degreesToUTM(longitude, latitude) {
return new LatLonEllipsoidal(latitude, longitude).toUtm();
}
UTMToDegrees(zone, hemisphereType, easting, northing) {
return this.geodesyToCesiumObject(new Utm(zone, hemisphereType, easting, northing).toLatLonE());
}
geodesyToCesiumObject(geodesyRadians) {
return {
longitude: geodesyRadians.lon,
latitude: geodesyRadians.lat,
height: geodesyRadians['height'] ? geodesyRadians['height'] : 0
};
}
/**
* middle point between two points
* @param first (latitude,longitude) in radians
* @param second (latitude,longitude) in radians
*/
midPointToCartesian3(first, second) {
const toDeg = (rad) => Math$1.toDegrees(rad);
const firstPoint = new LatLonVectors(toDeg(first.latitude), toDeg(first.longitude));
const secondPoint = new LatLonVectors(toDeg(second.latitude), toDeg(second.longitude));
const middlePoint = firstPoint.midpointTo(secondPoint);
return Cartesian3.fromDegrees(middlePoint.lon, middlePoint.lat);
}
middlePointByScreen(position0, position1) {
const scene = this.cesiumService.getScene();
const screenPosition1 = SceneTransforms.worldToWindowCoordinates(scene, position0);
const screenPosition2 = SceneTransforms.worldToWindowCoordinates(scene, position1);
const middleScreenPoint = new Cartesian2((screenPosition2.x + screenPosition1.x) / 2.0, (screenPosition2.y + screenPosition1.y) / 2.0);
return scene.pickPosition(middleScreenPoint);
}
/**
* initial bearing between two points
*
* * @return bearing in degrees
* @param first - {latitude,longitude} in radians
* @param second - {latitude,longitude} in radians
*/
bearingTo(first, second) {
const toDeg = (rad) => Math$1.toDegrees(rad);
const firstPoint = new LatLonVectors(toDeg(first.latitude), toDeg(first.longitude));
const secondPoint = new LatLonVectors(toDeg(second.latitude), toDeg(second.longitude));
const bearing = firstPoint.bearingTo(secondPoint);
return bearing;
}
/**
* initial bearing between two points
*
* @return bearing in degrees
*/
bearingToCartesian(firstCartesian3, secondCartesian3) {
const firstCart = Cartographic.fromCartesian(firstCartesian3);
const secondCart = Cartographic.fromCartesian(secondCartesian3);
return this.bearingTo(firstCart, secondCart);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CoordinateConverter, deps: [{ token: CesiumService, optional: true }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CoordinateConverter }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CoordinateConverter, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: CesiumService, decorators: [{
type: Optional
}] }] });
/**
* Abstract drawer. All drawers extends this class.
*/
class BasicDrawerService {
constructor() {
}
setPropsAssigner(assigner) {
this._propsAssigner = assigner;
}
}
/**
* General primitives drawer responsible of drawing Cesium primitives.
* Drawers the handle Cesium primitives extend it.
*/
class PrimitivesDrawerService extends BasicDrawerService {
constructor(drawerType, cesiumService) {
super();
this.drawerType = drawerType;
this.cesiumService = cesiumService;
this._show = true;
}
init() {
this._cesiumCollection = new this.drawerType();
this._primitiveCollectionWrap = new PrimitiveCollection();
this._primitiveCollectionWrap.add(this._cesiumCollection);
this.cesiumService.getScene().primitives.add(this._primitiveCollectionWrap);
}
add(cesiumProps, ...args) {
return this._cesiumCollection.add(cesiumProps);
}
update(entity, cesiumProps, ...args) {
if (this._propsAssigner) {
this._propsAssigner(entity, cesiumProps);
}
else {
Object.assign(entity, cesiumProps);
}
}
remove(entity) {
this._cesiumCollection.remove(entity);
}
removeAll() {
this._cesiumCollection.removeAll();
}
setShow(showValue) {
this._show = showValue;
this._primitiveCollectionWrap.show = showValue;
}
getShow() {
return this._show;
}
}
class GeoUtilsService {
static pointByLocationDistanceAndAzimuth(currentLocation, meterDistance, radianAzimuth, deprecated) {
const distance = meterDistance / Ellipsoid.WGS84.maximumRadius;
const cartographicLocation = currentLocation instanceof Cartesian3 ? Cartographic.fromCartesian(currentLocation) : currentLocation;
const cartesianLocation = currentLocation instanceof Cartesian3
? currentLocation
: Cartesian3.fromRadians(currentLocation.longitude, currentLocation.latitude, currentLocation.height);
let resultPosition;
let resultDistance;
let counter = 0;
let distanceFactorRangeMax = 0.1;
let distanceFactorRangeMin = -0.1;
while (counter === 0 ||
(counter < 16 && Math.max(resultDistance, meterDistance) / Math.min(resultDistance, meterDistance) > 1.000001)) {
const factor = distanceFactorRangeMin + (distanceFactorRangeMax - distanceFactorRangeMin) / 2;
resultPosition = GeoUtilsService._pointByLocationDistanceAndAzimuth(cartographicLocation, distance * (1 + factor), radianAzimuth);
resultDistance = this.distance(cartesianLocation, resultPosition);
if (resultDistance > meterDistance) {
distanceFactorRangeMax = distanceFactorRangeMin + (distanceFactorRangeMax - distanceFactorRangeMin) / 2;
}
else {
distanceFactorRangeMin = distanceFactorRangeMin + (distanceFactorRangeMax - distanceFactorRangeMin) / 2;
}
counter++;
}
return resultPosition;
}
static _pointByLocationDistanceAndAzimuth(cartographicLocation, distance, radianAzimuth) {
const curLat = cartographicLocation.latitude;
const curLon = cartographicLocation.longitude;
const destinationLat = Math.asin(Math.sin(curLat) * Math.cos(distance) + Math.cos(curLat) * Math.sin(distance) * Math.cos(radianAzimuth));
let destinationLon = curLon +
Math.atan2(Math.sin(radianAzimuth) * Math.sin(distance) * Math.cos(curLat), Math.cos(distance) - Math.sin(curLat) * Math.sin(destinationLat));
destinationLon = ((destinationLon + 3 * Math.PI) % (2 * Math.PI)) - Math.PI;
return Cartesian3.fromRadians(destinationLon, destinationLat);
}
static distance(pos0, pos1) {
return Cartesian3.distance(pos0, pos1);
}
static getPositionsDelta(position0, position1) {
return {
x: position1.x - position0.x,
y: position1.y - position0.y,
z: position1.z - position0.z,
};
}
static addDeltaToPosition(position, delta, updateReference = false) {
if (updateReference) {
position.x += delta.x;
position.y += delta.y;
position.z += delta.z;
const cartographic = Cartographic.fromCartesian(position);
cartographic.height = 0;
const cartesian = Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height);
position.x = cartesian.x;
position.y = cartesian.y;
position.z = cartesian.z;
return position;
}
else {
const cartesian = new Cartesian3(position.x + delta.x, position.y + delta.y, position.z + delta.z);
const cartographic = Cartographic.fromCartesian(cartesian);
cartographic.height = 0;
return Cartesian3.fromRadians(cartographic.longitude, cartographic.latitude, cartographic.height);
}
}
static middleCartesian3Point(position0, position1) {
return new Cartesian3(position1.x - position0.x / 2, position1.y - position0.y / 2, position1.z - position0.z / 2);
}
constructor(cesiumService) {
this.cesiumService = cesiumService;
}
screenPositionToCartesian3(screenPos) {
const camera = this.cesiumService.getViewer().camera;
return camera.pickEllipsoid(screenPos);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: GeoUtilsService, deps: [{ token: CesiumService }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: GeoUtilsService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: GeoUtilsService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: CesiumService }] });
/**
+ This drawer is responsible for drawing an arc over the Cesium map.
+ This implementation uses simple PolylineGeometry and Primitive parameters.
+ This doesn't allow us to change the position, color, etc.. of the arc but setShow only.
*/
class ArcDrawerService extends PrimitivesDrawerService {
constructor(cesiumService) {
super(PolylineCollection, cesiumService);
}
_calculateArcPositions(cesiumProps) {
const quality = cesiumProps.quality || 18;
const delta = (cesiumProps.delta) / quality;
const pointsArray = [];
for (let i = 0; i < quality + 1; ++i) {
const point = GeoUtilsService.pointByLocationDistanceAndAzimuth(cesiumProps.center, cesiumProps.radius, cesiumProps.angle + delta * i, true);
pointsArray.push(point);
}
return pointsArray;
}
_calculateTriangle(cesiumProps) {
return [
cesiumProps.center,
GeoUtilsService.pointByLocationDistanceAndAzimuth(cesiumProps.center, cesiumProps.radius, cesiumProps.angle, true)
];
}
_calculateArc(cesiumProps) {
const arcPoints = this._calculateArcPositions(cesiumProps);
return cesiumProps.drawEdges ? arcPoints.concat(this._calculateTriangle(cesiumProps)) : arcPoints;
}
add(cesiumProps) {
cesiumProps.positions = this._calculateArc(cesiumProps);
if (cesiumProps.color) {
const material = Material.fromType('Color');
material.uniforms.color = cesiumProps.color;
cesiumProps.material = material;
}
return this._cesiumCollection.add(cesiumProps);
}
update(primitive, cesiumProps) {
if (!cesiumProps.constantColor && cesiumProps.color &&
!primitive.material.uniforms.color.equals(cesiumProps.color)) {
primitive.material.uniforms.color = cesiumProps.color;
}
primitive.width = cesiumProps.width !== undefined ? cesiumProps.width : primitive.width;
primitive.show = cesiumProps.show !== undefined ? cesiumProps.show : primitive.show;
primitive.distanceDisplayCondition = cesiumProps.distanceDisplayCondition !== undefined ?
cesiumProps.distanceDisplayCondition : primitive.distanceDisplayCondition;
primitive.positions = this._calculateArc(cesiumProps);
return primitive;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ArcDrawerService, deps: [{ token: CesiumService }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ArcDrawerService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: ArcDrawerService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: CesiumService }] });
class GraphicsType {
static { this.ellipse = Cesium$1.EllipseGraphics; }
static { this.ellipsoid = Cesium$1.EllipsoidGraphics; }
static { this.polygon = Cesium$1.PolygonGraphics; }
static { this.polyline = Cesium$1.PolylineGraphics; }
static { this.polylineVolume = Cesium$1.PolylineVolumeGraphics; }
static { this.box = Cesium$1.BoxGraphics; }
static { this.corridor = Cesium$1.CorridorGraphics; }
static { this.cylinder = Cesium$1.CylinderGraphics; }
static { this.label = Cesium$1.LabelGraphics; }
static { this.billboard = Cesium$1.BillboardGraphics; }
static { this.model = Cesium$1.ModelGraphics; }
static { this.path = Cesium$1.PathGraphics; }
static { this.point = Cesium$1.PointGraphics; }
static { this.rectangle = Cesium$1.RectangleGraphics; }
static { this.wall = Cesium$1.WallGraphics; }
}
class OptimizedEntityCollection {
constructor(entityCollection, collectionSize = -1, updateRate = -1) {
this.entityCollection = entityCollection;
this._isSuspended = false;
this._isHardSuspend = false;
this._updateRate = updateRate;
this._collectionSize = collectionSize;
}
setShow(show) {
this.entityCollection.show = show;
}
get isSuspended() {
return this._isSuspended;
}
get updateRate() {
return this._updateRate;
}
set updateRate(value) {
this._updateRate = value;
}
get collectionSize() {
return this._collectionSize;
}
set collectionSize(value) {
this._collectionSize = value;
}
collection() {
return this.entityCollection;
}
isFree() {
return this._collectionSize < 1 || this.entityCollection.values.length < this._collectionSize;
}
add(entity) {
this.suspend();
return this.entityCollection.add(entity);
}
remove(entity) {
this.suspend();
return this.entityCollection.remove(entity);
}
removeNoSuspend(entity) {
this.entityCollection.remove(entity);
}
removeAll() {
this.suspend();
this.entityCollection.removeAll();
}
onEventSuspension(callback, once = false) {
this._onEventSuspensionCallback = { callback, once };
return () => {
this._onEventSuspensionCallback = undefined;
};
}
onEventResume(callback, once = false) {
this._onEventResumeCallback = { callback, once };
if (!this._isSuspended) {
this.triggerEventResume();
}
return () => {
this._onEventResumeCallback = undefined;
};
}
triggerEventSuspension() {
if (this._onEventSuspensionCallback !== undefined) {
const callback = this._onEventSuspensionCallback.callback;
if (this._onEventSuspensionCallback.once) {
this._onEventSuspensionCallback = undefined;
}
callback();
}
}
triggerEventResume() {
if (this._onEventResumeCallback !== undefined) {
const callback = this._onEventResumeCallback.callback;
if (this._onEventResumeCallback.once) {
this._onEventResumeCallback = undefined;
}
callback();
}
}
suspend() {
if (this._updateRate < 0) {
return;
}
if (this._isHardSuspend) {
return;
}
if (!this._isSuspended) {
this._isSuspended = true;
this.entityCollection.suspendEvents();
this.triggerEventSuspension();
this._suspensionTimeout = setTimeout(() => {
this.entityCollection.resumeEvents();
this.triggerEventResume();
this._isSuspended = false;
this._suspensionTimeout = undefined;
}, this._updateRate);
}
}
hardSuspend() {
this.entityCollection.suspendEvents();
this._isHardSuspend = true;
}
hardResume() {
this.entityCollection.resumeEvents();
this._isHardSuspend = false;
}
}
/**
* General primitives drawer responsible of drawing Cesium primitives.
* Drawers the handle Cesium primitives extend it.
*/
class EntitiesDrawerService extends BasicDrawerService {
constructor(cesiumService, graphicsType, defaultOptions = {
collectionMaxSize: -1,
collectionSuspensionTime: -1,
collectionsNumber: 1,
}) {
super();
this.cesiumService = cesiumService;
this.graphicsType = graphicsType;
this.defaultOptions = defaultOptions;
this.entityCollections = new Map();
this.graphicsTypeName = "Unknown";
// Fix bad enum compilation
for (const i in GraphicsType) {
if (GraphicsType[i] === this.graphicsType) {
this.graphicsTypeName = i;
}
}
}
getFreeEntitiesCollection() {
let freeEntityCollection = null;
this.entityCollections.forEach(entityCollection => {
if (entityCollection.isFree()) {
freeEntityCollection = entityCollection;
}
});
return freeEntityCollection;
}
init(options) {
const finalOptions = options || this.defaultOptions;
const dataSources = [];
for (let i = 0; i < finalOptions.collectionsNumber; i++) {
const dataSource = new CustomDataSource(this.graphicsTypeName);
dataSources.push(dataSource);
this.cesiumService.getViewer().dataSources.add(dataSource);
this.entityCollections.set(dataSource.entities, new OptimizedEntityCollection(dataSource.entities, finalOptions.collectionMaxSize, finalOptions.collectionSuspensionTime));
}
return dataSources;
}
add(cesiumProps) {
const optimizedEntityCollection = this.getFreeEntitiesCollection();
if (optimizedEntityCollection === null) {
throw new Error('No more free entity collections');
}
const entityObject = {
position: cesiumProps.position !== undefined ? cesiumProps.position : undefined,
description: cesiumProps.description !== undefined ? cesiumProps.description : undefined,
orientation: cesiumProps.orientation !== undefined ? cesiumProps.orientation : undefined,
viewFrom: cesiumProps.viewFrom !== undefined ? cesiumProps.viewFrom : undefined,
[this.graphicsTypeName]: cesiumProps,
};
if (cesiumProps.name !== undefined) {
entityObject.name = cesiumProps.name;
}
if (cesiumProps.availability !== undefined) {
entityObject.availability = cesiumProps.availability;
}
return optimizedEntityCollection.add(entityObject);
}
update(entity, cesiumProps) {
this.suspendEntityCollection(entity);
if (entity.position instanceof CallbackProperty) {
if (entity.position._isConstant) {
entity.position = cesiumProps.position;
}
}
entity.position = cesiumProps.position !== undefined ? cesiumProps.position : undefined;
entity.name = cesiumProps.name !== undefined ? cesiumProps.name : entity.name;
entity.description = cesiumProps.description !== undefined ? cesiumProps.description : entity.description;
entity.orientation = cesiumProps.orientation !== undefined ? cesiumProps.orientation : entity.orientation;
entity.viewFrom = cesiumProps.viewFrom !== undefined ? cesiumProps.viewFrom : entity.viewFrom;
entity.availability = cesiumProps.availability !== undefined ? cesiumProps.availability : cesiumProps.availability;
if (this._propsAssigner) {
this._propsAssigner(entity[this.graphicsTypeName], cesiumProps);
}
else {
Object.assign(entity[this.graphicsTypeName], cesiumProps);
}
}
remove(entity) {
const optimizedEntityCollection = this.entityCollections.get(entity.entityCollection);
optimizedEntityCollection.remove(entity);
}
removeAll() {
this.entityCollections.forEach(entityCollection => {
entityCollection.removeAll();
});
}
setShow(showValue) {
this.entityCollections.forEach(entityCollection => {
entityCollection.setShow(showValue);
});
}
suspendEntityCollection(entity) {
const id = entity.entityCollection;
if (!this.entityCollections.has(id)) {
throw new Error('No EntityCollection for entity.entityCollection');
}
const entityCollection = this.entityCollections.get(id);
entityCollection.suspend();
}
}
/**
* This drawer is responsible for drawing billboards.
*/
class BillboardDrawerService extends EntitiesDrawerService {
constructor(cesiumService) {
super(cesiumService, GraphicsType.billboard);
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: BillboardDrawerService, deps: [{ token: CesiumService }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: BillboardDrawerService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: BillboardDrawerService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: CesiumService }] });
/**
* This drawer is responsible for drawing czml dataSources.
*/
class CzmlDrawerService extends BasicDrawerService {
constructor(cesiumService) {
super();
this.cesiumService = cesiumService;
}
init(options) {
const dataSources = [];
this.czmlStream = new CzmlDataSource('czml');
dataSources.push(this.czmlStream);
this.cesiumService.getViewer().dataSources.add(this.czmlStream);
return dataSources;
}
// returns the packet, provided by the stream
add(cesiumProps) {
this.czmlStream.process(cesiumProps.czmlPacket);
return cesiumProps;
}
update(entity, cesiumProps) {
this.czmlStream.process(cesiumProps.czmlPacket);
}
remove(entity) {
this.czmlStream.entities.removeById(entity.acEntity.id);
}
removeAll() {
this.czmlStream.entities.removeAll();
}
setShow(showValue) {
this.czmlStream.entities.show = showValue;
}
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CzmlDrawerService, deps: [{ token: CesiumService }], target: i0.ɵɵFactoryTarget.Injectable }); }
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CzmlDrawerService }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.4", ngImport: i0, type: CzmlDrawerService, decorators: [{
type: Injectable
}], ctorParameters: () => [{ type: CesiumService }] });
/**
* This drawer is responsible for drawing ellipses.
*/
class