abdd27ed2b1154f081435ee5d79db618d2add8e0
[ccsdk/features.git] / sdnr / wt / odlux / apps / lineOfSightApp / src / utils / map.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18
19 import * as mapboxgl from "mapbox-gl";
20 import { LatLon } from "../model/LatLon";
21
22
23 export const addBaseSource = (map : mapboxgl.Map, name: string) =>{
24
25   if(!map.getSource(name))
26
27     map.addSource(name, {
28         type: 'geojson',
29         data: { type: "FeatureCollection", features: [] }
30     });
31
32 }
33
34 export const addPoint = (map : mapboxgl.Map, point: LatLon) =>{
35   const json = `{
36     "type": "Feature",
37     "properties": {},
38     "geometry": {
39       "type": "Point",
40       "coordinates": 
41         [${point.longitude}, ${point.latitude}]
42       }
43     }`;
44     
45     
46       (map.getSource("route") as mapboxgl.GeoJSONSource).setData(JSON.parse(json));
47 }
48
49 export const addBaseLayer = (map: mapboxgl.Map, sourceName: string) =>{
50
51   if(!map.getLayer('line'))
52     map.addLayer({
53         'id': 'line',
54         'type': 'line',
55         'source': sourceName,
56         'layout': {
57           'line-join': 'round',
58           'line-cap': 'round'
59         },
60         'paint': {
61           'line-color': '#88A',
62           'line-width': 6,
63           'line-opacity': 0.75
64         }
65       });
66
67       if(!map.getLayer('points'))
68       map.addLayer({
69         id: 'points',
70         type: 'circle',
71         source: sourceName,
72         paint: {
73           'circle-radius': 5,
74           'circle-color': '#223b53',
75           'circle-stroke-color': '#225ba3',
76           'circle-stroke-width': 3,
77           'circle-opacity': 0.5
78         }
79       });
80 }
81
82 export const calculateDistanceInMeter = (lat1: number, lon1: number, lat2: number, lon2: number) => {
83     const lonRad1 = toRad(lon1);
84     const latRad1 = toRad(lat1);
85     const lonRad2 = toRad(lon2);
86     const latRad2 = toRad(lat2);
87   
88     const dLon = lonRad2 - lonRad1;
89     const dLat = latRad2 - latRad1;
90     const a = Math.pow(Math.sin(dLat / 2), 2) +
91         Math.cos(latRad1) * Math.cos(latRad2) *
92         Math.pow(Math.sin(dLon / 2), 2);
93   
94     const c = 2 * Math.asin(Math.sqrt(a));
95   
96     return 6378 * c;
97   
98   }
99   
100   function toRad(value: number) {
101     return (value * Math.PI) / 180;
102   }
103