/
point.js
159 lines (139 loc) Β· 3.99 KB
/
point.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
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
'use strict';
const { GenericGeographicClass } = require('./lib/common');
const geoHash = require('./lib/geohash');
module.exports = class Point extends GenericGeographicClass {
constructor (coordinates, options) {
const defaults = {
inverted: false,
precision: 6 // 0.11m of precision
};
if (typeof options === 'boolean') options = { inverted: options };
if (typeof options === 'number') options = { precision: options };
super({
defaults,
options
});
this._coordinates = {};
this.coordinates = coordinates;
}
set coordinates (coordinates) {
if (!coordinates) {
throw new Error('Wrong arguments.');
}
// OTHER GEO POINT OBJECT
else if (coordinates && coordinates instanceof this.constructor) {
this.coordinates = coordinates.coordinates;
}
// ARRAY WITH LONGITUDE AND LATITUDE
else if (coordinates instanceof Array && coordinates.length === 2) {
if (this._options.inverted) {
this._coordinates = {
latitude: coordinates[1],
longitude: coordinates[0]
};
} else {
this._coordinates = {
latitude: coordinates[0],
longitude: coordinates[1]
};
}
}
// BROWSER GEOLOCATION
else if (coordinates.coords && coordinates.coords.latitude && coordinates.coords.longitude) {
this.coordinates = [
coordinates.coords.latitude,
coordinates.coords.longitude
];
}
// IF SIMPLE LATITUDE AND LONGITUDE OBJECT
else if (coordinates.lat && coordinates.long) {
this.coordinates = [
coordinates.lat,
coordinates.long
];
} else if (coordinates.lat && coordinates.lng) {
this.coordinates = [
coordinates.lat,
coordinates.lng
];
} else if (coordinates.latitude && coordinates.longitude) {
this.coordinates = [
coordinates.latitude,
coordinates.longitude
];
}
// IF GEOHASH
else if (typeof coordinates === 'string') {
let [ latitude, longitude ] = geoHash.decode(coordinates);
this.coordinates = [
latitude,
longitude
];
}
// IF NONE OF THE CONDITIONS MATCH, THROW ERROR
else {
throw new Error('Wrong arguments.');
}
// ENSURE COORDINATES ARE NUMERIC
if (typeof this._coordinates.latitude === 'string') {
this._coordinates.latitude = parseFloat(this._coordinates.latitude);
}
if (typeof this._coordinates.longitude === 'string') {
this._coordinates.longitude = parseFloat(this._coordinates.longitude);
}
// VERIFY COORDINATES ARE VALID
if (
typeof this._coordinates.latitude !== 'number' ||
typeof this._coordinates.longitude !== 'number' ||
this._coordinates.latitude > 90 ||
this._coordinates.latitude < -90 ||
this._coordinates.longitude > 180 ||
this._coordinates.longitude < -180
) {
throw new Error('Coordinates are invalid.');
}
// OPTIONS PRECISION
if (typeof this._options.precision === 'number') {
Object.entries(this._coordinates).map(([ key, value ]) => {
this._coordinates[key] = parseFloat(value.toFixed(this._options.precision));
});
}
}
get coordinates () {
return this._coordinates;
}
get short () {
return {
lat: this._coordinates.latitude,
long: this._coordinates.longitude
};
}
get shortest () {
return {
lat: this._coordinates.latitude,
lng: this._coordinates.longitude
};
}
get array () {
return [ this._coordinates.latitude, this._coordinates.longitude ];
}
get arrayInverted () {
return [ this._coordinates.longitude, this._coordinates.latitude ];
}
get geohash () {
return geoHash.encode(this.array);
}
toJSON () {
return this._coordinates;
}
toGeoJSON (properties) {
return {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: this.arrayInverted
},
...properties && { properties }
};
}
};