|
|
|
<?php
//degree format 121°8'6" degrees° minutes' seconds"
//decimal format 121.135
//accepts a coordinate in degree format and returns the equivalent decimal
//N & S are latitudes
//E & W are longitudes
//South latitudes and West longitudes are negative decimals
//calling examples
//degree2decimal('121°86"N'); returns 121.135
//degree2decimal('121°86"S'); returns -121.135
//degree2decimal('121°86"E'); returns 121.135
//degree2decimal('121°86"W'); returns -121.135
function degree2decimal($deg_coord="")
{
$dpos=strpos($deg_coord,'°');
$mpos=strpos($deg_coord,'');
$spos=strpos($deg_coord,'"');
$mlen=(($mpos-$dpos)-1);
$slen=(($spos-$mpos)-1);
$direction=substr(strrev($deg_coord),0,1);
$degrees=substr($deg_coord,0,$dpos);
$minutes=substr($deg_coord,$dpos+1,$mlen);
$seconds=substr($deg_coord,$mpos+1,$slen);
$seconds=($seconds/60);
$minutes=($minutes+$seconds);
$minutes=($minutes/60);
$decimal=($degrees+$minutes);
//South latitudes and West longitudes need to return a negative result
if (($direction=="S") or ($direction=="W"))
{ $decimal=$decimal*(-1);}
return $decimal;
}
//accepts a coordinate in decimal format and returns the equivalent degree
//as a string
//calling examples
//decimal2degree('121.135,'LAT'); will return 121°86"N
//decimal2degree('-121.135,'LAT'); will return 121°86"S
//decimal2degree('121.135,'LON'); will return 121°86"E
//decimal2degree('-121.135,'LON'); will return 121°86"W
function decimal2degree($decimal_coord="",$latorlon="")
{
//121.135
//degrees=121
//minutes=.135*60=(8).1
//seconds=.1*60=(6)
//121°86"
$decpos=strpos($decimal_coord,'.');
$whole_part=substr($decimal_coord,0,$decpos);
$decimal_part=abs($decimal_coord-$whole_part);
$minutes=intval($decimal_part*60);
$seconds=intval((($decimal_part*60)-$minutes)*60);
if ($latorlon=='LAT')
{if ($whole_part<0)
{
$whole_part=($whole_part*(-1));
$L='S';
}
else
{
$L='N';
}
}//end if
else
{if($latorlon=='LON')
{if ($whole_part<0)
{
$whole_part=($whole_part*(-1));
$L='W';
}
else
{
$L='E';
}
}//end if
}//end if
$degree=$whole_part.'°'.$minutes.''.$seconds.'"';
$degree.=$L;
return $degree;
}
//accepts two decimal latitude, longitude coordinates and returns the distance
//between the two
function decimal_distance($lat1="",$lon1="",$lat2="",$lon2="")
{
//$radius is determined using the following formula
//(360 degrees)*(60 minutes per degree)*(1.852) km per minute
//give a circumference of 40003.2 km
//radius is circumference/(2*pi) which gives us 6637km or 3956miles
$radius=3956;
$lat1 = deg2rad ($lat1);
$lat2 = deg2rad ($lat2);
$lon1 = deg2rad ($lon1);
$lon2 = deg2rad ($lon2);
//Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine",
//Sky and Telescope, vol. 68, no. 2, 1984, p. 159):
$dlon=$lon2-$lon1;
$dlat=$lat2-$lat1;
$sinlat=sin($dlat/2);
$sinlon=sin($dlon/2);
$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlon*$sinlon);
$c=2*asin(min(1,sqrt($a)));
$d=$radius*$c;
//
return round($d,2);
}
//accepts two degree latitude, longitude coordinates and returns the distance
//between the two
function degree_distance($lat1="",$lon1="",$lat2="",$lon2="")
{
$lat1=degree2decimal($lat1);
$lat2=degree2decimal($lat2);
$lon1=degree2decimal($lon1);
$lon2=degree2decimal($lon2);
$distance=decimal_distance($lat1,$lon1,$lat2,$lon2);
return $distance;
}
?>
See it in action at http://www.geoarmy.com/distance_calculation/ |
|
| Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET Categories : Algorithms, HTML and PHP, PHP, Variables | | | A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL | | | Boolean Keyword Interpreter Categories : PHP, Algorithms, Search Engines | | | Diffusion-Limited Aggregation visualization Categories : PHP, Graphics, Algorithms, Math. | | | Browse a MySQL database & draw a tree view & load final items into a template page. Categories : MySQL, Complete Programs, Algorithms, PHP, Databases | | | Recursive function to move files on a filesystem. It can be minor changed in order to copy recursively.
Categories : PHP, Filesystem, Algorithms | | | Fast PI calculator. Can easily find the 1000th decimal place of pi in 5 seconds. Categories : PHP, BC math, Algorithms | | | The Porter Word Stemming Algorithm in PHP
Reduces words to their base stem for search engines and indexing Categories : Algorithms, PHP, Strings | | | Mail-lib provides a simple interface to the sendmail program. Note: you must actually have sendmail on your machine (sorry windows NT users). Categories : Algorithms, Email, PHP | | | A recursive function to traverse a multi-dimensional array where the
dimensions are not known Categories : Arrays, PHP, Algorithms | | | SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge
response login algorithms over an insecure connection. Categories : Algorithms, PHP, Security | | | Calculate Body Mass Index Categories : PHP, Algorithms, Regexps | | | minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B. Categories : PHP, Arrays, Algorithms | | | A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array. Categories : Algorithms, Arrays, PHP, Complete Programs | | | Paginating the mySQL data Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP | |
| | | | Daniel Boos wrote :1249
//radius is circumference/(2*pi) which gives us 6637km or 3956miles
6637km is wrong it`s ~6367km
| |
|
|
|