-
Notifications
You must be signed in to change notification settings - Fork 3
/
os-transform.js
165 lines (132 loc) · 6.12 KB
/
os-transform.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// os-transform.js v0.4.0
window.os = window.os || {};
os.Transform = {
// Bounds object (projected and geographic coordinates) for extent of GB.
_maxBounds: {
projected: [[ 0.0, 0.0 ], [ 699999.9, 1299999.9 ]],
geographic: [[ -8.74, 49.84 ], [ 1.96, 60.9 ]]
},
/**
* Test whether coordinates are within the permitted bounds.
* @param {object} coordinates - The easting + northing or latlng to be validated.
*/
_checkBounds: function(coordinates) {
var isValid = true;
if( coordinates.hasOwnProperty('ea') && coordinates.hasOwnProperty('no') ) {
if( (coordinates.ea < this._maxBounds.projected[0][0] || coordinates.ea > this._maxBounds.projected[1][0])
|| (coordinates.no < this._maxBounds.projected[0][1] || coordinates.no > this._maxBounds.projected[1][1]) ) {
isValid = false;
}
}
else if( coordinates.hasOwnProperty('lat') && coordinates.hasOwnProperty('lng') ) {
if( (coordinates.lng < this._maxBounds.geographic[0][0] || coordinates.lng > this._maxBounds.geographic[1][0])
|| (coordinates.lat < this._maxBounds.geographic[0][1] || coordinates.lat > this._maxBounds.geographic[1][1]) ) {
isValid = false;
}
}
var message = isValid ? '' : 'Coordinates out of range.';
return { valid: isValid, message: message };
},
/**
* Test whether a standard grid reference with a valid format has been provided.
* param {string} gridref - The grid reference to be validated.
*/
_validateGridRef: function(gridref) {
var regex = /^[THJONS][VWXYZQRSTULMNOPFGHJKABCDE] ?[0-9]{1,5} ?[0-9]{1,5}$/;
var match = Array.isArray(gridref.toUpperCase().match(regex)) ? true : false;
var isValid = (gridref.replaceAll(" ", "").length % 2 === 0) && match ? true: false;
var message = isValid ? '' : 'Invalid grid reference.';
return { valid: isValid, message: message };
},
/**
* Return latlng from an input easting + northing.
* @param {object} coordinates - The easting + northing to be transformed.
* @param {integer} decimals - [optional] The specified number of decimal places.
*/
toLatLng: function(coordinates, decimals = 7) {
var test = this._checkBounds(coordinates)
if(! test.valid ) {
console.log(test.message);
return {};
}
var point = proj4('EPSG:27700', 'EPSG:4326', [ coordinates.ea, coordinates.no ]);
var lng = Number(point[0].toFixed(decimals));
var lat = Number(point[1].toFixed(decimals));
return { lat: lat, lng: lng };
},
/**
* Return easting + northing from an input latlng.
* @param {object} coordinates - The latlng to be transformed.
* @param {integer} decimals - [optional] The specified number of decimal places.
*/
fromLatLng: function(coordinates, decimals = 2) {
var test = this._checkBounds(coordinates)
if(! test.valid ) {
console.log(test.message);
return {};
}
var point = proj4('EPSG:4326', 'EPSG:27700', [ coordinates.lng, coordinates.lat ]);
var e = Number(point[0].toFixed(decimals));
var n = Number(point[1].toFixed(decimals));
return { ea: e, no: n };
},
/**
* Return grid reference [plain | encoded | components] from an input easting + northing.
* @param {object} coordinates - The easting + northing to be converted.
*/
toGridRef: function(coordinates) {
var test = this._checkBounds(coordinates)
if(! test.valid ) {
console.log(test.message);
return {};
}
var prefixes = [
[ 'SV', 'SW', 'SX', 'SY', 'SZ', 'TV', 'TW' ],
[ 'SQ', 'SR', 'SS', 'ST', 'SU', 'TQ', 'TR' ],
[ 'SL', 'SM', 'SN', 'SO', 'SP', 'TL', 'TM' ],
[ 'SF', 'SG', 'SH', 'SJ', 'SK', 'TF', 'TG' ],
[ 'SA', 'SB', 'SC', 'SD', 'SE', 'TA', 'TB' ],
[ 'NV', 'NW', 'NX', 'NY', 'NZ', 'OV', 'OW' ],
[ 'NQ', 'NR', 'NS', 'NT', 'NU', 'OQ', 'OR' ],
[ 'NL', 'NM', 'NN', 'NO', 'NP', 'OL', 'OM' ],
[ 'NF', 'NG', 'NH', 'NJ', 'NK', 'OF', 'OG' ],
[ 'NA', 'NB', 'NC', 'ND', 'NE', 'OA', 'OB' ],
[ 'HV', 'HW', 'HX', 'HY', 'HZ', 'JV', 'JW' ],
[ 'HQ', 'HR', 'HS', 'HT', 'HU', 'JQ', 'JR' ],
[ 'HL', 'HM', 'HN', 'HO', 'HP', 'JL', 'JM' ]
];
var x = Math.floor(coordinates.ea / 100000);
var y = Math.floor(coordinates.no / 100000);
var prefix = prefixes[y][x];
var e = Math.floor(coordinates.ea % 100000); // Math.round(coordinates.ea % 100000);
var n = Math.floor(coordinates.no % 100000); // Math.round(coordinates.no % 100000);
e = String(e).padStart(5, '0');
n = String(n).padStart(5, '0');
var text = prefix + ' ' + e + ' ' + n,
html = prefix + ' ' + e + ' ' + n;
return { text: text, html: html, letters: prefix, eastings: e, northings: n };
},
/**
* Return easting + northing from an input grid reference.
* @param {string} gridref - The grid reference to be converted.
*/
fromGridRef: function(gridref) {
gridref = String(gridref).trim();
var test = this._validateGridRef(gridref)
if(! test.valid ) {
console.log(test.message);
return {};
}
var gridLetters = "VWXYZQRSTULMNOPFGHJKABCDE";
var ref = gridref.toUpperCase().replaceAll(" ", "");
var majorEasting = gridLetters.indexOf(ref[0]) % 5 * 500000 - 1000000;
var majorNorthing = Math.floor(gridLetters.indexOf(ref[0]) / 5) * 500000 - 500000;
var minorEasting = gridLetters.indexOf(ref[1]) % 5 * 100000;
var minorNorthing = Math.floor(gridLetters.indexOf(ref[1]) / 5) * 100000;
var i = (ref.length-2) / 2;
var m = Math.pow(10, 5-i);
var e = majorEasting + minorEasting + (ref.substr(2, i) * m);
var n = majorNorthing + minorNorthing + (ref.substr(i+2, i) * m);
return { ea: e, no: n };
}
};