i've to calculate the distance between
(40.851774999999996,14.268123999999998)
and each coordinates into results of an sql query:
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($results as $key => $value) {
echo "distance = ". calculateDistance("40.851774999999996","14.268123999999998",$value['lat'],$value['lng'])."<br>";
}
Where calculateDistance is
function calculateDistance($targetLat,$targetLng,$currentLat,$currentLng){
$r = 6371; // km
$dLat = $targetLat-$currentLat;
$dLng = $targetLng-$currentLng;
$a = sin($dLat/2)*sin($dLat/2) + sin($dLng/2)*sin($dLng/2);
$c = 2*atan2(sqrt($a), sqrt(1-$a));
return $r*$c;
}
it gives me strange result like:
distance = NAN //-> NAN???
distance = 3392.8405117312 // TOO MUCH!
distance = 3392.8405117312 // TOO MUCH!
...
Where is the problem? can someone help me to fix it? :)
According to this answer:
Calculate distance between two latitude-longitude points? (Haversine formula)
You don't convert from degrees to radians.
You formula is incorrect:
They say:
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
You wrote:
$a = sin($dLat/2)*sin($dLat/2) + sin($dLng/2)*sin($dLng/2);
The cosine is missing in your code.
You need to convert degrees to radians before using it in sin function.
$radians = $degrees * (M_PI/180);
Look at this function, too. It looks a little bit different.
You are referring to the Haversine formula:
http://en.wikipedia.org/wiki/Haversine_formula
There are plenty of examples and code snippets in these pages:
http://rosettacode.org/wiki/Haversine_formula
http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe
I've used this snippet in my code, which works very well:
http://www.codecodex.com/wiki/Calculate_distance_between_two_points_on_a_globe#PHP
Related
I'm having some trouble computing the distance between two GPS points by their coordinates.
point a
x = 7,2562
y = 47,7434599999999
point b
x = 7,21978
y = 47,73836
I used the Haversine formula as described here. The result I get is 4.09 km.
However, locating those points on a map using a tool like this, I can measure a distance of 2.8 km
Several other formulas I tried also return a result around 4 km.
Any ideas what I would be missing ?
I think is because u are using the function in miles, in Kms you can use something like that:
public static function distance(
array $from,
array $to
) {
if (empty($from['lat']) || empty($to['lat'])) {
return $to['distance'];
}
$latitude1 = (float) $from['lat'];
$latitude2 = (float) $to['lat'];
$longitude1 = (float) $from['lng'];
$longitude2 = (float) $to['lng'];
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2)))
+ (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta)))
;
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
$distance = (is_nan($distance)) ? 0 : $distance * 1.609344;
return $distance;
}
As pointed out by Roland Starke in the comments, the problem was the order of the coordinates. (7, 47 not 47, 7)
I have a table in mysql with 4 columns:
Latitude_1
Longitude_1
Latitude_2
Longitude_2
Now I want to calculate the heading for all rows to be used in a kml file.
I found this function:
// Takes two sets of geographic coordinates in decimal degrees and produces bearing (azimuth) from the first set of coordinates to the second set.//
public static function bearing($lat1, $lon1, $lat2, $lon2) {
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);
$lonDelta = $lon2 - $lon1;
$y = sin($lonDelta) * cos($lat2);
$x = cos($lat1) * sin($lat2) - sin($lat1) * cos($lat2) * cos($lonDelta);
$brng = atan2($y, $x);
$brng = $brng * (180 / pi());
if ( $brng < 0 ) { $brng += 360; }
return $brng;
}
Now I hope that someone shows me a query that echoes all headings (bearings) of the table based on the above mentioned function
To combine an expression expr, take a look to the mysql math functions and find proper equivalents for php ones - https://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html
Simplify your expr if possible
Use your result expression expr in following query SELECT expr FROM your_table
Profit
i have array of lat long like :
var locationList = new Array( '23.2531803, 72.4774396', '22.808782, 70.823863', '24.3310019, 72.8516531', '22.3073095, 73.1810976', '22.3038945, 70.8021599', '23.850809, 72.114838' );
i want get nearest around 25 km 's lat long from first given array which is 23.2531803, 72.4774396
are there any calculation for nearest 25 km 's lat long from given array.
NOTE: for some reason i can not use sql query, because i get lat long from given address
Step 1: Calculate the distance between your start coordinate and every subcoordinate
Step 2: Pick the smallest distance
Step 3: Is it < 25 km? Success!
How to calculate distance between two coordinates:
function distance($lat1, $lon1, $lat2, $lon2) {
$D = 6371; // Earth Radius
$dLat = $lat2-$lat1;
$dLon = $lon2-$lon1;
$a = sin($dLat/2) * sin($dLat/2) +
sin($dLon/2) * sin($dLon/2) * cos($lat1) * cos($lat2);
$b = 2 * atan2(sqrt($a), sqrt(1-$a));
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
return $D * $c;
}
This function thinks of the Earth as a perfect ball, which it is not - slight variations do apply, but are neglible at the 25km diameter you want.
First of all your data array is awfull. You need to make your data more computer readable. Then you can use Pythagorean theorem to calculate the distance from each location. You can save the first distance and index in an variable then replace it with new distance and index if it's shorter.
var closest = {id:0,dist:-1};
var myloc = [23.2531303, 72.4774398]
for(var i = 0; i < locationList.length; i++)
{
var long_lat = locationList[i].match(/([0-9.]+)+/)
long_lat[0] = parseFloat(long_lat[0]);
long_lat[1] = parseFloat(long_lat[1]);
var dist = Math.sqrt(Math.pow(long_lat[0] - myloc[0], 2)+Math.pow(long_lat[1] - myloc[1], 2)));
if((closest.dist == -1) || (closest.dist > dist))
{
closest.dist = dist;
closest.id = i;
}
}
When you like to calculate in JS you may use google.maps.geometry.spherical.computeDistanceBetween() to calculate the distance between the single points.
Store the results in an array, sort the array and when the smallest entry is <25km you got what you want.
I have a from location (latitude, longitude) and to location (latitude, longitude). After calculating, it should show me what would be the nearest way to go from using a compass. The following is PHP code to do that, but its showing the wrong direction, I need little help on this.
function GreatCircleDirection ($OrigLat, $DestLat, $OrigLong, $DestLong, $Distance)
{
$Result = 0.0;
$L1 = deg2rad($OrigLat);
$L2 = deg2rad($DestLat);
$D = deg2rad($Distance / 60); # divide by 60 for nautical miles NM to degree
$I1 = deg2rad($OrigLong);
$I2 = deg2rad($DestLong);
$Dlong = $I1 - $I2;
$A = sin($L2) - cos($D + $L1 - pi() / 2);
$B = acos($A / (cos($L1) * sin($D)) + 1);
if ((abs($Dlong) < pi() and $Dlong < 0) or (abs($Dlong) > pi() and $Dlong > 0))
{
//$B = (2 * pi()) - $B;
}
$Result = $B;
return rad2deg($Result);
}
function GreatCircleDistance ($OrigLat , $DestLat, $OrigLong, $DestLong)
{
$L1 = deg2rad($OrigLat);
$L2 = deg2rad($DestLat);
$I1 = deg2rad($OrigLong);
$I2 = deg2rad($DestLong);
$D = acos(cos($L1 - $L2) - (1 - cos($I1 - $I2)) * cos($L1) * cos($L2));
# One degree of such an arc on the earth's surface is 60 international nautical miles NM
return rad2deg($D * 60);
}
Bug on if condition:
this is the values in the if condition of greatCircleDirection function, need to know what to change to fix it.
if (0.57700585070933 < 3.1415926535898 and 0.57700585070933 < 0) or (0.57700585070933 > 3.1415926535898 and 0.57700585070933 > 0)
example:
from lat: 33.71,
to lat: 21,
from long: 73.06,
to long: 40 ,
distance: 1908.842544944
direction 104.96527938779 (direction should be 255.87 or so)
Computing the distance is unnecessary; it simply adds more operations and can introduces more numerical errors. Using your style of coding, something like this should work:
function GreatCircleDirection($OrigLat, $OrigLong, $DestLat, $DestLong)
{
$L1 = deg2rad($OrigLat);
$I1 = deg2rad($OrigLong);
$L2 = deg2rad($DestLat);
$I2 = deg2rad($DestLong);
return rad2deg(atan2((sin($I2-$I1),cos($L1)*tan($L2)-sin($L1)*cos($I2-$I1)));
}
The atan2 function takes care of identifying the correct quadrant for the direction, and gives you the angle between -180 to 180 measured from true North, e.g., GreaterCircleDirection(39,-77,21,40) evaluates to 56.76 degrees. Sign convention used: latitudes are positive when north, negative when south; longitudes are positive when east, negative when west.
The calculation is discussed in, among other places, http://patriot.net/~abdali/ftp/qibla.pdf.
Well, your distance calculation checks out. But I see that the answer you get for the initial bearing is (0+105)mod360 rather than (0-105)mod360 (approximately) so I suspect a wrong sign somewhere in the if statement in your GreatCircleDirection function.
Perhaps the worked examples, under "Using the Sine Rule", at http://www.krysstal.com/sphertrig.html will help.
I need to convert coordinates in the following form:
N42-53.9°
W072-16.2°
Into something that is like the following:
-90.7311
0.346944
A php function would be greatly appreciated - or just a formula would also be nice enough.
I found an online JS calculator and a PHP solution:
<?php
function DMStoDEC($deg,$min,$sec)
{
// Converts DMS ( Degrees / minutes / seconds )
// to decimal format longitude / latitude
return $deg+((($min*60)+($sec))/3600);
}
function DECtoDMS($dec)
{
// Converts decimal longitude / latitude to DMS
// ( Degrees / minutes / seconds )
// This is the piece of code which may appear to
// be inefficient, but to avoid issues with floating
// point math we extract the integer part and the float
// part by using a string function.
$vars = explode(".",$dec);
$deg = $vars[0];
$tempma = "0.".$vars[1];
$tempma = $tempma * 3600;
$min = floor($tempma / 60);
$sec = $tempma - ($min*60);
return array("deg"=>$deg,"min"=>$min,"sec"=>$sec);
}
?>
Do it with simple mathematics:
arcsecond is 1⁄3,600 of a degree
arcminute is 1/60 of a degree
S,E negative, N,W positive
example: S 23° 25' 33.8: -1 * 23+25/60+33.8/3600 = -23,426055555555555555555555555556°