Class: GeoRuby::Gpx4r::GpxFile
- Inherits:
-
Object
- Object
- GeoRuby::Gpx4r::GpxFile
- Includes:
- Enumerable
- Defined in:
- lib/geo_ruby/gpx.rb
Overview
An interface to GPX files
Instance Attribute Summary collapse
-
#file_root ⇒ Object
readonly
:xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax, :file_length.
-
#record_count ⇒ Object
readonly
:xmin, :ymin, :xmax, :ymax, :zmin, :zmax, :mmin, :mmax, :file_length.
Class Method Summary collapse
-
.open(file, *opts) ⇒ Object
opens a GPX “file”.
Instance Method Summary collapse
-
#[](i) ⇒ Object
Returns record
i
. -
#as_line_string ⇒ Object
(also: #as_polyline)
Return the GPX file as LineString.
-
#as_polygon ⇒ Object
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.
-
#close ⇒ Object
Closes a gpxfile.
-
#each ⇒ Object
(also: #each_record)
Goes through each record.
-
#empty? ⇒ Boolean
Tests if the file has no record.
-
#envelope ⇒ Object
Return GPX Envelope.
-
#initialize(file, *opts) ⇒ GpxFile
constructor
Opens a GPX file.
-
#records ⇒ Object
Returns all the records.
-
#reload! ⇒ Object
force the reopening of the files compsing the shp.
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_root ⇒ Object (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_count ⇒ Object (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_string ⇒ Object 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_polygon ⇒ Object
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 |
#close ⇒ Object
Closes a gpxfile
39 40 41 |
# File 'lib/geo_ruby/gpx.rb', line 39 def close @gpx.close end |
#each ⇒ Object 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
44 45 46 |
# File 'lib/geo_ruby/gpx.rb', line 44 def empty? record_count == 0 end |
#envelope ⇒ Object
Return GPX Envelope
80 81 82 |
# File 'lib/geo_ruby/gpx.rb', line 80 def envelope @envelope ||= as_polygon.envelope end |
#records ⇒ Object
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 |