File

projects/angular-cesium/src/lib/angular-cesium/services/coordinate-converter/coordinate-converter.service.ts

Description

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 '

Index

Methods

Constructor

constructor(cesiumService?: CesiumService)
Parameters :
Name Type Optional
cesiumService CesiumService Yes

Methods

bearingTo
bearingTo(first: literal type, second: literal type)

initial bearing between two points

  • @return bearing in degrees
Parameters :
Name Type Optional Description
first literal type No
  • {latitude,longitude} in radians
second literal type No
  • {latitude,longitude} in radians
Returns : any
bearingToCartesian
bearingToCartesian(firstCartesian3: Cartesian3, secondCartesian3: Cartesian3)

initial bearing between two points

Parameters :
Name Type Optional
firstCartesian3 Cartesian3 No
secondCartesian3 Cartesian3 No
Returns : any

bearing in degrees

cartesian3ToCartographic
cartesian3ToCartographic(cartesian: Cartesian3, ellipsoid?: any)
Parameters :
Name Type Optional
cartesian Cartesian3 No
ellipsoid any Yes
Returns : any
Static cartesian3ToLatLon
cartesian3ToLatLon(cartesian3: Cartesian3, ellipsoid?: any)
Parameters :
Name Type Optional
cartesian3 Cartesian3 No
ellipsoid any Yes
Returns : literal type
degreesToCartographic
degreesToCartographic(longitude: number, latitude: number, height?: number)
Parameters :
Name Type Optional
longitude number No
latitude number No
height number Yes
Returns : any
degreesToUTM
degreesToUTM(longitude: number, latitude: number)
Parameters :
Name Type Optional
longitude number No
latitude number No
Returns : any
Private geodesyToCesiumObject
geodesyToCesiumObject(geodesyRadians: LatLon)
Parameters :
Name Type Optional
geodesyRadians LatLon No
Returns : { longitude: any; latitude: any; height: any; }
middlePointByScreen
middlePointByScreen(position0: Cartesian3, position1: Cartesian3)
Parameters :
Name Type Optional
position0 Cartesian3 No
position1 Cartesian3 No
Returns : Cartesian3
midPointToCartesian3
midPointToCartesian3(first: literal type, second: literal type)

middle point between two points

Parameters :
Name Type Optional Description
first literal type No

(latitude,longitude) in radians

second literal type No

(latitude,longitude) in radians

Returns : any
radiansToCartographic
radiansToCartographic(longitude: number, latitude: number, height?: number)
Parameters :
Name Type Optional
longitude number No
latitude number No
height number Yes
Returns : any
screenToCartesian3
screenToCartesian3(screenPos: literal type, addMapCanvasBoundsToPos?: boolean)
Parameters :
Name Type Optional
screenPos literal type No
addMapCanvasBoundsToPos boolean Yes
Returns : any
screenToCartographic
screenToCartographic(screenPos: literal type, ellipsoid?: any)
Parameters :
Name Type Optional
screenPos literal type No
ellipsoid any Yes
Returns : any
UTMToDegrees
UTMToDegrees(zone: number, hemisphereType: hemisphere, easting: number, northing: number)
Parameters :
Name Type Optional
zone number No
hemisphereType hemisphere No
easting number No
northing number No
Returns : { longitude: any; latitude: any; height: any; }
import { Injectable, Optional } from '@angular/core';
import { CesiumService } from '../cesium/cesium.service';
import * as geodesy from 'geodesy';
import { hemisphere, LatLon, LatLonEllipsoidal, Utm } from 'geodesy';
import { Cartesian3 } from '../../models/cartesian3';

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);
 *  }
 * }
 *
 */
@Injectable()
export class CoordinateConverter {
  constructor(@Optional() private cesiumService?: CesiumService) {
  }

  static cartesian3ToLatLon(cartesian3: Cartesian3, ellipsoid?: any): {lon: number, lat: number; height: number} {
    const cart = Cesium.Cartographic.fromCartesian(cartesian3, ellipsoid);
    return {
      lon: Cesium.Math.toDegrees(cart.longitude),
      lat: Cesium.Math.toDegrees(cart.latitude),
      height: cart.height
    };
  }

  screenToCartesian3(screenPos: { x: number, y: number }, addMapCanvasBoundsToPos?: boolean) {
    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: { x: number, y: number }, ellipsoid?: any) {
    return this.cartesian3ToCartographic(this.screenToCartesian3(screenPos), ellipsoid);
  }

  cartesian3ToCartographic(cartesian: Cartesian3, ellipsoid?: any) {
    return Cesium.Cartographic.fromCartesian(cartesian, ellipsoid);
  }

  degreesToCartographic(longitude: number, latitude: number, height?: number) {
    return Cesium.Cartographic.fromDegrees(longitude, latitude, height);
  }

  radiansToCartographic(longitude: number, latitude: number, height?: number) {
    return Cesium.Cartographic.fromRadians(longitude, latitude, height);
  }

  degreesToUTM(longitude: number, latitude: number) {
    return new LatLonEllipsoidal(latitude, longitude).toUtm();
  }

  UTMToDegrees(zone: number, hemisphereType: hemisphere, easting: number, northing: number) {
    return this.geodesyToCesiumObject(new Utm(zone, hemisphereType, easting, northing).toLatLonE());
  }

  private geodesyToCesiumObject(geodesyRadians: LatLon) {
    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: { latitude: number, longitude: number }, second: { latitude: number, longitude: number }) {
    const toDeg = (rad: number) => Cesium.Math.toDegrees(rad);
    const firstPoint = new LatLonVectors(toDeg(first.latitude), toDeg(first.longitude));
    const secondPoint = new LatLonVectors(toDeg(second.latitude), toDeg(second.longitude));
    const middlePoint: any = firstPoint.midpointTo(secondPoint);

    return Cesium.Cartesian3.fromDegrees(middlePoint.lon, middlePoint.lat);
  }

  middlePointByScreen(position0: Cartesian3, position1: Cartesian3): Cartesian3 {
    const scene = this.cesiumService.getScene();
    const screenPosition1 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, position0);
    const screenPosition2 = Cesium.SceneTransforms.wgs84ToWindowCoordinates(scene, position1);
    const middleScreenPoint =
      new Cesium.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: { latitude: number, longitude: number }, second: { latitude: number, longitude: number }) {
    const toDeg = (rad: number) => Cesium.Math.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: Cartesian3, secondCartesian3: Cartesian3) {
    const firstCart = Cesium.Cartographic.fromCartesian(firstCartesian3);
    const secondCart = Cesium.Cartographic.fromCartesian(secondCartesian3);

    return this.bearingTo(firstCart, secondCart);
  }
}

result-matching ""

    No results matching ""