class GeoRuby::Gpx4r::GpxFile
An interface to GPX files
Attributes
file_root[R]
record_count[R]
Public Class Methods
new(file, *opts)
click to toggle source
Opens a GPX file. Both “abc.shp” and “abc” are accepted.
# File lib/geo_ruby/gpx.rb, line 12 def initialize(file, *opts) # with_z = true, with_m = true) @file_root = file.gsub(/\.gpx$/i, '') fail MalformedGpxException.new('Missing GPX File') unless File.exist? @file_root + '.gpx' @points, @envelope = [], nil @gpx = File.open(@file_root + '.gpx', 'rb') opt = opts.reduce({}) { |a, e| e.merge(a) } parse_file(opt[:with_z], opt[:with_m]) end
open(file, *opts) { |gpxfile| ... }
click to toggle source
opens a GPX “file”. If a block is given, the GpxFile
object is yielded to it and is closed upon return. Else a call to open
is equivalent to GpxFile.new(...)
.
# File lib/geo_ruby/gpx.rb, line 28 def self.open(file, *opts) gpxfile = GpxFile.new(file, *opts) if block_given? yield gpxfile # gpxfile.close else gpxfile end end
Public Instance Methods
[](i)
click to toggle source
Returns record i
# File lib/geo_ruby/gpx.rb, line 57 def [](i) get_record(i) end
as_line_string()
click to toggle source
Return the GPX file as LineString
# File lib/geo_ruby/gpx.rb, line 67 def as_line_string GeoRuby::SimpleFeatures::LineString.from_points(@points) end
Also aliased as: as_polyline
as_polygon()
click to toggle source
Return the GPX file as a Polygon If the GPX isn't closed, a line from the first to the last point will be created to close it.
# File lib/geo_ruby/gpx.rb, line 75 def as_polygon GeoRuby::SimpleFeatures::Polygon.from_points([@points[0] == @points[-1] ? @points : @points.push(@points[0].clone)]) end
close()
click to toggle source
Closes a gpxfile
# File lib/geo_ruby/gpx.rb, line 39 def close @gpx.close end
each() { |get_record(i)| ... }
click to toggle source
Goes through each record
# File lib/geo_ruby/gpx.rb, line 49 def each (0...record_count).each do |i| yield get_record(i) end end
Also aliased as: each_record
empty?()
click to toggle source
Tests if the file has no record
# File lib/geo_ruby/gpx.rb, line 44 def empty? record_count == 0 end
envelope()
click to toggle source
Return GPX Envelope
# File lib/geo_ruby/gpx.rb, line 80 def envelope @envelope ||= as_polygon.envelope end
records()
click to toggle source
Returns all the records
# File lib/geo_ruby/gpx.rb, line 62 def records @points end
reload!()
click to toggle source
force the reopening of the files compsing the shp. Close before calling this.
# File lib/geo_ruby/gpx.rb, line 23 def reload! initialize(@file_root) end
Private Instance Methods
get_record(i)
click to toggle source
# File lib/geo_ruby/gpx.rb, line 86 def get_record(i) @points[i] end
parse_file(with_z, with_m)
click to toggle source
wpt => waypoint => TODO? rte(pt) => route trk(pt) => track /
# File lib/geo_ruby/gpx.rb, line 93 def parse_file(with_z, with_m) data = @gpx.read @file_mode = data =~ /trkpt/ ? '//trkpt' : (data =~ /wpt/ ? '//wpt' : '//rtept') Nokogiri.HTML(data).search(@file_mode).each do |tp| z = z.inner_text.to_f if with_z && z = tp.at('ele') m = m.inner_text if with_m && m = tp.at('time') @points << GeoRuby::SimpleFeatures::Point.from_coordinates([tp['lon'].to_f, tp['lat'].to_f, z, m], 4326, with_z, with_m) end close @record_count = @points.length envelope rescue => e raise MalformedGpxException.new("Bad GPX. Error: #{e}") # trackpoint.at("gpxdata:hr").nil? # heartrate end