-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilityFunctions.js
92 lines (81 loc) · 2.47 KB
/
utilityFunctions.js
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
/*jshint esversion: 8 */
/**
* Collection of functions that will be used by more than one js in the server
**/
var request = require('request');
var yaml = require('js-yaml');
var fs = require('fs')
module.exports = {
request: request,
/**
* @function indicateStatus
* @desc sends a POST request to the status API so the client side can know what the server is doing.
* @param text String, the message of the status
*/
indicateStatus : async function(text){
var output;
var requestURL = "http://localhost:3000/statuses";
request.post(requestURL, {form:
{
message: text,
created_at: Date.now()
}
});},
/**
* @function parseBBOX
* @desc function that takes a string of 4 coordinates and parses it into an Array
* @param bboxString String that describes a boox in 2 coordinates
* @returns array of length 4 or error
*/
parseBBOX: function(bboxString){
//QUERY BoundingBox
//create boundingBox geojson from given parameters
try {
bbox = bboxString.split(",");
}catch(err){
return(err)
}
//numberify the strings
for(let i = 0; i < bbox.length; i++){
bbox[i] = parseFloat(bbox[i]);
//return error when bbox coord was not given a number
if(isNaN(bbox[i])){
var err = new Error("bbox parameter "+i+" is not a number");
return err;
}
}
//check validity of bbox
if(bbox.length != 4){
var err = new Error("invalid parameter for bbox");
return err;
}
//check validity of bbox coordinates
if(!(
(bbox[0]>bbox[2])&& //north to be more north than south
(bbox[1]<bbox[3])&& //west to be less east than east
bbox[0]<=85 && bbox[0]>=-85&& //north and south in range of 85 to -85 degrees
bbox[2]<=85 && bbox[2]>=-85&&
bbox[1]<=180 && bbox[1]>=-180&& //east and west in range of 180 to -180 degrees
bbox[3]<=180 && bbox[3]>=-180
)){
var err = new Error("bbox coordinates are not geographically valid");
return err;
};
return bbox
},
/**
* @function loadConfigs
* @desc reads the config.yaml and returns an object containing the values
* @returns object, containting several attributes and values that represent configuration arguments
*/
loadConfigs: function(path){
try {
//load and return the document in the path
const doc = yaml.safeLoad(fs.readFileSync(path, 'utf-8'));
return(doc);
} catch (e){
console.log(e);
return false;
}
}
};