Skip to content

Instantly share code, notes, and snippets.

@mdbauman
Created March 1, 2017 22:16
Show Gist options
  • Save mdbauman/32dd9963436f6ff006483e8eff431e8c to your computer and use it in GitHub Desktop.
Save mdbauman/32dd9963436f6ff006483e8eff431e8c to your computer and use it in GitHub Desktop.
Albers Equal Area Conic projection in PHP
<?php
//based on albers.js by Tom Carden https://gist.github.com/RandomEtc/476238
//ex. for a 954.66669*601.46283pt SVG of contiguous US, I used:
// $albers = new Albers([23.0,-96.0], [29.5, 45.5]);
// $coords = $albers->project($lat,$long);
// $coords = ['x'=>$coords['x']*1315+485, 'y'=>$coords['y']*1315+654]; //scale and translate
class Albers{
private static $origin, $parallels, $n, $c, $C, $p0, $lat0, $lng0, $phi1, $phi2;
private $t, $p;
function __construct($origin = array(23.0,-96.0), $parallels=array(29.5, 45.5)){
self::$origin = $origin;
self::$parallels = $parallels;
self::$lat0 = deg2rad(self::$origin[0]);
self::$lng0 = deg2rad(self::$origin[1]);
self::$phi1 = deg2rad(self::$parallels[0]);
self::$phi2 = deg2rad(self::$parallels[1]);
self::$n = 0.5 * (sin(self::$phi1) + sin(self::$phi2));
self::$c = cos(self::$phi1);
self::$C = self::$c*self::$c + 2*self::$n*sin(self::$phi1);
self::$p0 = sqrt(self::$C - 2*self::$n*sin(self::$lat0)) / self::$n;
}
function project($lat, $lng) {
$this->t = self::$n * (deg2rad($lng) - self::$lng0);
$this->p = sqrt(self::$C - 2 * self::$n * sin(deg2rad($lat))) / self::$n;
return[
'x'=>$this->p * sin($this->t),
'y'=>$this->p * cos($this->t) - self::$p0
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment