【问题标题】:Use awk to convert GPS Position to Latitude & Longitude使用 awk 将 GPS 位置转换为纬度和经度
【发布时间】:2015-10-01 04:53:29
【问题描述】:

我正在编写一个基于 EXIF 标头重命名文件的 bash 脚本。 exiftool 返回以下 GPS 位置字符串,我需要将其格式化为纬度/经度坐标以便与 Google Maps API 一起使用。

GPS Position : 40 deg 44' 49.36" N, 73 deg 56' 28.18" W

谷歌地图:

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611

这是我的代码

awk -v FS="[ \t]" '{print $0,substr($1,length($1),1)substr($2,length($2),1)}' $1 \
| sed 's/\xc2\xb0\([0-9]\{1,2\}\).\([NEWS]\)/ \1 0 \2/g;s/\xc2\xb0\([NEWS]\)/ 0 0 \1/g;s/[^0-9NEWS]/ /g' \
| awk '{if ($9=="NE") {printf ("%.4f\t%.4f\n",$1+$2/60+$3/3600,$5+$6/60+$7/3600)} \
else if ($9=="NW") {printf ("%.4f\t%.4f\n",$1+$2/60+$3/3600,-($5+$6/60+$7/3600))} \
else if ($9=="SE") {printf ("%.4f\t%.4f\n",-($1+$2/60+$3/3600),$5+$6/60+$7/3600)} \
else if ($9=="SW") {printf ("%.4f\t%.4f\n",-($1+$2/60+$3/3600),-($5+$6/60+$7/3600))}}' 

我收到此错误:

sed: RE error: illegal byte sequence

我需要一个有效的 awk 命令来去除“deg”和 NSEW 文本,并除以 3600 和 60 这篇文章:

how to convert gps north and gps west to lat/long in objective c

40 度 44' 49.36" N,73 度 56' 28.18" W > 40.7470444,-073.9411611

请帮忙!

【问题讨论】:

标签: bash awk gps


【解决方案1】:

对于这种特殊情况,如果特殊字符有问题,我会写如下。当然它的缺点是错误检查不会那么严格。

 # remove the string till the colon and characters other than numbers, dots, spaces and NSEW
 sed 's!^.*:\|[^0-9\. NSEW]!!g' filename |
 # calculate the latitude and longitude with some error checks
 awk '/^\s*([0-9.]+\s+){3}[NS]\s+([0-9.]+\s+){3}[EW]\s*$/ {
        lat=($1+$2/60+$3/3600); if ($4 == "S") lat=-lat;
        lon=($5+$6/60+$7/3600); if ($8 == "W") lon=-lon;
        printf("%.4f,%.4f\n", lat, lon); next  }

      { print "Error on line " NR; exit 1 }'

【讨论】:

  • 在这种情况下,sed 似乎对源字符串(GPS 位置)没有影响。我正在尝试,但脚本没有返回任何内容(如果我添加您的支票,则“第 1 行出错”除外):echo "40 deg 44' 49.36\" N, 73 deg 56' 28.18\" W" | awk '/^\s*([0-9.]+\s+){3}[NS]\s+([0-9.]+\s+){3}[EW]\s*$/ { lat=($1+$2/60+$3/3600); if ($4 == "S") lat=-lat; lon=($5+$6/60+$7/3600); if ($8 == "W") lon=-lon; printf("%.4f,%.4f\n", lat, lon); }'
【解决方案2】:

在 PHP 中:

$parts = explode(" ",str_replace(array("deg",",","'","\""),"",$argv[1]));

print_r($parts);

$lat_deg = $parts[0];
$lat_min = $parts[1];
$lat_sec = $parts[2];
$lat_dir = $parts[3];

$lon_deg = $parts[4];
$lon_min = $parts[5];
$lon_sec = $parts[6];
$lon_dir = $parts[7];

if ($lat_dir == "N") {
    $lat_sin = "+";
    } else {
    $lat_sin = "-";
    }

if ($lon_dir == "N") {
    $lon_sin = "+";
    } else {
    $lon_sin = "-";
    }

$latitiude = $lat_sin.($lat_deg+($lat_min/60)+($lat_sec/3600));
$longitude = $lon_sin.($lon_deg+($lon_min/60)+($lon_sec/3600));

echo substr($latitiude,0,-5).",".substr($longitude,0,-5);

【讨论】:

    猜你喜欢
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 2011-03-28
    • 2013-10-30
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多