Skip to content

Commit 3322e7e

Browse files
2.0.2. Fast encoding of coordinates
1 parent 2955da1 commit 3322e7e

File tree

3 files changed

+791
-34
lines changed

3 files changed

+791
-34
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ limitations under the License.
2525

2626
# PHP Files for Mapcode Support
2727

28-
mapcode_data.php - Data table for mapcode support
29-
mapcode_func.php - Key routines for mapcode support
30-
31-
mapcode_ctrynams.php - Optional array with english territory names
28+
mapcode_data.php - Data table for mapcode support
29+
mapcode_func.php - Key routines for mapcode support
30+
mapcode_fast_encode.php - Data for fast encoding of coordinates
31+
mapcode_ctrynams.php - Optional array with english territory names
3232

3333
sample.php - Sample php code to interpret or generate mapcodes
3434
(upload all 4 files to a server, then open sample.php in a web browser)
@@ -42,6 +42,12 @@ limitations under the License.
4242

4343
# Version History
4444

45+
* 2.0.2
46+
47+
Ported fast_encode from C library (4x faster global encoding);
48+
49+
Minor improvements (stricter tests);
50+
4551
* 2.0.0
4652

4753
Initial open source release. (The release starts at 2.0.0 because the

mapcode_api.php

Lines changed: 62 additions & 30 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.0.0');
19+
define('mapcode_phpversion', '2.0.2');
2020

2121
$xdivider19 = array(
2222
360, 360, 360, 360, 360, 360, 361, 361, 361, 361,
@@ -66,7 +66,7 @@ function parentname2($disam)
6666

6767
function parentletter($isocode)
6868
{
69-
$p = -1;
69+
$p = false;
7070
$srch = $isocode . ",";
7171
$len = strlen($srch);
7272
if ($len == 3) {
@@ -76,7 +76,7 @@ function parentletter($isocode)
7676
$p = stripos(parents3, $srch);
7777
}
7878
}
79-
if ($p < 0) {
79+
if ($p === false) {
8080
return -2;
8181
}
8282
return 1 + ($p / $len);
@@ -164,8 +164,8 @@ function iso2ccode($territory)
164164

165165
if (strlen($isocode) == 2) {
166166
$isocode = $GLOBALS['disambiguate'] . $isocode;
167-
}
168-
167+
}
168+
169169
{
170170
if (strlen($isocode) == 3) {
171171
$isoa = alias2iso($isocode);
@@ -195,7 +195,7 @@ function iso2ccode($territory)
195195

196196
$a = alias2iso($testiso);
197197
if (strlen($a)) {
198-
return iso2ccode($a);
198+
return iso2ccode($a);
199199
}
200200

201201
if (strlen($isocode) == 2) {
@@ -219,8 +219,7 @@ function getTerritoryNumber($territory, $contextTerritoryNumber = -1)
219219
if ($contextTerritoryNumber >= 0) {
220220
set_disambiguate($GLOBALS['entity_iso'][$contextTerritoryNumber]);
221221
}
222-
$nr = iso2ccode($territory);
223-
return $nr;
222+
return iso2ccode($territory);
224223
}
225224

226225
/// PUBLIC - return name of $territory (optional keepindex=1 for bracketed aliases)
@@ -285,7 +284,7 @@ function getTerritoryAlphaCode($territory, $international = 1)
285284
{
286285
$territoryNumber = getTerritoryNumber($territory);
287286
if ($territoryNumber < 0 || $territoryNumber > ccode_earth) {
288-
return -1;
287+
return -1;
289288
}
290289
$n = $GLOBALS['entity_iso'][$territoryNumber];
291290
if (startsdigit($n)) {
@@ -571,7 +570,7 @@ function decodeExtension($extensionchars, $Coord, $dividerx4, $dividery, $ydirec
571570
while ($idx < $len) {
572571
$halfcolumn = 0;
573572
$c = decodeChar($extensionchars[$idx++]);
574-
if ($c < 0 || $c > 30) {
573+
if ($c < 0 || $c == 30) {
575574
return 0;
576575
}
577576
$row1 = (int)($c / 5);
@@ -945,17 +944,18 @@ function aeu_unpack($str)
945944
return '';
946945
}
947946

948-
for ($v = 0; $v <= $lastpos; $v++) {
947+
$nrletters = 0;
948+
for ($v = 0; $v <= $lastpos; $v++) {
949949
if ($v != $dotpos) {
950950
if (decodeChar($str[$v]) < 0) {
951951
return '';
952-
} else {
953-
if ($voweled && decodeChar($str[$v]) > 9) {
954-
return '';
955-
}
952+
} else if (decodeChar($str[$v]) > 9) {
953+
$nrletters++;
956954
}
957955
}
958956
}
957+
if (!$voweled && !$nrletters) return '';
958+
if ($voweled && $nrletters) return '';
959959

960960
return $str;
961961
}
@@ -993,7 +993,7 @@ function aeu_pack($r, $short = 0)
993993
return $r . $rest;
994994
}
995995

996-
define('MAXLANS', 14);
996+
define('MAPCODE_ALPHABETS_TOTAL', 14);
997997

998998
$lannam = array(
999999
"Roman",
@@ -1085,7 +1085,7 @@ function to_ascii($str)
10851085
$result .= chr($c);
10861086
} else {
10871087
$found = 0;
1088-
for ($lan = 0; $lan < MAXLANS; $lan++) {
1088+
for ($lan = 0; $lan < MAPCODE_ALPHABETS_TOTAL; $lan++) {
10891089
for ($j = 0; $j < 36; $j++) {
10901090
if ($c == $GLOBALS['asc2lan'][$lan][$j]) {
10911091
$result .= $letters[$j];
@@ -1102,8 +1102,9 @@ function to_ascii($str)
11021102
}
11031103
}
11041104
}
1105-
if ($result[0] == 'A') {
1106-
$result = aeu_pack(aeu_unpack($result));
1105+
$p = strrpos($result,' '); if ($p===false) $p=0; else $p++;
1106+
if ($result[$p] == 'A') {
1107+
$result = substr($result,0,$p) . aeu_pack(aeu_unpack(substr($result,$p)));
11071108
}
11081109
return $result;
11091110
}
@@ -1192,7 +1193,7 @@ function master_decode($mapcode, $territoryNumber = -1)
11921193
$postfixlength = $mclen - 1 - $prefixlength;
11931194
$incodex = $prefixlength * 10 + $postfixlength;
11941195

1195-
$result = null;
1196+
$result = 0;
11961197
for ($m = $from; $m <= $upto; $m++) {
11971198
$codexm = Codex($m);
11981199

@@ -1210,7 +1211,7 @@ function master_decode($mapcode, $territoryNumber = -1)
12101211
}
12111212
}
12121213
if ($fitssomewhere == 0) {
1213-
$result = null;
1214+
$result = 0;
12141215
}
12151216
}
12161217
break;
@@ -1227,7 +1228,7 @@ function master_decode($mapcode, $territoryNumber = -1)
12271228
$result = decodeAutoHeader($mapcode, $extensionchars, $m);
12281229
break;
12291230
} else {
1230-
$result = null;
1231+
$result = 0;
12311232
}
12321233
}
12331234
}
@@ -1255,7 +1256,7 @@ function master_decode($mapcode, $territoryNumber = -1)
12551256
$result->lat = 90;
12561257
}
12571258
}
1258-
return $result;
1259+
return $result;
12591260
}
12601261

12611262
function decode($mapcodeString, $territory = -1)
@@ -1265,7 +1266,7 @@ function decode($mapcodeString, $territory = -1)
12651266
if ($contextTerritoryNumber < 0) {
12661267
$contextTerritoryNumber = ccode_earth;
12671268
}
1268-
1269+
12691270
$p = strpos($mapcodeString, ' ');
12701271
if ($p !== false) {
12711272
$territory = substr($mapcodeString, 0, $p);
@@ -1603,16 +1604,46 @@ function mapcoderEngine($enc, $tn, $getshortest, $isrecursive, $state_override,
16031604
{
16041605
$dsr = $GLOBALS['debugStopRecord'];
16051606
$results = array();
1607+
$use_redivar=0;
16061608

1607-
$fromTerritory = 0;
1608-
$uptoTerritory = ccode_earth;
1609-
if (is_numeric($tn) && $tn >= 0 && $tn <= $uptoTerritory) {
1610-
$fromTerritory = $tn;
1611-
$uptoTerritory = $tn;
1609+
$fromRun = 0;
1610+
$uptoRun = ccode_earth;
1611+
if (is_numeric($tn) && $tn >= 0 && $tn <= $uptoRun) {
1612+
$fromRun = $tn;
1613+
$uptoRun = $tn;
1614+
}
1615+
else if ($GLOBALS[redivar])
1616+
{
1617+
$use_redivar=1;
1618+
$HOR = 1;
1619+
$i = 0; // pointer into redivar
1620+
for (;;) {
1621+
$v2 = $GLOBALS[redivar][$i++];
1622+
$HOR = 1 - $HOR;
1623+
if ($v2 >= 0 && $v2 < 1024) { // leaf?
1624+
$fromRun = $i;
1625+
$uptoRun = $i + $v2;
1626+
break;
1627+
}
1628+
else {
1629+
$coord = ($HOR ? $enc->coord32->lon : $enc->coord32->lat);
1630+
if ($coord > $v2) {
1631+
$i = $GLOBALS[redivar][$i];
1632+
}
1633+
else {
1634+
$i++;
1635+
}
1636+
}
1637+
}
16121638
}
16131639

16141640
$debugStopFailed = 1;
1615-
for ($territoryNumber = $fromTerritory; $territoryNumber <= $uptoTerritory; $territoryNumber++) {
1641+
for ($run = $fromRun; $run <= $uptoRun; $run++) {
1642+
if ($use_redivar)
1643+
$territoryNumber = ($run == $uptoRun ? ccode_earth : $GLOBALS[redivar][$run]);
1644+
else
1645+
$territoryNumber = $run;
1646+
16161647
$original_length = count($results);
16171648
$from = dataFirstRecord($territoryNumber);
16181649
$upto = dataLastRecord($territoryNumber);
@@ -1752,3 +1783,4 @@ function encodeShortest($latitudeDegrees, $longitudeDegrees, $territory = -1)
17521783

17531784
?>
17541785

1786+

0 commit comments

Comments
 (0)