osm-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d6914beed298de2adfe541882f7af0900238ac71a2526cd2b8cf02af79714d6c
4
+ data.tar.gz: fbaafa9dd9b5fd9e0eef6d94366a99eb2503151446980320169f525a98b3c9fd
5
+ SHA512:
6
+ metadata.gz: 2884743ee6007b59692099b36073f5b538fa0cfb9aedc65b797988f1447f59c4064d0efcfb5b323fa7f487c54322440acfcdacd8bf13b31b2ae32d7021fa702c
7
+ data.tar.gz: 716bda4e07b52e699fae7409111a321fb900520deb591d0b4d7464d3b27632739bd312d2d88e79de8c3cead7376ccac18c5cb232f2a4ae6157abe8c5cab95d99
data/CHANGELOG.md ADDED
File without changes
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at lukas.diener@hotmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in osm-rb.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ osm-rb (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.1)
16
+ rspec-support (~> 3.9.1)
17
+ rspec-expectations (3.9.1)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 2.0)
30
+ osm-rb!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 2.0.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Lukas_Skywalker
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # OSM
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'osm-rb'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install osm-rb
18
+
19
+ ## Usage
20
+
21
+ OSM-rb exposes two distincts APIs.
22
+
23
+ ### Calculator
24
+
25
+ The calculator helps you with everything regarding projections, tile names and bounds.
26
+
27
+ To create a calculator you have to pass it a zoom level, padding and the target map size
28
+
29
+ ```ruby
30
+ padding = OpenStruct.new(x: 50, y: 40)
31
+ target_size = OpenStruct.new(x: 800, y: 600)
32
+ calculator = Calculator.new(padding, target_size)
33
+ ```
34
+
35
+ Once you have created a calculator, you can use it to calculate and set the required zoom
36
+ level to fit your bounds
37
+
38
+ ```ruby
39
+ path = [
40
+ OpenStruct.new(lat: 46.95127, lng: 7.43878),
41
+ OpenStruct.new(lat: 46.92771, lng: 7.44610),
42
+ OpenStruct.new(lat: 46.94764, lng: 7.37527)
43
+ ]
44
+ zoom = calculator.zoom_level(path)
45
+ zoom # => 13
46
+ ```
47
+
48
+ Calculate required tiles to fit the map as specified
49
+
50
+ ```ruby
51
+ tiles = calculator.tiles(path)
52
+ tiles[0].x # => 136485
53
+ tiles[0].y # => 92249
54
+ ```
55
+
56
+ Calculate the number of tiles required
57
+
58
+ ```ruby
59
+ dimensions = calculator.tile_dimensions(path)
60
+ dimensions.x # => 12
61
+ dimensions.y # => 8
62
+ ```
63
+
64
+ ## Development
65
+
66
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
67
+
68
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/code-fabrik/osm-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
77
+
78
+ ## Code of Conduct
79
+
80
+ Everyone interacting in the OSM project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/code-fabrik/osm-rb/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "osm"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,147 @@
1
+ require 'ostruct'
2
+
3
+ module OSM
4
+ TILE_SIZE = 256
5
+
6
+ class Calculator
7
+ attr_reader :zoom, :padding, :target_size
8
+
9
+ def initialize(zoom, padding, target_size)
10
+ @zoom = zoom
11
+ @padding = padding
12
+ @target_size = target_size
13
+ end
14
+
15
+ def zoom_level(path)
16
+ 18.downto(1) do |level|
17
+ d = Calculator.new(level, padding, target_size)
18
+ path_size = d.path_pixel_dimensions(path)
19
+ if path_size.x + padding.x <= target_size.x && path_size.y + padding.y <= target_size.y
20
+ # puts "Fit #{path_size} into #{target_size} at zoom level #{level}"
21
+ return level
22
+ end
23
+ end
24
+ return 1
25
+ end
26
+
27
+ def pixels(path)
28
+ top_left = top_left_tile(path)
29
+ path.map do |point|
30
+ pixel = lat_lng_to_pixel(point)
31
+ OpenStruct.new(x: pixel.x - (top_left.x * TILE_SIZE), y: pixel.y - (top_left.y * TILE_SIZE))
32
+ end
33
+ end
34
+
35
+ def tiles(path)
36
+ top_left = top_left_tile(path)
37
+ bottom_right = bottom_right_tile(path)
38
+ tiles = []
39
+ (top_left.y..bottom_right.y).each do |y|
40
+ (top_left.x..bottom_right.x).each do |x|
41
+ tiles << OpenStruct.new(x: x, y: y)
42
+ end
43
+ end
44
+ tiles
45
+ end
46
+
47
+ def top_left_tile(path)
48
+ center = center_pixel(path)
49
+ x = center.x - (target_size.x / 2)
50
+ y = center.y - (target_size.y / 2)
51
+ OpenStruct.new(x: (x / TILE_SIZE).floor, y: (y / TILE_SIZE).floor)
52
+ end
53
+
54
+ def bottom_right_tile(path)
55
+ center = center_pixel(path)
56
+ x = center.x + (target_size.x / 2)
57
+ y = center.y + (target_size.y / 2)
58
+ OpenStruct.new(x: (x / TILE_SIZE).floor, y: (y / TILE_SIZE).floor)
59
+ end
60
+
61
+ def center_pixel(path)
62
+ top_left = path_top_left_pixel(path)
63
+ bottom_right = path_bottom_right_pixel(path)
64
+ delta = OpenStruct.new(x: bottom_right.x - top_left.x, y: bottom_right.y - top_left.y)
65
+ OpenStruct.new(x: top_left.x + (delta.x / 2), y: top_left.y + (delta.y / 2))
66
+ end
67
+
68
+ def tile_dimensions(path)
69
+ t = tiles(path)
70
+ width = t.map(&:x).max - t.map(&:x).min
71
+ height = t.map(&:y).max - t.map(&:y).min
72
+ OpenStruct.new(x: width + 1, y: height + 1)
73
+ end
74
+
75
+ def path_pixel_dimensions(path)
76
+ top_left = path_top_left_pixel(path)
77
+ bottom_right = path_bottom_right_pixel(path)
78
+ OpenStruct.new(x: bottom_right.x - top_left.x, y: bottom_right.y - top_left.y)
79
+ end
80
+
81
+ def crop_dimensions(path)
82
+ center = center_pixel(path)
83
+ top_left = top_left_tile(path)
84
+ left = center.x - (top_left.x * TILE_SIZE) - (target_size.x / 2)
85
+ top = center.y - (top_left.y * TILE_SIZE) - (target_size.y / 2)
86
+ OpenStruct.new(x: left, y: top)
87
+ end
88
+
89
+ private
90
+
91
+ def path_top_left_pixel(path)
92
+ min_deg = path_min(path)
93
+ max_deg = path_max(path)
94
+ point = OpenStruct.new(lat: max_deg.lat, lng: min_deg.lng)
95
+ lat_lng_to_pixel(point)
96
+ end
97
+
98
+ def path_bottom_right_pixel(path)
99
+ min_deg = path_min(path)
100
+ max_deg = path_max(path)
101
+ point = OpenStruct.new(lat: min_deg.lat, lng: max_deg.lng)
102
+ lat_lng_to_pixel(point)
103
+ end
104
+
105
+ def path_min(path)
106
+ OpenStruct.new(lat: path.map(&:lat).min, lng: path.map(&:lng).min)
107
+ end
108
+
109
+ def path_max(path)
110
+ OpenStruct.new(lat: path.map(&:lat).max, lng: path.map(&:lng).max)
111
+ end
112
+
113
+ def scale
114
+ 2 ** zoom
115
+ end
116
+
117
+ def lat_lng_to_pixel(lat_lng)
118
+ lat_deg = lat_lng.lat
119
+ lng_deg = lat_lng.lng
120
+ lat_rad = lat_deg/180 * Math::PI
121
+ n = scale
122
+ x = ((lng_deg + 180.0) / 360.0 * n)
123
+ y = ((1.0 - Math::log(Math::tan(lat_rad) + (1 / Math::cos(lat_rad))) / Math::PI) / 2.0 * n)
124
+ OpenStruct.new(x: (x * TILE_SIZE).floor, y: (y * TILE_SIZE).floor)
125
+ end
126
+
127
+ def pixel_to_lat_lng(pixel)
128
+ xtile = pixel.x / TILE_SIZE.to_f
129
+ ytile = pixel.y / TILE_SIZE.to_f
130
+ n = scale
131
+ lon_deg = xtile / n * 360.0 - 180.0
132
+ lat_rad = Math::atan(Math::sinh(Math::PI * (1 - 2 * ytile / n)))
133
+ lat_deg = 180.0 * (lat_rad / Math::PI)
134
+ OpenStruct.new(lat: lat_deg, lng: lon_deg)
135
+ end
136
+
137
+ def lat_lng_to_tile(lat_lng)
138
+ point = lat_lng_to_pixel(lat_lng)
139
+ OpenStruct.new(x: (point.x / TILE_SIZE).floor, y: (point.y / TILE_SIZE).floor)
140
+ end
141
+
142
+ def tile_to_lat_lng(pixel)
143
+ tile = OpenStruct.new(x: pixel.x.floor, y: pixel.y.floor)
144
+ pixel_to_lat_lng(tile)
145
+ end
146
+ end
147
+ end
data/lib/osm.rb ADDED
@@ -0,0 +1,5 @@
1
+ module OSM
2
+ VERSION = '0.0.1'
3
+
4
+ require_relative 'osm/calculator'
5
+ end
data/osm-rb.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "osm"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "osm-rb"
7
+ spec.version = OSM::VERSION
8
+ spec.authors = ["Lukas_Skywalker"]
9
+ spec.email = ["lukas.diener@hotmail.com"]
10
+
11
+ spec.summary = "Manipulation and export of OSM map tiles"
12
+ spec.description = %w{OSM-rb allows you to manipulate and export OSM map tiles. This includes downloading
13
+ tiles,cropping to any specified region, drawing routes on the map and exporting them
14
+ as PNG files. }
15
+ spec.homepage = "https://github.com/code-fabrik/osm-rb"
16
+ spec.license = "MIT"
17
+
18
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
19
+
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/code-fabrik/osm-rb"
22
+ spec.metadata["changelog_uri"] = "https://github.com/code-fabrik/osm-rb/blob/master/CHANGELOG.md"
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 2.0"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: osm-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lukas_Skywalker
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-30 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: '["OSM-rb", "allows", "you", "to", "manipulate", "and", "export", "OSM",
56
+ "map", "tiles.", "This", "includes", "downloading", "tiles,cropping", "to", "any",
57
+ "specified", "region,", "drawing", "routes", "on", "the", "map", "and", "exporting",
58
+ "them", "as", "PNG", "files."]'
59
+ email:
60
+ - lukas.diener@hotmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - CHANGELOG.md
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/osm.rb
75
+ - lib/osm/calculator.rb
76
+ - osm-rb.gemspec
77
+ homepage: https://github.com/code-fabrik/osm-rb
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ allowed_push_host: https://rubygems.org
82
+ homepage_uri: https://github.com/code-fabrik/osm-rb
83
+ source_code_uri: https://github.com/code-fabrik/osm-rb
84
+ changelog_uri: https://github.com/code-fabrik/osm-rb/blob/master/CHANGELOG.md
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.1.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Manipulation and export of OSM map tiles
104
+ test_files: []