Skip to content

Commit 808d87a

Browse files
2.1.1 useful routine distanceInMeters added to API and unit test
1 parent a4f5b5c commit 808d87a

File tree

5 files changed

+70
-21
lines changed

5 files changed

+70
-21
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ of the Mapcode library by Rijn Buve and Matthew Lowden.
4343

4444
# Version History
4545

46+
* 2.1.1 - August 2015
47+
48+
Added useful routine DistanceInMeters to API
49+
4650
* 2.1.0 - August 2015
4751

4852
Rewrote fraction floating points to integer arithmetic

mapcode.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ var mapcode_dataversion = "2.0";
121121

122122
// *************************** mapcode_org *********************
123123

124-
var mapcode_javaversion = '2.1.0/Data2.0';
124+
var mapcode_javaversion = '2.1.1/Data2.0';
125125

126126
/// PRIVATE returns string without leading spaces and plus-signs, and trailing spaces
127127
function trim(str) {
@@ -1939,26 +1939,11 @@ function master_encode(orgy, orgx, tn, isrecursive, stop_with_one_result, alloww
19391939
/// PUBLIC returns {distance,width,height}, the distance between two coordinates, and the longitudinal distance ("width") and latitudinal distance ("height") - all expressed in meters
19401940
/// Warning: accurate only for coordinates within a few hundred meters of each other
19411941
function distanceInMeters(latDeg1, lonDeg1, latDeg2, lonDeg2) {
1942-
var worstParallel = 0; // assume equator
1943-
if (latDeg1 > latDeg2) {
1944-
if (latDeg1 < 0) {
1945-
worstParallel = latDeg2;
1946-
} else if (latDeg2 > 0) {
1947-
worstParallel = latDeg1;
1948-
}
1949-
}
1950-
else {
1951-
if (latDeg2 < 0) {
1952-
worstParallel = latDeg1;
1953-
} else if (latDeg1 > 0) {
1954-
worstParallel = latDeg2;
1955-
}
1956-
}
1957-
var dy = (latDeg2 - latDeg1) * 1000000 / 9;
19581942
if (lonDeg1 < 0 && lonDeg2 > 1) { lonDeg1 += 360; }
19591943
if (lonDeg2 < 0 && lonDeg1 > 1) { lonDeg2 += 360; }
1960-
var dx = (lonDeg2 - lonDeg1) * Math.cos(Math.PI * worstParallel / 180) * 1000000 / 9;
1961-
return {distance: Math.sqrt(dx * dx + dy * dy), width: (dx < 0 ? -dx : dx), height: (dy < 0 ? -dy : dy)};
1944+
var dx = 111319.49079327 * (lonDeg2 - lonDeg1) * Math.cos((latDeg1 + latDeg2) * Math.PI / 360.0);
1945+
var dy = 110946.25213273 * (latDeg2 - latDeg1);
1946+
return Math.sqrt(dx * dx + dy * dy);
19621947
}
19631948

19641949
/// PUBLIC convert a mapcode (skipping the territory abbreviation) into a particular alphabet

mapcode_library_js.doc

2 KB
Binary file not shown.

unittest/test_encodes.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11

22
var testdata = [
33

4+
// some notable examples
5+
"", 55.01500050000000, 20.94998200000000, 0,0,
6+
"", 55.01502500000000, 20.95002500000000, 0,0,
7+
"", 21.61999950000000, -102.02419949999999, 0,0,
8+
"", 21.62000350000000, -102.02423750000000, 0,0,
9+
"", 29.72705500000000, 73.87498650000001,0,0,
10+
"", 29.72702500000000, 73.87502499999999,0,0,
11+
"", 28.67999500000000, 78.71705600000000,0,0,
12+
"", 28.68002500000000, 78.71702500000001,0,0,
13+
"", 5.785780,169.791750, 0,0, // grid
14+
"", -9.34015920,-171.99626665, 0,0, // encompassing
15+
"", 49.4632595, -2.559890, 0,0, // inner grid
16+
"", 49.4632625, -2.5599018, 0,0, // old
17+
"", 47.1495405, 9.5319995, 0,0, // inner grid AUT
18+
"", 41.90350000000000, 12.45200000000000 , 0,0,
19+
"", 12.16892100000000, -68.26226699999999, 0,0,
20+
"", 21.90678099999900, -72.05700000000100, 0,0,
21+
"", 21.90669750000000 , -72.05699749999999, 0,0,
22+
"", 21.90678099999900, -72.05700000000100, 0,0,
23+
424
"IN-NL WKS.H6",26.904854, 95.138497, 5,21,
525
"NLD 00.XX",52.383984,4.865401375, 0,0,
626

unittest/unittest.html

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</head>
1616
<body>
1717

18-
Mapcode Javascript Unit Test 2.1.0<BR>
18+
Mapcode Javascript Unit Test 2.1.1<BR>
1919
<DIV id="out"></DIV>
2020
<DIV id="perc"></DIV>
2121

@@ -137,7 +137,7 @@
137137
nrTests++;
138138
var p = decode(str);
139139
if ( p ) {
140-
var dm = distanceInMeters(y, x, p.y, p.x).distance;
140+
var dm = distanceInMeters(y, x, p.y, p.x);
141141
var maxerror = maxErrorForPrecision[precision];
142142
if (dm>maxerror) {
143143
nrErrors++;
@@ -336,6 +336,44 @@
336336
}
337337
}
338338

339+
function distance_tests()
340+
{
341+
if (mapcode_javaversion>'2.1.0') {
342+
var i,distance;
343+
var coordpairs = [
344+
// lat1, lon1, lat2, lon2, expected distance * 100000
345+
1,1,1,1,0,
346+
0,0,0,1,11131949079,
347+
89,0,89,1,194279300,
348+
3,0,3,1,11116693130,
349+
-3,0,-3,1,11116693130,
350+
-3,-179.5,-3,179.5,11116693130,
351+
-3,179.5,-3,-179.5,11116693130,
352+
3,8,3,9,11116693130,
353+
3,-8,3,-9,11116693130,
354+
3,-0.5,3,0.5,11116693130,
355+
54,5,54.000001,5,11095,
356+
54,5,54,5.000001,6543,
357+
54,5,54.000001,5.000001,12880,
358+
90,0,90,50,0,
359+
0.11,0.22,0.12,0.2333,185011466,
360+
-1];
361+
362+
for(i=0;coordpairs[i]!=-1;i+=5) {
363+
nrTests++;
364+
distance = distanceInMeters(
365+
coordpairs[i],coordpairs[i+1],
366+
coordpairs[i+2],coordpairs[i+3]);
367+
if ( Math.round(100000.0 * distance) != coordpairs[i+4] ) {
368+
nrErrors++;
369+
out('*** ERROR *** distanceInMeters '+i+' failed: '+distance);
370+
}
371+
}
372+
}
373+
}
374+
375+
376+
339377
var myVar = setInterval(function(){stepper()},20);
340378
var dostep=0;
341379
function stepper()
@@ -351,6 +389,8 @@
351389
alphabet_tests();
352390
break;
353391
case 3:
392+
out('<HR>Distance tests');
393+
distance_tests();
354394
out('<HR>Territory tests');
355395
out( (ccode_earth+1) + ' territories');
356396
break;

0 commit comments

Comments
 (0)