用Ruby解析经纬度 [英] Parsing latitude and longitude with Ruby

查看:108
本文介绍了用Ruby解析经纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Ruby下解析一些用户提交的包含纬度和经度的字符串。

结果应该在双精度型中给出



示例:

 08º04'49''09º13'12''

结果:

  8.080278 9.22 

我看过Geokit和GeoRuby,但还没有找到解决方案。任何提示?

''.gsub(/(\ d +)°(\d +)'(\ d +)'')
$ 1.to_f + $ 2.to_f / 60 + $ 3.to_f / 3600
结束
#=> 8.08027777777778 9.22

编辑:或者将结果作为一个浮点数组:

 08°04'49''09°13'12''。scan(/(\ d +)°(\d + )'(\d +)''/)。map do | d,m,s | 
d.to_f + m.to_f / 60 + s.to_f / 3600
end
#=> [8.08027777777778,9.22]


I need to parse some user submitted strings containing latitudes and longitudes, under Ruby.

The result should be given in a double

Example:

08º 04' 49'' 09º 13' 12''

Result:

8.080278 9.22

I've looked to both Geokit and GeoRuby but haven't found a solution. Any hint?

解决方案

"08° 04' 49'' 09° 13' 12''".gsub(/(\d+)° (\d+)' (\d+)''/) do
  $1.to_f + $2.to_f/60 + $3.to_f/3600
end
#=> "8.08027777777778 9.22"

Edit: or to get the result as an array of floats:

"08° 04' 49'' 09° 13' 12''".scan(/(\d+)° (\d+)' (\d+)''/).map do |d,m,s|
  d.to_f + m.to_f/60 + s.to_f/3600
end
#=> [8.08027777777778, 9.22]

这篇关于用Ruby解析经纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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