1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 04:54:18 +00:00

Add Coordinates class and some tests

This commit is contained in:
Steffo 2021-05-22 03:50:04 +02:00
parent bc2310f18e
commit 7ca7ae49a5
Signed by: steffo
GPG key ID: 6965406171929D01
4 changed files with 85 additions and 15 deletions

View file

@ -1,7 +1,7 @@
import "@testing-library/jest-dom/extend-expect"
import { Condition, ConditionHashtag, ConditionLocation, ConditionTime, ConditionUser } from "./Condition" import { Condition, ConditionHashtag, ConditionLocation, ConditionTime, ConditionUser } from "./Condition"
import TimeRay from "./TimeRay" import TimeRay from "./TimeRay"
import MapArea from "./MapArea" import MapArea from "./MapArea"
import Coordinates from "./Coordinates"
test("Condition can be constructed", () => { test("Condition can be constructed", () => {
@ -28,7 +28,7 @@ test("ConditionTime can be constructed", () => {
}) })
test("ConditionLocation can be constructed", () => { test("ConditionLocation can be constructed", () => {
const mapArea = new MapArea(1000, 0.000, 0.000) const mapArea = new MapArea(1000, new Coordinates(0.000, 0.000))
expect(new ConditionLocation(mapArea)).toBeTruthy() expect(new ConditionLocation(mapArea)).toBeTruthy()
expect(new ConditionLocation(mapArea, 1)).toBeTruthy() expect(new ConditionLocation(mapArea, 1)).toBeTruthy()
@ -50,7 +50,7 @@ test("ConditionTime has the correct type", () => {
}) })
test("ConditionLocation has the correct type", () => { test("ConditionLocation has the correct type", () => {
const mapArea = new MapArea(1000, 0.000, 0.000) const mapArea = new MapArea(1000, new Coordinates(0.000, 0.000))
expect(new ConditionLocation(mapArea).type).toBe(3) expect(new ConditionLocation(mapArea).type).toBe(3)
}) })

View file

@ -0,0 +1,42 @@
/**
* 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
}
/**
* @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)}`
}
/**
* Transform the object in a Geolib compatible-one.
*/
toGeolib() {
return {
latitude: this.lat,
longitude: this.lng,
}
}
}

View file

@ -1,23 +1,27 @@
import {getDistance} from "geolib"
/** /**
* An area on a map, defined by a latitude `lat`, a longitude `lng` and a radius `rad` in meters. * An area on a map, defined by a `center` and a `radius` in meters.
*/ */
export default class MapArea { export default class MapArea {
radius
center
/** /**
* @param rad - Radius of the area in meters. * @param radius - The radius of the area in meters.
* @param lat - Latitude of the center of the radius. * @param center - The center of the area.
* @param lng - Longitude of the center of the radius.
*/ */
constructor(rad, lat, lng) { constructor(radius, center) {
this.rad = rad this.radius = radius
this.lat = lat this.center = center
this.lng = lng
} }
/** /**
* @returns {string} * @returns {string}
*/ */
toString() { toString() {
return `${this.rad} ${this.lat.toFixed(7)} ${this.lng.toFixed(7)}` return `${this.radius} ${this.center.toString()}`
} }
/** /**
@ -26,9 +30,20 @@ export default class MapArea {
* @returns {string} * @returns {string}
*/ */
toHumanString() { toHumanString() {
if(this.rad >= 2000) { if(this.radius >= 2000) {
const kmRadius = Math.round(this.rad / 1000) const kmRadius = Math.round(this.radius / 1000)
return `${kmRadius}km ${this.lat.toFixed(3)} ${this.lng.toFixed(3)}` return `${kmRadius}km ${this.center.toHumanString()}`
} }
return `${this.radius}m ${this.center.toHumanString()}`
}
/**
* Check if a pair of coordinates is included in the area.
*
* @param coords - The coordinates to check.
* @returns {boolean}
*/
includes(coords) {
return getDistance(this.center.toGeolib(), coords.toGeolib()) <= this.radius
} }
} }

View file

@ -0,0 +1,13 @@
import Coordinates from "./Coordinates"
import MapArea from "./MapArea"
test("MapArea can be constructed", () => {
const mapArea = new MapArea(1000, new Coordinates(0.0, 0.0))
expect(mapArea).toBeTruthy()
})
test("MapArea can be rendered to a spec-compatible string", () => {
const mapArea = new MapArea(1000, new Coordinates(0.0, 0.0))
expect(mapArea.toString()).toBe("1000 0.0000000 0.0000000")
})