Skip to content

Instantly share code, notes, and snippets.

@netsgnut
Last active October 16, 2017 16:39
Show Gist options
  • Save netsgnut/5907731 to your computer and use it in GitHub Desktop.
Save netsgnut/5907731 to your computer and use it in GitHub Desktop.
An utility function for converting coordinates from HK80 to WGS84. Dug from an ancient project.
<?php
/**
* Coordinates.php
* An utility for converting HK80 to WGS84 coordinates
*
* Copyright (C) 2010, 2013 Kelvin Wong
* Licensed under the MIT License (http://opensource.org/licenses/MIT)
*
* Note: For the glossaries and conventions for coordinates convertion, please refer to
* the website of Geodetic Survey of Hong Kong.
*
* References:
* - http://www.geodetic.gov.hk/smo/gsi/programs/en/GSS/grid/grid_index.htm
* - http://www.geodetic.gov.hk/smo/gsi/data/parameter/7P_ITRF96_HK80_V1.pdf
* - http://www.geodetic.gov.hk/data/parameter/SchematicDiagram.pdf
*/
class Coordinates {
// Reference ellipsoid of HK80 (based on International 1910 / Transverse Mercator)
static $a = 6378388; // Semi-major axis
static $f = 0.0033670033670034; // Flattening
static $e2 = 0.0067226700223333; // First eccentricity (e^2 = 2f - f^2)
static $m0 = 1; // Scale factor along central meridian
// Reference point of HK80 (Patridge Hill Trigonometric Station)
static $lat0 = 0.38942018399288; // deg2rad( 22d 18' 43.68" N)
static $lng0 = 1.99279173737270; // deg2rad(114d 10' 42.80" E)
static $N0 = 819069.80;
static $E0 = 836694.05;
/**
* doBisectIter
* @desc find the root of $f($x) = 0 by bisection method
* @param f the function to be evaluated
* @param x1 the starting value
* @param x2 the ending value
* @param epsilon precision
* @return the root got by the iteration
*/
static function doBisectIter($f, $x1, $x2, $epsilon = 0.00000001) {
$y1 = $f($x1);
$y2 = $f($x2);
// Ensure the signs are opposite
if ($y1 * $y2 > 0)
return false;
// Ensure the y1 must be the smaller side
if ($y1 > 0)
return call_user_func(array('self', __FUNCTION__), $f, $x2, $x1, $epsilon);
$x = ($x1 + $x2) / 2;
$y = $f($x);
if (abs($y) <= $epsilon)
return $x;
return call_user_func(array('self', __FUNCTION__), $f, ($y<0)?($x):($x1), ($y>0)?($x):($x2), $epsilon);
}
/**
* getMeridianDist
* @param lat latitude
* @return meridian distance
*/
static function getMeridianDist($lat) {
$a = self::$a;
$e2 = self::$e2;
$A0 = 1 - $e2 / 4 - 3 * $e2 * $e2 / 64;
$A2 = 3 * ($e2 + $e2 * $e2 / 4) / 8;
$A4 = 15 * $e2 * $e2 / 256;
return $a * ($A0 * $lat - $A2 * sin(2*$lat) - $A4 * sin(4*$lat));
}
/**
* convertHK80GridToCartesian
* @param hk1980 grid reference in HK80, in terms of n and e
* @return corresponding Cartesian co-ordinates
*/
static function convertHK80GridToCartesian($hk1980) {
$N = $hk1980['n'];
$E = $hk1980['e'];
// Constants
$a = self::$a;
$e2 = self::$e2;
$N0 = self::$N0;
$E0 = self::$E0;
$m0 = self::$m0;
$lat0 = self::$lat0;
$lng0 = self::$lng0;
// Calculate dN and dE
$dN = $N - $N0;
$dE = $E - $E0;
// Calculate meridian distance
$M0 = self::getMeridianDist($lat0);
$M = ($dN + $M0) / $m0;
// Step 1: HK80 Grid => HK80 Geographic Co-ordinates
// Newtonian iteration to get the latitude reference evaluated, given meridian distance
$latP = self::doBisectIter(
create_function('$x', 'return '.$M.' - Coordinates::getMeridianDist($x);'),
-10000, 10000);
// By the latitude reference, the v (radius of curvature in prime vertical),
// p (that in meridian) and phi (isometric latitude) at point P are evaluated
$vP = $a / pow(1 - $e2 * pow(sin($latP), 2), 0.5);
$pP = $a * (1 - $e2) / pow(1 - $e2 * pow(sin($latP), 2), 1.5);
$phiP = $vP / $pP;
// The latitude/longitude (at HK1980) is evaluated by the radius of curvature
$lat = $latP - tan($latP) * pow($dE/$m0, 2) / (2*$pP*$vP);
$lng = $lng0 + $dE / ($m0*$vP * cos($latP)) - (pow($dE, 3) / (6 * pow($m0*$vP, 3) * cos($latP)))*($phiP + 2 * pow(tan($latP), 2));
// Step 2: HK80 Geographic => WGS84 Geographic Co-ordinates
// This is the lazy and fast way, suitable for general usage
// This uses a rule of thumb to convert coordinate from HK80 to WGS84 directly
// TODO: Implement the proper way :P
return array(
'lat' => rad2deg($lat) - 5.5/3600,
'lng' => rad2deg($lng) + 8.8/3600
);
}
};
?>
Copyright (c) 2010, 2013 Kelvin Wong
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.
@kwunyeung
Copy link

Thanks! This is extremely useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment