aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/apps/networkMapApp/src/utils/mapUtils.ts
blob: da3e97ee3c3c83229ac6ce1c6fdb32c2af10f24f (plain)
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
/**
 * ============LICENSE_START========================================================================
 * ONAP : ccsdk feature sdnr wt odlux
 * =================================================================================================
 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. All rights reserved.
 * =================================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * ============LICENSE_END==========================================================================
 */

const EARTHRADIUSM = 6378137;

type updatedCoordinates = { south: number, west: number, north: number, east: number };


export const increaseBoundingBox = (map: mapboxgl.Map) =>{

    const bbox = map.getBounds();

    const distance = map.getCenter().distanceTo(bbox.getNorthEast()); // radius of visible area (center -> corner) (in meters)

    //calculate new boundingBox
    const increasedBoundingBox = addDistance(bbox.getSouth(), bbox.getWest(), bbox.getNorth(), bbox.getEast(), (distance / 1000) / 2);
    return increasedBoundingBox;
}



export const addDistance = (south: number, west: number, north: number, east: number, distanceKm: number): updatedCoordinates => {

    const distanceInM = distanceKm * 1000;

    const dLat = distanceInM / EARTHRADIUSM;
    const dLon = distanceInM / (EARTHRADIUSM * Math.cos(Math.PI * (north + south) / 360));

    const latOffset = dLat * 180 / Math.PI;
    const lonOffset = dLon * 180 / Math.PI;

    const newEast = checkLongitude(east + lonOffset);
    const newWest = checkLongitude(west - lonOffset);
    const newNorth = checkLatitude(north + latOffset);
    const newSouth = checkLatitude(south - latOffset);

    return { east: newEast, north: newNorth, south: newSouth, west: newWest };

}


//taken from https://www.movable-type.co.uk/scripts/latlong.html
export const calculateMidPoint = (lat1: number, lon1: number, lat2: number, lon2: number) =>{

    const dLon = degrees_to_radians(lon2 - lon1);

    //convert to radians
    lat1 = degrees_to_radians(lat1);
    lat2 = degrees_to_radians(lat2);
    lon1 = degrees_to_radians(lon1);

    const Bx = Math.cos(lat2) * Math.cos(dLon);
    const By = Math.cos(lat2) * Math.sin(dLon);
    const lat3 = Math.atan2(Math.sin(lat1) + Math.sin(lat2), Math.sqrt((Math.cos(lat1) + Bx) * (Math.cos(lat1) + Bx) + By * By));
    const lon3 = lon1 + Math.atan2(By, Math.cos(lat1) + Bx);

    return [radians_to_degrees(lon3), radians_to_degrees(lat3)];
}


export const LatLonToDMS = (value:number, isLon:boolean=false) =>{
    const absoluteValue = Math.abs(value);
    const d = Math.floor(absoluteValue);
    const m = Math.floor((absoluteValue -d)* 60);
    const s = (absoluteValue - d - m / 60 ) * 3600;
    const dms=`${d}° ${m}' ${s.toFixed(2)}"`

    const sign = Math.sign(value);

    if(isLon){
        return (sign === -1 || sign === -0 ) ? dms + " W" : dms + " E";
    }else{
        return (sign === -1 || sign === -0 ) ? dms + " S" : dms + " N";
    } 
}

// Because features come from tiled vector data, feature geometries may be split
// or duplicated across tile boundaries and, as a result, features may appear
// multiple times in query results.

//taken from https://docs.mapbox.com/mapbox-gl-js/example/filter-features-within-map-view/

export const getUniqueFeatures = (array: mapboxgl.MapboxGeoJSONFeature[], comparatorProperty:string) =>{
    var existingFeatureKeys: any = {};
    
    var uniqueFeatures = array.filter(function(el) {
    if (existingFeatureKeys[el.properties![comparatorProperty]]) {
    return false;
    } else {
    existingFeatureKeys[el.properties![comparatorProperty]] = true;
    return true;
    }
    });
     
    return uniqueFeatures;
    }

const radians_to_degrees = (radians:number) =>{
    
    var pi = Math.PI;
    return radians * (180/pi);
}

 const degrees_to_radians = (degrees: number) =>
    {
    return degrees * (Math.PI/180);
    }


const checkLatitude = (lat: number) => {

    if (lat > 90)
        return 90;
    else if (lat < -90)
        return -90;
    else
        return lat;

}

const checkLongitude = (lon: number) => {
    if (lon > 180)
        return 180;
    else if (lon < -180)
        return -180;
    else
        return lon;
}