Skip to content

Commit ddea9d5

Browse files
2.1.1 useful routine distanceInMeters added to API and unit test
1 parent 325f0a6 commit ddea9d5

File tree

4 files changed

+73
-50
lines changed

4 files changed

+73
-50
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ limitations under the License.
4444

4545
# Version History
4646

47+
* 2.1.1
48+
49+
Added useful routine DistanceInMeters to API
50+
4751
* 2.1.0
4852

4953
Rewrote fraction floating points to integer arithmetic;

mapcode_api.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
define('mapcode_phpversion', '2.1.0');
19+
define('mapcode_phpversion', '2.1.1');
2020

2121
$xdivider19 = array(
2222
360, 360, 360, 360, 360, 360, 361, 361, 361, 361,
@@ -1577,11 +1577,6 @@ function encodeNameless($enc, $m, $firstcode, $extraDigits)
15771577
$SIDE = smartdiv($m);
15781578
$orgSIDE = $SIDE;
15791579
$XSIDE = $SIDE;
1580-
if (isSpecialShape($m)) {
1581-
$XSIDE *= $SIDE;
1582-
$SIDE = 1 + (int)(($mm->maxy - $mm->miny) / 90);
1583-
$XSIDE = (int)($XSIDE / $SIDE);
1584-
}
15851580

15861581
$dividerx4 = xDivider4($mm->miny, $mm->maxy);
15871582
$xFracture = (int)(4 * $enc->fraclon / 3240000);
@@ -1599,6 +1594,9 @@ function encodeNameless($enc, $m, $firstcode, $extraDigits)
15991594

16001595
$v = $storage_offset;
16011596
if (isSpecialShape($m)) {
1597+
$XSIDE *= $SIDE;
1598+
$SIDE = 1 + (int)(($mm->maxy - $mm->miny) / 90);
1599+
$XSIDE = (int)($XSIDE / $SIDE);
16021600
$v += encodeSixWide($dx, $SIDE - 1 - $dy, $XSIDE, $SIDE);
16031601
} else {
16041602
$v += ($dx * $SIDE + $dy);
@@ -1797,29 +1795,11 @@ function mapcoderEngine($enc, $tn, $getshortest, $isrecursive, $state_override,
17971795

17981796
function distanceInMeters($latDeg1, $lonDeg1, $latDeg2, $lonDeg2)
17991797
{
1800-
$worstParallel = 0;
1801-
if ($latDeg1 > $latDeg2) {
1802-
if ($latDeg1 < 0) {
1803-
$worstParallel = $latDeg2;
1804-
} else {
1805-
if ($latDeg2 > 0) {
1806-
$worstParallel = $latDeg1;
1807-
}
1808-
}
1809-
} else {
1810-
if ($latDeg2 < 0) {
1811-
$worstParallel = $latDeg1;
1812-
} else {
1813-
if ($latDeg1 > 0) {
1814-
$worstParallel = $latDeg2;
1815-
}
1816-
}
1817-
}
1818-
$dy = ($latDeg2 - $latDeg1);
18191798
if ($lonDeg1 < 0 && $lonDeg2 > 1) { $lonDeg1 += 360; }
18201799
if ($lonDeg2 < 0 && $lonDeg1 > 1) { $lonDeg2 += 360; }
1821-
$dx = ($lonDeg2 - $lonDeg1) * cos(deg2rad($worstParallel));
1822-
return sqrt($dx * $dx + $dy * $dy) * 1000000.0 / 9.0;
1800+
$dx = 111319.49079327 * ($lonDeg2 - $lonDeg1) * cos(deg2rad(($latDeg1 + $latDeg2) / 2.0));
1801+
$dy = 110946.25213273 * ($latDeg2 - $latDeg1);
1802+
return sqrt($dx * $dx + $dy * $dy);
18231803
}
18241804

18251805
/// PUBLIC convert a mapcode (without territory abbreviation) into a particular alphabet

mapcode_library_php.doc

8.5 KB
Binary file not shown.

unittest/unittest.php

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function progress(id,x,total) {
2121
include 'test_territories.php';
2222
include 'test_encodes.php';
2323

24-
echo "Mapcode Unittest version 2.1.0<BR>";
24+
echo "Mapcode Unittest version 2.1.1<BR>";
2525
echo "Mapcode PHP version " . mapcode_phpversion . "<BR>";
2626
echo "Mapcode DATA version " . mapcode_dataversion . "<BR>";
2727
if ($redivar) echo "Mapcode fast_encode loaded<BR>";
@@ -349,37 +349,73 @@ function test_encodes()
349349
}
350350
}
351351

352+
function distance_tests()
353+
{
354+
if (mapcode_phpversion>='2.1.1') {
355+
$coordpairs = array(
356+
// lat1, lon1, lat2, lon2, expected distance * 100000
357+
// lat1, lon1, lat2, lon2, expected distance * 100000
358+
1,1,1,1,0,
359+
0,0,0,1,11131949079,
360+
89,0,89,1,194279300,
361+
3,0,3,1,11116693130,
362+
-3,0,-3,1,11116693130,
363+
-3,-179.5,-3,179.5,11116693130,
364+
-3,179.5,-3,-179.5,11116693130,
365+
3,8,3,9,11116693130,
366+
3,-8,3,-9,11116693130,
367+
3,-0.5,3,0.5,11116693130,
368+
54,5,54.000001,5,11095,
369+
54,5,54,5.000001,6543,
370+
54,5,54.000001,5.000001,12880,
371+
90,0,90,50,0,
372+
0.11,0.22,0.12,0.2333,185011466,
373+
-1);
374+
375+
for($i=0;$coordpairs[$i]!=-1;$i+=5) {
376+
$GLOBALS['nrTests']++;
377+
$distance = distanceInMeters(
378+
$coordpairs[$i],$coordpairs[$i+1],
379+
$coordpairs[$i+2],$coordpairs[$i+3]);
380+
if ( floor(0.5+(100000.0 * $distance)) != $coordpairs[$i+4] ) {
381+
$GLOBALS['nrErrors']++;
382+
echo '*** ERROR *** distanceInMeters '.$i.' failed: '.$distance.'<BR>';
383+
}
384+
}
385+
}
386+
}
387+
352388

353389
$next_corner_to_test = 0;
354390
function test_corner_encodes()
355391
{
356-
$tests_per_timeslot = 5;
357-
$last = dataLastRecord(ccode_earth);
358-
for ($m=$GLOBALS['next_corner_to_test']; $m<$last; $m++) {
359-
if ($GLOBALS['nrErrors']>20) {
360-
echo 'Too many errors!<BR>';
361-
return 0;
362-
}
363-
if ($tests_per_timeslot-- == 0) {
364-
$GLOBALS['next_corner_to_test'] = $m;
392+
$tests_per_timeslot = 5;
393+
$last = dataLastRecord(ccode_earth);
394+
for ($m=$GLOBALS['next_corner_to_test']; $m<$last; $m++) {
395+
if ($GLOBALS['nrErrors']>20) {
396+
echo 'Too many errors!<BR>';
397+
return 0;
398+
}
399+
if ($tests_per_timeslot-- == 0) {
400+
$GLOBALS['next_corner_to_test'] = $m;
365401
echo '<script>progress("prog1",'.$m.','.$last.');</script>';
366-
return 1;
367-
}
368-
$mm = minmaxSetup($m);
402+
return 1;
403+
}
404+
$mm = minmaxSetup($m);
369405
// center
370-
test_encode_decode( "", ($mm->miny+$mm->maxy)/2000000, ($mm->minx+$mm->maxx)/2000000, 0,0 );
406+
test_encode_decode( "", ($mm->miny+$mm->maxy)/2000000, ($mm->minx+$mm->maxx)/2000000, 0,0 );
371407
// corner just inside
372-
test_encode_decode( "", $mm->miny/1000000.0, $mm->minx/1000000.0, 0,0 );
408+
test_encode_decode( "", $mm->miny/1000000.0, $mm->minx/1000000.0, 0,0 );
373409
// corner just outside y
374-
test_encode_decode( "", ($mm->miny-0.000001)/1000000.0, ($mm->minx)/1000000.0, 0,0 );
410+
test_encode_decode( "", ($mm->miny-0.000001)/1000000.0, ($mm->minx)/1000000.0, 0,0 );
375411
// corner just outside x
376-
test_encode_decode( "", ($mm->miny)/1000000.0, ($mm->minx-0.000001)/1000000.0, 0,0 );
377-
// corner opposite just inside
378-
test_encode_decode( "", ($mm->maxy-0.000001)/1000000.0, ($mm->maxx-0.000001)/1000000.0, 0,0 );
379-
// corner opposite just outside
380-
test_encode_decode( "", ($mm->maxy)/1000000.0, ($mm->maxx)/1000000.0, 0,0 );
381-
}
382-
return 0;
412+
test_encode_decode( "", ($mm->miny)/1000000.0, ($mm->minx-0.000001)/1000000.0, 0,0 );
413+
// corner opposite just inside
414+
test_encode_decode( "", ($mm->maxy-0.000001)/1000000.0, ($mm->maxx-0.000001)/1000000.0, 0,0 );
415+
// corner opposite just outside
416+
test_encode_decode( "", ($mm->maxy)/1000000.0, ($mm->maxx)/1000000.0, 0,0 );
417+
}
418+
return 0;
383419
}
384420

385421

@@ -388,6 +424,9 @@ function test_corner_encodes()
388424
echo '<HR>Character tests<BR>';
389425
alphabet_tests();
390426

427+
echo '<HR>Distance tests<BR>';
428+
distance_tests();
429+
391430
echo '<HR>Territory tests<BR>';
392431
echo MAX_CCODE . " territories<BR>";
393432
test_territories(); // uses test_territory()

0 commit comments

Comments
 (0)