...Lat N48 42.29, Long W122.54.40 into this Lat 48.4229, Long -122.5440I'm not sure that is a true conversion. There are standards for the representation of these things. Let me see if I can find an example, since I've done this before and researched it pretty thoroughly!
<?php // demo/normalize_geocodes.php
/**
* http://www.experts-exchange.com/questions/28931418/use-php-to-convert-lat-and-long-coordinates.html
*
* Lat N48 42.29, Long W122.54.40 into this Lat 48.4229, Long -122.5440
*
* http://en.wikipedia.org/wiki/Geographic_coordinate_conversion
* http://www.earthpoint.us/Convert.aspx
* https://en.wikipedia.org/wiki/IERS_Reference_Meridian
*
* Latitude Distance from the Equator
* 90 N = 90 North Pole
* 90 S = -90 South Pole
*
* Longitude Distance from Prime Meridian (Greenwich)
* 180 W = -180
* 180 E = 180
*/
error_reporting(E_ALL);
echo '<pre>';
// A COLLECTION OF GATHERED TEST DATA
$testdata = array
( '40:26:46N,079:56:55W'
, '40:26:46.302N, 079:56:55.903W'
, '-40°26\'47"N, 079°58\'36"W' // SOUTHERN HEMISPHERE
, '40d 26\' 47" N, 079d 58\' 36" W'
, '40.446195N, 79.948862W'
, '40.446195, -79.948862'
, '40.446195,-79.948862'
, '40° 26.7717, -79° 56.93172'
, '38.930434,-77.147772' // MY HOUSE
, '45.55252525134013,-122.607421875' // PORTLAND, OREGON
, 'Lat N48 42.29, Long W122.54.40' // FROM THE QUESTION AT E-E
, '48° 42\'29"N, 122° 54\'40"W'
)
;
// PROCESS EACH OF THE TEST DATA STRINGS
foreach ($testdata as $ll)
{
// SHOW WHAT WE'RE WORKING WITH
echo PHP_EOL . "RAW DATA: $ll";
echo PHP_EOL;
$llp = explode(',', $ll);
$lat = trim($llp[0]);
$lon = trim($llp[1]);
$llo = new Geocode($lat, $lon);
print_r($llo);
echo PHP_EOL;
}
Class Geocode
{
public $lat, $lon, $geo; // DECIMAL VALUES
public $lat_dms, $lon_dms; // DEGREES, MINUTES, SECONDS
public function __construct($lat, $lon)
{
// INITIALIZE THE RETURN OBJECT
$this->lat
= $this->lon
= $this->geo
= $this->lat_dms
= $this->lon_dms
= FALSE
;
// DETERMINE IF THIS IS SOUTHERN LATITUDE
$lat_sign = NULL;
$lat_char = 'N';
if ( (strpos($lat, 'S') !== FALSE) || (strpos($lat, '-') !== FALSE) )
{
$lat_sign = '-';
$lat_char = 'S';
}
// DETERMINE IF THIS IS WESTERN LONGITUDE
$lon_sign = NULL;
$lon_char = 'E';
if ( (strpos($lon, 'W') !== FALSE) || (strpos($lon, '-') !== FALSE) )
{
$lon_sign = '-';
$lon_char = 'W';
}
// NORMALIZE TO DECIMAL
$this->lat = $this->normalize_dec($lat);
$this->lon = $this->normalize_dec($lon);
// NORMALIZE TO D°M'S"
$this->lat_dms = $this->normalize_dms($this->lat, $lat_char);
$this->lon_dms = $this->normalize_dms($this->lon, $lon_char);
// COMPLETE THE GEOCODE
$this->geo
= $lat_sign
. $this->lat
. ','
. $lon_sign
. $this->lon
;
// COMPLETE THE LAT AND LON
$arr = explode(',', $this->geo);
$this->lat = $arr[0];
$this->lon = $arr[1];
return $this;
}
// COMPLETE THE DDD.ddddddd
protected function normalize_dec($lxx)
{
$lxx = trim($lxx);
$lxx = html_entity_decode($lxx, ENT_QUOTES);
$lxx = strtoupper($lxx);
// SPLIT INTO DEGREES, MINUTES, SECONDS
$arr = preg_split('/[^0-9\.]/', $lxx, -1, PREG_SPLIT_NO_EMPTY);
// COMPUTE THE DECIMAL VALUE
$sec = isset($arr[2]) ? $arr[2] : 0;
$min = isset($arr[1]) ? $arr[1] : 0;
$sec = $sec + $min * 60.0;
$sec = $sec / 3600.0;
$dms = $arr[0] + $sec;
$lxx = number_format($dms,7);
$lxx = rtrim($lxx, '0');
$lxx = rtrim($lxx, '.');
return $lxx;
}
// COMPLETE THE D°M'S"
protected function normalize_dms($lxx, $lxx_char)
{
$lat_d = intval($lxx);
$lat_ms = $lxx - $lat_d;
$lat_m = $lat_ms * 60.0;
$lat_m = intval($lat_m);
$lat_s = $lat_ms * 60.0 - $lat_m;
$lat_s = $lat_s * 60.0;
$lat_s = number_format($lat_s,3);
$lat_s = rtrim($lat_s, '0');
$lat_s = rtrim($lat_s, '.');
$dms
= $lat_d
. '°'
. $lat_m
. '''
. $lat_s
. '"'
. $lxx_char;
;
return $dms;
}
}
Turn this Lat N48.42.29, Long W122.54.40
into this Lat -48.42.29, Long -122.54.40
Meaning, instead of N/W use -
?
if so, do this:
$ouput = str_replace("N", "-", $input);
$ouput = str_replace("W", "-", $output);
where $input=Lat N48.42.29, Long W122.54.40 ;