-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcoordinate-converter.js
90 lines (80 loc) · 2.64 KB
/
coordinate-converter.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
import {
decimalPositionToDegreeMinutesSecondsString,
decimalPositionToDegreeString,
degreeLatToDecimal,
degreeLngToDecimal,
degreeMinutesSecondsLatToDecimal,
degreeMinutesSecondsLngToDecimal
} from "./utils";
class GeographicCoordinateConverterBuilder {
/***
* Return a converter instance from an array of coordinates or from a string
* @param coordinate string|array
* @returns {GeographicCoordinateConverter}
*/
fromDecimal(coordinate) {
if (Array.isArray(coordinate)) {
return new GeographicCoordinateConverter(coordinate[0], coordinate[1]);
} else {
const latlng = coordinate.split(" ");
return new GeographicCoordinateConverter(Number.parseFloat(latlng[0]), Number.parseFloat(latlng[1]));
}
}
/**
* Return a converter instance from a degree coordinate string
* @param coordinate string
* @returns {GeographicCoordinateConverter}
*/
fromDegreeMinutes(coordinate) {
const slicePoint = Math.max(coordinate.indexOf('s'), coordinate.indexOf('S'), coordinate.indexOf('n'), coordinate.indexOf('N'));
let latDecimal = coordinate.slice(0, slicePoint + 1).replace(/\s/g,'');
let lngDecimal = coordinate.slice(slicePoint + 2).replace(/\s/g,'');
return new GeographicCoordinateConverter(degreeLatToDecimal(latDecimal), degreeLngToDecimal(lngDecimal));
}
/**
* Return a converter instance from a degree minutes seconds coordinate string
* @param coordinate string
* @returns {GeographicCoordinateConverter}
*/
fromDegreeMinutesSeconds(coordinate) {
const slicePoint = Math.max(coordinate.indexOf('s'), coordinate.indexOf('S'), coordinate.indexOf('n'), coordinate.indexOf('N'));
let latDecimal = coordinate.slice(0, slicePoint + 1).replace(/\s/g,'');
let lngDecimal = coordinate.slice(slicePoint + 2).replace(/\s/g,'');
return new GeographicCoordinateConverter(degreeMinutesSecondsLatToDecimal(latDecimal), degreeMinutesSecondsLngToDecimal(lngDecimal));
}
}
export const CoordinateConverter = new GeographicCoordinateConverterBuilder();
export class GeographicCoordinateConverter {
constructor(lat, lng) {
this.lat = lat;
this.lng = lng;
}
/**
* Returns a string in decimal format
* @returns {string}
*/
toDecimal() {
return `${this.lat} ${this.lng}`;
}
/**
* Returns a array of latitude longitude
* @returns {*[]}
*/
toDecimalArray() {
return [this.lat, this.lng];
}
/**
* Returns a string in degree format
* @returns {*}
*/
toDegreeMinutes() {
return decimalPositionToDegreeString(this.lat, this.lng);
}
/**
* Returns a string in decimal minutes seconds format
* @returns {*}
*/
toDegreeMinutesSeconds() {
return decimalPositionToDegreeMinutesSecondsString(this.lat, this.lng);
}
}