-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPotholeData.js
109 lines (102 loc) · 4.09 KB
/
PotholeData.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* Creates PotholeData object
*
* Parameters:
* • id : integer
* • latitude : number (or null)
* • longitude : number (or null)
* • snappedToRoad : boolean
* • filled : boolean
* • surfaceArea : number
* • bagsNeeded : number
* • imageURL : string
* Methods:
* • setCoordinates() : sets latitude,longitude (and lat,lng) of this
* -Parameters:
* • coordorLatitude : coordinates object or number
* • longitude: number
* • setAddress() : sets street address of this
* -Parameters:
* • address : string
* • isSnappedToRoad() : sets/returns whether or not coordinates of this have been snapped to road
* -Parameters:
* • snapped : boolean (optional)
* -NOTE: if no parameters have been specified, function simply returns this._snappedToRoad
*/
// TODO: implement way to check for string first or second argument
function PotholeData(id, latitude, longitude, snappedToRoad, filled, surfaceArea, bagsNeeded, imageURL) {
if ((isNaN(Number(latitude))) || (isNaN(Number(longitude)))) {
throw new TypeError(
"Expected latitude,longitude to be number, but got address instead. Thrown from PotholeData constructor");
}
this._id = parseInt(id) || 0;
this._streetAddress = '';
this._snappedToRoad = snappedToRoad || false; // assume coordinates have not been snapped to road
this.lat = Number(latitude);
this.lng = Number(longitude);
// For good measure, we give this latitude,longitude members
this.latitude = this.lat;
this.longitude= this.lng;
// now, onto the rest of the members...
this.isFilled = filled || false;
this.area = Number(surfaceArea);
this.bagsNeeded = Number(bagsNeeded);
this.imageURL = imageURL;
this.potholeState = getPotholeStateFor(this);
// methods. in here, instead of as members of PotholeData.prototype, so that, when JSON.stringify happens,
// they are sent to client.
this.isSnappedToRoad = function(snapped) {
if (snapped === undefined) return this._snappedToRoad;
// make sure snapped is boolean at this point
snapped = !(!(snapped));
this._snappedToRoad = snapped;
}
this.setCoordinates = function(coordinateOrLatitude, longitude) {
// if coordinateOrLatitude is a coordinates object
if ((coordinateOrLatitude.lat !== undefined) || (coordinateOrLatitude.latitude !== undefined)) {
// we use it to set this.lat,this.lng, this.latitude,this.longitude
this.lat = Number(coordinateOrLatitude.lat) || Number(coordinateOrLatitude.latitude);
this.lng = Number(coordinateOrLatitude.lng) || Number(coordinateOrLatitude.longitude);
}
// otherwise, we assume it's a number and use it and longitude to set those members
else {
this.lat = coordinateOrLatitude;
this.lng = longitude;
}
this.latitude = this.lat;
this.longitude = this.lng;
};
// for the Mustache.js rendering. Computes volume of this in cubic feet. 1 bag needed == 5/12 cubic foot
this.volume = function() { return this.bagsNeeded * 5/12; };
}
// methods
PotholeData.prototype.setID = function (id) {
this._id = id;
}
PotholeData.prototype.setStreetAddress = function(address) {
this._streetAddress = address;
}
/* Checks whether or not a provided coordinate is valid
* Parameters:
* • coord : (number) the coordinate to check
* • isLatitude : (boolean) whether or not this coordinate is a latitude
* Returns:
* • whether or not coord is valid
*/
PotholeData.isValidCoord = function(coord, isLatitude) {
isLatitude = !(!(isLatitude));
return (coord === parseFloat(coord)) && (isLatitude ?
(Math.abs(coord) <= 90) :
(Math.abs(coord) <= 180));
};
/* Checks whether or not lat,lng (and latitude,longitude) of this are valid
* Parameters: none
* Returns :
* • whether or not this contains valid lat,lng and latitude,longitude
*/
PotholeData.prototype.hasValidGPSCoords = function() {
return (
(PotholeData.isValidCoord(this.lat, true) && PotholeData.isValidCoord(this.latitude, true)) &&
(PotholeData.isValidCoord(this.lng, false) && PotholeData.isValidCoord(this.longitude, false))
);
//return true;
}