eviltransform 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/eviltransform.gemspec +41 -0
- data/lib/eviltransform.rb +143 -0
- data/lib/eviltransform/version.rb +3 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b36b157db4ceed0aec94322a745fa7d8d442c5a5484bf946215c901db6567629
|
4
|
+
data.tar.gz: e1b17bbbfd318e21e5cc3868dbbeba85606e60409a05dc31646d7c90a5254014
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 58b52892d4a2bd6d60df2585e3d035208c9368d4ba09ddf8337e71d4ef6b279c8fea5c4393bf1d96cffe0a16005b649510586717f7e2a32f930db21a40f254bd
|
7
|
+
data.tar.gz: ac9798934b3700e3d37c40b0a28772cbee2c4ba50e8c32c7d1a30cc812e424d9dceb58c9da3c36a4ec3d5ca6d59eadc7d3ffb53dc5aa9ee0706c8ffbb6bd24b1
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
eviltransform (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.11.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
bundler (~> 1.16)
|
17
|
+
eviltransform!
|
18
|
+
minitest (~> 5.0)
|
19
|
+
rake (~> 10.0)
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jane Macfarlane
|
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,49 @@
|
|
1
|
+
# Eviltransform
|
2
|
+
|
3
|
+
A collection of functions for working with geospatial coordinates in China.
|
4
|
+
|
5
|
+
For information on geospatial coordinates in China, visit the wikipedia article
|
6
|
+
[Restrictions on geographic data in China][1]
|
7
|
+
|
8
|
+
This code is a simple translation of the Python version found at
|
9
|
+
https://github.com/googollee/eviltransform
|
10
|
+
and is meant as a ruby contribution to that project.
|
11
|
+
|
12
|
+
[1]: https://en.wikipedia.org/wiki/Restrictions_on_geographic_data_in_China
|
13
|
+
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'eviltransform'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install eviltransform
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
Please see the [test file][2] for usage examples.
|
34
|
+
|
35
|
+
[2]: https://github.com/doctorjane/eviltransform/blob/master/test/eviltransform_test.rb
|
36
|
+
|
37
|
+
## Development
|
38
|
+
|
39
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
40
|
+
|
41
|
+
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).
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/doctorjane/eviltransform.
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "eviltransform"
|
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,41 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "eviltransform/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "eviltransform"
|
8
|
+
spec.version = Eviltransform::VERSION
|
9
|
+
spec.authors = ["Jane Macfarlane"]
|
10
|
+
spec.email = ["jane@doctorjane.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Transform coordinates between earth(WGS-84) and mars in china(GCJ-02)}
|
13
|
+
spec.description = %q{
|
14
|
+
This is a straight translation of the Python implementation of EvilTransform
|
15
|
+
(https://github.com/googollee/eviltransform).
|
16
|
+
}
|
17
|
+
# spec.homepage = "https://github.com/doctorjane/eviltransform"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
21
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
22
|
+
# if spec.respond_to?(:metadata)
|
23
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
24
|
+
# else
|
25
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
# "public gem pushes."
|
27
|
+
# end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
39
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
40
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
41
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require "eviltransform/version"
|
2
|
+
|
3
|
+
module Eviltransform
|
4
|
+
def self.outOfChina(lat, lng)
|
5
|
+
if (lng >= 72.004 && lng <= 137.8347 && lat >= 0.8293 && lat <= 55.8271)
|
6
|
+
return false
|
7
|
+
else
|
8
|
+
return true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.transform(x, y)
|
13
|
+
xy = x * y
|
14
|
+
absX = Math.sqrt(x.abs)
|
15
|
+
xPi = x * Math::PI
|
16
|
+
yPi = y * Math::PI
|
17
|
+
d = 20.0*Math.sin(6.0*xPi) + 20.0*Math.sin(2.0*xPi)
|
18
|
+
|
19
|
+
lat = d
|
20
|
+
lng = d
|
21
|
+
|
22
|
+
lat += 20.0*Math.sin(yPi) + 40.0*Math.sin(yPi/3.0)
|
23
|
+
lng += 20.0*Math.sin(xPi) + 40.0*Math.sin(xPi/3.0)
|
24
|
+
|
25
|
+
lat += 160.0*Math.sin(yPi/12.0) + 320*Math.sin(yPi/30.0)
|
26
|
+
lng += 150.0*Math.sin(xPi/12.0) + 300.0*Math.sin(xPi/30.0)
|
27
|
+
|
28
|
+
lat *= 2.0 / 3.0
|
29
|
+
lng *= 2.0 / 3.0
|
30
|
+
|
31
|
+
lat += -100.0 + 2.0*x + 3.0*y + 0.2*y*y + 0.1*xy + 0.2*absX
|
32
|
+
lng += 300.0 + x + 2.0*y + 0.1*x*x + 0.1*xy + 0.1*absX
|
33
|
+
|
34
|
+
return [lat, lng]
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.delta(lat, lng)
|
38
|
+
earthR = 6378137.0
|
39
|
+
ee = 0.00669342162296594323
|
40
|
+
dLat, dLng = transform(lng - 105.0, lat - 35.0)
|
41
|
+
radLat = lat / 180.0 * Math::PI
|
42
|
+
magic = Math.sin(radLat)
|
43
|
+
magic = 1.0 - ee * magic * magic
|
44
|
+
sqrtMagic = Math.sqrt(magic)
|
45
|
+
dLat = (dLat * 180.0) / ((earthR * (1.0 - ee)) / (magic * sqrtMagic) * Math::PI)
|
46
|
+
dLng = (dLng * 180.0) / (earthR / sqrtMagic * Math.cos(radLat) * Math::PI)
|
47
|
+
return [dLat, dLng]
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.wgs2gcj(wgsLat, wgsLng)
|
51
|
+
dlat, dlng = delta(wgsLat, wgsLng)
|
52
|
+
return [wgsLat + dlat, wgsLng + dlng]
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.gcj2wgs(gcjLat, gcjLng)
|
56
|
+
dlat, dlng = delta(gcjLat, gcjLng)
|
57
|
+
return [gcjLat - dlat, gcjLng - dlng]
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.gcj2wgs_exact(gcjLat, gcjLng)
|
61
|
+
initDelta = 0.01
|
62
|
+
threshold = 0.000_001
|
63
|
+
dLat = dLng = initDelta
|
64
|
+
mLat = gcjLat - dLat
|
65
|
+
mLng = gcjLng - dLng
|
66
|
+
pLat = gcjLat + dLat
|
67
|
+
pLng = gcjLng + dLng
|
68
|
+
(0...30).each do |i|
|
69
|
+
wgsLat = (mLat + pLat) / 2.0
|
70
|
+
wgsLng = (mLng + pLng) / 2.0
|
71
|
+
tmplat, tmplng = wgs2gcj(wgsLat, wgsLng)
|
72
|
+
dLat = tmplat - gcjLat
|
73
|
+
dLng = tmplng - gcjLng
|
74
|
+
if dLat.abs < threshold and dLng.abs < threshold
|
75
|
+
return wgsLat, wgsLng
|
76
|
+
end
|
77
|
+
if dLat > 0
|
78
|
+
pLat = wgsLat
|
79
|
+
else
|
80
|
+
mLat = wgsLat
|
81
|
+
end
|
82
|
+
if dLng > 0
|
83
|
+
pLng = wgsLng
|
84
|
+
else
|
85
|
+
mLng = wgsLng
|
86
|
+
end
|
87
|
+
end
|
88
|
+
[wgsLat, wgsLng]
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.distance(latA, lngA, latB, lngB)
|
92
|
+
earthR = 6378137.0
|
93
|
+
pi180 = Math::PI / 180
|
94
|
+
arcLatA = latA * pi180
|
95
|
+
arcLatB = latB * pi180
|
96
|
+
x = (Math.cos(arcLatA) * Math.cos(arcLatB) *
|
97
|
+
Math.cos((lngA - lngB) * pi180))
|
98
|
+
y = Math.sin(arcLatA) * Math.sin(arcLatB)
|
99
|
+
s = x + y
|
100
|
+
if s > 1
|
101
|
+
s = 1
|
102
|
+
elsif s < -1
|
103
|
+
s = -1
|
104
|
+
end
|
105
|
+
alpha = Math.acos(s)
|
106
|
+
distance = alpha * earthR
|
107
|
+
return distance
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.gcj2bd(gcjLat, gcjLng)
|
111
|
+
x = gcjLng
|
112
|
+
y = gcjLat
|
113
|
+
z = Math.hypot(x, y) + 0.00002 * Math.sin(y * Math::PI)
|
114
|
+
theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * Math::PI)
|
115
|
+
bdLng = z * Math.cos(theta) + 0.0065
|
116
|
+
bdLat = z * Math.sin(theta) + 0.006
|
117
|
+
return [bdLat, bdLng]
|
118
|
+
end
|
119
|
+
|
120
|
+
def self.bd2gcj(bdLat, bdLng)
|
121
|
+
x = bdLng - 0.0065
|
122
|
+
y = bdLat - 0.006
|
123
|
+
z = Math.hypot(x, y) - 0.00002 * Math.sin(y * Math::PI)
|
124
|
+
theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math::PI)
|
125
|
+
gcjLng = z * Math.cos(theta)
|
126
|
+
gcjLat = z * Math.sin(theta)
|
127
|
+
return [gcjLat, gcjLng]
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.wgs2bd(wgsLat, wgsLng)
|
131
|
+
lat, lng = wgs2gcj(wgsLat, wgsLng)
|
132
|
+
clat, clng = gcj2bd(lat, lng)
|
133
|
+
return [clat, clng]
|
134
|
+
end
|
135
|
+
|
136
|
+
def self.bd2wgs(bdLat, bdLng)
|
137
|
+
return gcj2wgs(*bd2gcj(bdLat, bdLng))
|
138
|
+
end
|
139
|
+
|
140
|
+
def self.bd2wgs_exact(bdLat, bdLng)
|
141
|
+
return gcj2wgs_exact(*bd2gcj(bdLat, bdLng))
|
142
|
+
end
|
143
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eviltransform
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jane Macfarlane
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-22 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: |2
|
56
|
+
|
57
|
+
This is a straight translation of the Python implementation of EvilTransform
|
58
|
+
(https://github.com/googollee/eviltransform).
|
59
|
+
email:
|
60
|
+
- jane@doctorjane.org
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- eviltransform.gemspec
|
75
|
+
- lib/eviltransform.rb
|
76
|
+
- lib/eviltransform/version.rb
|
77
|
+
homepage:
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.7.7
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Transform coordinates between earth(WGS-84) and mars in china(GCJ-02)
|
101
|
+
test_files: []
|