4

I am trying to convert the Exif longitude and latitude data into a Google friendly decimal value. I have read a lot of tutorials, but nothing seems to be working for me. I may well be doing this completely wrong.

I successfully get the Exif data as follows:

$exif_data = exif_read_data($path . '/user/images/4.jpg');

$egeoLong = $exif_data['GPSLongitude'];
$egeoLat = $exif_data['GPSLatitude'];
$egeoLongR = $exif_data['GPSLongitudeRef'];
$egeoLatR = $exif_data['GPSLatitudeRef'];

From there I end up with the following array for the values:

deg Long: 118/1
min Long: 2147/100
sec Long: 0/1
Longitude Ref: W

deg Lat: 34/1
min Lat: 433/100
sec Lat: 0/1
Latitude Ref: N

And I run them through this function to obtain a decimal value (I got this function here on Stackoverflow).

$geoLong = gpsDecimal($egeoLong[0], $egeoLong[1], $egeoLong[2], $egeoLongR);
$geoLat = gpsDecimal($egeoLat[0], $egeoLat[1], $egeoLat[2], $egeoLatR);

function gpsDecimal($deg, $min, $sec, $hemi) {
    $d = $deg+((($min*60)+($sec))/3600);
    return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $d;
}

Which returns the following... that puts the coordinates in the middle of the ocean... not where I took the photo.

Latitude: 41.216666666667 Longitude: -153.78333333333

Can anyone tell me what I am doing wrong, or a better way to achieve this. Any help is much appreciated.

Chris
  • 833
  • 2
  • 17
  • 37

2 Answers2

3

I found that same formula, and it didn't work for me either. What I finally figured out, and what worked for me, was:

function toDecimal($deg, $min, $sec, $hem) 
{
    $d = $deg + ((($min/60) + ($sec/3600))/100);
    return ($hem=='S' || $hem=='W') ? $d*=-1 : $d;
}
LOlliffe
  • 1,647
  • 5
  • 22
  • 43
  • 1
    I think you made a mistake. It's not both minutes and seconds that are supposed to be divided by 100, but just seconds, so the good formula looks like : $d = $deg + ((($min/60) + ($sec/3600)/100)); – Buzut Apr 10 '14 at 23:02
1

I found that the best approximization is (based on coordinate translated by google maps)

$deg + ($min/60) + ($sec/36000000)
volperossa
  • 1,339
  • 20
  • 33