Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ruby将exif gps数据数组格式转换为纬度和经度浮动_Ruby_Gps - Fatal编程技术网

使用ruby将exif gps数据数组格式转换为纬度和经度浮动

使用ruby将exif gps数据数组格式转换为纬度和经度浮动,ruby,gps,Ruby,Gps,从文件检索exif数据时,坐标以3个数字的数组形式提供给您 i.exif['gps_latitude'] => ["6/1", "4/1", "911/50"] 这需要转换为更常见的浮点格式以供一般使用。您可以使用以下代码在ruby中进行转换: arr = exif.gps_latitude pic.lat = arr[0].to_f + arr[1].to_f/60 + arr[2].to_f/3600 pic.lat *= exif.gps_latitude_ref == 'S' ?

从文件检索exif数据时,坐标以3个数字的数组形式提供给您

i.exif['gps_latitude']
=> ["6/1", "4/1", "911/50"]

这需要转换为更常见的浮点格式以供一般使用。

您可以使用以下代码在ruby中进行转换:

arr = exif.gps_latitude
pic.lat = arr[0].to_f + arr[1].to_f/60 + arr[2].to_f/3600
pic.lat *= exif.gps_latitude_ref == 'S' ? -1 : 1

arr = exif.gps_longitude
pic.lng = arr[0].to_f + arr[1].to_f/60 + arr[2].to_f/3600
pic.lng *= exif.gps_longitude_ref == 'W' ? -1 : 1

我能想到的最直接的方法是将字符串转换为数字:

并通过以下方式计算十进制度数的值:

lat = d + m / 60 + s / 3600
#=> (1092911/180000)
要获取浮点值,请执行以下操作:

lat.to_f
#=> 6.071727777777777

此代码忽略分母,例如
50
。这3个数字转换为
6°4′18.22〃
,而不是
6°4′911〃
lat.to_f
#=> 6.071727777777777