gpx2atlas 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1748ef15ece9c33ef4a80e7317b95a4f6749ebb9
4
+ data.tar.gz: 71734f47a12113282ad61e484f6226a6a66d3d58
5
+ SHA512:
6
+ metadata.gz: 45a6b510c56e954273ca641fe30fde905ecf421f3a2ed41599c76e6bbc14912636369acfb4bb399be5db5f337ba4182c042b6a3eff62c352b6ab6499082cff73
7
+ data.tar.gz: 9855f2e845e030fc11c0a52cd8757d5b01e1e91d1d6209cde92d5499925fcc68b2bc5b88fa5fc564de59b8f1c126b0f6f2a20998e99c600d6aeca98f075b7d3a
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gpx2atlas.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Joakim Reinert
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Gpx2atlas
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gpx2atlas'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gpx2atlas
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/gpx2atlas/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'gpx2atlas'
4
+ require 'optparse'
5
+
6
+ options = {overlap: {x: 0, y: 0}}
7
+
8
+ parser = OptionParser.new do |opts|
9
+ opts.banner = "Usage: #{__FILE__} <options> <input_gpx> <output_gpx>"
10
+ opts.separator ""
11
+ opts.separator "Options:"
12
+
13
+ opts.on("-s", "--scale X", "Use the scale 1:X") do |scale|
14
+ options[:scale] = scale.to_f
15
+ end
16
+
17
+ opts.on("-d", "--dimensions DIMENSIONS", "Set DIMENSIONS of Atlas tiles in the form WIDTHxHEIGHT where WIDTH and HEIGHT are both given in mm") do |dims|
18
+ width, height = dims.split(/[xX]/).map(&:to_f)
19
+ options[:medium_size] = {width: width, height: height}
20
+ end
21
+
22
+ opts.on("-o", "--overlap X:Y", "Overlap Atlas tiles X percent horizontally and Y percent vertically. Set it in the form X:Y. Default is 0:0") do |dims|
23
+ x, y = dims.split(':').map(&:to_f)
24
+ options[:overlap] = {x: x / 100, y: y / 100}
25
+ end
26
+
27
+ opts.on_tail("-h", "--help", "Show this message and exit") do
28
+ puts opts
29
+ exit
30
+ end
31
+
32
+ end
33
+
34
+ parser.parse!
35
+
36
+ unless options[:scale] && options[:medium_size] && ARGV[-2] && ARGV[-1]
37
+ puts parser.help
38
+ exit 1
39
+ end
40
+
41
+ if File.exists? ARGV[-1]
42
+ print "File #{ARGV[-1]} exists! Overwrite? (y/n): "
43
+ unless $stdin.gets.chomp =~ /^[yY]$/
44
+ exit
45
+ end
46
+ end
47
+
48
+ gpx = Gpx2Atlas.coverage_layer(ARGV[-2], options)
49
+
50
+ gpx.write(ARGV[-1])
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gpx2atlas/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gpx2atlas"
8
+ spec.version = Gpx2atlas::VERSION
9
+ spec.authors = ["Joakim Reinert"]
10
+ spec.email = ["mail@jreinert.com"]
11
+ spec.summary = %q{Tiny utility to create qgis atlas coverage layers from tracks}
12
+ spec.homepage = "https://github.com/jreinert/gpx2atlas"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_dependency "gpx"
23
+ spec.add_dependency "libxml-ruby"
24
+ end
@@ -0,0 +1,43 @@
1
+ require "gpx2atlas/version"
2
+ require "gpx2atlas/bounding_box"
3
+ require "gpx"
4
+
5
+ module Gpx2Atlas
6
+ include GPX
7
+
8
+ def self.coverage_layer(gpx_file, options)
9
+ gpx = GPXFile.new(gpx_file: gpx_file)
10
+ medium_size = {
11
+ width: options[:medium_size][:width] * (1 - options[:overlap][:x]),
12
+ height: options[:medium_size][:height] * (1 - options[:overlap][:y])
13
+ }
14
+
15
+ bboxes = []
16
+ points = gpx.tracks.first.points.map {|p| [p.lat, p.lon]}
17
+
18
+ range = 0...points.size
19
+
20
+ loop do
21
+ bbox, count = BoundingBox.best_fit(points[range],
22
+ medium_size: medium_size,
23
+ scale: options[:scale])
24
+ bboxes << bbox
25
+
26
+ break unless (range.begin...range.end).include?(range.begin + count)
27
+
28
+ range = (range.begin + count - 1)...range.end
29
+ end
30
+
31
+ result = GPXFile.new
32
+ result.waypoints = bboxes.map do |bbox|
33
+ waypoint = Waypoint.new
34
+ waypoint.lat = bbox.lat_deg
35
+ waypoint.lon = bbox.lon_deg
36
+
37
+ waypoint
38
+ end
39
+
40
+ result
41
+ end
42
+
43
+ end
@@ -0,0 +1,120 @@
1
+ require 'gpx2atlas/util'
2
+
3
+ module Gpx2Atlas
4
+ class BoundingBox
5
+ include Util
6
+
7
+ using Refinements
8
+
9
+ attr_reader :lat_deg, :lon_deg, :lat_min, :lat_max, :lon_min, :lon_max
10
+
11
+ def initialize(lat_deg, lon_deg, options = {})
12
+ @lat_deg = lat_deg
13
+ @lon_deg = lon_deg
14
+
15
+ lat = @lat_deg.to_rad
16
+ lon = @lon_deg.to_rad
17
+
18
+ if options[:medium_size] && options[:scale]
19
+ @width = options[:medium_size][:width] / 1000 * options[:scale]
20
+ @height = options[:medium_size][:height] / 1000 * options[:scale]
21
+ else
22
+ @width = options[:size][:width]
23
+ @height = options[:size][:height]
24
+ end
25
+
26
+ radius = WGS84.earth_radius(lat)
27
+ pradius = radius * Math.cos(lat)
28
+
29
+ @lat_min = lat - @height / 2.0 / radius
30
+ @lat_max = lat + @height / 2.0 / radius
31
+ @lon_min = lon - @width / 2.0 / pradius
32
+ @lon_max = lon + @width / 2.0 / pradius
33
+ end
34
+
35
+ def scale(horizontal, vertical)
36
+ BoundingBox.new(@lat_deg, @lon_deg, size: {
37
+ width: @width * horizontal,
38
+ height: @height * vertical
39
+ })
40
+ end
41
+
42
+ def move_percent!(x, y)
43
+ lat_deg_delta = (@lat_max - @lat_min).to_deg * x
44
+ lon_deg_delta = (@lon_max - @lon_min).to_deg * y
45
+ move!(lat_deg_delta, lon_deg_delta)
46
+ end
47
+
48
+ def move!(lat_deg_delta, lon_deg_delta)
49
+ new_bbox = BoundingBox.new(@lat_deg + lat_deg_delta,
50
+ @lon_deg + lon_deg_delta,
51
+ size: {width: @width, height: @height})
52
+ @lat_deg = new_bbox.lat_deg
53
+ @lon_deg = new_bbox.lon_deg
54
+ @lat_min = new_bbox.lat_min
55
+ @lat_max = new_bbox.lat_max
56
+ @lon_min = new_bbox.lon_min
57
+ @lon_max = new_bbox.lon_max
58
+
59
+ self
60
+ end
61
+
62
+ def move(lat_deg_delta, lon_deg_delta)
63
+ self.dup.move!(lat_deg_delta, lon_deg_delta)
64
+ end
65
+
66
+ def move_percent(x, y)
67
+ self.dup.move_percent!(x, y)
68
+ end
69
+
70
+ def includes?(lat_deg, lon_deg)
71
+ lat = lat_deg.to_rad
72
+ lon = lon_deg.to_rad
73
+
74
+ lat >= @lat_min && lat <= @lat_max && lon >= @lon_min && lon <= @lon_max
75
+ end
76
+
77
+ def self.fit(coordinates, options = {})
78
+ result = BoundingBox.new(coordinates.first[0], coordinates.first[1], options)
79
+ last_coord = coordinates.first
80
+ coordinates[1..-1].each_with_index do |(lat_deg, lon_deg), i|
81
+ unless result.includes?(lat_deg, lon_deg)
82
+ lat_deg_delta = lat_deg - last_coord[0]
83
+ lon_deg_delta = lon_deg - last_coord[1]
84
+
85
+ new_box = result.move(lat_deg_delta, lon_deg_delta)
86
+
87
+ unless coordinates[0..i].all? {|coord| new_box.includes?(coord[0], coord[1])}
88
+ return false
89
+ end
90
+
91
+ result = new_box
92
+ end
93
+
94
+ last_coord = [lat_deg, lon_deg]
95
+ end
96
+
97
+ result
98
+ end
99
+
100
+ def self.best_fit(coordinates, options = {})
101
+ max = coordinates.size
102
+
103
+ bbox = fit(coordinates[0...max], options)
104
+
105
+ until bbox
106
+ max /= 2
107
+ bbox = fit(coordinates[0...max], options)
108
+ end
109
+
110
+ while max < coordinates.size
111
+ max += 1
112
+ new_bbox = fit(coordinates[0...max], options)
113
+ new_bbox ? bbox = new_bbox : break
114
+ end
115
+
116
+ [bbox, max]
117
+ end
118
+
119
+ end
120
+ end
@@ -0,0 +1,34 @@
1
+ module Gpx2Atlas
2
+ module Util
3
+ module Refinements
4
+ refine Numeric do
5
+
6
+ def to_deg
7
+ 180.0 * self / Math::PI
8
+ end
9
+
10
+ def to_rad
11
+ Math::PI * self / 180.0
12
+ end
13
+
14
+ end
15
+ end
16
+
17
+ module WGS84
18
+
19
+ # Semi-axes of WGS-84 geoidal reference
20
+ A = 6378137.0 # Major semiaxis
21
+ B = 6356752.3 # Minor semiaxis
22
+
23
+ def self.earth_radius(lat)
24
+ a_n = A**2 * Math.cos(lat)
25
+ b_n = B**2 * Math.sin(lat)
26
+ a_d = A * Math.cos(lat)
27
+ b_d = B * Math.sin(lat)
28
+
29
+ Math.sqrt((a_n**2 + b_n**2) / (a_d**2 + b_d**2))
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Gpx2atlas
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gpx2atlas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joakim Reinert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gpx
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: libxml-ruby
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - mail@jreinert.com
72
+ executables:
73
+ - gpx2atlas
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - bin/gpx2atlas
83
+ - gpx2atlas.gemspec
84
+ - lib/gpx2atlas.rb
85
+ - lib/gpx2atlas/bounding_box.rb
86
+ - lib/gpx2atlas/util.rb
87
+ - lib/gpx2atlas/version.rb
88
+ homepage: https://github.com/jreinert/gpx2atlas
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.2.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Tiny utility to create qgis atlas coverage layers from tracks
112
+ test_files: []
113
+ has_rdoc: