-
Notifications
You must be signed in to change notification settings - Fork 45
/
test.js
156 lines (150 loc) · 5.89 KB
/
test.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
const should = require('chai').should(); // eslint-disable-line no-unused-vars
const mgrs = require('../');
const { readFileSync } = require('fs');
describe('First MGRS set', () => {
const mgrsStr = '33UXP04';
const point = mgrs.toPoint(mgrsStr);
it('Longitude of point from MGRS correct.', () => {
point[0].should.be.closeTo(16.41450, 0.000001);
});
it('Latitude of point from MGRS correct.', () => {
point[1].should.be.closeTo(48.24949, 0.000001);
});
it('MGRS reference with highest accuracy correct.', () => {
mgrs.forward(point).should.equal('33UXP0500444997');
});
it('MGRS reference with 1-digit accuracy correct.', () => {
mgrs.forward(point,1).should.equal(mgrsStr);
});
it('MGRS reference with 0-digit accuracy correct.', () => {
mgrs.forward(point, 0).should.equal('33UXP');
});
});
describe('Second MGRS set', () => {
const mgrsStr = '24XWT783908'; // near UTM zone border, so there are two ways to reference this
const point = mgrs.toPoint(mgrsStr);
it('Longitude of point from MGRS correct.', () => {
point[0].should.be.closeTo(-32.66433, 0.00001);
});
it('Latitude of point from MGRS correct.', () => {
point[1].should.be.closeTo(83.62778, 0.00001);
});
it('MGRS reference with 3-digit accuracy correct.', () => {
mgrs.forward(point,3).should.equal('25XEN041865');
});
it('MGRS reference with 5-digit accuracy, northing all zeros', () => {
mgrs.forward([0,0],5).should.equal('31NAA6602100000');
});
it('MGRS reference with 5-digit accuracy, northing one digit', () => {
mgrs.forward([0,0.00001],5).should.equal('31NAA6602100001');
});
it('MGRS reference with 0-digit accuracy correct.', () => {
mgrs.forward(point, 0).should.equal('25XEN');
});
});
describe ('third mgrs set', () => {
const mgrsStr = '11SPA7234911844';
const point = [-115.0820944, 36.2361322];
it('MGRS reference with highest accuracy correct.', () => {
mgrs.forward(point).should.equal(mgrsStr);
});
it('MGRS reference with 0-digit accuracy correct.', () => {
mgrs.forward(point, 0).should.equal('11SPA');
});
});
describe ('data validation', () => {
describe('toPoint function', () => {
it('toPoint throws an error when a blank string is passed in', () => {
try {
mgrs.toPoint('');
false.should.be.true; // to make sure it errors
} catch (error) {
error.should.be.a('error');
error.message.should.equal('toPoint received a blank string');
}
});
it('toPoint should return the same result whether or not spaces are included in the MGRS String', () => {
const [ lon1, lat1 ] = mgrs.toPoint('4QFJ 12345 67890');
const [ lon2, lat2] = mgrs.toPoint('4QFJ1234567890');
lat1.should.equal(lat2);
lon1.should.equal(lon2);
});
});
describe('forward function', () => {
it('forward throws an error when array of strings passed in', () => {
try {
mgrs.forward(['40', '40']);
false.should.be.true; // to make sure it errors
} catch (error) {
error.should.be.a('error');
error.message.should.equal('forward received an array of strings, but it only accepts an array of numbers.');
}
});
it('forward throws an error when longitude is outside bounds', () => {
try {
mgrs.forward([90, 180]);
false.should.be.true; // to make sure it errors
} catch (error) {
error.should.be.a('error');
error.message.should.equal('forward received an invalid latitude of 180');
}
});
it('forward throws an error when latitude is outside bounds', () => {
try {
mgrs.forward([90, 270]);
false.should.be.true; // to make sure it errors
} catch (error) {
error.should.be.a('error');
error.message.should.equal('forward received an invalid latitude of 270');
}
});
it('forward throws an error when latitude is near the north pole', () => {
try {
mgrs.forward([45, 88]);
false.should.be.true; // to make sure it errors
} catch (error) {
error.should.be.a('error');
error.message.should.equal('forward received a latitude of 88, but this library does not support conversions of points in polar regions below 80°S and above 84°N');
}
});
it('forward throws an error when latitude is near the south pole', () => {
try {
mgrs.forward([45, -88]);
false.should.be.true; // to make sure it errors
} catch (error) {
error.should.be.a('error');
error.message.should.equal('forward received a latitude of -88, but this library does not support conversions of points in polar regions below 80°S and above 84°N');
}
});
});
describe('getLetterDesignator', () => {
it('should return Z when latitude band is outside latitude handled by library', () => {
const latitude = -83.3026329741;
const letter = mgrs.getLetterDesignator(latitude);
letter.should.equal('Z');
});
});
});
if (process.env.CHECK_GEOTRANS) {
describe('Consistency with GEOTRANS', () => {
it('Should be consistent with GEOTRANS', () => {
const fileText = readFileSync('./test/testing-data.csv', 'utf8');
const lines = fileText.split('\n')
.filter(Boolean) // remove blank lines if any
.slice(1); // remove header
lines.forEach(line => {
const [ mgrsString, expectedLongitude, expectedLatitude ] = line.split('\t');
// mgrs library doesn't support polar regions
if (['A','B','Y','Z'].includes(mgrsString[0])) return;
const [ actualLongitude, actualLatitude ] = mgrs.toPoint(mgrsString);
try {
actualLatitude.should.be.closeTo(Number.parseFloat(expectedLatitude), 0.0000015);
actualLongitude.should.be.closeTo(Number.parseFloat(expectedLongitude), 0.0000015);
} catch (error) {
console.error('mgrsString:', mgrsString);
throw error;
}
});
});
});
}