Last active
May 31, 2019 04:36
-
-
Save rjmackay/5982636 to your computer and use it in GitHub Desktop.
Tile utility class Handles converting slippy map tile numbers to lat/lon values Ported from:
http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Tile utility class | |
* | |
* Handles converting slippy map tile numbers to lat/lon values | |
* Ported from: | |
* http://svn.openstreetmap.org/applications/routing/pyroute/tilenames.py | |
* http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Java | |
*/ | |
class Util_Tile { | |
/** | |
* Get number of tiles for whole map | |
* @param int $zoom | |
* @return int number of tiles | |
*/ | |
public static function numTiles($zoom) | |
{ | |
return pow(2, $zoom); | |
} | |
/** | |
* Get the bounding box for a set of tile values | |
* | |
* @param int $zoom | |
* @param int $x | |
* @param int $y | |
* @return BoundingBox | |
*/ | |
public static function tileToBoundingBox($zoom, $x, $y) | |
{ | |
$bb = new BoundingBox(); | |
$bb->north = self::tileToLat($y, $zoom); | |
$bb->south = self::tileToLat($y + 1, $zoom); | |
$bb->west = self::tileToLon($x, $zoom); | |
$bb->east = self::tileToLon($x + 1, $zoom); | |
return $bb; | |
} | |
/** | |
* Get longitude from tile x value | |
* | |
* @param int $x | |
* @param int $zoom | |
* @return float longitude | |
*/ | |
public static function tileToLon($x, $zoom) | |
{ | |
return $x / self::numTiles($zoom) * 360.0 - 180.0; | |
} | |
/** | |
* Get latitude from tile x value | |
* | |
* @param int $y | |
* @param int $zoom | |
* @return float latitude | |
*/ | |
public static function tileToLat($y, $zoom) | |
{ | |
$n = pi() * (1 - 2 * $y / self::numTiles($zoom)); | |
return rad2deg(atan(sinh($n))); | |
} | |
} | |
/** | |
* Bounding Box class | |
*/ | |
class BoundingBox { | |
public $north; | |
public $south; | |
public $east; | |
public $west; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment