php - need help converting and calculating distance -


i'm stuck , need help. have database has on 3000 latitudes , longitudes , i'm trying convert them decimal lat , lon can display distance between 2 coordinates. 2 coordinates passed in url of page. first (lat1 & lon1) converted second (lat2 & lon2) not. latitude , longitude in database stored this: 26°56.34308' -094°41.32328' i'm thinking commas should removed. i've got code few sources i'm not sure how put them properly.

///// 2 locations url  $lat1 = $_get[lat1]; $lon1 = $_get[lon1];  ////// lat2 & lon2 ones need converted  $lat2 = $_get[lat2]; $lon2 = $_get[lon2];  ///// convert lat2 & lon2 decimal format  $pos1 = strrpos($mystring, "°"); $pos2 = strrpos($mystring, "."); $pos3 = strrpos($mystring, "'"); // subsring 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 ) // decimal format longitude / latitude     return $deg+((($min*60)+($sec))/3600); }  //////calculate 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"; 

you have latitudes , longitudes in database? kind of database? databases mysql , postgresql can calculate distance between 2 points in simple sql query. in order this, have make sure data in spatial column point column. (if don't have data in point columns, can create new point column , update new column data in non point column)

then, calculate distance between 2 points, execute sql query.

<?php  $dbh = mysql_connect($host, $username, $password); mysql_select_db($dbname);  // 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 distance between lat1, lon1 , lat2, lon2  // put points table use query // assuming table has 2 columns point1 , point2 $query = "insert mytable (point1, point2) values( geomfromtext('point({$lat1} {$lon1})'), geomfromtext('point({$lat2} {$lon2})') )"; mysql_query($query);  // distance between 2 points stored in point columns // assuming table has 2 point columns point1 , point2 $query = "select glength(geomfromtext(concat('linestring(', x(point1), ' ', y(point1), ',', x(point2), ' ', y(point2), ')'))) mytable"; $dbr = mysql_query($query); list($length) = mysql_fetch_array($dbr); // $length has distance between point1 , point2  ?> 

you can distance conversions there. (also, take care when using variables in queries without checking them did above $lat1 , $lon1 etc.... make sure , check them make sure contain numbers if getting values user input)


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -