From e9af164c8a1061a3f2bbe5cde608aee2ddcf2ad3 Mon Sep 17 00:00:00 2001 From: int2001 Date: Wed, 13 Dec 2023 16:34:24 +0000 Subject: [PATCH] trim gridlocator before parsing it. Some Exports consist of trailing whitespaces at gridlocator :( --- application/libraries/Qra.php | 135 +++++++++++++++++----------------- 1 file changed, 68 insertions(+), 67 deletions(-) diff --git a/application/libraries/Qra.php b/application/libraries/Qra.php index 40fc8562..38359c06 100644 --- a/application/libraries/Qra.php +++ b/application/libraries/Qra.php @@ -173,78 +173,79 @@ function get_bearing($lat1, $lon1, $lat2, $lon2) { } function qra2latlong($strQRA) { - if (substr_count($strQRA, ',') > 0) { - if (substr_count($strQRA, ',') == 3) { - // Handle grid corners - $grids = explode(',', $strQRA); - $gridlengths = array(strlen($grids[0]), strlen($grids[1]), strlen($grids[2]), strlen($grids[3])); - $same = array_count_values($gridlengths); - if (count($same) != 1) { - return false; - } - $coords = array(0, 0); - for($i=0; $i<4; $i++) { - $cornercoords[$i] = qra2latlong($grids[$i]); - $coords[0] += $cornercoords[$i][0]; - $coords[1] += $cornercoords[$i][1]; - } - return array (round($coords[0]/4), round($coords[1]/4)); - } else if (substr_count($strQRA, ',') == 1) { - // Handle grid lines - $grids = explode(',', $strQRA); - if (strlen($grids[0]) != strlen($grids[1])) { - return false; - } - $coords = array(0, 0); - for($i=0; $i<2; $i++) { - $linecoords[$i] = qra2latlong($grids[$i]); - } - if ($linecoords[0][0] != $linecoords[1][0]) { - $coords[0] = round((($linecoords[0][0] + $linecoords[1][0]) / 2),1); - } else { - $coords[0] = round($linecoords[0][0],1); - } - if ($linecoords[0][1] != $linecoords[1][1]) { - $coords[1] = round(($linecoords[0][1] + $linecoords[1][1]) / 2); - } else { - $coords[1] = round($linecoords[0][1]); - } - return $coords; - } else { - return false; - } - } + $strQRA=trim($strQRA); + if (substr_count($strQRA, ',') > 0) { + if (substr_count($strQRA, ',') == 3) { + // Handle grid corners + $grids = explode(',', $strQRA); + $gridlengths = array(strlen($grids[0]), strlen($grids[1]), strlen($grids[2]), strlen($grids[3])); + $same = array_count_values($gridlengths); + if (count($same) != 1) { + return false; + } + $coords = array(0, 0); + for($i=0; $i<4; $i++) { + $cornercoords[$i] = qra2latlong($grids[$i]); + $coords[0] += $cornercoords[$i][0]; + $coords[1] += $cornercoords[$i][1]; + } + return array (round($coords[0]/4), round($coords[1]/4)); + } else if (substr_count($strQRA, ',') == 1) { + // Handle grid lines + $grids = explode(',', $strQRA); + if (strlen($grids[0]) != strlen($grids[1])) { + return false; + } + $coords = array(0, 0); + for($i=0; $i<2; $i++) { + $linecoords[$i] = qra2latlong($grids[$i]); + } + if ($linecoords[0][0] != $linecoords[1][0]) { + $coords[0] = round((($linecoords[0][0] + $linecoords[1][0]) / 2),1); + } else { + $coords[0] = round($linecoords[0][0],1); + } + if ($linecoords[0][1] != $linecoords[1][1]) { + $coords[1] = round(($linecoords[0][1] + $linecoords[1][1]) / 2); + } else { + $coords[1] = round($linecoords[0][1]); + } + return $coords; + } else { + return false; + } + } - if ((strlen($strQRA) % 2 == 0) && (strlen($strQRA) <= 10)) { // Check if QRA is EVEN (the % 2 does that) and smaller/equal 8 - $strQRA = strtoupper($strQRA); - if (strlen($strQRA) == 4) $strQRA .= "LL"; // Only 4 Chars? Fill with center "LL" as only A-R allowed - if (strlen($strQRA) == 6) $strQRA .= "55"; // Only 6 Chars? Fill with center "55" - if (strlen($strQRA) == 8) $strQRA .= "LL"; // Only 8 Chars? Fill with center "LL" as only A-R allowed + if ((strlen($strQRA) % 2 == 0) && (strlen($strQRA) <= 10)) { // Check if QRA is EVEN (the % 2 does that) and smaller/equal 8 + $strQRA = strtoupper($strQRA); + if (strlen($strQRA) == 4) $strQRA .= "LL"; // Only 4 Chars? Fill with center "LL" as only A-R allowed + if (strlen($strQRA) == 6) $strQRA .= "55"; // Only 6 Chars? Fill with center "55" + if (strlen($strQRA) == 8) $strQRA .= "LL"; // Only 8 Chars? Fill with center "LL" as only A-R allowed - if (!preg_match('/^[A-R]{2}[0-9]{2}[A-X]{2}[0-9]{2}[A-X]{2}$/', $strQRA)) { - return false; - } + if (!preg_match('/^[A-R]{2}[0-9]{2}[A-X]{2}[0-9]{2}[A-X]{2}$/', $strQRA)) { + return false; + } - list($a, $b, $c, $d, $e, $f, $g, $h, $i, $j) = str_split($strQRA, 1); // Maidenhead is always alternating. e.g. "AA00AA00AA00" - doesn't matter how deep. 2 chars, 2 numbers, etc. - $a = ord($a) - ord('A'); - $b = ord($b) - ord('A'); - $c = ord($c) - ord('0'); - $d = ord($d) - ord('0'); - $e = ord($e) - ord('A'); - $f = ord($f) - ord('A'); - $g = ord($g) - ord('0'); - $h = ord($h) - ord('0'); - $i = ord($i) - ord('A'); - $j = ord($j) - ord('A'); + list($a, $b, $c, $d, $e, $f, $g, $h, $i, $j) = str_split($strQRA, 1); // Maidenhead is always alternating. e.g. "AA00AA00AA00" - doesn't matter how deep. 2 chars, 2 numbers, etc. + $a = ord($a) - ord('A'); + $b = ord($b) - ord('A'); + $c = ord($c) - ord('0'); + $d = ord($d) - ord('0'); + $e = ord($e) - ord('A'); + $f = ord($f) - ord('A'); + $g = ord($g) - ord('0'); + $h = ord($h) - ord('0'); + $i = ord($i) - ord('A'); + $j = ord($j) - ord('A'); - $nLong = ($a*20) + ($c*2) + ($e/12) + ($g/120) + ($i/2880) - 180; - $nLat = ($b*10) + $d + ($f/24) + ($h/240) + ($j/5760) - 90; + $nLong = ($a*20) + ($c*2) + ($e/12) + ($g/120) + ($i/2880) - 180; + $nLat = ($b*10) + $d + ($f/24) + ($h/240) + ($j/5760) - 90; - $arLatLong = array($nLat, $nLong); - return $arLatLong; - } else { - return array(0, 0); - } + $arLatLong = array($nLat, $nLong); + return $arLatLong; + } else { + return array(0, 0); + } }