map_project 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +128 -0
- data/MIT-LICENSE +20 -0
- data/README.md +75 -0
- data/README.rdoc +3 -0
- data/Rakefile +34 -0
- data/lib/map_project/map_project.rb +103 -0
- data/lib/map_project/version.rb +3 -0
- data/lib/map_project.rb +28 -0
- data/lib/tasks/map_project_tasks.rake +4 -0
- data/map_project.gemspec +19 -0
- data/spec/lib/map_project_helper.rb +57 -0
- data/spec/spec_helper.rb +1 -0
- data/test/dummy/README.rdoc +28 -0
- data/test/dummy/Rakefile +6 -0
- data/test/dummy/app/assets/images/.keep +0 -0
- data/test/dummy/app/assets/javascripts/application.js +13 -0
- data/test/dummy/app/assets/stylesheets/application.css +15 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -0
- data/test/dummy/app/controllers/concerns/.keep +0 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/mailers/.keep +0 -0
- data/test/dummy/app/models/.keep +0 -0
- data/test/dummy/app/models/concerns/.keep +0 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/bin/bundle +3 -0
- data/test/dummy/bin/rails +4 -0
- data/test/dummy/bin/rake +4 -0
- data/test/dummy/bin/setup +29 -0
- data/test/dummy/config/application.rb +26 -0
- data/test/dummy/config/boot.rb +5 -0
- data/test/dummy/config/database.yml +25 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +41 -0
- data/test/dummy/config/environments/production.rb +79 -0
- data/test/dummy/config/environments/test.rb +42 -0
- data/test/dummy/config/initializers/assets.rb +11 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/dummy/config/initializers/inflections.rb +16 -0
- data/test/dummy/config/initializers/mime_types.rb +4 -0
- data/test/dummy/config/initializers/session_store.rb +3 -0
- data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/test/dummy/config/locales/en.yml +23 -0
- data/test/dummy/config/routes.rb +56 -0
- data/test/dummy/config/secrets.yml +22 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/lib/assets/.keep +0 -0
- data/test/dummy/log/.keep +0 -0
- data/test/dummy/public/404.html +67 -0
- data/test/dummy/public/422.html +67 -0
- data/test/dummy/public/500.html +66 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/map_project_test.rb +7 -0
- data/test/test_helper.rb +20 -0
- metadata +147 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 474548cc104238aacab99ddcbe5d8faca4d153eb
|
4
|
+
data.tar.gz: dfe5978a8013cf8260eaff6033635d75c36f62ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70f6a278ebb3a0935ead754f4dc1ad92322e529dac92e8b740145015a8fc7fbfb6ce4fd190bd35f75433227f497cb50e6c8e33a58f185974938d8cf82109a1ec
|
7
|
+
data.tar.gz: 96e4a40bdd64c22f3acd603a397f228d70c87dbefa39c11cb2d6b7a60408580989f0800898e01a8438c1cf8cdec79bd2bf88e74a25d34934272aad7af182cd37
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Declare your gem's dependencies in map_project.gemspec.
|
4
|
+
# Bundler will treat runtime dependencies like base dependencies, and
|
5
|
+
# development dependencies will be added by default to the :development group.
|
6
|
+
gemspec
|
7
|
+
gem 'rspec'
|
8
|
+
# Declare any dependencies that are still in development here instead of in
|
9
|
+
# your gemspec. These might include edge Rails or gems from your path or
|
10
|
+
# Git. Remember to move these dependencies to your gemspec before releasing
|
11
|
+
# your gem to rubygems.org.
|
12
|
+
|
13
|
+
# To use a debugger
|
14
|
+
# gem 'byebug', group: [:development, :test]
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
map_project (0.0.1)
|
5
|
+
rails (~> 4.2.6)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionmailer (4.2.7.1)
|
11
|
+
actionpack (= 4.2.7.1)
|
12
|
+
actionview (= 4.2.7.1)
|
13
|
+
activejob (= 4.2.7.1)
|
14
|
+
mail (~> 2.5, >= 2.5.4)
|
15
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
16
|
+
actionpack (4.2.7.1)
|
17
|
+
actionview (= 4.2.7.1)
|
18
|
+
activesupport (= 4.2.7.1)
|
19
|
+
rack (~> 1.6)
|
20
|
+
rack-test (~> 0.6.2)
|
21
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
22
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
23
|
+
actionview (4.2.7.1)
|
24
|
+
activesupport (= 4.2.7.1)
|
25
|
+
builder (~> 3.1)
|
26
|
+
erubis (~> 2.7.0)
|
27
|
+
rails-dom-testing (~> 1.0, >= 1.0.5)
|
28
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
29
|
+
activejob (4.2.7.1)
|
30
|
+
activesupport (= 4.2.7.1)
|
31
|
+
globalid (>= 0.3.0)
|
32
|
+
activemodel (4.2.7.1)
|
33
|
+
activesupport (= 4.2.7.1)
|
34
|
+
builder (~> 3.1)
|
35
|
+
activerecord (4.2.7.1)
|
36
|
+
activemodel (= 4.2.7.1)
|
37
|
+
activesupport (= 4.2.7.1)
|
38
|
+
arel (~> 6.0)
|
39
|
+
activesupport (4.2.7.1)
|
40
|
+
i18n (~> 0.7)
|
41
|
+
json (~> 1.7, >= 1.7.7)
|
42
|
+
minitest (~> 5.1)
|
43
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
44
|
+
tzinfo (~> 1.1)
|
45
|
+
arel (6.0.3)
|
46
|
+
builder (3.2.2)
|
47
|
+
concurrent-ruby (1.0.2)
|
48
|
+
diff-lcs (1.2.5)
|
49
|
+
erubis (2.7.0)
|
50
|
+
globalid (0.3.7)
|
51
|
+
activesupport (>= 4.1.0)
|
52
|
+
i18n (0.7.0)
|
53
|
+
json (1.8.3)
|
54
|
+
loofah (2.0.3)
|
55
|
+
nokogiri (>= 1.5.9)
|
56
|
+
mail (2.6.4)
|
57
|
+
mime-types (>= 1.16, < 4)
|
58
|
+
mime-types (3.1)
|
59
|
+
mime-types-data (~> 3.2015)
|
60
|
+
mime-types-data (3.2016.0521)
|
61
|
+
mini_portile2 (2.1.0)
|
62
|
+
minitest (5.9.0)
|
63
|
+
nokogiri (1.6.8)
|
64
|
+
mini_portile2 (~> 2.1.0)
|
65
|
+
pkg-config (~> 1.1.7)
|
66
|
+
pkg-config (1.1.7)
|
67
|
+
rack (1.6.4)
|
68
|
+
rack-test (0.6.3)
|
69
|
+
rack (>= 1.0)
|
70
|
+
rails (4.2.7.1)
|
71
|
+
actionmailer (= 4.2.7.1)
|
72
|
+
actionpack (= 4.2.7.1)
|
73
|
+
actionview (= 4.2.7.1)
|
74
|
+
activejob (= 4.2.7.1)
|
75
|
+
activemodel (= 4.2.7.1)
|
76
|
+
activerecord (= 4.2.7.1)
|
77
|
+
activesupport (= 4.2.7.1)
|
78
|
+
bundler (>= 1.3.0, < 2.0)
|
79
|
+
railties (= 4.2.7.1)
|
80
|
+
sprockets-rails
|
81
|
+
rails-deprecated_sanitizer (1.0.3)
|
82
|
+
activesupport (>= 4.2.0.alpha)
|
83
|
+
rails-dom-testing (1.0.7)
|
84
|
+
activesupport (>= 4.2.0.beta, < 5.0)
|
85
|
+
nokogiri (~> 1.6.0)
|
86
|
+
rails-deprecated_sanitizer (>= 1.0.1)
|
87
|
+
rails-html-sanitizer (1.0.3)
|
88
|
+
loofah (~> 2.0)
|
89
|
+
railties (4.2.7.1)
|
90
|
+
actionpack (= 4.2.7.1)
|
91
|
+
activesupport (= 4.2.7.1)
|
92
|
+
rake (>= 0.8.7)
|
93
|
+
thor (>= 0.18.1, < 2.0)
|
94
|
+
rake (11.2.2)
|
95
|
+
rspec (3.5.0)
|
96
|
+
rspec-core (~> 3.5.0)
|
97
|
+
rspec-expectations (~> 3.5.0)
|
98
|
+
rspec-mocks (~> 3.5.0)
|
99
|
+
rspec-core (3.5.3)
|
100
|
+
rspec-support (~> 3.5.0)
|
101
|
+
rspec-expectations (3.5.0)
|
102
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
103
|
+
rspec-support (~> 3.5.0)
|
104
|
+
rspec-mocks (3.5.0)
|
105
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
106
|
+
rspec-support (~> 3.5.0)
|
107
|
+
rspec-support (3.5.0)
|
108
|
+
sprockets (3.7.0)
|
109
|
+
concurrent-ruby (~> 1.0)
|
110
|
+
rack (> 1, < 3)
|
111
|
+
sprockets-rails (3.2.0)
|
112
|
+
actionpack (>= 4.0)
|
113
|
+
activesupport (>= 4.0)
|
114
|
+
sprockets (>= 3.0.0)
|
115
|
+
thor (0.19.1)
|
116
|
+
thread_safe (0.3.5)
|
117
|
+
tzinfo (1.2.2)
|
118
|
+
thread_safe (~> 0.1)
|
119
|
+
|
120
|
+
PLATFORMS
|
121
|
+
ruby
|
122
|
+
|
123
|
+
DEPENDENCIES
|
124
|
+
map_project!
|
125
|
+
rspec
|
126
|
+
|
127
|
+
BUNDLED WITH
|
128
|
+
1.12.5
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Lu Zou
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# map_project
|
2
|
+
This gem provides a set of tools which can help users to map geo lat-lng to viewport coords. For example, you can use this gem to convert lat-long points to html `<div>` coords.
|
3
|
+
|
4
|
+
# Install
|
5
|
+
In Gemfile:
|
6
|
+
```ruby
|
7
|
+
gem 'map_project'
|
8
|
+
```
|
9
|
+
and bundle:
|
10
|
+
```ruby
|
11
|
+
$ bundle install
|
12
|
+
```
|
13
|
+
Then, include:
|
14
|
+
```ruby
|
15
|
+
include MapProject
|
16
|
+
```
|
17
|
+
In any files you need.
|
18
|
+
|
19
|
+
# Usage:
|
20
|
+
***
|
21
|
+
If you have a boundary:
|
22
|
+
```ruby
|
23
|
+
{sw: [MIN_LAT, MIN_LNG],
|
24
|
+
ne: [MAX_LAT, MAX_LNG]})
|
25
|
+
```
|
26
|
+
and you know your viewport sizes (ex. width and height of a `<div>`), you can get the zoom level:
|
27
|
+
```ruby
|
28
|
+
zoom_level = get_bounds_zoom_level(boundary, viewport_width, viewport_height)
|
29
|
+
```
|
30
|
+
***
|
31
|
+
Once you get the zoom level, you can do a lot of more:<br />
|
32
|
+
First, create a map_project object:
|
33
|
+
```ruby
|
34
|
+
map_projection = MapProject.new({zoom_level: ZOOMLEVEL, lat: LAT, long: LONG, viewport_w: VIEWPORT_WIDTH, viewport_h: VIEWPORT_HEIGHT})
|
35
|
+
```
|
36
|
+
The `:lat` and `:long` are the geo position of the center.
|
37
|
+
Example:
|
38
|
+
```ruby
|
39
|
+
map_projection = MapProject.new({zoom_level: 15, lat: 37.294742, long: -122.019252, viewport_w: 1024, viewport_h: 1024})
|
40
|
+
```
|
41
|
+
|
42
|
+
Then, you can:<br />
|
43
|
+
Convert geo lat-lng to Google World Coords:
|
44
|
+
```ruby
|
45
|
+
map_projection.world_coords
|
46
|
+
```
|
47
|
+
Convert geo lat-lng to Google Pixel Coords:
|
48
|
+
```ruby
|
49
|
+
map_projection.pixel_coords
|
50
|
+
```
|
51
|
+
Get number of pixels per latitude/longitude inside this map area:
|
52
|
+
```ruby
|
53
|
+
map_projection.pixel_per_lat
|
54
|
+
map_projection.pixel_per_long
|
55
|
+
```
|
56
|
+
Map any geo lat-lng points to viewport coords:
|
57
|
+
```ruby
|
58
|
+
(0,0) (0, x)
|
59
|
+
------------
|
60
|
+
| |
|
61
|
+
| viewport |
|
62
|
+
| |
|
63
|
+
------------
|
64
|
+
(y, 0) (x, y)
|
65
|
+
map_project.offset_on_viewport(input_lat, input_long)
|
66
|
+
```
|
67
|
+
Get pixel boundary on this viewport:
|
68
|
+
```ruby
|
69
|
+
map_projection.pixel_bounds_based_on_center
|
70
|
+
```
|
71
|
+
The input boundary you used for getting zoom level may be different from the real boundary on map.
|
72
|
+
Get actual geo boundary on this viewport:
|
73
|
+
```ruby
|
74
|
+
map_projection.geo_bounds_based_on_center
|
75
|
+
```
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'MapProject'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
Bundler::GemHelper.install_tasks
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'lib'
|
28
|
+
t.libs << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
t.verbose = false
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
task default: :test
|
@@ -0,0 +1,103 @@
|
|
1
|
+
module MapProject
|
2
|
+
TILE_SIZE = 256.freeze
|
3
|
+
FACTOR_TO_RAD = Rational(Math::PI, 180).freeze
|
4
|
+
FACTOR_TO_DEG = Rational(180.0, Math::PI).freeze
|
5
|
+
PIXEL_PER_DEG = Rational(TILE_SIZE, 360).freeze
|
6
|
+
PIXEL_PER_RAD = Rational(TILE_SIZE, (2 * Math::PI)).freeze
|
7
|
+
ZERO_ZERO_PX = [TILE_SIZE / 2, TILE_SIZE / 2].freeze
|
8
|
+
GOOGLE_MAPS_MAX_LAT = 85.05115.freeze
|
9
|
+
GOOGLE_MAPS_MAX_LONG = 180.freeze
|
10
|
+
|
11
|
+
class MapProject
|
12
|
+
attr_accessor :viewport_size, :pixel_per_lat, :pixel_per_long, :world_coords, :center_lat,
|
13
|
+
:center_long, :geo_bounds_based_on_center, :pixel_bounds_based_on_center
|
14
|
+
|
15
|
+
def initialize(opts)
|
16
|
+
@viewport_w = opts[:viewport_w]
|
17
|
+
@viewport_h = opts[:viewport_h]
|
18
|
+
@center_lat = opts[:lat]
|
19
|
+
@center_long = opts[:long]
|
20
|
+
@zoom_level = opts[:zoom_level].nil? ? get_zoom_from_bound(opts[:bound]) : opts[:zoom_level]
|
21
|
+
@tile_number = 2 ** @zoom_level
|
22
|
+
end
|
23
|
+
|
24
|
+
def world_coords
|
25
|
+
@world_coords ||= lat_lng_to_world(@center_lat, @center_long)
|
26
|
+
end
|
27
|
+
|
28
|
+
def pixel_coords
|
29
|
+
@pixel_coords ||= world_to_pixel(world_coords)
|
30
|
+
end
|
31
|
+
|
32
|
+
# pixel per lat at current latlng and zoom level
|
33
|
+
def pixel_per_lat
|
34
|
+
@pixel_per_lat ||= Rational(pixel_coords[0], GOOGLE_MAPS_MAX_LAT - @center_lat)
|
35
|
+
end
|
36
|
+
|
37
|
+
# pixel per long at current latlng and zoom level
|
38
|
+
def pixel_per_long
|
39
|
+
@pixel_per_long ||= Rational(pixel_coords[1], GOOGLE_MAPS_MAX_LONG + @center_long)
|
40
|
+
end
|
41
|
+
|
42
|
+
# project the map coords to viewport, return a point's offsets on viewport
|
43
|
+
# (0,0) (0, x)
|
44
|
+
# ------------
|
45
|
+
# | |
|
46
|
+
# | viewport |
|
47
|
+
# | |
|
48
|
+
# ------------
|
49
|
+
# (y, 0) (x, y)
|
50
|
+
# Return: the projected offset of the point
|
51
|
+
# Usage example: css sprite
|
52
|
+
def offset_on_viewport(p_lat, p_long)
|
53
|
+
input_pixel_coords = world_to_pixel(lat_lng_to_world(p_lat, p_long))
|
54
|
+
|
55
|
+
[
|
56
|
+
(pixel_bounds_based_on_center[:sw][1] - input_pixel_coords[1]).abs,
|
57
|
+
(pixel_bounds_based_on_center[:ne][0] - input_pixel_coords[0]).abs
|
58
|
+
]
|
59
|
+
end
|
60
|
+
|
61
|
+
# Return: pixel bounds of the viewport
|
62
|
+
# PS: Top Left is 0,0
|
63
|
+
# in pixel coords, lat long increase => farther from 0,0
|
64
|
+
def pixel_bounds_based_on_center
|
65
|
+
viewport_radius_w = @viewport_w / 2
|
66
|
+
viewport_radius_h = @viewport_h / 2
|
67
|
+
@pixel_bounds_based_on_center ||= {
|
68
|
+
sw: [pixel_coords[0] + viewport_radius_h, pixel_coords[1] - viewport_radius_w],
|
69
|
+
ne: [pixel_coords[0] - viewport_radius_h, pixel_coords[1] + viewport_radius_w]
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
# Return: latlng bounds of the viewport
|
74
|
+
def geo_bounds_based_on_center
|
75
|
+
viewport_radius_w = @viewport_w / 2
|
76
|
+
viewport_radius_h = @viewport_h / 2
|
77
|
+
max_lat = @center_lat + Rational(viewport_radius_h, pixel_per_lat)
|
78
|
+
min_lat = @center_lat - Rational(viewport_radius_h, pixel_per_lat)
|
79
|
+
max_long = @center_long + Rational(viewport_radius_w, pixel_per_long)
|
80
|
+
min_long = @center_long - Rational(viewport_radius_w, pixel_per_long)
|
81
|
+
@geo_bounds_based_on_center ||= {
|
82
|
+
sw: [min_lat, min_long],
|
83
|
+
ne: [max_lat, max_long]
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def lat_lng_to_world(p_lat, p_long)
|
90
|
+
sin_y = Math.sin(Rational(p_lat * Math::PI, 180))
|
91
|
+
sin_y = [[sin_y, -1].max, 1].min
|
92
|
+
world_coord_long = TILE_SIZE * (0.5 + Rational(p_long, 360))
|
93
|
+
world_coord_lat = TILE_SIZE * (0.5 - Rational(Math.log(Rational(1 + sin_y, 1 - sin_y)), 4 * Math::PI))
|
94
|
+
[world_coord_lat, world_coord_long]
|
95
|
+
end
|
96
|
+
|
97
|
+
def world_to_pixel(world_coords)
|
98
|
+
scale = 1 << @zoom_level
|
99
|
+
[(world_coords[0] * scale).floor,
|
100
|
+
(world_coords[1] * scale).floor]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
data/lib/map_project.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'map_project/map_project.rb'
|
2
|
+
|
3
|
+
module MapProject
|
4
|
+
LN2 = 0.6931471805599453.freeze
|
5
|
+
WORLD_PX = 256.freeze
|
6
|
+
ZOOM_MAX = 21.freeze
|
7
|
+
# Get the zoom level for a viewport given latlng boundary
|
8
|
+
def get_bounds_zoom_level(bounds, viewport_w, viewport_h)
|
9
|
+
ne = bounds[:ne]
|
10
|
+
sw = bounds[:sw]
|
11
|
+
lat_fraction = (lat_rad(ne[0]) - lat_rad(sw[0])) / Math::PI
|
12
|
+
lng_diff = ne[1] - sw[1]
|
13
|
+
lng_fraction = ((lng_diff < 0) ? (lng_diff + 360) : lng_diff) / 360
|
14
|
+
lat_zoom = zoom(viewport_h, WORLD_PX, lat_fraction)
|
15
|
+
lng_zoom = zoom(viewport_w, WORLD_PX, lng_fraction)
|
16
|
+
[[lat_zoom, lng_zoom].min, ZOOM_MAX].min
|
17
|
+
end
|
18
|
+
|
19
|
+
def lat_rad(lat)
|
20
|
+
sin = Math.sin(lat * Math::PI / 180)
|
21
|
+
rad_x2 = Math.log((1 + sin) / (1 - sin)) / 2
|
22
|
+
[[rad_x2, Math::PI].min, -Math::PI].max / 2
|
23
|
+
end
|
24
|
+
|
25
|
+
def zoom(map_px, world_px, fraction)
|
26
|
+
(Math.log(map_px / world_px / fraction) / LN2).floor
|
27
|
+
end
|
28
|
+
end
|
data/map_project.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "map_project/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "map_project"
|
9
|
+
s.version = MapProject::VERSION
|
10
|
+
s.authors = ["Lu Zou"]
|
11
|
+
s.email = ["luxizou.web@gmail.com"]
|
12
|
+
s.homepage = "https://github.com/luzou0526/map_project"
|
13
|
+
s.summary = "Tools converting geo latlng map projection."
|
14
|
+
s.description = "Gem provides a way to calculate zoom level using boudary, and project map to viewports(html elements)"
|
15
|
+
s.license = "MIT"
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path('../../../lib/map_project.rb', __FILE__)
|
2
|
+
|
3
|
+
describe 'map_project' do
|
4
|
+
include MapProject
|
5
|
+
|
6
|
+
TEST_VIEW_PORT_HEIGHT = 1024.freeze
|
7
|
+
TEST_VIEW_PORT_WIDTH = 1024.freeze
|
8
|
+
|
9
|
+
let(:map_project_test_bounds) do
|
10
|
+
{
|
11
|
+
:sw => [ 37.176897, -121.976457],
|
12
|
+
:ne => [ 37.429432000000006, -121.75589500000001]
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#get_bounds_zoom_level' do
|
17
|
+
it 'should return a proper zoom level covers the bounds' do
|
18
|
+
expect(get_bounds_zoom_level(map_project_test_bounds, TEST_VIEW_PORT_WIDTH, TEST_VIEW_PORT_HEIGHT)).to eq(12)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:map_project_test_obj) do
|
23
|
+
MapProject::MapProject.new({zoom_level: 12, lat: 37.3031645, long: -121.866176, viewport_w: TEST_VIEW_PORT_WIDTH, viewport_h: TEST_VIEW_PORT_HEIGHT})
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'MapProject class' do
|
27
|
+
it 'world_coords' do
|
28
|
+
expect(map_project_test_obj.world_coords).to eq([99.37241713266029, 41.33960817777778])
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'pixel_coords' do
|
32
|
+
expect(map_project_test_obj.pixel_coords).to eq([407029, 169327])
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'pixel_per_lat' do
|
36
|
+
expect(map_project_test_obj.pixel_per_lat).to eq(Rational(57284239147780800512, 6719931553296621))
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'pixel_per_long' do
|
40
|
+
expect(map_project_test_obj.pixel_per_long).to eq(Rational(372354010792853504, 127837630910167))
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'offset_on_viewport' do
|
44
|
+
expect(map_project_test_obj.offset_on_viewport(37.3, -121.8)).to eq([704, 524])
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'pixel_bounds_based_on_center' do
|
48
|
+
expect(map_project_test_obj.pixel_bounds_based_on_center).to eq({:sw=>[407541, 168815], :ne=>[406517, 169839]})
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'geo_bounds_based_on_center' do
|
52
|
+
expect(map_project_test_obj.geo_bounds_based_on_center).to eq({:sw=>[37.24310251774321, -122.04195728643394],
|
53
|
+
:ne=>[37.36322648225679, -121.69039471356605]})
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
@@ -0,0 +1,28 @@
|
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|
data/test/dummy/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any styles
|
10
|
+
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
11
|
+
* file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
6
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
7
|
+
<%= csrf_meta_tags %>
|
8
|
+
</head>
|
9
|
+
<body>
|
10
|
+
|
11
|
+
<%= yield %>
|
12
|
+
|
13
|
+
</body>
|
14
|
+
</html>
|
data/test/dummy/bin/rake
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
|
4
|
+
# path to your application root.
|
5
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
6
|
+
|
7
|
+
Dir.chdir APP_ROOT do
|
8
|
+
# This script is a starting point to setup your application.
|
9
|
+
# Add necessary setup steps to this file:
|
10
|
+
|
11
|
+
puts "== Installing dependencies =="
|
12
|
+
system "gem install bundler --conservative"
|
13
|
+
system "bundle check || bundle install"
|
14
|
+
|
15
|
+
# puts "\n== Copying sample files =="
|
16
|
+
# unless File.exist?("config/database.yml")
|
17
|
+
# system "cp config/database.yml.sample config/database.yml"
|
18
|
+
# end
|
19
|
+
|
20
|
+
puts "\n== Preparing database =="
|
21
|
+
system "bin/rake db:setup"
|
22
|
+
|
23
|
+
puts "\n== Removing old logs and tempfiles =="
|
24
|
+
system "rm -f log/*"
|
25
|
+
system "rm -rf tmp/cache"
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system "touch tmp/restart.txt"
|
29
|
+
end
|