2021-05-22 01:50:04 +00:00
|
|
|
/**
|
|
|
|
* A pair of coordinates, latitude `lat` and longitude `lng`.
|
|
|
|
*/
|
2021-05-23 03:35:17 +00:00
|
|
|
import { LatLng } from "leaflet/dist/leaflet-src.esm"
|
2021-05-24 12:28:53 +00:00
|
|
|
import { SerializationError } from "./Errors"
|
2021-05-23 03:35:17 +00:00
|
|
|
|
|
|
|
|
2021-05-22 01:50:04 +00:00
|
|
|
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) {
|
2021-05-24 12:28:53 +00:00
|
|
|
const match = /[{]([0-9.-]+),([0-9.-]+)[}]/.exec(str)
|
2021-05-22 02:08:39 +00:00
|
|
|
if(!match) {
|
2021-05-24 12:28:53 +00:00
|
|
|
throw new SerializationError(str)
|
2021-05-22 02:08:39 +00:00
|
|
|
}
|
2021-05-24 12:28:53 +00:00
|
|
|
return new Coordinates(Number(match[1]), Number(match[2]))
|
|
|
|
}
|
|
|
|
|
|
|
|
static fromLatLng(latlng) {
|
|
|
|
return new Coordinates(latlng.lat, latlng.lng)
|
2021-05-22 02:08:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 01:50:04 +00:00
|
|
|
/**
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
toString() {
|
2021-05-25 13:59:09 +00:00
|
|
|
return `${this.lat} ${this.lng}`
|
2021-05-22 01:50:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2021-05-22 01:50:04 +00:00
|
|
|
*/
|
|
|
|
toGeolib() {
|
|
|
|
return {
|
|
|
|
latitude: this.lat,
|
|
|
|
longitude: this.lng,
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 02:08:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform this object in a 2-ple.
|
|
|
|
*
|
2021-05-30 12:05:39 +00:00
|
|
|
* @returns {Array}
|
2021-05-22 02:08:39 +00:00
|
|
|
*/
|
|
|
|
toArray() {
|
|
|
|
return [this.lat, this.lng]
|
|
|
|
}
|
2021-05-23 03:35:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform this object in a {@link LatLng} / Leaflet compatible-one.
|
|
|
|
*
|
|
|
|
* @returns {LatLng}
|
|
|
|
*/
|
|
|
|
toLatLng() {
|
|
|
|
return new LatLng(this.lat, this.lng)
|
|
|
|
}
|
2021-05-22 01:50:04 +00:00
|
|
|
}
|