gpx 0.7 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +82 -0
- data/Gemfile +4 -0
- data/README.md +100 -0
- data/Rakefile +10 -31
- data/bin/gpx_distance +10 -0
- data/bin/gpx_smooth +63 -0
- data/gpx.gemspec +17 -9
- data/lib/gpx.rb +16 -16
- data/lib/gpx/bounds.rb +43 -52
- data/lib/gpx/gpx.rb +17 -20
- data/lib/gpx/gpx_file.rb +292 -217
- data/lib/gpx/magellan_track_log.rb +105 -107
- data/lib/gpx/point.rb +58 -74
- data/lib/gpx/route.rb +31 -43
- data/lib/gpx/segment.rb +229 -198
- data/lib/gpx/track.rb +103 -109
- data/lib/gpx/trackpoint.rb +37 -12
- data/lib/gpx/version.rb +3 -0
- data/lib/gpx/waypoint.rb +3 -25
- data/tests/gpx10_test.rb +3 -3
- data/tests/gpx_file_test.rb +27 -25
- data/tests/gpx_files/arches.gpx +1 -1
- data/tests/gpx_files/big.gpx +1 -1
- data/tests/gpx_files/one_track.gpx +2 -1
- data/tests/gpx_files/waypoints.gpx +2 -0
- data/tests/magellan_test.rb +3 -3
- data/tests/output_test.rb +14 -17
- data/tests/route_test.rb +7 -7
- data/tests/segment_test.rb +40 -8
- data/tests/track_file_test.rb +3 -3
- data/tests/track_point_test.rb +30 -0
- data/tests/track_test.rb +20 -20
- data/tests/waypoint_test.rb +11 -7
- metadata +61 -10
- data/ChangeLog +0 -60
- data/Gemfile.lock +0 -23
- data/README.rdoc +0 -46
- data/tests/output/myoutput.gpx +0 -759
- data/tests/output/new_gpx_file_from_scratch.gpx +0 -105
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d9422311878830d3e5f57b2a671379cc2c5d972
|
4
|
+
data.tar.gz: a08f7e74bb07c826d57fbaf8095c54d6057ab5cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 904238365d5532474c876b710dd62a025e211f626e8a95585046d0473e9eb046c191de2c87418a3c59fbf9a047ce6fee67ed514f82f85b81d58ae944262d8436
|
7
|
+
data.tar.gz: e08bde0c7857f130ab3c6ad24a565c922377a5b965fe54b5be4d3af8ec609c1d77af8aeed39ebf7cd31a0d5f9e431b32f447fb11763c4e71e4dd9111658e4711
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
## [0.8.1] - 2015-08-01
|
2
|
+
|
3
|
+
* Added support for a GPX `desc` tag. (@doc75)
|
4
|
+
|
5
|
+
## [0.8] - 2015-05-14
|
6
|
+
|
7
|
+
### Introduced changes from andrewhao/gpx fork:
|
8
|
+
|
9
|
+
* Fix Rakefiles and move to use Nokigiri (Guillaume Dott)
|
10
|
+
* Fix compatibility with 1.0 GPX schemas (Douglas Robertson)
|
11
|
+
* Added Ruby 2.2 compatibility and GPX track smoothing capabilities
|
12
|
+
(@kbb29)
|
13
|
+
* Adding time to waypoints, restore xmlns:nsi attribute (@merlos)
|
14
|
+
* Refactor distance calculation methods to the TrackPoint (Andrew Hao)
|
15
|
+
* Adding Travis, Code Climate badges.
|
16
|
+
|
17
|
+
## [0.7] - 2015-05-09
|
18
|
+
|
19
|
+
* @rb2k: Make tests and code ruby 1.9 compatible (#3)
|
20
|
+
* Switch XML parsing library to hpricot from libxml
|
21
|
+
* @ustas-v: Fix bug #2160. Invalid elevation segment. (#4)
|
22
|
+
* Doug Fales explicitly released this library under MIT license.
|
23
|
+
* Updated changelog and fixing some indentation in waypoint.rb.
|
24
|
+
|
25
|
+
## [0.6] - 2010-02-27
|
26
|
+
|
27
|
+
* Putting the gem building stuff into a gemspec.
|
28
|
+
* Fixing some tests since git does not believe in empty directories.
|
29
|
+
* Fixing README formatting.
|
30
|
+
* README edits.
|
31
|
+
* More rdoc tweaks.
|
32
|
+
* Changing README to rdoc ext for github.
|
33
|
+
* Adding the ability to write GPX to a string in addition to a file. Thanks to Douglas Robertson for the patch.
|
34
|
+
* Adding a patch from Douglas Robertson that allows using version 1.0 of the schema for output.
|
35
|
+
|
36
|
+
## [0.5] - 2009-07-07
|
37
|
+
|
38
|
+
* Adding changelog.
|
39
|
+
* Revving to version 0.5.
|
40
|
+
* Changing my contact email address.
|
41
|
+
* Patches from Tom Verbeure (mtbguru.com) to work with libxml-ruby 1.x.
|
42
|
+
* Patch from Kang-min Liu to support speed element.
|
43
|
+
|
44
|
+
## [0.4] - 2008-02-19
|
45
|
+
|
46
|
+
* Revving to 0.4.
|
47
|
+
* Adding some new unit tests and fixing several file export bugs reported by Jochen Topf. New unit tests also uncovered a bug where the number of trackpoints reported in a file was twice the actual number.
|
48
|
+
|
49
|
+
## [0.3] - 2008-02-11
|
50
|
+
|
51
|
+
* Going to version 0.3.
|
52
|
+
* Updating unit tests in light of recent fixes to routes and waypoints code.
|
53
|
+
* Thanks to Mike Gauland for discovering some route- and waypoint-related bugs. I've fixed them and also added #to_s on Waypoint so it's easier to debug.
|
54
|
+
* Thanks to Christian Koerner for finding and fixing these bugs in the waypoint code.
|
55
|
+
* Another patch from Gaku Ueda. This one allows you to pass in a string of GPX data using the :gpx_date => option. Thanks Gaku!
|
56
|
+
|
57
|
+
## [0.2] - 2007-11-30
|
58
|
+
|
59
|
+
* Updating the version #.
|
60
|
+
* Updates courtesy of Gaku Ueda:
|
61
|
+
* Adding support for GPX 1.0 as well as 1.1 (since libxml namespace parsing was hard-coded to 1.1. previously).
|
62
|
+
* Adding a GPX 1.0 unit test file.
|
63
|
+
* Miscellaneous updates to make it work with Ruby 1.8.6.
|
64
|
+
* First stab at using libxml-ruby instead of REXML. I'm seeing the unit tests finish in under 14 seconds. That is compared to 2 minutes using REXML. (Doug Fales <doug@falesafeconsulting.com>)
|
65
|
+
* Fixing more nil time exceptions. (Doug Fales <doug@falesafeconsulting.com>)
|
66
|
+
* Fixing an exception in contains_time?. (Doug Fales <doug@falesafeconsulting.com>)
|
67
|
+
* A couple of fixes to make the library comply with the different attribute names possible on the bounds element. (Doug Fales <doug@falesafeconsulting.com>)
|
68
|
+
* Fixing nil time bug. (Doug Fales <doug@falesafeconsulting.com>)
|
69
|
+
|
70
|
+
## [0.1] - 2006-10-14
|
71
|
+
|
72
|
+
* Initial import of gpx gem. (Doug Fales <doug@falesafeconsulting.com>)
|
73
|
+
|
74
|
+
[unreleased]: https://github.com/dougfales/gpx/compare/v0.8...HEAD
|
75
|
+
[0.8]: https://github.com/dougfales/gpx/compare/v0.7...v0.8
|
76
|
+
[0.7]: https://github.com/dougfales/gpx/compare/v0.6...v0.7
|
77
|
+
[0.6]: https://github.com/dougfales/gpx/compare/v0.5...v0.6
|
78
|
+
[0.5]: https://github.com/dougfales/gpx/compare/v0.4...v0.5
|
79
|
+
[0.4]: https://github.com/dougfales/gpx/compare/v0.3...v0.4
|
80
|
+
[0.3]: https://github.com/dougfales/gpx/compare/v0.2...v0.3
|
81
|
+
[0.2]: https://github.com/dougfales/gpx/compare/v0.1...v0.2
|
82
|
+
[0.1]: https://github.com/dougfales/gpx/commit/371a3fa0b971c9a66c66f941eb4f5c3fa18e424f
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# GPX Gem
|
2
|
+
|
3
|
+
[<img src="https://travis-ci.org/dougfales/gpx.svg" alt="Build Status" />](https://travis-ci.org/dougfales/gpx)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/dougfales/gpx/badges/gpa.svg)](https://codeclimate.com/github/dougfales/gpx)
|
5
|
+
|
6
|
+
Copyright (C) 2006 Doug Fales doug@falesafeconsulting.com
|
7
|
+
|
8
|
+
Released under the MIT License.
|
9
|
+
|
10
|
+
## What It Does
|
11
|
+
|
12
|
+
This library reads GPX files and provides an API for reading and manipulating
|
13
|
+
the data as objects. For more info on the GPX format, see
|
14
|
+
http://www.topografix.com/gpx.asp.
|
15
|
+
|
16
|
+
In addition to parsing GPX files, this library is capable of converting
|
17
|
+
Magellan NMEA files to GPX, and writing new GPX files. It can crop and delete
|
18
|
+
rectangular areas within a file, and it also calculates some meta-data about
|
19
|
+
the tracks and points in a file (such as distance, duration, average speed,
|
20
|
+
etc).
|
21
|
+
|
22
|
+
## Examples
|
23
|
+
|
24
|
+
Reading a GPX file, and cropping its contents to a given area:
|
25
|
+
```ruby
|
26
|
+
gpx = GPX::GPXFile.new(:gpx_file => filename) # Read GPX file
|
27
|
+
bounds = GPX::Bounds.new(params) # Create a rectangular area to crop
|
28
|
+
gpx.crop(bounds) # Crop it
|
29
|
+
gpx.write(filename) # Save it
|
30
|
+
```
|
31
|
+
|
32
|
+
Converting a Magellan track log to GPX:
|
33
|
+
```ruby
|
34
|
+
if GPX::MagellanTrackLog::is_magellan_file?(filename)
|
35
|
+
GPX::MagellanTrackLog::convert_to_gpx(filename, "#{filename}.gpx")
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Exporting an ActiveRecord to GPXFile (as Waypoints)
|
40
|
+
```ruby
|
41
|
+
#
|
42
|
+
# Our active record in this example is called stop
|
43
|
+
#
|
44
|
+
|
45
|
+
# models/stop.rb
|
46
|
+
class Stop < ActiveRecord::Base
|
47
|
+
# This model has the following attributes:
|
48
|
+
# name
|
49
|
+
# lat
|
50
|
+
# lon
|
51
|
+
# updated_at
|
52
|
+
|
53
|
+
def self.to_gpx
|
54
|
+
require 'GPX'
|
55
|
+
gpx = GPX::GPXFile.new
|
56
|
+
all.each do |stop|
|
57
|
+
gpx.waypoints << GPX::Waypoint.new({name: stop.name, lat: stop.lat, lon: stop.lon, time: stop.updated_at})
|
58
|
+
end
|
59
|
+
gpx.to_s
|
60
|
+
end
|
61
|
+
end # class
|
62
|
+
|
63
|
+
|
64
|
+
# controllers/stops.rb
|
65
|
+
def index
|
66
|
+
@stops = Stop.all
|
67
|
+
respond_to do |format|
|
68
|
+
format.html {render :index}
|
69
|
+
format.gpx { send_data @stops.to_gpx, filename: controller_name + '.gpx' }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
# Add this line to config/initializers/mime_types.rb
|
75
|
+
Mime::Type.register "application/gpx+xml", :gpx
|
76
|
+
|
77
|
+
|
78
|
+
# To get the xml file:
|
79
|
+
# http://localhost:3000/stops.gpx
|
80
|
+
```
|
81
|
+
|
82
|
+
You have a complete example on how to create a gpx file from scratch on `tests/output_text.rb`.
|
83
|
+
|
84
|
+
|
85
|
+
## Notes
|
86
|
+
|
87
|
+
This library was written to bridge the gap between my Garmin Geko
|
88
|
+
and my website, WalkingBoss.org (RIP). For that reason, it has always been more of a
|
89
|
+
work-in-progress than an attempt at full GPX compliance. The track side of the
|
90
|
+
library has seen much more use than the route/waypoint side, so if you're doing
|
91
|
+
something with routes or waypoints, you may need to tweak some things.
|
92
|
+
|
93
|
+
Since this code uses XML to read an entire GPX file into memory, it is not
|
94
|
+
the fastest possible solution for working with GPX data, especially if you are
|
95
|
+
working with tracks from several days or weeks.
|
96
|
+
|
97
|
+
Finally, it should be noted that none of the distance/speed calculation or
|
98
|
+
crop/delete code has been tested under International Date Line-crossing
|
99
|
+
conditions. That particular part of the code will likely be unreliable if
|
100
|
+
you're zig-zagging across 180 degrees longitude routinely.
|
data/Rakefile
CHANGED
@@ -1,24 +1,18 @@
|
|
1
|
-
require '
|
2
|
-
require 'rake'
|
1
|
+
require 'bundler/gem_tasks'
|
3
2
|
require 'rake/testtask'
|
4
|
-
require '
|
5
|
-
require 'rake/gempackagetask'
|
6
|
-
require File.dirname(__FILE__) + '/lib/gpx'
|
7
|
-
|
8
|
-
PKG_VERSION = GPX::VERSION
|
9
|
-
PKG_NAME = "gpx"
|
10
|
-
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
11
|
-
RUBY_FORGE_PROJECT = "gpx"
|
12
|
-
RUBY_FORGE_USER = ENV['RUBY_FORGE_USER'] || "dougfales"
|
13
|
-
RELEASE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
14
|
-
|
15
|
-
PKG_FILES = FileList[
|
16
|
-
"lib/**/*", "bin/*", "tests/**/*", "[A-Z]*", "Rakefile", "doc/**/*"
|
17
|
-
]
|
3
|
+
require 'rdoc/task'
|
18
4
|
|
19
5
|
desc "Default Task"
|
20
6
|
task :default => [ :test ]
|
21
7
|
|
8
|
+
namespace :ci do
|
9
|
+
task :build do
|
10
|
+
puts "Creating tests/output directory..."
|
11
|
+
FileUtils.mkdir_p "tests/output"
|
12
|
+
Rake::Task[:test].invoke
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
22
16
|
# Run the unit tests
|
23
17
|
desc "Run all unit tests"
|
24
18
|
Rake::TestTask.new("test") { |t|
|
@@ -27,12 +21,6 @@ Rake::TestTask.new("test") { |t|
|
|
27
21
|
t.verbose = true
|
28
22
|
}
|
29
23
|
|
30
|
-
# Make a console, useful when working on tests
|
31
|
-
desc "Generate a test console"
|
32
|
-
task :console do
|
33
|
-
verbose( false ) { sh "irb -I lib/ -r 'gpx'" }
|
34
|
-
end
|
35
|
-
|
36
24
|
# Genereate the RDoc documentation
|
37
25
|
desc "Create documentation"
|
38
26
|
Rake::RDocTask.new("doc") { |rdoc|
|
@@ -41,12 +29,3 @@ Rake::RDocTask.new("doc") { |rdoc|
|
|
41
29
|
rdoc.rdoc_files.include('README')
|
42
30
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
43
31
|
}
|
44
|
-
|
45
|
-
desc "Report code statistics (KLOCs, etc) from the application"
|
46
|
-
task :stats do
|
47
|
-
require 'code_statistics'
|
48
|
-
CodeStatistics.new(
|
49
|
-
["Library", "lib"],
|
50
|
-
["Units", "tests"]
|
51
|
-
).to_s
|
52
|
-
end
|
data/bin/gpx_distance
ADDED
data/bin/gpx_smooth
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path('../../lib/gpx', __FILE__)
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
|
7
|
+
def str_to_int_or_time(str)
|
8
|
+
if str =~ /\A\d{10}\Z/
|
9
|
+
return Time.at(str.to_i)
|
10
|
+
elsif str =~ /\A\d+\Z/
|
11
|
+
return str.to_i
|
12
|
+
else
|
13
|
+
return DateTime.strptime(str, '%Y%m%d-%H:%M:%S').to_time
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
options = {}
|
19
|
+
OptionParser.new do |opts|
|
20
|
+
opts.banner = "Usage: smooth [options]"
|
21
|
+
|
22
|
+
opts.on("-i", "--input-file FILE", "Input file to read gpx data from (if omitted, data will be read from stdin)") do |v|
|
23
|
+
options[:infile] = v
|
24
|
+
end
|
25
|
+
opts.on("-o", "--output-file FILE", "Output file to write smoothed gpx data to (if omitted, data will be written to stdout)") do |v|
|
26
|
+
options[:outfile] = v
|
27
|
+
end
|
28
|
+
opts.on("-s", "--start-time [YYYYMMDD-HH:MM:SS|EPOCH|OFFSET]", "Start smoothing from time or offset specified (if omitted start from the start of the file)") do |v|
|
29
|
+
options[:start] = v
|
30
|
+
end
|
31
|
+
opts.on("-e", "--end-time [YYYYMMDD-HH:MM:SS|EPOCH|OFFSET]", "Finish smoothing from time or offset specified (if omitted finish at the end of the file)") do |v|
|
32
|
+
options[:end] = v
|
33
|
+
end
|
34
|
+
end.parse!
|
35
|
+
|
36
|
+
|
37
|
+
if options[:infile]
|
38
|
+
input = File.open(options[:infile])
|
39
|
+
else
|
40
|
+
input = $stdin
|
41
|
+
end
|
42
|
+
|
43
|
+
options[:start] = str_to_int_or_time(options[:start]) if options[:start]
|
44
|
+
options[:end] = str_to_int_or_time(options[:end]) if options[:end]
|
45
|
+
|
46
|
+
gpx = GPX::GPXFile.new(:gpx_data => input)
|
47
|
+
$stderr.puts "read track with distance #{gpx.distance}"
|
48
|
+
|
49
|
+
#1419062980
|
50
|
+
gpx.tracks.each do |track|
|
51
|
+
track.segments.each do |segment|
|
52
|
+
segment.smooth_location_by_average({:end => options[:end], :start => options[:start]})
|
53
|
+
end
|
54
|
+
end
|
55
|
+
gpx.recalculate_distance
|
56
|
+
$stderr.puts "smoothed distance #{gpx.distance}"
|
57
|
+
|
58
|
+
if options[:outfile]
|
59
|
+
gpx.write(options[:outfile], false)
|
60
|
+
else
|
61
|
+
puts gpx.to_s(false)
|
62
|
+
end
|
63
|
+
|
data/gpx.gemspec
CHANGED
@@ -1,16 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gpx/version'
|
5
|
+
|
3
6
|
Gem::Specification.new do |s|
|
4
7
|
s.name = 'gpx'
|
5
8
|
s.version = GPX::VERSION
|
9
|
+
s.authors = ["Guillaume Dott", "Doug Fales", "Andrew Hao"]
|
10
|
+
s.email = ["guillaume+github@dott.fr", "doug.fales@gmail.com", "andrewhao@gmail.com"]
|
6
11
|
s.summary = %q{A basic API for reading and writing GPX files.}
|
7
12
|
s.description = %q{A basic API for reading and writing GPX files.}
|
8
|
-
|
9
|
-
s.
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split($/)
|
15
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
16
|
+
s.require_paths = ["lib"]
|
10
17
|
s.has_rdoc = true
|
11
|
-
|
12
|
-
s.
|
13
|
-
s.
|
14
|
-
s.
|
15
|
-
s.
|
18
|
+
|
19
|
+
s.homepage = "http://www.github.com/dougfales/gpx"
|
20
|
+
s.add_dependency 'rake'
|
21
|
+
s.add_dependency 'nokogiri'
|
22
|
+
s.add_development_dependency 'bundler'
|
23
|
+
s.add_development_dependency 'minitest'
|
16
24
|
end
|
data/lib/gpx.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2006 Doug Fales
|
2
|
+
# Copyright (c) 2006 Doug Fales
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -20,19 +20,19 @@
|
|
20
20
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
#++
|
23
|
-
|
24
|
-
require 'rubygems'
|
25
|
-
require 'hpricot'
|
26
|
-
require 'date'
|
23
|
+
|
27
24
|
require 'time'
|
28
|
-
require '
|
29
|
-
|
30
|
-
require 'gpx/
|
31
|
-
|
32
|
-
require 'gpx/
|
33
|
-
require 'gpx/
|
34
|
-
require 'gpx/
|
35
|
-
require 'gpx/
|
36
|
-
require 'gpx/
|
37
|
-
require 'gpx/
|
38
|
-
require 'gpx/
|
25
|
+
require 'nokogiri'
|
26
|
+
|
27
|
+
require File.expand_path('../gpx/version', __FILE__)
|
28
|
+
|
29
|
+
require File.expand_path('../gpx/gpx', __FILE__)
|
30
|
+
require File.expand_path('../gpx/gpx_file', __FILE__)
|
31
|
+
require File.expand_path('../gpx/bounds', __FILE__)
|
32
|
+
require File.expand_path('../gpx/track', __FILE__)
|
33
|
+
require File.expand_path('../gpx/route', __FILE__)
|
34
|
+
require File.expand_path('../gpx/segment', __FILE__)
|
35
|
+
require File.expand_path('../gpx/point', __FILE__)
|
36
|
+
require File.expand_path('../gpx/trackpoint', __FILE__)
|
37
|
+
require File.expand_path('../gpx/waypoint', __FILE__)
|
38
|
+
require File.expand_path('../gpx/magellan_track_log', __FILE__)
|
data/lib/gpx/bounds.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
#--
|
2
|
-
# Copyright (c) 2006 Doug Fales
|
2
|
+
# Copyright (c) 2006 Doug Fales
|
3
3
|
#
|
4
4
|
# Permission is hereby granted, free of charge, to any person obtaining
|
5
5
|
# a copy of this software and associated documentation files (the
|
@@ -21,63 +21,54 @@
|
|
21
21
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
22
|
#++
|
23
23
|
module GPX
|
24
|
-
|
25
|
-
|
24
|
+
class Bounds < Base
|
25
|
+
attr_accessor :min_lat, :max_lat, :max_lon, :min_lon, :center_lat, :center_lon
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
27
|
+
# Creates a new bounds object with the passed-in min and max longitudes
|
28
|
+
# and latitudes.
|
29
|
+
def initialize(opts = { :min_lat => 90.0, :max_lat => -90.0, :min_lon => 180.0, :max_lon => -180.0})
|
30
|
+
@min_lat, @max_lat = opts[:min_lat].to_f, opts[:max_lat].to_f
|
31
|
+
@min_lon, @max_lon = opts[:min_lon].to_f, opts[:max_lon].to_f
|
32
|
+
end
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
34
|
+
# Returns the middle latitude.
|
35
|
+
def center_lat
|
36
|
+
distance = (max_lat - min_lat)/2.0
|
37
|
+
(min_lat + distance)
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
# Returns the middle longitude.
|
41
|
+
def center_lon
|
42
|
+
distance = (max_lon - min_lon)/2.0
|
43
|
+
(min_lon + distance)
|
44
|
+
end
|
45
45
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
bnd['maxlat'] = max_lat.to_s
|
51
|
-
bnd['maxlon'] = max_lon.to_s
|
52
|
-
bnd
|
53
|
-
end
|
46
|
+
# Returns true if the pt is within these bounds.
|
47
|
+
def contains?(pt)
|
48
|
+
(pt.lat >= min_lat and pt.lat <= max_lat and pt.lon >= min_lon and pt.lon <= max_lon)
|
49
|
+
end
|
54
50
|
|
55
|
-
|
56
|
-
|
57
|
-
|
51
|
+
# Adds an item to itself, expanding its min/max lat/lon as needed to
|
52
|
+
# contain the given item. The item can be either another instance of
|
53
|
+
# Bounds or a Point.
|
54
|
+
def add(item)
|
55
|
+
if(item.respond_to?(:lat) and item.respond_to?(:lon))
|
56
|
+
@min_lat = item.lat if item.lat < @min_lat
|
57
|
+
@min_lon = item.lon if item.lon < @min_lon
|
58
|
+
@max_lat = item.lat if item.lat > @max_lat
|
59
|
+
@max_lon = item.lon if item.lon > @max_lon
|
60
|
+
else
|
61
|
+
@min_lat = item.min_lat if item.min_lat < @min_lat
|
62
|
+
@min_lon = item.min_lon if item.min_lon < @min_lon
|
63
|
+
@max_lat = item.max_lat if item.max_lat > @max_lat
|
64
|
+
@max_lon = item.max_lon if item.max_lon > @max_lon
|
58
65
|
end
|
66
|
+
end
|
59
67
|
|
60
|
-
|
61
|
-
|
62
|
-
#
|
63
|
-
|
64
|
-
if(item.respond_to?(:lat) and item.respond_to?(:lon))
|
65
|
-
@min_lat = item.lat if item.lat < @min_lat
|
66
|
-
@min_lon = item.lon if item.lon < @min_lon
|
67
|
-
@max_lat = item.lat if item.lat > @max_lat
|
68
|
-
@max_lon = item.lon if item.lon > @max_lon
|
69
|
-
else
|
70
|
-
@min_lat = item.min_lat if item.min_lat < @min_lat
|
71
|
-
@min_lon = item.min_lon if item.min_lon < @min_lon
|
72
|
-
@max_lat = item.max_lat if item.max_lat > @max_lat
|
73
|
-
@max_lon = item.max_lon if item.max_lon > @max_lon
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
# Returns the min_lat, min_lon, max_lat, and max_lon in a labeled string.
|
78
|
-
def to_s
|
79
|
-
"min_lat: #{min_lat} min_lon: #{min_lon} max_lat: #{max_lat} max_lon: #{max_lon}"
|
80
|
-
end
|
68
|
+
# Returns the min_lat, min_lon, max_lat, and max_lon in a labeled string.
|
69
|
+
def to_s
|
70
|
+
"min_lat: #{min_lat} min_lon: #{min_lon} max_lat: #{max_lat} max_lon: #{max_lon}"
|
71
|
+
end
|
81
72
|
|
82
|
-
|
73
|
+
end
|
83
74
|
end
|