point.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { Validate } from '../validate';
  2. import { SYMBOL_GEO_POINT } from '../helper/symbol';
  3. import { isArray } from '../utils/type';
  4. export class Point {
  5. constructor(longitude, latitude) {
  6. Validate.isGeopoint('longitude', longitude);
  7. Validate.isGeopoint('latitude', latitude);
  8. this.longitude = longitude;
  9. this.latitude = latitude;
  10. }
  11. parse(key) {
  12. return {
  13. [key]: {
  14. type: 'Point',
  15. coordinates: [this.longitude, this.latitude]
  16. }
  17. };
  18. }
  19. toJSON() {
  20. return {
  21. type: 'Point',
  22. coordinates: [
  23. this.longitude,
  24. this.latitude,
  25. ],
  26. };
  27. }
  28. toReadableString() {
  29. return `[${this.longitude},${this.latitude}]`;
  30. }
  31. static validate(point) {
  32. return point.type === 'Point' &&
  33. isArray(point.coordinates) &&
  34. Validate.isGeopoint('longitude', point.coordinates[0]) &&
  35. Validate.isGeopoint('latitude', point.coordinates[1]);
  36. }
  37. get _internalType() {
  38. return SYMBOL_GEO_POINT;
  39. }
  40. }