Ruby GPX file parser

▼魔方 西西 提交于 2019-12-22 10:07:47

问题


Advise what can parse GPX file in Ruby?

I tried gpx, but it does not work with Ruby Enterprise Edition (https://github.com/dougfales/gpx/issues/1).

I would not like to write a parser.


回答1:


I was looking for the same thing but in the end a GPX file is just another XML file so you can parse it with XML parsing libraries like Nokogiri. Here is how I extract all the latitudes and longitudes form a GPX log:

#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML(open(my-log.gpx))
trackpoints = doc.xpath('//xmlns:trkpt')
points = Array.new
trackpoints.each do |trkpt|
  points << [trkpt.xpath('@lat').to_s.to_f, trkpt.xpath('@lon').to_s.to_f]
end

There are probably better ways to do this but this works for me.



来源:https://stackoverflow.com/questions/2987480/ruby-gpx-file-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!