dongyidao1461 2012-05-26 21:31
浏览 70

GPS坐标转换 - PHP

I have the following code to extract and display EXIF information from an image.

    <?
    // +++++++++++++++++++++++++ //
    // +++++++ GPS EXIF +++++++ //
    // +++++++++++++++++++++++++ //

    // --- Change the 'image1.jpg' value below to the path of your file. --- //
    $Image = 'image1.jpg';

    // --- The EXIF read command --- //
    $ReadEXIFData = read_exif_data($Image, 0, true);


    // +++++++++++++++++ //
    // +++ FILE INFO +++ //
    // +++++++++++++++++ //
    // --- Get the filename --- //
    $Filename = $ReadEXIFData['FILE']['FileName'];
    // --- Get the file size --- //
    $FileSize = $ReadEXIFData['FILE']['FileSize'];
    // --- Converts to user-friendly kb, mb and gb sizes --- //
    if ($FileSize >= 1024) { $ImageFileSize = number_format($FileSize / 1024, 2).'kb'; }
    elseif ($FileSize >= 1048576) { $ImageFileSize = number_format($FileSize / 1048576, 2).'mb'; }
    elseif ($FileSize >= 1073741824) { $ImageFileSize = number_format($FileSize / 1073741824, 2).'gb'; }
    // --- Get the file height --- //
    $FileHeight = $ReadEXIFData['COMPUTED']['Height'].'px';
    // --- Get the file height --- //
    $FileWidth = $ReadEXIFData['COMPUTED']['Width'].'px';



    // +++++++++++++++++++++++ //
    // +++ GPS COORDINATES +++ //
    // +++++++++++++++++++++++ //
    // --- Get GPS hemisphire --- //
    $GPSHemisphire = $ReadEXIFData["GPS"]["GPSLatitudeRef"];

    // --- Get GPS degrees latitude --- //
    $GPSDegreesLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][0]);
    // --- Get GPS minutes latitude --- //
    $GPSMinutesLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][1]);
    // --- Get GPS seconds latitude --- //
    $GPSSecondsLat = intval($ReadEXIFData["GPS"]["GPSLatitude"][2]);
    //  --- Get GPS Hemisphere latitude --- // 
    $GPSSecondsLat2 = ($ReadEXIFData["GPS"]["GPSLatitudeRef"]);
    // --- Decimal Latitude goes here --- //


    // --- Get GPS degrees longitude --- //
    $GPSDegreesLon = intval($ReadEXIFData["GPS"]["GPSLongitude"][0]);
    // --- Get GPS minutes longitude --- //
    $GPSMinutesLon = intval($ReadEXIFData["GPS"]["GPSLongitude"][1]);
    // --- Get GPS seconds longitude --- //
    $GPSSecondsLon = intval($ReadEXIFData["GPS"]["GPSLongitude"][2]);
    //  --- Get GPS Hemisphere longitude --- // 
    $GPSSecondsLon2 = ($ReadEXIFData["GPS"]["GPSLongitudeRef"]);
    // --- Decimal Longitude goes here --- //


    // --- Get GPS altitude --- //
    $GPSAltitude = $ReadEXIFData["GPS"]["GPSAltitude"][0];


    ?>

The information is then printed in an html page as can be seen here http://ukf.com/easyexif/easyexif3.php

The problem is that the GPS co-ordinates returned are in Degrees/Minutes/Seconds.

To convert them to Decimal I have this script:

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

    function divide($a) {
     $e = explode('/', $a);
     if (!$e[0] || !$e[1]) {
      return 0;
     } else {
      return $e[0] / $e[1];
     }
    }

    function getGPS() {
     global $exif;
     if ($exif) {  $lat = $exif['GPS']['GPSLatitude'];
     $log = $exif['GPS']['GPSLongitude'];
      if (!$lat || !$log) return null;
      $lat_degrees = divide($lat[0]);
      $lat_minutes = divide($lat[1]);
      $lat_seconds = divide($lat[2]);
      $lat_hemi = $exif['GPS']['GPSLatitudeRef'];
      $log_degrees = divide($log[0]);
      $log_minutes = divide($log[1]);
      $log_seconds = divide($log[2]);
      $log_hemi = $exif['GPS']['GPSLongitudeRef'];
      $lat_decimal = toDecimal($lat_degrees, $lat_minutes, $lat_seconds, $lat_hemi);
      $log_decimal = toDecimal($log_degrees, $log_minutes, $log_seconds, $log_hemi);
      return array($lat_decimal, $log_decimal);
     } else{
      return null;
     }
    }

What I need is the Decimal Logitude and Latitude returned in the form

    $GPSDECLON
    $GPSDECLAT

so I can include them with the above information in my html page.

My PHP knowledge currently is so limited I have no idea how to go about including the conversion into my existing script.

Can anyone help?? If so it would be greatly apreciated.

  • 写回答

1条回答 默认 最新

  • dongqian5569 2012-05-26 22:44
    关注

    Something is amiss with the seconds presented on the web page - they have to be in the range [0,60[

    That said, the conversion is what it says in the first function of the php code you posted:

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

    That is, there are 60 minutes to a degree, and 60 seconds to a minute. If you are in the South hemisphere you flip the latitude, if you are in the "West hemisphere" you flip the longitude value, these are just basically signs of the two measurements.

    So...

    $GPSDECLON = toDecimal($GPSDegreesLon, $GPSMinutesLon, $GPSSecondsLon, $EWHemisphere)

    Where $EWHemisphere is "E" or "W" depending on how your hemisphere variable encodes it. Then it is a similar line for the latitude except you need to know if it is "N" or "S" for the sign.

    Hope that helps and I apologize if I'm missing your question here...

    评论

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单