All files / src/app/touchpad/geography geography-providers.ts

11.86% Statements 7/59
0% Branches 0/2
28.57% Functions 2/7
12.96% Lines 7/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                      1x   2x                   1x                                                                                                                       1x                                                   1x                                                     1x                                                                             1x  
import { HttpClient } from '@angular/common/http';
 
import { GeoAdminCoordinates, Waterbody, squaredEuclideanDistance, GeoAdminPoint } from './utils';
import { WGS84ToLV03Url, addressSearchUrl, waterbodySearchUrl, pointSearchUrl } from 'src/config/urls';
import { Address, Direction } from './utils';
import { loadModules } from 'esri-loader';
 
 
/**
 * The interface for geographic information providers in the application.
 * GeographyProviders provide information about addresses and points on a map.
 */
export interface GeographyProvider {
  getAddress(point: __esri.Point): Promise<string>;
  getPoint(address: string): Promise<__esri.Point | undefined>;
  computeDistance(pointA: __esri.Point, pointB: __esri.Point): Promise<number>;
  getDirection(pointA: __esri.Point, pointB: __esri.Point): Direction;
}
 
export class GeoAdminProvider implements GeographyProvider {
 
  constructor(private http: HttpClient) {}
 
  /**
   * Get the nearest address or water body to a point in Switzerland.
   *
   * @param point An Esri Point with WGS84 coordinates for which the nearest
   * address must be found.
   *
   * @returns The nearest address or water body.
   */
  async getAddress(point: __esri.Point): Promise<string> {
      // Transform the input point's WGS84 coordinates to LV03.
    let request = WGS84ToLV03Url + '?easting=' + point.x + '&northing=' + point.y;
    const lv03coordinates = await this.http.get<GeoAdminCoordinates>(request).toPromise();
 
    // Search for addresses near the input point.
    request = addressSearchUrl + lv03coordinates.coordinates[0] + ',' + lv03coordinates.coordinates[1] + '&tolerance=100';
    const {results: addresses} = await this.http.get<{results: Address[]}>(request).toPromise();
 
    // Search for water bodies near the point.
    request = waterbodySearchUrl + lv03coordinates.coordinates[0] + ',' + lv03coordinates.coordinates[1] + '&tolerance=100';
    const {results: waterbodies} = await this.http.get<{results: Waterbody[]}>(request).toPromise();
 
    // Select the address or water body that is nearest to the input point and return
    // its name.
    let closestResult = '';
    let closestDistance = 100000;
 
    addresses.forEach(
      (address: Address) => {
        const distance = squaredEuclideanDistance(
          {x: address.geometry.x, y: address.geometry.y},
          {x: lv03coordinates.coordinates[0], y: lv03coordinates.coordinates[1]}
        );
        if (distance < closestDistance) {
          closestDistance = distance;
          closestResult = address.attributes.deinr + ' ' + address.attributes.strname1;
        }
      }
    );
 
    waterbodies.forEach(
      (waterbody: Waterbody) => {
        waterbody.geometry.paths.forEach(
          (path: number[][]) => path.forEach(
              (p: number[]) => {
                const distance = squaredEuclideanDistance(
                  {x: p[0], y: p[1]},
                  {x: lv03coordinates.coordinates[0], y: lv03coordinates.coordinates[1]}
                );
                if (distance < closestDistance) {
                  closestDistance = distance;
                  closestResult = waterbody.attributes.name;
                }
            }
          )
        );
      }
    );
 
    return closestResult;
  }
 
  /**
   * Get the point (WGS84 coordinates) for some input address.
   *
   * @param address The address for which the coordinates must be found.
   *
   * @returns An Esri point with the coordinates of the input address in WGS84.
   */
  async getPoint(address: string): Promise<__esri.Point | undefined> {
    const [Point] = await loadModules(['esri/geometry/Point']);
 
    const request = pointSearchUrl + address;
    const points = await this.http.get<{results: GeoAdminPoint[]}>(request).toPromise();
 
    if (points.results.length === 0) {
      return undefined;
    }
 
    const point: __esri.Point = new Point({
      latitude: points.results[0].attrs.lat,
      longitude: points.results[0].attrs.lon
    });
 
    return point;
  }
 
  /**
   * Compute the distance between two points with WGS84 coordinates.
   *
   * @param pointA An Esri point for which the distance must be computed.
   * @param pointB A second Esri point for which the distance must be computed.
   *
   * @returns The distance between points A and B in meters.
   */
  async computeDistance(pointA: __esri.Point, pointB: __esri.Point): Promise<number> {
    const [
      Polyline,
      geometryEngine
    ] = await loadModules([
      'esri/geometry/Polyline',
      'esri/geometry/geometryEngine'
    ]);
 
    const line: __esri.Polyline = new Polyline({
      spatialReference: pointA.spatialReference,
      paths: [
        [[pointA.x, pointA.y], [pointB.x, pointB.y]]
      ]
    });
 
    return geometryEngine.geodesicLength(line, 'meters');
  }
 
  /**
   * Get the direction from a point A to a point B.
   *
   * @param pointA The Esri point with WGS84 coordinates from which the direction must be computed.
   * @param pointB The Esri point with WGS84 coordinates to which the direction must be computed.
   *
   * @returns The direction from point A to point B.
   */
  getDirection(pointA: __esri.Point, pointB: __esri.Point): Direction {
    const noAngle = 999;
    const dx = pointB.y - pointA.y;
    const dy = pointB.x - pointA.x;
 
    let radians: number;
    if (dx > 0) {
        radians = (Math.PI * 0.5) - Math.atan(dy / dx);
    } else if (dx < 0) {
        radians = (Math.PI * 1.5) - Math.atan(dy / dx);
    } else if (dy > 0) {
        radians = 0;
    } else if (dy < 0) {
        radians = Math.PI;
    } else {
        radians = noAngle;
    }
 
    const angle = radians * 180 / Math.PI;
 
    if (angle < 22.5 || angle >= 337.5) {
        return Direction.RIGHT;
    } else if (angle < 67.5) {
        return Direction.UP_RIGHT;
    } else if (angle < 112.5) {
        return Direction.UP;
    } else if (angle < 157.5) {
        return Direction.UP_LEFT;
    } else if (angle < 202.5) {
        return Direction.LEFT;
    } else if (angle < 247.5) {
        return Direction.DOWN_LEFT;
    } else if (angle < 292.5) {
        return Direction.DOWN;
    } else if (angle < 337.5) {
        return Direction.DOWN_RIGHT;
    }
    return Direction.CENTER;
  }
}