Skip to content

Commit

Permalink
Merge pull request #3 from pjeweb/master
Browse files Browse the repository at this point in the history
Autoloading of projection classes
  • Loading branch information
judgej committed Apr 27, 2015
2 parents d82169b + 4a04a90 commit 312a9f3
Show file tree
Hide file tree
Showing 19 changed files with 651 additions and 757 deletions.
185 changes: 82 additions & 103 deletions src/Common.php

Large diffs are not rendered by default.

149 changes: 72 additions & 77 deletions src/Datum.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

/**
* Author : Julien Moquet, Jason Judge
*
*
* Inspired by Proj4js from Mike Adair madairATdmsolutions.ca
* and Richard Greenwood rich@greenwoodma$p->com
* License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
* and Richard Greenwood rich@greenwoodma$p->com
* License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
*/

use Academe\Proj4Php\Proj4 as Proj4Php;
Expand All @@ -20,28 +20,28 @@ class Datum {
public $datum_params;

/**
* @param type $proj
* @param type $proj
*/
public function __construct($proj)
{
public function __construct($proj) {
$this->datum_type = Proj4php::$common->PJD_WGS84; //default setting

if( isset($proj->datumCode) && $proj->datumCode == 'none' ) {
if (isset($proj->datumCode) && $proj->datumCode == 'none') {
$this->datum_type = Proj4Php::$common->PJD_NODATUM;
}

if( isset( $proj->datum_params ) ) {
for( $i = 0; $i < sizeof( $proj->datum_params ); $i++ ) {
$proj->datum_params[$i] = floatval( $proj->datum_params[$i] );
if (isset($proj->datum_params)) {
for ($i = 0; $i < sizeof($proj->datum_params); $i++) {
$proj->datum_params[$i] = floatval($proj->datum_params[$i]);
}

if( $proj->datum_params[0] != 0 || $proj->datum_params[1] != 0 || $proj->datum_params[2] != 0 ) {
if ($proj->datum_params[0] != 0 || $proj->datum_params[1] != 0 || $proj->datum_params[2] != 0) {
$this->datum_type = Proj4php::$common->PJD_3PARAM;
}

if( sizeof( $proj->datum_params ) > 3 ) {
if( $proj->datum_params[3] != 0 || $proj->datum_params[4] != 0 ||
$proj->datum_params[5] != 0 || $proj->datum_params[6] != 0 ) {
if (sizeof($proj->datum_params) > 3) {
if ($proj->datum_params[3] != 0 || $proj->datum_params[4] != 0 ||
$proj->datum_params[5] != 0 || $proj->datum_params[6] != 0
) {

$this->datum_type = Proj4php::$common->PJD_7PARAM;
$proj->datum_params[3] *= Proj4php::$common->SEC_TO_RAD;
Expand All @@ -66,12 +66,12 @@ public function __construct($proj)
/**
* @param type $dest
* @return boolean Returns TRUE if the two datums match, otherwise FALSE.
* @throws type
* @throws type
*/
public function compare_datums( $dest ) {
public function compare_datums($dest) {
if ($this->datum_type != $dest->datum_type) {
return false; // false, datums are not equal
} else if ($this->a != $dest->a || abs( $this->es - $dest->es ) > 0.000000000050) {
} else if ($this->a != $dest->a || abs($this->es - $dest->es) > 0.000000000050) {
// the tolerence for es is to ensure that GRS80 and WGS84
// are considered identical
return false;
Expand Down Expand Up @@ -112,11 +112,10 @@ public function compare_datums( $dest ) {
* Z : Calculated Geocentric Z coordinate, in meters (output)
*
*/
public function geodetic_to_geocentric($p)
{
public function geodetic_to_geocentric($p) {
$Longitude = $p->x;
$Latitude = $p->y;
$Height = isset( $p->z ) ? $p->z : 0; //Z value not always supplied
$Height = isset($p->z) ? $p->z : 0; //Z value not always supplied
$Error_Code = 0; // GEOCENT_NO_ERROR;

/*
Expand All @@ -138,12 +137,12 @@ public function geodetic_to_geocentric($p)
$Longitude -= (2 * Proj4php::$common->PI);
}

$Sin_Lat = sin( $Latitude ); /* sin(Latitude) */
$Cos_Lat = cos( $Latitude ); /* cos(Latitude) */
$Sin_Lat = sin($Latitude); /* sin(Latitude) */
$Cos_Lat = cos($Latitude); /* cos(Latitude) */
$Sin2_Lat = $Sin_Lat * $Sin_Lat; /* Square of sin(Latitude) */
$Rn = $this->a / (sqrt( 1.0e0 - $this->es * $Sin2_Lat )); /* Earth radius at location */
$p->x = ($Rn + $Height) * $Cos_Lat * cos( $Longitude );
$p->y = ($Rn + $Height) * $Cos_Lat * sin( $Longitude );
$Rn = $this->a / (sqrt(1.0e0 - $this->es * $Sin2_Lat)); /* Earth radius at location */
$p->x = ($Rn + $Height) * $Cos_Lat * cos($Longitude);
$p->y = ($Rn + $Height) * $Cos_Lat * sin($Longitude);
$p->z = (($Rn * (1 - $this->es)) + $Height) * $Sin_Lat;

return $Error_Code;
Expand All @@ -152,10 +151,9 @@ public function geodetic_to_geocentric($p)
/**
*
* @param object $p
* @return type
* @return type
*/
public function geocentric_to_geodetic($p)
{
public function geocentric_to_geodetic($p) {
// local defintions and variables
// end-criterium of loop, accuracy of sin(Latitude)
$genau = 1.E-12;
Expand All @@ -166,28 +164,28 @@ public function geocentric_to_geodetic($p)
$Z = $p->z ? $p->z : 0.0; //Z value not always supplied

/*
$P; // distance between semi-minor axis and location
$P; // distance between semi-minor axis and location
$RR; // distance between center and location
$CT; // sin of geocentric latitude
$ST; // cos of geocentric latitude
$CT; // sin of geocentric latitude
$ST; // cos of geocentric latitude
$RX;
$RK;
$RN; // Earth radius at location
$CPHI0; // cos of start or old geodetic latitude in iterations
$SPHI0; // sin of start or old geodetic latitude in iterations
$RN; // Earth radius at location
$CPHI0; // cos of start or old geodetic latitude in iterations
$SPHI0; // sin of start or old geodetic latitude in iterations
$CPHI; // cos of searched geodetic latitude
$SPHI; // sin of searched geodetic latitude
$SDPHI; // end-criterium: addition-theorem of sin(Latitude(iter)-Latitude(iter-1))
$At_Pole; // indicates location is in polar region
$iter; // of continous iteration, max. 30 is always enough (s.a.)
$SPHI; // sin of searched geodetic latitude
$SDPHI; // end-criterium: addition-theorem of sin(Latitude(iter)-Latitude(iter-1))
$At_Pole; // indicates location is in polar region
$iter; // of continous iteration, max. 30 is always enough (s.a.)
$Longitude;
$Latitude;
$Height;
*/

$At_Pole = false;
$P = sqrt( $X * $X + $Y * $Y );
$RR = sqrt( $X * $X + $Y * $Y + $Z * $Z );
$P = sqrt($X * $X + $Y * $Y);
$RR = sqrt($X * $X + $Y * $Y + $Z * $Z);

// special cases for latitude and longitude
if ($P / $this->a < $genau) {
Expand All @@ -197,15 +195,15 @@ public function geocentric_to_geodetic($p)

// if (X,Y,Z)=(0.,0.,0.) then Height becomes semi-minor axis
// of ellipsoid (=center of mass), Latitude becomes PI/2
if ( $RR / $this->a < $genau) {
if ($RR / $this->a < $genau) {
$Latitude = Proj4php::$common->HALF_PI;
$Height = -$this->b;
return;
}
} else {
/* ellipsoidal (geodetic) longitude
* interval: -PI < Longitude <= +PI */
$Longitude = atan2( $Y, $X );
$Longitude = atan2($Y, $X);
}

/* --------------------------------------------------------------
Expand All @@ -219,7 +217,7 @@ public function geocentric_to_geodetic($p)
*/
$CT = $Z / $RR;
$ST = $P / $RR;
$RX = 1.0 / sqrt( 1.0 - $this->es * (2.0 - $this->es) * $ST * $ST );
$RX = 1.0 / sqrt(1.0 - $this->es * (2.0 - $this->es) * $ST * $ST);
$CPHI0 = $ST * (1.0 - $this->es) * $RX;
$SPHI0 = $CT * $RX;
$iter = 0;
Expand All @@ -228,13 +226,13 @@ public function geocentric_to_geodetic($p)
// until |sin(Latitude(iter)-Latitude(iter-1))| < genau
do {
++$iter;
$RN = $this->a / sqrt( 1.0 - $this->es * $SPHI0 * $SPHI0 );
$RN = $this->a / sqrt(1.0 - $this->es * $SPHI0 * $SPHI0);

/* ellipsoidal (geodetic) height */
$Height = $P * $CPHI0 + $Z * $SPHI0 - $RN * (1.0 - $this->es * $SPHI0 * $SPHI0);

$RK = $this->es * $RN / ($RN + $Height);
$RX = 1.0 / sqrt( 1.0 - $RK * (2.0 - $RK) * $ST * $ST );
$RX = 1.0 / sqrt(1.0 - $RK * (2.0 - $RK) * $ST * $ST);
$CPHI = $ST * (1.0 - $RK) * $RX;
$SPHI = $CT * $RX;
$SDPHI = $SPHI * $CPHI0 - $CPHI * $SPHI0;
Expand All @@ -243,44 +241,43 @@ public function geocentric_to_geodetic($p)
} while ($SDPHI * $SDPHI > $genau2 && $iter < $maxiter);

// ellipsoidal (geodetic) latitude
$Latitude = atan( $SPHI / abs( $CPHI ) );
$Latitude = atan($SPHI / abs($CPHI));

$p->x = $Longitude;
$p->y = $Latitude;
$p->z = $Height;

return $p;
}

/**
/**
* Convert_Geocentric_To_Geodetic
* The method used here is derived from 'An Improved Algorithm for
* Geocentric to Geodetic Coordinate Conversion', by Ralph Toms, Feb 1996
*
*
* @param object Point $p
* @return object Point $p
*/
public function geocentric_to_geodetic_noniter($p)
{
public function geocentric_to_geodetic_noniter($p) {
/*
$Longitude;
$Latitude;
$Height;
$W; // distance from Z axis
$W2; // square of distance from Z axis
$T0; // initial estimate of vertical component
$T1; // corrected estimate of vertical component
$S0; // initial estimate of horizontal component
$W; // distance from Z axis
$W2; // square of distance from Z axis
$T0; // initial estimate of vertical component
$T1; // corrected estimate of vertical component
$S0; // initial estimate of horizontal component
$S1; // corrected estimate of horizontal component
$Sin_B0; // sin(B0), B0 is estimate of Bowring aux variable
$Sin3_B0; // cube of sin(B0)
$Sin_B0; // sin(B0), B0 is estimate of Bowring aux variable
$Sin3_B0; // cube of sin(B0)
$Cos_B0; // cos(B0)
$Sin_p1; // sin(phi1), phi1 is estimated latitude
$Cos_p1; // cos(phi1)
$Rn; // Earth radius at location
$Sum; // numerator of cos(phi1)
$At_Pole; // indicates location is in polar region
$Sin_p1; // sin(phi1), phi1 is estimated latitude
$Cos_p1; // cos(phi1)
$Rn; // Earth radius at location
$Sum; // numerator of cos(phi1)
$At_Pole; // indicates location is in polar region
*/

// cast from string to float
Expand Down Expand Up @@ -311,18 +308,18 @@ public function geocentric_to_geodetic_noniter($p)
}
}
$W2 = $X * $X + $Y * $Y;
$W = sqrt( $W2 );
$W = sqrt($W2);
$T0 = $Z * Proj4php::$common->AD_C;
$S0 = sqrt( $T0 * $T0 + $W2 );
$S0 = sqrt($T0 * $T0 + $W2);
$Sin_B0 = $T0 / $S0;
$Cos_B0 = $W / $S0;
$Sin3_B0 = $Sin_B0 * $Sin_B0 * $Sin_B0;
$T1 = $Z + $this->b * $this->ep2 * $Sin3_B0;
$Sum = $W - $this->a * $this->es * $Cos_B0 * $Cos_B0 * $Cos_B0;
$S1 = sqrt( $T1 * $T1 + $Sum * $Sum );
$S1 = sqrt($T1 * $T1 + $Sum * $Sum);
$Sin_p1 = $T1 / $S1;
$Cos_p1 = $Sum / $S1;
$Rn = $this->a / sqrt( 1.0 - $this->es * $Sin_p1 * $Sin_p1 );
$Rn = $this->a / sqrt(1.0 - $this->es * $Sin_p1 * $Sin_p1);
if ($Cos_p1 >= Proj4php::$common->COS_67P5) {
$Height = $W / $Cos_p1 - $Rn;
} else if ($Cos_p1 <= -Proj4php::$common->COS_67P5) {
Expand All @@ -331,21 +328,20 @@ public function geocentric_to_geodetic_noniter($p)
$Height = $Z / $Sin_p1 + $Rn * ($this->es - 1.0);
}
if ($At_Pole == false) {
$Latitude = atan( $Sin_p1 / $Cos_p1 );
$Latitude = atan($Sin_p1 / $Cos_p1);
}

$p->x = $Longitude;
$p->y = $Latitude;
$p->z = $Height;

return $p;
}

/************************************************************** */
// pj_geocentic_to_wgs84( p )
// p = point to transform in geocentric coordinates (x,y,z)
public function geocentric_to_wgs84 ($p)
{
public function geocentric_to_wgs84($p) {
if ($this->datum_type == Proj4php::$common->PJD_3PARAM) {
// if( x[io] == HUGE_VAL )
// continue;
Expand All @@ -362,8 +358,8 @@ public function geocentric_to_wgs84 ($p)
$M_BF = $this->datum_params[6];
// if( x[io] == HUGE_VAL )
// continue;
$p->x = $M_BF * ( $p->x - $Rz_BF * $p->y + $Ry_BF * $p->z) + $Dx_BF;
$p->y = $M_BF * ( $Rz_BF * $p->x + $p->y - $Rx_BF * $p->z) + $Dy_BF;
$p->x = $M_BF * ($p->x - $Rz_BF * $p->y + $Ry_BF * $p->z) + $Dx_BF;
$p->y = $M_BF * ($Rz_BF * $p->x + $p->y - $Rx_BF * $p->z) + $Dy_BF;
$p->z = $M_BF * (-$Ry_BF * $p->x + $Rx_BF * $p->y + $p->z) + $Dz_BF;
}
}
Expand All @@ -373,8 +369,7 @@ public function geocentric_to_wgs84 ($p)
// pj_geocentic_from_wgs84()
// coordinate system definition,
// point to transform in geocentric coordinates (x,y,z)
public function geocentric_from_wgs84($p)
{
public function geocentric_from_wgs84($p) {
if ($this->datum_type == Proj4php::$common->PJD_3PARAM) {
//if( x[io] == HUGE_VAL )
// continue;
Expand Down
6 changes: 2 additions & 4 deletions src/Defs/Epsg27700.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use Academe\Proj4Php\Proj4;

class Epsg27700
{
public function init()
{
class Epsg27700 {
public function init() {
// Add this entry to the static global. Not a good way to handle it when this is essentially just a
// lumnp of string data.

Expand Down
6 changes: 2 additions & 4 deletions src/Defs/Epsg900913.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use Academe\Proj4Php\Proj4;

class Epsg900913
{
public function init()
{
class Epsg900913 {
public function init() {
// Add this entry to the static global. Not a good way to handle it when this is essentially just a
// lumnp of string data.

Expand Down
15 changes: 6 additions & 9 deletions src/LongLat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@

/**
* Author : Julien Moquet, Jason Judge
*
*
* Inspired by Proj4js from Mike Adair madairATdmsolutions.ca
* and Richard Greenwood rich@greenwoodmap.com
* License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
* and Richard Greenwood rich@greenwoodmap.com
* License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
*/
class LongLat {

public function init()
{
public function init() {
}

public function forward($pt)
{
public function forward($pt) {
return $pt;
}

public function inverse($pt)
{
public function inverse($pt) {
return $pt;
}
}
Loading

0 comments on commit 312a9f3

Please sign in to comment.