GithubHelp home page GithubHelp logo

gpx's Introduction

GPX Gem

Build Status Code Climate

What It Does

This library reads GPX files and provides an API for reading and manipulating the data as objects. For more info on the GPX format, see http://www.topografix.com/gpx.asp.

In addition to parsing GPX files, this library is capable of converting Magellan NMEA files to GPX, converting GeoJSON data to GPX, and writing new GPX files. It can crop and delete rectangular areas within a file, and it also calculates some meta-data about the tracks and points in a file (such as distance, duration, average speed, etc).

Requirements

  • As of 1.1.1, gpx requires at least Ruby 2.7 to run.
  • As of 1.0.0, gpx requires at least Ruby 2.2 to run.

Installation

Add to your gemfile:

gem 'gpx'

Examples

Reading a GPX file, and cropping its contents to a given area:

gpx =  GPX::GPXFile.new(:gpx_file => filename)   # Read GPX file
bounds = GPX::Bounds.new(params)                 # Create a rectangular area to crop
gpx.crop(bounds)                                 # Crop it
gpx.write(filename)                              # Save it

Converting a Magellan track log to GPX:

if GPX::MagellanTrackLog::is_magellan_file?(filename)
 GPX::MagellanTrackLog::convert_to_gpx(filename, "#{filename}.gpx")
end

Converting GeoJSON data to GPX can be achieved by providing a file path, file, or the data in string format:

# Converting from a file name
gpx_file = GPX::GeoJSON.convert_to_gpx(geojson_file: 'mygeojsonfile.json')

# Converting from a string
data = JSON.generate(my_geojson_hash)
gpx_file = GPX::GeoJSON.convert_to_gpx(geojson_data: data)

Exporting an ActiveRecord to GPXFile (as Waypoints)

#
# Our active record in this example is called stop
#

# models/stop.rb
class Stop < ActiveRecord::Base
   # This model has the following attributes:
   # name
   # lat
   # lon
   # updated_at

   def self.to_gpx
     require 'GPX'
     gpx = GPX::GPXFile.new
     all.each do |stop|
       gpx.waypoints << GPX::Waypoint.new({name: stop.name, lat: stop.lat, lon: stop.lon, time: stop.updated_at})
     end
     gpx.to_s
   end
 end # class


# controllers/stops.rb
def index
   @stops = Stop.all
   respond_to do |format|
    format.html {render :index}
    format.gpx { send_data @stops.to_gpx, filename: controller_name + '.gpx' }
   end
end


# Add this line to config/initializers/mime_types.rb
Mime::Type.register "application/gpx+xml", :gpx


# To get the xml file:
#   http://localhost:3000/stops.gpx

You have a complete example on how to create a gpx file from scratch on tests/output_text.rb.

Notes

This library was written to bridge the gap between my Garmin Geko and my website, WalkingBoss.org (RIP). For that reason, it has always been more of a work-in-progress than an attempt at full GPX compliance. The track side of the library has seen much more use than the route/waypoint side, so if you're doing something with routes or waypoints, you may need to tweak some things.

Since this code uses XML to read an entire GPX file into memory, it is not the fastest possible solution for working with GPX data, especially if you are working with tracks from several days or weeks.

Finally, it should be noted that none of the distance/speed calculation or crop/delete code has been tested under International Date Line-crossing conditions. That particular part of the code will likely be unreliable if you're zig-zagging across 180 degrees longitude routinely.

License

MIT

gpx's People

Contributors

andrewhao avatar doc75 avatar dougfales avatar douglasr avatar gdott9 avatar kbb29 avatar koos303 avatar locodelassembly avatar merlos avatar moveson avatar nathanvda avatar niborg avatar rb2k avatar sh1nduu avatar simonneutert avatar tyrauber avatar wallclimber21 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gpx's Issues

Ability to get the lat max/min, lon max/min

Would be useful if this library let you get the maximum and minimum values for longitude and latitude. Use case being you would be able to draw a box on a map that fits the whole gpx file inside of it.

GPX to geoJson

Not really an issue, more like a question/help/feature request maybe.

I would like to convert a GPX file to geoJson format and then display it using MapBox.

Maybe it's just my limited knowledge, but I don't find any proper way of doing it, not with this gem or any other similar solutions.

Furthermore, would it be possible to extract, calculate elevation gains and drops from the data in GPX.

Generally, I am using Strava gpx from either activity or routes, and I would like to draw it on a MapBox enabled map plus have some additional information with it.

Sorry for maybe very simple question.
Thx

New release?

Tried to use this gem for it's GPX::GeoJSON functionality, but found out that currently released version 1.0 does not have #38 in it. Would it be possible to release new version so that the README.md on master matches released version? Or just in general to prevent confusion.

gpx installs latest version of nokogiri which is broken on Mac OS

FYI: I reinstalled the gpx gem and it pulled a new version of nokogiri (1.8.0) which fails to install (dies trying to compile libxml2 with and undefined macro). Looking at the nokogiri pages I see this is a known issue that has been around some time.

I note that gpx now requires nokogiri 1.7 -- I tried installing that but it has the same problems. 1.6.7.2 installed fine.

Mac os 10.12.5

Change track duration calculation

Hello,

I noticed that when a track contains several segments with a pause between the segment, then the duration is including the pause.
I would like to change that, by removing the pause in the calculation of duration (ultimate goal is to calculate average speed without pause)

I am just having some questions:

  1. Would it be acceptable Pulll Request or you consider pause being part of the track duration ?
  2. If this is acceptable, should we make the calculation only in gpx_file or should we delegate the calculation to segment and track class ?

I am willing to perform this modification. But I also want to prevent proposing a modification that would not be accepted ;-)

Let me know if anything is not clear.

The problem with parsing file

The problem with parsing file from official site http://www.topografix.com/gpx_sample_files.asp

gpx = GPX::GPXFile.new(:gpx_file => "#{RAILS_ROOT}/test/fixtures/fells_loop.gpx")
/opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/libxml-ruby-1.1.4/lib/libxml/node.rb:57: [BUG] Segmentation fault
ruby 1.8.7 (2009-12-24 patchlevel 248) [i686-darwin10.2.0], MBARI 0x6770, Ruby Enterprise Edition 2010.01

Abort trap

Accept StringIO and TempFile

This line checks if the input is of the the type File. It would be helpful if it also accepted Tempfile and StringIO so you could give it the result of open-uri to load gpx files from http.

New contributors?

Hey @dougfales - pulling a long thread here. I'm not really able to devote much time to gem maintenance these days, and would like to find another contributor to move this gem forward. Would you be open to doing so? Folks like @niborg have volunteered in the past (not to put you on the spot!) but might be good to explicitly put out a call for a maintainer in the README, if you are open. WDYT?

adding to the namespace for output files

HI

I am still fiddling with getting data out of the Garmin extension files and I have come across an issue where some other software is refusing to read GPX files with the garmin extension unless the proper xmlns headers are present so I have been cutting and pasting them in with an editor.

I have been looking at the gpx_file code and it looks like I should be able to pass these in as an option when creating the output object but I can't figure out exactly how.

Garmin headers define the following additional ns: xmlns:gpxx, xmlns:wptx1, xmlns:gpxtpx along with a long list of urls for the locations.

Any hints appreciated

Yanked v0.8

It was tagged at the wrong point. I wanted v0.8 to be the merge point of the two forks, but I neglected to tag it correctly.

New merge point will be v0.8.1

is there a way to it gpx to parse the garmin extension?

Hi

I need to parse some gpx files from a Garmin Etrex 30 which have some data the extension that I need. The schemas are available.
I have had a brief look at the code but I can't figure out any way of getting data out.

Pythagorean distance is confusing and not useful

The TrackPoint#pythagorean_distance_from method returns units in radians, although the comment line above it indicates it returns units in km.

Even if the result is multiplied by RADIUS to return km, the distance is highly inaccurate at most latitudes, because the method assumes a degree of longitude is equal to ~111 km regardless of latitude. For example, at 37 degrees latitude (the latitude of the points in the test file), calculations between points with significant difference in longitude are off by about 26%.

I would suggest the TrackPoint#pythagorean_distance_from method be deleted to avoid confusion. This would require removal of Segment#pythagorean_distance as well. Comments note that this method is not currently in use.

Dosen't work with Ruby Enterprise Edition

Generate error "TypeError: The first argument must be a document or node " on Ruby Enterprise Edition.

TypeError: The first argument must be a document or node.
/opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/libxml-ruby-1.1.4/lib/libxml/node.rb:57:in register_namespaces_from_node' /opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/libxml-ruby-1.1.4/lib/libxml/node.rb:57:incontext'
/opt/ruby-enterprise-1.8.7-2010.01/lib/ruby/gems/1.8/gems/libxml-ruby-1.1.4/lib/libxml/node.rb:71:in find' gpx (0.6) [v] lib/gpx/point.rb:42:ininitialize'
gpx (0.6) [v] lib/gpx/trackpoint.rb:31:in initialize' gpx (0.6) [v] lib/gpx/segment.rb:51:innew'
gpx (0.6) [v] lib/gpx/segment.rb:51:in initialize' gpx (0.6) [v] lib/gpx/segment.rb:50:ineach'
gpx (0.6) [v] lib/gpx/segment.rb:50:in initialize' gpx (0.6) [v] lib/gpx/track.rb:46:innew'
gpx (0.6) [v] lib/gpx/track.rb:46:in initialize' gpx (0.6) [v] lib/gpx/track.rb:45:ineach'
gpx (0.6) [v] lib/gpx/track.rb:45:in initialize' gpx (0.6) [v] lib/gpx/gpx_file.rb:83:innew'
gpx (0.6) [v] lib/gpx/gpx_file.rb:83:in initialize' gpx (0.6) [v] lib/gpx/gpx_file.rb:82:ineach'
gpx (0.6) [v] lib/gpx/gpx_file.rb:82:in `initialize'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.