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.



8
9
10
11
12
13
14
15
16
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 8

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



3
4
5
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 3

def file_root
  @file_root
end

#record_countObject (readonly)

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



3
4
5
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 3

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



24
25
26
27
28
29
30
31
32
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 24

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



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

def [](i)
  get_record(i)
end

#as_line_stringObject Also known as: as_polyline

Return the GPX file as LineString



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

def as_line_string
  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.



71
72
73
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 71

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

#closeObject

Closes a gpxfile



35
36
37
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 35

def close
  @gpx.close
end

#eachObject Also known as: each_record

Goes through each record



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

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)


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

def empty?
  record_count == 0
end

#envelopeObject

Return GPX Envelope



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

def envelope
  @envelope ||= as_polygon.envelope
end

#recordsObject

Returns all the records



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

def records
  @points
end

#reload!Object

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



19
20
21
# File 'lib/geo_ruby/gpx4r/gpx.rb', line 19

def reload!
  initialize(@file_root)
end