Class: GeoRuby::Gpx4r::GpxFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/geo_ruby/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.



12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#file_rootObject (readonly)

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



7
8
9
# File 'lib/geo_ruby/gpx.rb', line 7

def file_root
  @file_root
end

#record_countObject (readonly)

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



7
8
9
# File 'lib/geo_ruby/gpx.rb', line 7

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



28
29
30
31
32
33
34
35
36
# 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

Instance Method Details

#[](i) ⇒ Object

Returns record i



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

def [](i)
  get_record(i)
end

#as_line_stringObject Also known as: as_polyline

Return the GPX file as LineString



67
68
69
# File 'lib/geo_ruby/gpx.rb', line 67

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.



75
76
77
# 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

#closeObject

Closes a gpxfile



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

def close
  @gpx.close
end

#eachObject Also known as: each_record

Goes through each record



49
50
51
52
53
# File 'lib/geo_ruby/gpx.rb', line 49

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)


44
45
46
# File 'lib/geo_ruby/gpx.rb', line 44

def empty?
  record_count == 0
end

#envelopeObject

Return GPX Envelope



80
81
82
# File 'lib/geo_ruby/gpx.rb', line 80

def envelope
  @envelope ||= as_polygon.envelope
end

#recordsObject

Returns all the records



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

def records
  @points
end

#reload!Object

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



23
24
25
# File 'lib/geo_ruby/gpx.rb', line 23

def reload!
  initialize(@file_root)
end