Converting latitude/longitude with PHP

Ok, so what I’m about to show you is hardly rocket science. It is however, the first chunk of math I’ve done with PHP that is more than 2 lines long. So I figured why not, I’ll post it. I was coding up a USGS gaging station finder and realized that I had latitude/longitude values for the stations in decimal degrees (such as 32.872352° latitude, -91.867904° longitude), but couldn’t I convert these to degrees/minutes/seconds (such as 32° 52’ 20.47” latitude, 91° 52’ 4.45” longitude)? Surely I could figure this out.

Surely. So this is how I did it. Now I do realize that much of the math could be combined to save on the quantity of calculations, but then it would just be even more confusing for me.

  1. <?php
  2. // convert decimal degrees to degrees/minutes/seconds \\
  3. //      some definitions:
  4. //      DD=decimal degrees
  5. //      DMS=degrees/minutes/seconds
  6. //
  7. // How this works:
  8. // Say you have 32.872352 lat and -91.632563 long
  9. // The whole units of the decimal will remain the same, so
  10. // 1) multiply the decimal by 60 (0.872352 * 60 = 52.34112), the whole number becomes the minutes (52)
  11. // 2) take the remaining decimal (0.34112) and multiply by 60, this is now the seconds, round this to two decimal places (0.34221 * 60 = 20.53)
  12. // 3) string them all together 32 52 20.53
  13. //
  14. // ** LATITUDE ** \\
  15. // DEGREES \\
  16. // get integer value of latitude from db -> where $lat_dd is a floating point number like 32.872352
  17. $lat_dd_int = intval("$lat_dd");
  18. //  MINUTES \\
  19. //  get DD minutes (whats after the decimal point)-> Lat DD floating point - lat DD integer
  20. $lat_dd_m = $lat_dd - $lat_dd_int;
  21. // get DMS minutes as floating point -> 60 * DD minutes floating
  22. $lat_dms_m_float = 60 * $lat_dd_m;
  23. // get int value of lat DMS floating point
  24. $lat_dms_m_int = intval("$lat_dms_m_float");
  25. // SECONDS \\
  26. // get DD seconds -> DMS minutes floating - DMS minutes integer
  27. $lat_dd_s = $lat_dms_m_float - $lat_dms_m_int;
  28. // convert DD secs to DMS secs -> 60 * DD seconds
  29. $lat_dms_s = 60 * $lat_dd_s;
  30. // ** LONGITUDE ** \\
  31. // DEGREES \\
  32. // get integer value of longitude from db -> where $long_dd is a floating point number like -91.632563
  33. // this number remains negative
  34. $long_dd_int = intval("$long_dd");
  35. //  MINUTES \\
  36. //  get DD minutes (whats after the decimal point)-> Long DD floating point - long DD integer
  37. //  use abs to get rid of the negative sign
  38. $long_dd_m = abs($long_dd - $long_dd_int);
  39. // get DMS minutes as floating point -> 60 * DD minutes floating
  40. $long_dms_m_float = 60 * $long_dd_m;
  41. // get int value of long DMS floating
  42. $long_dms_m_int = intval("$long_dms_m_float");
  43. // SECONDS \\
  44. // get DD seconds -> DMS minutes floating - DMS minutes integer
  45. $long_dd_s = $long_dms_m_float - $long_dms_m_int;
  46. // convert DD secs to DMS secs -> 60 * DD seconds
  47. $long_dms_s = 60 * $long_dd_s;
  48. // end conversion of lat/long \\
  49. // next just echo the output with the degree/minute/second symbols in place
  50. ?>
  51. Latitude: <?php echo $lat_dd_int; ?>&#176; <?php echo $lat_dms_m_int; ?>&#8217; <?php echo round($lat_dms_s, 2); ?>&#8221;
  52. Longitude: <?php echo $long_dd_int; ?>&#176; <?php echo $long_dms_m_int; ?>&#8217; <?php echo round($long_dms_s, 2); ?>&#8221;

No Comments

  1. D says:

    Added n/s e/w for hemisphere, and get lat/long from request…saved me lots of work by the way…thanks!!
    < ?php
    // convert decimal degrees to degrees/minutes/seconds \\
    // some definitions:
    // DD=decimal degrees
    // DMS=degrees/minutes/seconds
    //
    // How this works:
    // Say you have 32.872352 lat and -91.632563 long
    // The whole units of the decimal will remain the same, so
    // 1) multiply the decimal by 60 (0.872352 * 60 = 52.34112), the whole number becomes the minutes (52)
    // 2) take the remaining decimal (0.34112) and multiply by 60, this is now the seconds, round this to two decimal places (0.34221 * 60 = 20.53)
    // 3) string them all together 32 52 20.53
    //
    // ** LATITUDE ** \\
    // DEGREES \\
    // get integer value of latitude from db -> where $lat_dd is a floating point number like 32.872352
    $lat_dd = $_REQUEST['lat'];
    $long_dd = $_REQUEST['long'];
    if ($lat_dd<0) $lat_hemi="S";
    else $lat_hemi="N";
    if ($long_dd<0) $long_hemi="W";
    else $long_hemi = "E";
    $lat_dd=abs($lat_dd);
    $long_dd=abs($long_dd);
    $lat_dd_int = intval("$lat_dd");
    // MINUTES \\
    // get DD minutes (whats after the decimal point)-> Lat DD floating point – lat DD integer
    $lat_dd_m = $lat_dd – $lat_dd_int;
    // get DMS minutes as floating point -> 60 * DD minutes floating
    $lat_dms_m_float = 60 * $lat_dd_m;
    // get int value of lat DMS floating point
    $lat_dms_m_int = intval(“$lat_dms_m_float”);
    // SECONDS \\
    // get DD seconds -> DMS minutes floating – DMS minutes integer
    $lat_dd_s = $lat_dms_m_float – $lat_dms_m_int;
    // convert DD secs to DMS secs -> 60 * DD seconds
    $lat_dms_s = 60 * $lat_dd_s;
    // ** LONGITUDE ** \\
    // DEGREES \\
    // get integer value of longitude from db -> where $long_dd is a floating point number like -91.632563
    // this number remains negative
    $long_dd_int = intval(“$long_dd”);
    // MINUTES \\
    // get DD minutes (whats after the decimal point)-> Long DD floating point – long DD integer
    // use abs to get rid of the negative sign
    $long_dd_m = abs($long_dd – $long_dd_int);
    // get DMS minutes as floating point -> 60 * DD minutes floating
    $long_dms_m_float = 60 * $long_dd_m;
    // get int value of long DMS floating
    $long_dms_m_int = intval(“$long_dms_m_float”);
    // SECONDS \\
    // get DD seconds -> DMS minutes floating – DMS minutes integer
    $long_dd_s = $long_dms_m_float – $long_dms_m_int;
    // convert DD secs to DMS secs -> 60 * DD seconds
    $long_dms_s = 60 * $long_dd_s;
    // end conversion of lat/long \\
    // next just echo the output with the degree/minute/second symbols in place
    ?>
    Latitude: < ?php echo $lat_dd_int; ?>° < ?php echo $lat_dms_m_int; ?>’ < ?php echo round($lat_dms_s, 2); ?>”< ?php echo ($lat_hemi); ?>
    Longitude: < ?php echo $long_dd_int; ?>° < ?php echo $long_dms_m_int; ?>’ < ?php echo round($long_dms_s, 2); ?>”< ?php echo ($long_hemi); ?>

Leave a Reply