0

I'm stuck and need some help. I have a database that has over 3000 latitudes and longitudes and I'm trying to convert them to decimal lat and lon so I can display the distance between two coordinates. The two coordinates are passed in the url of the page. The first (lat1 & lon1) is already converted but the second (lat2 & lon2) is not. The latitude and Longitude in my database are stored like This: 26°56.34308' -094°41.32328' so I'm thinking that the commas should probably be removed. I've got some code from a few sources but I'm not sure how to put them together properly.

///// Get the two locations from the url

$lat1 = $_GET[lat1];
$lon1 = $_GET[lon1];

////// lat2 & Lon2 are the ones that need to be converted

$lat2 = $_GET[lat2];
$lon2 = $_GET[lon2];

///// Convert lat2 & lon2 into decimal format

$pos1 = strrpos($mystring, "°");
$pos2 = strrpos($mystring, ".");
$pos3 = strrpos($mystring, "'");
// Get subsring from a string: substr(source, start, length)
$deg = substr($mystring, 0, $pos1);
$min = substr($mystring, $pos1, $pos2 - $pos1);
$sec = substr($mystring, $pos2, $pos3 - $pos2);

function DMStoDEC($deg,$min,$sec) {
// Converts DMS ( Degrees / minutes / seconds )
// to decimal format longitude / latitude
    return $deg+((($min*60)+($sec))/3600);
}

//////calculate the distance

function distance($lat1, $lon1, $lat2, $lon2, $unit) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
        cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    $unit = strtoupper($unit);

    if ($unit == "K") {
        return ($miles * 1.609344);
    } else if ($unit == "N") {
        return ($miles * 0.8684);
    } else {
        return $miles;
    }
}

// Miles
echo distance($lat1, $lon1, $lat2, $lon2, "m") . " miles<br><br>";

//Kilometers
echo distance($lat1, $lon1, $lat2, $lon2, "k") . " kilometers<br><br>";

//Nautical miles
echo distance($lat1, $lon1, $lat2, $lon2, "N") . " Nautical miles";
Justin Petty
  • 283
  • 4
  • 21

2 Answers2

0

You have the latitudes and longitudes in a database? What kind of database? Some databases like MySQL and PostgresQL can easily calculate the distance between 2 points in a simple SQL query. In order to do this, you would have to make sure that your data is in a spatial column like a point column. (If you don't have your data in point columns, you can easily create a new point column and update the new column with the data in the non point column)

Then, to calculate distance between 2 points, simply execute a SQL query.

<?php

$dbh = mysql_connect($host, $username, $password);
mysql_select_db($dbname);

// Get distance between 2 points manually
$query = "select GLength(GeomFromText('LineString({$lat1} {$lon1},{$lat2} {$lon2})'))";
$dbr = mysql_query($query);
list($length) = mysql_fetch_array($dbr);
// Now, $length has the distance between lat1, lon1 and lat2, lon2

// To put points into a table use this query
// Assuming that your table has 2 columns point1 and point2
$query = "insert into mytable (point1, point2) values( geomfromtext('point({$lat1} {$lon1})'), geomfromtext('point({$lat2} {$lon2})') )";
mysql_query($query);

// Get distance between 2 points that are stored in point columns
// Assuming the table has 2 point columns point1 and point2
$query = "select GLength(GeomFromText(concat('LineString(', x(point1), ' ', y(point1), ',', x(point2), ' ', y(point2), ')'))) from mytable";
$dbr = mysql_query($query);
list($length) = mysql_fetch_array($dbr);
// Now $length has the distance between point1 and point2

?>

You can then do any distance conversions from there. (Also, take care when using variables in queries without checking them like I did above with $lat1 and $lon1 etc.... Make sure and check them to make sure they only contain numbers if you are getting the values from user input)

Ray Perea
  • 5,640
  • 1
  • 35
  • 38
0

Checkout these links

MySql or PHP function to convert longitude and latitude to decimal degrees ,

Fastest Way to Find Distance Between Two Lat/Long Points and

Calculate distance given 2 points, latitude and longitude

may be you will get your solution here.

Community
  • 1
  • 1
Sumant
  • 954
  • 1
  • 18
  • 39