Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jDanek committed Aug 16, 2018
0 parents commit 3483118
Show file tree
Hide file tree
Showing 10 changed files with 555 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea/*
/vendor
/composer.lock
/phpunit.xml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Toolia

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# HamDx
A utility for working with the QTH Maidenhead grid system

#### Convertor class
- convert QTH locator to Lat/Lon
- convert Lat/Lon to QTH locator
- convert Lat/Lon to DMS (Degrees, Minutes, Seconds)
- convert DMS to Lat/Lon

#### Comparator class
- calculate distance between two Lat/Lon points (in km)
- calculate azimuth
25 changes: 25 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "toolia/ham-dx",
"description": "A utility for working with the Maidenhead grid system",
"license": "MIT",
"authors": [
{
"name": "Jiří Daněk",
"email": "jirka.danek@jdanek.eu",
"homepage": "https://jdanek.eu",
"role": "Developer"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"autoload": {
"psr-4": {
"Toolia\\HamDx\\": "src"
}
}
}
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="HamDx">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
51 changes: 51 additions & 0 deletions src/Comparator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types=1);

namespace Toolia\HamDx;

/**
* Class Comparator
*
* @author Jirka Daněk <jdanek.eu>
*/
class Comparator
{
/**
* Return calculated distance between two latitude and longitude points (in KM)
*
* @param float $lat1
* @param float $lon1
* @param float $lat2
* @param float $lon2
* @return float
*/
function between(float $lat1, float $lon1, float $lat2, float $lon2): float
{
$rad = M_PI / 180;
$lonDelta = $lon2 - $lon1;
$dist = sin($lat1 * $rad) * sin($lat2 * $rad) + cos($lat1 * $rad) * cos($lat2 * $rad) * cos($lonDelta * $rad);

return round(acos($dist) / $rad * 60 * 1.853);
}

/**
* Return calculated azimuth from latitude and longitude
*
* @param float $lat1
* @param float $lon1
* @param float $lat2
* @param float $lon2
* @return float
*/
function azimut(float $lat1, float $lon1, float $lat2, float $lon2): float
{
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);

$lonDelta = $lon2 - $lon1;
$azimut = rad2deg(atan(sin($lonDelta) * cos($lat2) / (sin($lat2) * cos($lat1) - cos($lat2) * sin($lat1) * cos($lonDelta))));
$azimut = ($lat1 > $lat2 ? (180.0 + $azimut) : $azimut);
return $azimut;
}
}
150 changes: 150 additions & 0 deletions src/Convertor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php declare(strict_types=1);

namespace Toolia\HamDx;

/**
* Class Convertor
*
* @author Jirka Daněk <jdanek.eu>
*/
class Convertor
{
const ROUND_PRECISION = 10;

/** @var array */
protected $valid_chars = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X'];

/**
* @param float $number
* @param int $precision
* @return float
*/
static function roundUp(float $number, int $precision = 2): float
{
$fig = (int)str_pad('1', $precision + 1, '0');
return (ceil($number * $fig) / $fig);
}

/**
* @param $number
* @param int $precision
* @return float
*/
static function roundDown(float $number, int $precision = 2): float
{
$fig = (int)str_pad('1', $precision + 1, '0');
return (floor($number * $fig) / $fig);
}


/**
* Convert QTH Maidenhead Grid locator to lat/lon
* Help & inspiration: http://radio.snezka.net
*
* @param string $locator
* @return array [latitude, longitude]
*/
function qthToCoords(string $locator): array
{
$flip = array_flip($this->valid_chars);
$locator = strtoupper($locator);

if (strlen($locator) === 2) $locator .= "55MM00AA"; // add center for 'field'
if (strlen($locator) === 4) $locator .= "MM00AA"; // add center for 'square'
if (strlen($locator) === 6) $locator .= "55AA"; // add center for 'subsquare'
if (strlen($locator) === 8) $locator .= "MM"; // add center for 'extended subsquare'

// calculate latitude and longitude
$lon = -180.0;
$lat = -90.0;

$lon += ($flip[$locator[0]] * 20) // fields
+ ($locator[2] * 2) // squares
+ ($flip[$locator[4]] / 12) // subsquares
+ ($locator[6] / 120) // extended square
+ ($flip[$locator[8]] / 2880) // extended subsquare
+ 0.000174; // center

$lat += ($flip[$locator[1]] * 10) // fields
+ ($locator[3]) // squares
+ ($flip[$locator[5]] / 24) // subsquares
+ ($locator[7] / 240) // extended square
+ ($flip[$locator[9]] / 5760) // extended subsquare
+ 0.0000868; // center

return [$lat, $lon];
}

/**
* Convert lat/lon to QTH Maidenhead Grid locator
* Help & inspiration: http://radio.snezka.net
*
* @param float $lat
* @param float $lon
* @return string
*/
function coordsToQth(float $lat, float $lon): string
{
return $this->valid_chars[floor(($lon + 180) / 20)]
. $this->valid_chars[floor(($lat + 90) / 10)]
. floor(fmod(($lon + 180) / 2, 10))
. floor(fmod($lat + 90, 10))
. $this->valid_chars[floor(fmod(($lon + 180) * 12, 24))]
. $this->valid_chars[floor(fmod(($lat + 90) * 24, 24))]
. floor(fmod(($lon + 180) * 120, 10))
. floor(fmod(($lat + 90) * 240, 10))
. strtolower($this->valid_chars[floor(fmod(($lon + 180) * 2880, 24))])
. strtolower($this->valid_chars[floor(fmod(($lat + 90) * 5760, 24))]);
}

/**
* Convert latitude and longitude location to degrees, minutes, seconds
*
* @param float $lat
* @param float $lon
* @return array[lat, lon]
*/
function coordsToDms(float $lat, float $lon): array
{
return [
$this->decimalToDms($lat),
$this->decimalToDms($lon, false)
];
}

/**
* Convert decimal to degrees, minutes, seconds
*
* @param float $decimal
* @param bool $isLatitude
* @return array
*/
function decimalToDms(float $decimal, bool $isLatitude = true): array
{
$exploded = explode(".", (string)$decimal);
$temp = '0.' . (isset($exploded[1]) ? $exploded[1] : 0);

$temp *= 3600;
$deg = (float)$exploded[0];
$min = floor($temp / 60);
$sec = floor($temp - ($min * 60));

$direction = ($isLatitude ? ($deg < 0 ? 'S' : 'N') : ($deg < 0 ? 'W' : 'E'));

return ['dir' => $direction, 'deg' => $deg, 'min' => $min, 'sec' => $sec];
}

/**
* Convert degrees, minutes, seconds to decimal
*
* @param float $deg
* @param float $min
* @param float $sec
* @return float
*/
function dmsToDecimal(float $deg, float $min, float $sec): float
{
return static::roundUp(($deg + ((($min * 60) + ($sec)) / 3600)), static::ROUND_PRECISION);
}

}
Loading

0 comments on commit 3483118

Please sign in to comment.