Class: GeoRuby::Gpx4r::GpxFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/geo_ruby/gpx4r/gpx.rb

Overview

An interface to GPX files

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, *opts) ⇒ GpxFile

Opens a GPX file. Both “abc.shp” and “abc” are accepted.



14
15
16
17
18
19
20
21
22
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 14

def initialize(file, *opts) #with_z = true, with_m = true)
  @file_root = file.gsub(/\.gpx$/i,"")
  raise MalformedGpxException.new("Missing GPX File") unless
    File.exists? @file_root + ".gpx"
  @points, @envelope = [], nil
  @gpx = File.open(@file_root + ".gpx", "rb")
  opt = opts.inject({}) { |o, h| h.merge(o) }
  parse_file(opt[:with_z], opt[:with_m])
end

Instance Attribute Details

#file_rootObject (readonly)

:xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax, :file_length



9
10
11
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 9

def file_root
  @file_root
end

#record_countObject (readonly)

:xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax, :file_length



9
10
11
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 9

def record_count
  @record_count
end

Class Method Details

.open(file, *opts) ⇒ Object

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(...).



30
31
32
33
34
35
36
37
38
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 30

def self.open(file, *opts)
  gpxfile = GpxFile.new(file, *opts)
  if block_given?
    yield gpxfile
    # gpxfile.close
  else
    gpxfile
  end
end

Instance Method Details

#[](i) ⇒ Object

Returns record i



59
60
61
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 59

def [](i)
  get_record(i)
end

#as_line_stringObject Also known as: as_polyline

Return the GPX file as LineString



69
70
71
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 69

def as_line_string
  GeoRuby::SimpleFeatures::LineString.from_points(@points)
end

#as_polygonObject

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.



77
78
79
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 77

def as_polygon
  GeoRuby::SimpleFeatures::Polygon.from_points([@points[0] == @points[-1] ?  @points : @points.push(@points[0].clone)])
end

#closeObject

Closes a gpxfile



41
42
43
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 41

def close
  @gpx.close
end

#eachObject Also known as: each_record

Goes through each record



51
52
53
54
55
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 51

def each
  (0...record_count).each do |i|
    yield get_record(i)
  end
end

#empty?Boolean

Tests if the file has no record

Returns:

  • (Boolean)


46
47
48
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 46

def empty?
  record_count == 0
end

#envelopeObject

Return GPX Envelope



82
83
84
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 82

def envelope
  @envelope ||= as_polygon.envelope
end

#recordsObject

Returns all the records



64
65
66
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 64

def records
  @points
end

#reload!Object

force the reopening of the files compsing the shp. Close before calling this.



25
26
27
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 25

def reload!
  initialize(@file_root)
end