easy_map_tiles 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f4173b1a98c99cdd8267461b4f34fa328e493e4
4
+ data.tar.gz: d034d5dfbb25f7f39533efc1631d62efcb3d9233
5
+ SHA512:
6
+ metadata.gz: def4a9258b02d483a57cea75b8894e4b5c8d17595d6a1613faee99fd846ac7e50a7e171ca53356c8c5f248c32bafa5cb71546c3df1679968cbe790d579bffb16
7
+ data.tar.gz: 5c815f2e02aef8575dad404e9f77a605914f4e2e099d9265a0f6311cd0a85488dcf4dcabfcd113f34066c5acf78672b0a386629171b9b644a81c8027da8f06ce
@@ -0,0 +1,3 @@
1
+ module EasyMapTiles
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,75 @@
1
+ require 'easy_map_tiles/version'
2
+
3
+ module EasyMapTiles
4
+
5
+ class << self
6
+
7
+ TO_DEG = Rational(180.0, Math::PI)
8
+ TO_RAD = Rational(Math::PI, 180)
9
+ TILE_SIZE = 256.0 # in px
10
+ EARTH_RADIUS = 6378137.0 # in meters
11
+ PX_PER_RAD = Rational(TILE_SIZE, (2 * Math::PI))
12
+
13
+ # returns the pixel coordinates at the given zoom level on the world map
14
+ def to_world_px(lat_deg, lon_deg, zoom)
15
+ # scale the latitude via mercator projections
16
+ # see http://en.wikipedia.org/wiki/Mercator_projection#Derivation_of_the_Mercator_projection
17
+ lat_scaled_rad = Math.log( Math.tan( Rational(Math::PI, 4) + Rational(TO_RAD * lat_deg, 2) ))
18
+ # https://developers.google.com/maps/documentation/javascript/examples/map-coordinates
19
+ wx = (TILE_SIZE / 2) + PX_PER_RAD * (TO_RAD * lon_deg)
20
+ wy = (TILE_SIZE / 2) - PX_PER_RAD * lat_scaled_rad
21
+ tiles_count = 2**zoom
22
+ [(wx * tiles_count).to_i, (wy * tiles_count).to_i]
23
+ end
24
+
25
+ def tile_url(lat_deg, lon_deg, zoom, satellite=false)
26
+ px,py = to_world_px(lat_deg, lon_deg, zoom)
27
+ x, y = Rational(px, TILE_SIZE).to_i, Rational(py, TILE_SIZE).to_i
28
+ if satellite
29
+ "https://khms#{rand(0..3)}.google.com/kh/v=131&x=#{x}&y=#{y}&z=#{zoom}"
30
+ else
31
+ "http://mt#{rand(0..3)}.google.com/vt/hl=en&x=#{x}&y=#{y}&z=#{zoom}"
32
+ end
33
+ end
34
+
35
+ def most_fitting_zoom_level(lat_deg, lon_deg, zoom_levels)
36
+ Array(zoom_levels).min_by do |zoom|
37
+ to_world_px(lat_deg, lon_deg, zoom).reduce(0){|s,e| s += (TILE_SIZE / 2 - (e % TILE_SIZE)).abs}
38
+ end
39
+ end
40
+
41
+ def tile_div(lat_deg, lon_deg, zoom_levels, opts={})
42
+ satellite = opts[:satellite] || false
43
+ marker = opts[:marker] || false
44
+ zoom = most_fitting_zoom_level(lat_deg, lon_deg, zoom_levels)
45
+ if marker
46
+ px,py = to_world_px(lat_deg, lon_deg, zoom).map{|e| (e % TILE_SIZE) / TILE_SIZE}
47
+ styles = [
48
+ "left: #{(px*100).round(3)}%",
49
+ "top: #{(py*100).round(3)}%"
50
+ ]
51
+ styles += [
52
+ 'border-radius: 50%',
53
+ 'background: rgba(255,50,50,0.9)',
54
+ 'width: 12px',
55
+ 'height: 12px',
56
+ 'position: absolute',
57
+ 'box-shadow: 0px 0px 6px 2px rgba(255,50,50,0.9)'
58
+ ] unless opts[:marker_styles] == false
59
+ marker_div = "<div class='emt-marker' style='#{styles.join(';')}'></div>"
60
+ end
61
+
62
+ styles = [
63
+ "background-image: url(\"#{tile_url(lat_deg, lon_deg, zoom, satellite)}\")"
64
+ ]
65
+ styles += [
66
+ 'background-size: cover',
67
+ 'position: relative',
68
+ 'display: inline-block',
69
+ 'width: 256px',
70
+ 'height: 256px'
71
+ ] unless opts[:tile_styles] == false
72
+ "<div class='emt-tile' style='#{styles.join(';')}'>#{marker_div}</div>"
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'easy_map_tiles'
3
+
4
+ describe EasyMapTiles do
5
+
6
+ it 'has a version' do
7
+ expect(EasyMapTiles::VERSION).to match(/\d+\.\d+\.\d+/)
8
+ end
9
+
10
+ it 'generates google map tiles' do
11
+ zoom_levels = [5,6,7,8,9,10]
12
+ divs = [
13
+ [39.915036, 116.404996, zoom_levels],
14
+ [21.029346, 105.838938, zoom_levels],
15
+ [52.516318, 13.402477, zoom_levels],
16
+ [13.741276, 100.533547, zoom_levels],
17
+ [-34.914309, -56.161885, zoom_levels],
18
+ [54.548059, 36.287218, zoom_levels]
19
+ ].map do |e|
20
+ EasyMapTiles.tile_div(*e, satellite: false, marker: true)
21
+ end
22
+ File.write('test.html', divs.join('<br><br>'))
23
+ puts "Check resulting easy_map_tiles in test.html"
24
+ end
25
+
26
+ # TODO: way more tests, including integration tests with capybara
27
+ end
@@ -0,0 +1,78 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+ RSpec.configure do |config|
18
+ # The settings below are suggested to provide a good initial experience
19
+ # with RSpec, but feel free to customize to your heart's content.
20
+ =begin
21
+ # These two settings work together to allow you to limit a spec run
22
+ # to individual examples or groups you care about by tagging them with
23
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
24
+ # get run.
25
+ config.filter_run :focus
26
+ config.run_all_when_everything_filtered = true
27
+
28
+ # Many RSpec users commonly either run the entire suite or an individual
29
+ # file, and it's useful to allow more verbose output when running an
30
+ # individual spec file.
31
+ if config.files_to_run.one?
32
+ # Use the documentation formatter for detailed output,
33
+ # unless a formatter has already been configured
34
+ # (e.g. via a command-line flag).
35
+ config.default_formatter = 'doc'
36
+ end
37
+
38
+ # Print the 10 slowest examples and example groups at the
39
+ # end of the spec run, to help surface which specs are running
40
+ # particularly slow.
41
+ config.profile_examples = 10
42
+
43
+ # Run specs in random order to surface order dependencies. If you find an
44
+ # order dependency and want to debug it, you can fix the order by providing
45
+ # the seed, which is printed after each run.
46
+ # --seed 1234
47
+ config.order = :random
48
+
49
+ # Seed global randomization in this process using the `--seed` CLI option.
50
+ # Setting this allows you to use `--seed` to deterministically reproduce
51
+ # test failures related to randomization by passing the same `--seed` value
52
+ # as the one that triggered the failure.
53
+ Kernel.srand config.seed
54
+
55
+ # rspec-expectations config goes here. You can use an alternate
56
+ # assertion/expectation library such as wrong or the stdlib/minitest
57
+ # assertions if you prefer.
58
+ config.expect_with :rspec do |expectations|
59
+ # Enable only the newer, non-monkey-patching expect syntax.
60
+ # For more details, see:
61
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ expectations.syntax = :expect
63
+ end
64
+
65
+ # rspec-mocks config goes here. You can use an alternate test double
66
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
67
+ config.mock_with :rspec do |mocks|
68
+ # Enable only the newer, non-monkey-patching expect syntax.
69
+ # For more details, see:
70
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
71
+ mocks.syntax = :expect
72
+
73
+ # Prevents you from mocking or stubbing a method that does not exist on
74
+ # a real object. This is generally recommended.
75
+ mocks.verify_partial_doubles = true
76
+ end
77
+ =end
78
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_map_tiles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - naema
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-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: '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: '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: Generate simple tiles from google maps that you can use for backgrounds
56
+ or previews.
57
+ email:
58
+ - naemyx@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/easy_map_tiles.rb
64
+ - lib/easy_map_tiles/version.rb
65
+ - spec/lib/easy_map_tiles_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: https://github.com/naema/easy_map_tiles
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Generate simple tiles from google maps
91
+ test_files: []
92
+ has_rdoc: