Giter Site home page Giter Site 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.0.0, gpx requires at least Ruby 2.2 to run.

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

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

Watchers

James Cloos avatar

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.