使用awk将GPS位置转换为“纬度"和“经度 [英] Use awk to convert GPS Position to Latitude & Longitude

查看:137
本文介绍了使用awk将GPS位置转换为“纬度"和“经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a bash script that renames files based on EXIF headers. exiftool returns the following GPS Position string, which I need to format into Latitude/Longitude coordinates for use with Google Maps API.

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

Google地图:

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:

What I need is a valid awk command to strip the "deg" and NSEW text, and divide by 3600 and 60 per this post:

如何在目标c中将北向gps和西向gps转换为纬度/经度

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

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

请帮助!

推荐答案

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

For this particular case, I would write it as below if special characters were a problem. Ofcourse it has the disadvantage that error checks would not be as stringent.

 # 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 }'

这篇关于使用awk将GPS位置转换为“纬度"和“经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆