spherical_mercator 0.1.0
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 +7 -0
- data/.gitignore +53 -0
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/.travis.yml +17 -0
- data/Gemfile +9 -0
- data/LICENSE +21 -0
- data/README.md +110 -0
- data/Rakefile +6 -0
- data/lib/spherical_mercator.rb +180 -0
- data/lib/spherical_mercator/version.rb +19 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/spherical_mercator/spherical_mercator_spec.rb +97 -0
- data/spherical_mercator.gemspec +21 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 16bd516923907417c8a6900535ae5492b8e2f028
|
4
|
+
data.tar.gz: 0d75745f7a75eec2b9b563af0461133d6d731831
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c1f7eebc8eea7dfadef4006a775e45dfa40aef3c4e763dcf0a70ddd7bef8f7cfcc38778c33239413fe2f4305a51cee228e36caeabb5b858a3ca41c3e2164b277
|
7
|
+
data.tar.gz: 525366eebcf95f726443b684462c9e702b493e8ac6a26b14cb46e61f2b633383f6b43d08c7a05f36fa9e5d694c78eb8ede60887a516fc9f4dd1279c59af4e815
|
data/.gitignore
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
.idea/
|
17
|
+
Gemfile.lock
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
language: ruby
|
2
|
+
before_install: gem install bundler
|
3
|
+
bundler_args: --without yard guard benchmarks
|
4
|
+
script: "rake spec"
|
5
|
+
rvm:
|
6
|
+
- 2.0.0
|
7
|
+
- 2.2.4
|
8
|
+
- 2.3.3
|
9
|
+
- 2.4.0
|
10
|
+
- jruby-9.0.5.0
|
11
|
+
- jruby-9.1.5.0
|
12
|
+
- jruby-9.1.6.0
|
13
|
+
- ruby-head
|
14
|
+
env: JRUBY_OPTS="$JRUBY_OPTS --debug"
|
15
|
+
matrix:
|
16
|
+
allow_failures:
|
17
|
+
- rvm: ruby-head
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Nikita Bulai
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# Ruby port of Mapbox Spherical Mercator
|
2
|
+
[](http://badge.fury.io/rb/spherical_mercator)
|
3
|
+
[](https://travis-ci.org/nbulaj/spherical_mercator)
|
4
|
+
[](https://gemnasium.com/nbulaj/spherical_mercator)
|
5
|
+
[](https://coveralls.io/github/nbulaj/spherical_mercator)
|
6
|
+
[](#license)
|
7
|
+
|
8
|
+
Spherical Mercator gem is a port of [Mapbox sphericalmercator JS lib](https://github.com/mapbox/sphericalmercator) that provides projection math
|
9
|
+
for converting between mercator meters, screen pixels (of 256x256 or configurable-size tiles), and latitude/longitude.
|
10
|
+
|
11
|
+
## Requirements
|
12
|
+
|
13
|
+
* Ruby >= 2.0
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Simple:
|
18
|
+
|
19
|
+
`gem install spherical_mercator`
|
20
|
+
|
21
|
+
For Gemfile:
|
22
|
+
|
23
|
+
`gem 'spherical_mercator'`
|
24
|
+
|
25
|
+
## API
|
26
|
+
|
27
|
+
_NOTE_: API description copied from the original repo.
|
28
|
+
|
29
|
+
Some datatypes are assumed to be arrays: `ll` is `[lon, lat]`, `xy` and `px` are
|
30
|
+
`[x, y]`.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# By default, precomputes up to z30
|
34
|
+
mercator = SphericalMercator.new(size: 256)
|
35
|
+
```
|
36
|
+
|
37
|
+
### `px(lon_lat, zoom)`
|
38
|
+
|
39
|
+
Convert lon, lat to screen pixel x, y from 0, 0 origin, at a certain zoom level.
|
40
|
+
The inverse of `ll`
|
41
|
+
|
42
|
+
### `ll(px, zoom)`
|
43
|
+
|
44
|
+
Convert screen pixel value to lon, lat, at a certain zoom level. The inverse
|
45
|
+
of `px`
|
46
|
+
|
47
|
+
### `bbox(x, y, zoom, tms_style, srs)`
|
48
|
+
|
49
|
+
Convert tile xyz value to bbox of the form `[w, s, e, n]`
|
50
|
+
|
51
|
+
* `x` {Number} x (longitude) number.
|
52
|
+
* `y` {Number} y (latitude) number.
|
53
|
+
* `zoom` {Number} zoom.
|
54
|
+
* `tms_style` {Boolean} whether to compute using tms-style. (optional, default `false`)
|
55
|
+
* `srs` {String} projection for resulting bbox ('WGS84'|'900913'). (optional, default 'WGS84')
|
56
|
+
|
57
|
+
Returns bbox array of values in form `[w, s, e, n]`.
|
58
|
+
|
59
|
+
### `xyz(bbox, zoom, tms_style, srs)`
|
60
|
+
|
61
|
+
Convert bbox to xyz bounds
|
62
|
+
|
63
|
+
* `bbox` {Number} bbox in the form `[w, s, e, n]`.
|
64
|
+
* `zoom` {Number} zoom.
|
65
|
+
* `tms_style` {Boolean} whether to compute using tms-style. (optional, default `false`)
|
66
|
+
* `srs` {String} projection of input bbox ('WGS84'|'900913'). (optional, default 'WGS84')
|
67
|
+
|
68
|
+
Returns {Object} XYZ bounds containing minX, maxX, minY, maxY properties.
|
69
|
+
|
70
|
+
### `convert(bbox, to)`
|
71
|
+
|
72
|
+
Convert bbox from 900913 to WGS84 or vice versa
|
73
|
+
|
74
|
+
* `bbox` {Number} bbox in the form `[w, s, e, n]`.
|
75
|
+
* `to` {String} projection of resulting bbox ('WGS84'|'900913'). (optional, default 'WGS84')
|
76
|
+
|
77
|
+
Returns bbox array of values in form `[w, s, e, n]`.
|
78
|
+
|
79
|
+
### `forward(lon_lat)`
|
80
|
+
|
81
|
+
Convert lon, lat values to mercator x, y
|
82
|
+
|
83
|
+
### `inverse(xy)`
|
84
|
+
|
85
|
+
Convert mercator x, y values to lon, lat
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
You are very welcome to help improve spherical_mercator if you have suggestions for features that other people can use.
|
90
|
+
|
91
|
+
To contribute:
|
92
|
+
|
93
|
+
1. Fork the project.
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`).
|
95
|
+
3. Implement your feature or bug fix.
|
96
|
+
4. Add documentation for your feature or bug fix.
|
97
|
+
5. Run <tt>rake doc:yard</tt>. If your changes are not 100% documented, go back to step 4.
|
98
|
+
6. Add tests for your feature or bug fix.
|
99
|
+
7. Run `rake` to make sure all tests pass.
|
100
|
+
8. Commit your changes (`git commit -am 'Add new feature'`).
|
101
|
+
9. Push to the branch (`git push origin my-new-feature`).
|
102
|
+
10. Create new pull request.
|
103
|
+
|
104
|
+
Thanks.
|
105
|
+
|
106
|
+
## License
|
107
|
+
|
108
|
+
Spherical Mercator gem is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
109
|
+
|
110
|
+
Copyright (c) 2017 Nikita Bulai (bulajnikita@gmail.com) and [original lib authors](https://github.com/mapbox/sphericalmercator/graphs/contributors).
|
data/Rakefile
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
require 'spherical_mercator/version'
|
2
|
+
|
3
|
+
class SphericalMercator
|
4
|
+
attr_reader :options
|
5
|
+
# Closures including constants and other precalculated values.
|
6
|
+
|
7
|
+
EPSLN = 1.0e-10
|
8
|
+
D2R = Math::PI / 180
|
9
|
+
R2D = 180.0 / Math::PI
|
10
|
+
# 900913 properties.
|
11
|
+
A = 6_378_137.0
|
12
|
+
MAX_EXTENT = 20_037_508.342_789_244
|
13
|
+
|
14
|
+
attr_accessor :size, :cache
|
15
|
+
|
16
|
+
attr_accessor :ac, :bc, :cc, :zc
|
17
|
+
|
18
|
+
# SphericalMercator constructor: precaches calculations
|
19
|
+
# for fast tile lookups.
|
20
|
+
def initialize(opts = {})
|
21
|
+
@cache = {}
|
22
|
+
@options = opts || {}
|
23
|
+
|
24
|
+
self.size = (options[:size] || 256).to_f
|
25
|
+
|
26
|
+
if cache[size].nil?
|
27
|
+
size = self.size
|
28
|
+
c = cache[self.size] = {}
|
29
|
+
c['Bc'] = []
|
30
|
+
c['Cc'] = []
|
31
|
+
c['zc'] = []
|
32
|
+
c['Ac'] = []
|
33
|
+
|
34
|
+
30.times do
|
35
|
+
c['Ac'].push(size.to_i)
|
36
|
+
c['Bc'].push(size / 360)
|
37
|
+
c['Cc'].push(size / (2 * Math::PI))
|
38
|
+
c['zc'].push((size / 2).to_i)
|
39
|
+
|
40
|
+
size *= 2
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
@ac = cache[self.size]['Ac']
|
45
|
+
@bc = cache[self.size]['Bc']
|
46
|
+
@cc = cache[self.size]['Cc']
|
47
|
+
@zc = cache[self.size]['zc']
|
48
|
+
end
|
49
|
+
|
50
|
+
# Convert lon lat to screen pixel value
|
51
|
+
#
|
52
|
+
# - `ll` {Array} `[lon, lat]` array of geographic coordinates.
|
53
|
+
# - `zoom` {Number} zoom level.
|
54
|
+
def px(lon_lat, zoom)
|
55
|
+
d = @zc[zoom]
|
56
|
+
# JS: Math.min(Math.max(Math.sin(D2R * ll[1]), -0.9999), 0.9999);
|
57
|
+
f = [[Math.sin(D2R * lon_lat[1]), -0.9999].max, 0.9999].min
|
58
|
+
x = (d + lon_lat[0] * @bc[zoom]).round
|
59
|
+
y = (d + 0.5 * Math.log((1 + f) / (1 - f)) * (-@cc[zoom])).round
|
60
|
+
(x > @ac[zoom]) && (x = @ac[zoom])
|
61
|
+
(y > @ac[zoom]) && (y = @ac[zoom])
|
62
|
+
|
63
|
+
# (x < 0) && (x = 0);
|
64
|
+
# (y < 0) && (y = 0);
|
65
|
+
[x, y]
|
66
|
+
end
|
67
|
+
|
68
|
+
# Convert screen pixel value to lon lat
|
69
|
+
#
|
70
|
+
# - `px` {Array} `[x, y]` array of geographic coordinates.
|
71
|
+
# - `zoom` {Number} zoom level.
|
72
|
+
def ll(px, zoom)
|
73
|
+
g = (px[1] - @zc[zoom]) / (-@cc[zoom])
|
74
|
+
lon = (px[0] - @zc[zoom]) / @bc[zoom]
|
75
|
+
lat = R2D * (2 * Math.atan(Math.exp(g)) - 0.5 * Math::PI)
|
76
|
+
[lon, lat]
|
77
|
+
end
|
78
|
+
|
79
|
+
# Convert tile xyz value to bbox of the form `[w, s, e, n]`
|
80
|
+
#
|
81
|
+
# - `x` {Number} x (longitude) number.
|
82
|
+
# - `y` {Number} y (latitude) number.
|
83
|
+
# - `zoom` {Number} zoom.
|
84
|
+
# - `tms_style` {Boolean} whether to compute using tms-style.
|
85
|
+
# - `srs` {String} projection for resulting bbox (WGS84|900913).
|
86
|
+
# - `return` {Array} bbox array of values in form `[w, s, e, n]`.
|
87
|
+
def bbox(x, y, zoom, tms_style = false, srs = 'WGS84')
|
88
|
+
# Convert xyz into bbox with srs WGS84
|
89
|
+
y = ((2**zoom) - 1) - y if tms_style
|
90
|
+
|
91
|
+
lower_left = [x * @size, (y + 1) * @size] # lower left
|
92
|
+
upper_right = [(x + 1) * @size, y * @size] # upper right
|
93
|
+
|
94
|
+
bbox = ll(lower_left, zoom).concat(ll(upper_right, zoom))
|
95
|
+
|
96
|
+
# If web mercator requested reproject to 900913.
|
97
|
+
if srs == '900913'
|
98
|
+
convert(bbox, '900913')
|
99
|
+
else
|
100
|
+
bbox
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Convert bbox to xyx bounds
|
105
|
+
#
|
106
|
+
# - `bbox` {Number} bbox in the form `[w, s, e, n]`.
|
107
|
+
# - `zoom` {Number} zoom.
|
108
|
+
# - `tms_style` {Boolean} whether to compute using tms-style.
|
109
|
+
# - `srs` {String} projection of input bbox (WGS84|900913).
|
110
|
+
# - `@return` {Object} XYZ bounds containing minX, maxX, minY, maxY properties
|
111
|
+
def xyz(bbox, zoom, tms_style = false, srs = 'WGS84')
|
112
|
+
# If web mercator provided reproject to WGS84.
|
113
|
+
bbox = convert(bbox, 'WGS84') if srs == '900913'
|
114
|
+
|
115
|
+
lower_left = [bbox[0], bbox[1]] # lower left
|
116
|
+
upper_right = [bbox[2], bbox[3]] # upper right
|
117
|
+
px_ll = px(lower_left, zoom)
|
118
|
+
px_ur = px(upper_right, zoom)
|
119
|
+
|
120
|
+
# Y = 0 for XYZ is the top hence minY uses px_ur[1].
|
121
|
+
x = [(px_ll[0] / @size).floor, ((px_ur[0] - 1) / @size).floor]
|
122
|
+
y = [(px_ur[1] / @size).floor, ((px_ll[1] - 1) / @size).floor]
|
123
|
+
|
124
|
+
bounds = {
|
125
|
+
minX: x.min < 0 ? 0 : x.min, # JS: Math.min.apply(Math, x)
|
126
|
+
minY: y.min < 0 ? 0 : y.min,
|
127
|
+
maxX: x.max,
|
128
|
+
maxY: y.max
|
129
|
+
}
|
130
|
+
|
131
|
+
if tms_style
|
132
|
+
tms = {
|
133
|
+
minY: ((2**zoom) - 1) - bounds[:maxY],
|
134
|
+
maxY: ((2**zoom) - 1) - bounds[:minY]
|
135
|
+
}
|
136
|
+
|
137
|
+
bounds[:minY] = tms[:minY]
|
138
|
+
bounds[:maxY] = tms[:maxY]
|
139
|
+
end
|
140
|
+
|
141
|
+
bounds
|
142
|
+
end
|
143
|
+
|
144
|
+
# Convert projection of given bbox.
|
145
|
+
#
|
146
|
+
# - `bbox` {Number} bbox in the form `[w, s, e, n]`.
|
147
|
+
# - `to` {String} projection of output bbox (WGS84|900913). Input bbox
|
148
|
+
# assumed to be the "other" projection.
|
149
|
+
# - `@return` {Object} bbox with reprojected coordinates.
|
150
|
+
def convert(bbox, to = 'WGS84')
|
151
|
+
if to == '900913'
|
152
|
+
forward(bbox.slice(0, 2)).concat(forward(bbox.slice(2, 4)))
|
153
|
+
else
|
154
|
+
inverse(bbox.slice(0, 2)).concat(inverse(bbox.slice(2, 4)))
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Convert lon/lat values to 900913 x/y.
|
159
|
+
def forward(lon_lat)
|
160
|
+
xy = [
|
161
|
+
A * lon_lat[0] * D2R,
|
162
|
+
A * Math.log(Math.tan((Math::PI * 0.25) + (0.5 * lon_lat[1] * D2R)))
|
163
|
+
]
|
164
|
+
|
165
|
+
# if xy value is beyond maxextent (e.g. poles), return maxextent.
|
166
|
+
(xy[0] > MAX_EXTENT) && (xy[0] = MAX_EXTENT)
|
167
|
+
(xy[0] < -MAX_EXTENT) && (xy[0] = -MAX_EXTENT)
|
168
|
+
(xy[1] > MAX_EXTENT) && (xy[1] = MAX_EXTENT)
|
169
|
+
(xy[1] < -MAX_EXTENT) && (xy[1] = -MAX_EXTENT)
|
170
|
+
xy
|
171
|
+
end
|
172
|
+
|
173
|
+
# Convert 900913 x/y values to lon/lat.
|
174
|
+
def inverse(xy)
|
175
|
+
[
|
176
|
+
(xy[0] * R2D / A),
|
177
|
+
((Math::PI * 0.5) - 2.0 * Math.atan(Math.exp(-xy[1] / A))) * R2D
|
178
|
+
]
|
179
|
+
end
|
180
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class SphericalMercator
|
2
|
+
def self.gem_version
|
3
|
+
Gem::Version.new VERSION::STRING
|
4
|
+
end
|
5
|
+
|
6
|
+
##
|
7
|
+
# AlphaCard semantic versioning
|
8
|
+
module VERSION
|
9
|
+
# Major version number
|
10
|
+
MAJOR = 0
|
11
|
+
# Minor version number
|
12
|
+
MINOR = 1
|
13
|
+
# Smallest version number
|
14
|
+
TINY = 0
|
15
|
+
|
16
|
+
# Full version number
|
17
|
+
STRING = [MAJOR, MINOR, TINY].compact.join('.')
|
18
|
+
end
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
if ENV['CI'] || ENV['TRAVIS'] || ENV['COVERALLS'] || ENV['JENKINS_URL']
|
2
|
+
require 'coveralls'
|
3
|
+
Coveralls.wear!
|
4
|
+
else
|
5
|
+
require 'simplecov'
|
6
|
+
SimpleCov.start
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'bundler/setup'
|
10
|
+
Bundler.setup
|
11
|
+
|
12
|
+
require 'spherical_mercator'
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
config.order = 'random'
|
16
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
MAX_EXTENT_MERC = [
|
4
|
+
-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244
|
5
|
+
].freeze
|
6
|
+
|
7
|
+
MAX_EXTENT_WGS84 = [
|
8
|
+
-180, -85.0511287798066, 180, 85.0511287798066
|
9
|
+
].freeze
|
10
|
+
|
11
|
+
def rand_float
|
12
|
+
rand(0.0...1.0)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe SphericalMercator do
|
16
|
+
let!(:sm) {SphericalMercator.new}
|
17
|
+
|
18
|
+
context '#bbox' do
|
19
|
+
it '[0,0,0] converted to proper bbox' do
|
20
|
+
expect(sm.bbox(0, 0, 1, true, 'WGS84')).to eq([-180, -85.05112877980659, 0, 0])
|
21
|
+
end
|
22
|
+
|
23
|
+
it '[0,0,1] converted to proper bbox' do
|
24
|
+
expect(sm.bbox(0, 0, 0, true, 'WGS84')).to eq([-180, -85.05112877980659, 180, 85.0511287798066])
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context '#xyz' do
|
29
|
+
it 'World extents converted to proper tile ranges' do
|
30
|
+
expect(sm.xyz([-180, -85.05112877980659, 0, 0], 1, true, 'WGS84')).to eq({minX: 0, minY: 0, maxX: 0, maxY: 0})
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'SW converted to proper tile ranges' do
|
34
|
+
expect(sm.xyz([-180, -85.05112877980659, 180, 85.0511287798066], 0, true, 'WGS84')).to eq({minX: 0, minY: 0, maxX: 0, maxY: 0})
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'broken' do
|
38
|
+
extent = [-0.087891, 40.95703, 0.087891, 41.044916]
|
39
|
+
xyz = sm.xyz(extent, 3, true, 'WGS84')
|
40
|
+
|
41
|
+
expect(xyz[:minX] <= xyz[:maxX]).to be_truthy
|
42
|
+
expect(xyz[:minY] <= xyz[:maxY]).to be_truthy
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'negative' do
|
46
|
+
extent = [-112.5, 85.0511, -112.5, 85.0511]
|
47
|
+
xyz = sm.xyz(extent, 0)
|
48
|
+
|
49
|
+
expect(xyz[:minY]).to be_zero
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'fuzz' do
|
53
|
+
1000.times do
|
54
|
+
x = [-180 + (360 * rand_float), -180 + (360 * rand_float)]
|
55
|
+
y = [-85 + (170 * rand_float), -85 + (170 * rand_float)]
|
56
|
+
z = (22 * rand_float).floor
|
57
|
+
|
58
|
+
extent = [
|
59
|
+
x.min,
|
60
|
+
y.min,
|
61
|
+
x.max,
|
62
|
+
y.max
|
63
|
+
]
|
64
|
+
|
65
|
+
xyz = sm.xyz(extent, z, true, 'WGS84')
|
66
|
+
|
67
|
+
if xyz[:minX] > xyz[:maxX]
|
68
|
+
expect(xyz[:minX] <= xyz[:maxX]).to be_truthy
|
69
|
+
end
|
70
|
+
|
71
|
+
if xyz[:minY] > xyz[:maxY]
|
72
|
+
expect(xyz[:minY] <= xyz[:maxY]).to be_truthy
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context '#convert' do
|
79
|
+
it 'MAX_EXTENT_WGS84' do
|
80
|
+
expect(sm.convert(MAX_EXTENT_WGS84, '900913')).to eq(MAX_EXTENT_MERC)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'MAX_EXTENT_MERC' do
|
84
|
+
expect(sm.convert(MAX_EXTENT_MERC, 'WGS84')).to eq(MAX_EXTENT_WGS84)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context '#extents' do
|
89
|
+
it 'Maximum extents enforced on conversion to tile ranges' do
|
90
|
+
expect(sm.xyz([-240, -90, 240, 90], 4, true, 'WGS84')).to eq({minX: 0, minY: 0, maxX: 15, maxY: 15})
|
91
|
+
end
|
92
|
+
|
93
|
+
it '' do
|
94
|
+
expect(sm.convert([-240, -90, 240, 90], '900913')).to eq(MAX_EXTENT_MERC)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
2
|
+
|
3
|
+
require 'spherical_mercator/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = 'spherical_mercator'
|
7
|
+
gem.version = SphericalMercator.gem_version
|
8
|
+
gem.date = '2017-10-03'
|
9
|
+
gem.summary = 'Spherical Mercator math in Ruby'
|
10
|
+
gem.description = 'Spherical Mercator provides projection math for converting between mercator' \
|
11
|
+
'meters, screen pixels (of 256x256 or configurable-size tiles), and latitude/longitude'
|
12
|
+
gem.authors = ['Nikita Bulai']
|
13
|
+
gem.email = 'bulajnikita@gmail.com'
|
14
|
+
gem.require_paths = ['lib']
|
15
|
+
gem.files = `git ls-files`.split($RS)
|
16
|
+
gem.homepage = 'http://github.com/nbulaj/spherical_mercator'
|
17
|
+
gem.license = 'MIT'
|
18
|
+
gem.required_ruby_version = '>= 2.0'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec', '~> 3.5'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spherical_mercator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nikita Bulai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.5'
|
27
|
+
description: Spherical Mercator provides projection math for converting between mercatormeters,
|
28
|
+
screen pixels (of 256x256 or configurable-size tiles), and latitude/longitude
|
29
|
+
email: bulajnikita@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
37
|
+
- ".travis.yml"
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/spherical_mercator.rb
|
43
|
+
- lib/spherical_mercator/version.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/spherical_mercator/spherical_mercator_spec.rb
|
46
|
+
- spherical_mercator.gemspec
|
47
|
+
homepage: http://github.com/nbulaj/spherical_mercator
|
48
|
+
licenses:
|
49
|
+
- MIT
|
50
|
+
metadata: {}
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '2.0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 2.6.11
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Spherical Mercator math in Ruby
|
71
|
+
test_files: []
|