1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-26 23:14:19 +00:00
pds-2021-g2-nest/nest_frontend/objects/Coordinates.js

66 lines
1.4 KiB
JavaScript
Raw Normal View History

/**
* A pair of coordinates, latitude `lat` and longitude `lng`.
*/
export default class Coordinates {
lat
lng
/**
* @param lat - Latitude.
* @param lng - Longitude.
*/
constructor(lat, lng) {
this.lat = lat
this.lng = lng
}
2021-05-22 02:08:39 +00:00
/**
* Create a new {@link Coordinates} from the format used by the backend.
*
* @param str - The string to create the object from.
* @returns {Coordinates}
*/
static fromCrawlerString(str) {
const match = /[{]([0-9.]+),([0-9.]+)[}]/.exec(str)
if(!match) {
throw new Error(`Invalid location string: ${str}`)
}
return new Coordinates(match[0], match[1])
}
/**
* @returns {string}
*/
toString() {
return `${this.lat.toFixed(7)} ${this.lng.toFixed(7)}`
}
/**
* Render the Coordinates as an human-readable string.
*
* @returns {string}
*/
toHumanString() {
return `${this.lat.toFixed(3)} ${this.lng.toFixed(3)}`
}
/**
2021-05-22 02:08:39 +00:00
* Transform this object in a Geolib compatible-one.
*/
toGeolib() {
return {
latitude: this.lat,
longitude: this.lng,
}
}
2021-05-22 02:08:39 +00:00
/**
* Transform this object in a 2-ple.
*
* @returns {[Number, Number]}
*/
toArray() {
return [this.lat, this.lng]
}
}