OMGM Code
Status: Beta
Brought to you by:
ifkkiruna
--- a +++ b/omgm/gm.php @@ -0,0 +1,178 @@ +<?php + +function Google_Tile_Factors($zoom, $tileSize = 256){ + + if ( !isset($zoom) || $zoom < 0 ) { + $zoom = 0; + } + + $value = array(); + + $value['zoom'] = $zoom ; + $value['PI'] = pi(); //3.1415926536 ; + $value['bc'] = 2 * $value['PI'] ; + $value['Wa'] = $value['PI'] / 180 ; + $value['cp'] = pow(2, ($value['zoom'] + 8) ); + $value['max'] = pow(2, $value['zoom']) - 1 ; # Maximum Tile Number + $value['pixLngDeg']= $value['cp'] / 360; + $value['pixLngRad']= $value['cp'] / $value['bc'] ; + $value['bmO'] = $value['cp'] / 2 ; + $value['tileSize'] = $tileSize ; + + return $value ; +} + +function Google_Tile_Calc($value, $ty, $tx){ + + $result = array(); + + $result["NAMEX"] = $tx; + $result["NAMEY"] = $ty; + + list( $result["PYN"], $result['PXW']) = Google_Tile_to_Pix( $value, $ty, $tx ) ; + + // Convert Pixels to Coordinates (Upper Left Corner)... + + list($result["LATN"],$result["LNGW"]) = PixtoCoordinate( $value, $result["PYN"], $result["PXW"] ) ; + + $result["PYS"] = $result["PYN"] + 255 ; + $result["PXE"] = $result["PXW"] + 255 ; + + // Convert Pixels to Coordinates (Lower Right Corner)... + + list($result["LATS"],$result["LNGE"]) = PixtoCoordinate( $value, $result["PYS"], $result["PXE"] ) ; + + return $result; +} + +function Google_Tile_to_Pix($value, $y, $x){ + return array( sprintf("%0.0f", $y * $value["tileSize"]), sprintf("%0.0f", $x * $value["tileSize"]) ) ; +} + +function PixtoCoordinate($value, $y ,$x){ + $e = (($y - $value["bmO"]) / $value["pixLngRad"]) * (-1) ; + + $d = array(); + + $d[1] = sprintf("%0.6f",($x - $value["bmO"]) / $value["pixLngDeg"]) ; + $d[0] = sprintf("%0.6f", (2 * atan(exp($e)) - $value["PI"] / 2) / $value["Wa"]) ; + + return $d; +} + +function Google_RelPix($lat, $lng, $zoom){ + $PI = pi(); //3.1415926536 ; + $bc = 2 * $PI; + $Wa = $PI / 180; + $cp = pow(2, ($zoom + 8) ); + $pixLngDeg = $cp / 360; + $pixLngRad = $cp / $bc ; + $bmO = $cp / 2 ; + + $d = array(2); + + $d[1] = sprintf("%0.0f", $bmO + $lng * $pixLngDeg ) ; + + $e = sin($lat * $Wa) ; + + if( $e > 0.99999 ){ + $e = 0.99999 ; + } + + if( $e < -0.99999 ){ + $e = -0.99999 ; + } + + $d[0] = sprintf("%0.0f", $bmO + 0.5 * log((1 + $e) / (1 - $e)) * (-1) * $pixLngRad ) ; + + // 0:x 1:y + return $d; +} + +function PixtoTileName($value, $y, $x, $yd, $xd, $parwho){ + $yn = floor($y / $value["tileSize"]); + $xn = floor($x / $value["tileSize"]); + + if( strcmp( $parwho, "Partial") != 0 ){ + if( strcmp( $yd, "N") == 0 ){ + $yn++; + } + else{ + $yn--; + } + + if( strcmp( $xd, "W" ) == 0 ){ + $xn++; + } + else{ + $xn--; + } + } + + if( $yn > $value["max"] ){ + $yn = $value["max"]; + } + elseif( $yn < 0 ){ + $yn = 0; + } + + if( $xn > $value["max"] ){ + $xn = $value["max"]; + } + elseif( $xn < 0 ){ + $xn = 0; + } + + return array($yn, $xn); +} + +/* +# Call as: <array of Hashes> = Google_Tiles(<LatitudeS>, <LongitudeW>, <LatitudeN>, <LongitudeE>, <Zoom>, [<option: tileSize>], [<option: +Partial/Whole>]) ; +# Partial/Whole option: (Default: Partial) +# Partial: Include the edge to create partial tiles +# Whole: Include only tiles that are contained by the bounds +# +# Returned Array Specifications: +# Each element is a reference to a Hash: +# NAMEY - Tile Name y +# NAMEX - Tile Name x +# PYS - Pixel South +# PXW - Pixel West +# PYN - Pixel North +# PXE - Pixel East +# LATS - South Latitude +# LNGW - West Longitude +# LATN - North Latitude +# LNGE - East Longitude +# +# Note: X is width, Y is height... +*/ +function Google_Tiles($latS, $lngW, $latN, $lngE, $zoom, $tileSize = 256, $parwho = "Partial"){ + $value = Google_Tile_Factors($zoom, $tileSize); + + $ret = array(); + $first = array(); + $last = array(); + + list( $first["NORTH"], $first["WEST"] ) = Google_RelPix($latN, $lngW, $zoom); + list( $first["NAMEY"], $first["NAMEX"] ) = PixtoTileName( $value, $first["NORTH"], $first["WEST"], 'N', 'W', $parwho ); + + list( $last["SOUTH"], $last["EAST"] ) = Google_RelPix($latS, $lngE, $zoom ) ; + list( $last["NAMEY"], $last["NAMEX"] ) = PixtoTileName( $value, $last["SOUTH"], $last["EAST"], 'S', 'E', $parwho ) ; + + for ( $ty = $first["NAMEY"] ; $ty <= $last["NAMEY"] ; $ty++ ){ + for ( $tx = $first["NAMEX"] ; $tx <= $last["NAMEX"] ; $tx++ ){ + $ret[] = Google_Tile_Calc( $value, $ty, $tx ); + } + } + + $ret[0]["NORTH"] = $first["NORTH"] ; + $ret[0]["WEST"] = $first["WEST"] ; + + $ret[ count($ret) - 1 ]["SOUTH"] = $last["SOUTH"] ; + $ret[ count($ret) - 1 ]["EAST"] = $last["EAST"] ; + + return $ret; +} +?>