Skip to content

Instantly share code, notes, and snippets.

@ramunasd
Last active September 20, 2020 14:11
Show Gist options
  • Save ramunasd/4444676 to your computer and use it in GitHub Desktop.
Save ramunasd/4444676 to your computer and use it in GitHub Desktop.
GlobalMercator.php provides support for converting between latitude and longitude coordinates and a “quadtree” representation that makes it easy to search a database for physically close points.
<?php
/*
GlobalMercator - part of Aggregate Map Tools
Version 1.0
Copyright (c) 2009 The Bivings Group
All rights reserved.
Author: John Bafford
http://www.bivings.com/
http://bafford.com/softare/aggregate-map-tools/
Based on GDAL2Tiles / globalmaptiles.py
Original python version Copyright (c) 2008 Klokan Petr Pridal. All rights reserved.
http://www.klokan.cz/projects/gdal2tiles/
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.
*/
class GlobalMercator
{
var $tileSize;
var $initialResolution;
var $originShift;
/**
* Initialize the TMS Global Mercator pyramid
*
* @param integer $tileSize
*/
function __construct($tileSize = 256)
{
$this->tileSize = $tileSize;
$this->initialResolution = 2 * M_PI * 6378137 / $this->tileSize; # 156543.03392804062 for tileSize 256 Pixels
$this->originShift = 2 * M_PI * 6378137 / 2.0; # 20037508.342789244
}
/**
* Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913
*
* @param float $lat
* @param float $lon
* @return array
*/
function LatLonToMeters($lat, $lon)
{
$mx = $lon * $this->originShift / 180.0;
$my = log( tan((90 + $lat) * M_PI / 360.0 )) / (M_PI / 180.0);
$my *= $this->originShift / 180.0;
return array($mx, $my);
}
/**
* Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum
*
* @param integer $mx
* @param integer $my
* @return array
*/
function MetersToLatLon($mx, $my)
{
$lon = ($mx / $this->originShift) * 180.0;
$lat = ($my / $this->originShift) * 180.0;
$lat = 180 / M_PI * (2 * atan( exp( $lat * M_PI / 180.0)) - M_PI / 2.0);
return array($lat, $lon);
}
/**
* Converts pixel coordinates in given zoom level of pyramid to EPSG:900913
*
* @param integer $px
* @param integer $py
* @param integer $zoom
* @return array
*/
function PixelsToMeters($px, $py, $zoom)
{
$res = $this->Resolution($zoom);
$mx = $px * $res - $this->originShift;
$my = $py * $res - $this->originShift;
return array($mx, $my);
}
/**
* Converts EPSG:900913 to pyramid pixel coordinates in given zoom level
*
* @param integer $mx
* @param integer $my
* @param integer $zoom
* @return array
*/
function MetersToPixels($mx, $my, $zoom)
{
$res = $this->Resolution($zoom);
$px = ($mx + $this->originShift) / $res;
$py = ($my + $this->originShift) / $res;
return array($px, $py);
}
/**
* Returns a tile covering region in given pixel coordinates
*
* @param integer $px
* @param integer $py
* @return array
*/
function PixelsToTile($px, $py)
{
$tx = floor($px / $this->tileSize);
$ty = floor($py / $this->tileSize);
return array($tx, $ty);
}
/**
* Returns tile for given mercator coordinates
*
* @param float $mx
* @param float $my
* @param integer $zoom
* @return array
*/
function MetersToTile($mx, $my, $zoom)
{
list($px, $py) = $this->MetersToPixels($mx, $my, $zoom);
return $this->PixelsToTile($px, $py);
}
/**
* Returns bounds of the given tile in EPSG:900913 coordinates
*
* @param integer $tx
* @param integer $ty
* @param integer $zoom
* @return array
*/
function TileBounds($tx, $ty, $zoom)
{
list($minx, $miny) = $this->PixelsToMeters( $tx*$this->tileSize, $ty*$this->tileSize, $zoom);
list($maxx, $maxy) = $this->PixelsToMeters( ($tx+1)*$this->tileSize, ($ty+1)*$this->tileSize, $zoom);
return array($minx, $miny, $maxx, $maxy);
}
/**
* Returns bounds of the given tile in latutude/longitude using WGS84 datum
*
* @param integer $tx
* @param integer $ty
* @param integer $zoom
* @return array
*/
function TileLatLonBounds($tx, $ty, $zoom)
{
$bounds = $this->TileBounds($tx, $ty, $zoom);
list($minLat, $minLon) = $this->MetersToLatLon($bounds[0], $bounds[1]);
list($maxLat, $maxLon) = $this->MetersToLatLon($bounds[2], $bounds[3]);
return array($minLat, $minLon, $maxLat, $maxLon);
}
/**
* Resolution (meters/pixel) for given zoom level (measured at Equator)
*
* @param integer $zoom
* @return float
*/
function Resolution($zoom)
{
return $this->initialResolution / (1 << $zoom);
}
/**
* Converts TMS tile coordinates to Microsoft QuadTree
*
* @param integer $tx
* @param integer $ty
* @param integer $zoom
* @return string
*/
function QuadTree($tx, $ty, $zoom)
{
$quadKey = '';
$ty = ((1 << $zoom) - 1) - $ty;
foreach(range($zoom, 1, -1) as $i)
{
$digit = 0;
$mask = 1 << ($i-1);
if(($tx & $mask) != 0){
$digit += 1;
}
if(($ty & $mask) != 0){
$digit += 2;
}
$quadKey .= $digit;
}
return $quadKey;
}
/**
* Converts a quadkey to tile coordinates
*
* @param string $quadtree
* @param integer $zoom
* @return array
*/
function QuadTreeToTile($quadtree, $zoom)
{
$tx = 0;
$ty = 0;
for($i = $zoom; $i >= 1; $i--)
{
$ch = $quadtree[$zoom - $i];
$mask = 1 << ($i-1);
$digit = $ch - '0';
if($digit & 1){
$tx += $mask;
}
if($digit & 2){
$ty += $mask;
}
}
$ty = ((1 << $zoom) - 1) - $ty;
return array($tx, $ty);
}
/**
* Converts a latitude and longitude to quadtree at the specified zoom level
*
* @param float $lat
* @param float $lon
* @param integer $zoom
* @return string
*/
function LatLonToQuadTree($lat, $lon, $zoom)
{
list($mx, $my) = $this->LatLonToMeters($lat, $lon);
list($tx, $ty) = $this->MetersToTile($mx, $my, $zoom);
return $this->QuadTree($tx, $ty, $zoom);
}
/**
* Converts a quadtree location into a latitude/longitude bounding rectangle
*
* @param string $quadtree
* @return array
*/
function QuadTreeToLatLon($quadtree)
{
$zoom = strlen($quadtree);
list($tx, $ty) = $this->QuadTreeToTile($quadtree, $zoom);
return $this->TileLatLonBounds($tx, $ty, $zoom);
}
/**
* Returns a list of all of the quadtree locations at a given zoom level within a latitude/longude box
*
* @param integer $zoom
* @param array $latLon
* @param array $latLonMax
* @return array
*/
function GetQuadTreeList($zoom, array $latLon, array $latLonMax = array())
{
list($lat, $lon) = $latLon;
if($latLonMax){
list($latMax, $lonMax) = $latLonMax;
if($latMax < $lat || $lonMax < $lon){
return false;
}
}
list($mx, $my) = $this->LatLonToMeters($lat, $lon);
list($tminx, $tminy) = $this->MetersToTile($mx, $my, $zoom);
if($latLonMax){
list($mx, $my) = $this->LatLonToMeters($latMax, $lonMax);
list($tmaxx, $tmaxy) = $this->MetersToTile($mx, $my, $zoom);
}else{
$tmaxx = $tminx;
$tmaxy = $tminy;
}
$arr = array();
foreach(range($tminy, $tmaxy) as $ty){
foreach(range($tminx, $tmaxx) as $tx){
$quadtree = $this->QuadTree($tx, $ty, $zoom);
$arr[$quadtree] = $this->TileLatLonBounds($tx, $ty, $zoom);
}
}
return $arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment