mars_geo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +1 -0
- data/lib/mars_geo/converter.rb +167 -0
- data/lib/mars_geo/point.rb +22 -0
- data/lib/mars_geo/version.rb +3 -0
- data/lib/mars_geo.rb +6 -0
- data/mars_geo.gemspec +19 -0
- data/spec/converter_spec.rb +46 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 jonny
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# MarsGeo
|
2
|
+
|
3
|
+
这是一个将真实经纬度转换成火星地址的Gem. 我们在做手机App开发时经常会用到地图,通常手机获得的经纬度放到
|
4
|
+
Google地图的时候会有几百米的偏差,这是因为某些原因国内地图都做了偏移加密,这个俗称火星坐标系。MarsGeo是
|
5
|
+
根据网上的一段Java代码翻译过来的,目的是把真实经纬度转换成火星坐标系好和Google地图匹配,至于准确性如何
|
6
|
+
我没有全面测试过,不过我自己使用的情况还是可以滴。
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'mars_geo'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mars_geo
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
在要使用的文件中 include MarsGeo Moudle
|
26
|
+
|
27
|
+
```
|
28
|
+
class HomeController < ApplicationController
|
29
|
+
include MarsGeo
|
30
|
+
|
31
|
+
def index
|
32
|
+
point = Point.new(31.108949, 121.333383)
|
33
|
+
offset_point = point.offset
|
34
|
+
p offset_point.lat
|
35
|
+
p offset_point.lng
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,167 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
module MarsGeo
|
4
|
+
class Converter
|
5
|
+
attr_accessor :casm_rr,:casm_t1,:casm_t2,:casm_x1,:casm_y1, :casm_x2,:casm_y2,:casm_f
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
self.casm_rr = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
def yj_sin2(x)
|
12
|
+
ff = 0
|
13
|
+
|
14
|
+
if x<0
|
15
|
+
x = -x
|
16
|
+
ff = 1
|
17
|
+
end
|
18
|
+
|
19
|
+
cc = (x / 6.28318530717959).to_i
|
20
|
+
tt = x - cc * 6.28318530717959
|
21
|
+
|
22
|
+
if tt > 3.1415926535897932
|
23
|
+
tt = tt - 3.1415926535897932
|
24
|
+
if ff == 1
|
25
|
+
ff = 0
|
26
|
+
elsif ff == 0
|
27
|
+
ff = 1
|
28
|
+
end
|
29
|
+
end
|
30
|
+
x = tt
|
31
|
+
ss = x
|
32
|
+
s2 = x
|
33
|
+
tt = tt * tt
|
34
|
+
s2 = s2 * tt
|
35
|
+
ss = ss - s2 * 0.166666666666667
|
36
|
+
s2 = s2 * tt
|
37
|
+
ss = ss + s2 * 8.33333333333333E-03
|
38
|
+
s2 = s2 * tt
|
39
|
+
ss = ss - s2 * 1.98412698412698E-04
|
40
|
+
s2 = s2 * tt
|
41
|
+
ss = ss + s2 * 2.75573192239859E-06
|
42
|
+
s2 = s2 * tt
|
43
|
+
ss = ss - s2 * 2.50521083854417E-08
|
44
|
+
ss = -ss if (ff == 1)
|
45
|
+
|
46
|
+
return ss;
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def transform_yj5(x, y)
|
51
|
+
tt = 300 + 1 * x + 2 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.sqrt(x * x))
|
52
|
+
tt = tt + (20 * yj_sin2(18.849555921538764 * x) + 20 * yj_sin2(6.283185307179588 * x)) * 0.6667
|
53
|
+
tt = tt + (20 * yj_sin2(3.141592653589794 * x) + 40 * yj_sin2(1.047197551196598 * x)) * 0.6667
|
54
|
+
tt = tt + (150 * yj_sin2(0.2617993877991495 * x) + 300 * yj_sin2(0.1047197551196598 * x)) * 0.6667
|
55
|
+
tt
|
56
|
+
end
|
57
|
+
|
58
|
+
def transform_yjy5(x,y)
|
59
|
+
tt = -100 + 2 * x + 3 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.sqrt(x * x))
|
60
|
+
tt = tt + (20 * yj_sin2(18.849555921538764 * x) + 20 * yj_sin2(6.283185307179588 * x)) * 0.6667
|
61
|
+
tt = tt + (20 * yj_sin2(3.141592653589794 * y) + 40 * yj_sin2(1.047197551196598 * y)) * 0.6667
|
62
|
+
tt = tt + (160 * yj_sin2(0.2617993877991495 * y) + 320 * yj_sin2(0.1047197551196598 * y)) * 0.6667
|
63
|
+
end
|
64
|
+
|
65
|
+
def transform_jy5(x, xx)
|
66
|
+
a = 6378245
|
67
|
+
e = 0.00669342
|
68
|
+
n = Math.sqrt(1 - e * yj_sin2(x * 0.0174532925199433) * yj_sin2(x * 0.0174532925199433))
|
69
|
+
n = (xx * 180) / (a / n * Math.cos(x * 0.0174532925199433) * 3.1415926)
|
70
|
+
end
|
71
|
+
|
72
|
+
def transform_jyj5(x,yy)
|
73
|
+
a = 6378245
|
74
|
+
e = 0.00669342
|
75
|
+
mm = 1 - e * yj_sin2(x * 0.0174532925199433) * yj_sin2(x * 0.0174532925199433)
|
76
|
+
m = (a * (1 - e)) / (mm * Math.sqrt(mm))
|
77
|
+
return (yy * 180) / (m * 3.1415926)
|
78
|
+
end
|
79
|
+
|
80
|
+
def random_yj
|
81
|
+
casm_a = 314159269
|
82
|
+
casm_c = 453806245
|
83
|
+
self.casm_rr = casm_a * self.casm_rr + casm_c
|
84
|
+
t = (self.casm_rr / 2).to_i
|
85
|
+
self.casm_rr = self.casm_rr - t * 2
|
86
|
+
self.casm_rr = self.casm_rr.to_f / 2
|
87
|
+
end
|
88
|
+
|
89
|
+
def ini_casm(w_time, w_lng, w_lat)
|
90
|
+
self.casm_t1 = w_time
|
91
|
+
self.casm_t2 = w_time
|
92
|
+
tt = (w_time / 0.357).to_i
|
93
|
+
self.casm_rr = w_time - tt * 0.357
|
94
|
+
self.casm_rr = 0.3 if (w_time == 0)
|
95
|
+
self.casm_x1 = w_lng
|
96
|
+
self.casm_y1 = w_lat
|
97
|
+
self.casm_x2 = w_lng
|
98
|
+
self.casm_y2 = w_lat
|
99
|
+
self.casm_f = 3
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
|
104
|
+
def wgtochina_lb(wg_flag,wg_lat,wg_lng,wg_heit,wg_week,wg_time)
|
105
|
+
|
106
|
+
point = {lat:0.0,lng:0.0}
|
107
|
+
|
108
|
+
return nil if (wg_heit > 5000)
|
109
|
+
x_l = wg_lng;
|
110
|
+
x_l = x_l / 3686400.0;
|
111
|
+
y_l = wg_lat;
|
112
|
+
y_l = y_l / 3686400.0;
|
113
|
+
|
114
|
+
return nil if (x_l < 72.004)
|
115
|
+
return nil if (x_l > 137.8347)
|
116
|
+
return nil if (y_l < 0.8293)
|
117
|
+
return nil if (y_l > 55.8271)
|
118
|
+
|
119
|
+
if (wg_flag == 0)
|
120
|
+
ini_casm(wg_time, wg_lng, wg_lat)
|
121
|
+
point[:lat] = wg_lng
|
122
|
+
point[:lng] = wg_lat
|
123
|
+
return point;
|
124
|
+
end
|
125
|
+
|
126
|
+
casm_t2 = wg_time
|
127
|
+
casm_t1,casm_f = 0,0
|
128
|
+
t1_t2 = (casm_t2 - casm_t1) / 1000.0
|
129
|
+
if (t1_t2 <= 0)
|
130
|
+
casm_t1 = casm_t2
|
131
|
+
casm_f = casm_f + 1
|
132
|
+
casm_x1 = casm_x2
|
133
|
+
casm_f = casm_f + 1
|
134
|
+
casm_y1 = casm_y2
|
135
|
+
casm_f = casm_f + 1
|
136
|
+
else
|
137
|
+
if t1_t2 > 120
|
138
|
+
if casm_f == 3
|
139
|
+
casm_f = 0
|
140
|
+
casm_x2 = wg_lng
|
141
|
+
casm_y2 = wg_lat
|
142
|
+
x1_x2 = casm_x2 - casm_x1
|
143
|
+
y1_y2 = casm_y2 - casm_y1
|
144
|
+
casm_v = Math.sqrt(x1_x2 * x1_x2 + y1_y2 * y1_y2) / t1_t2
|
145
|
+
return point if (casm_v > 3185)
|
146
|
+
end
|
147
|
+
casm_t1 = casm_t2
|
148
|
+
casm_f = casm_f + 1
|
149
|
+
casm_x1 = casm_x2
|
150
|
+
casm_f = casm_f + 1
|
151
|
+
casm_y1 = casm_y2
|
152
|
+
casm_f = casm_f + 1
|
153
|
+
end
|
154
|
+
end
|
155
|
+
x_add = transform_yj5(x_l - 105, y_l - 35)
|
156
|
+
y_add = transform_yjy5(x_l - 105, y_l - 35)
|
157
|
+
h_add = wg_heit
|
158
|
+
|
159
|
+
|
160
|
+
x_add = x_add + h_add * 0.001 + yj_sin2(wg_time * 0.0174532925199433) + random_yj()
|
161
|
+
y_add = y_add + h_add * 0.001 + yj_sin2(wg_time * 0.0174532925199433) + random_yj()
|
162
|
+
point[:lng] = ((x_l + transform_jy5(y_l, x_add)) * 3686400).to_i
|
163
|
+
point[:lat] = ((y_l + transform_jyj5(y_l, y_add)) * 3686400).to_i
|
164
|
+
return point
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MarsGeo
|
2
|
+
class Point
|
3
|
+
attr_accessor :lat,:lng
|
4
|
+
|
5
|
+
|
6
|
+
def initialize(lat,lng)
|
7
|
+
self.lat, self.lng = lat, lng
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
def offset
|
13
|
+
temp_lat = self.lat * 3686400
|
14
|
+
temp_lng = self.lng * 3686400
|
15
|
+
converter = Converter.new
|
16
|
+
hash_point = converter.wgtochina_lb(1,temp_lat.to_i,temp_lng.to_i,0,0,0)
|
17
|
+
temp_lat = hash_point[:lat] / 3686400.0
|
18
|
+
temp_lng = hash_point[:lng] / 3686400.0
|
19
|
+
Point.new(temp_lat,temp_lng)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/mars_geo.rb
ADDED
data/mars_geo.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mars_geo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "mars_geo"
|
8
|
+
gem.version = MarsGeo::VERSION
|
9
|
+
gem.authors = ["Jonny"]
|
10
|
+
gem.email = ["mars131@gmail.com"]
|
11
|
+
gem.description = %q{Mars geograpic lib}
|
12
|
+
gem.summary = %q{Implementation of china geograpic encrypt which offset the position for display correctly in chinese map service }
|
13
|
+
gem.homepage = "https://github.com/jonnyzheng/mars_geo"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'mars_geo'
|
2
|
+
|
3
|
+
include MarsGeo
|
4
|
+
|
5
|
+
describe MarsGeo::Converter do
|
6
|
+
|
7
|
+
it 'method yj_sin2 should correct' do
|
8
|
+
converter = Converter.new
|
9
|
+
value = converter.yj_sin2(31.5566)
|
10
|
+
value.should eq(0.14020995787539087)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'method transform_yj5 should correct' do
|
14
|
+
converter = Converter.new
|
15
|
+
value = converter.transform_yj5(31.5566,121.77887615)
|
16
|
+
value.should eq(1116.0853927686755)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'method transform_yjy5 should correct' do
|
20
|
+
converter = Converter.new
|
21
|
+
value = converter.transform_yjy5(31.5566,121.77887615)
|
22
|
+
value.should eq(3767.9839409629326)
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
it 'method transform_jy5 should correct' do
|
27
|
+
converter = Converter.new
|
28
|
+
value = converter.transform_jy5(31.5566,121.77887615)
|
29
|
+
value.should eq(0.0012826033424097633)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'method transform_jyj5 should correct' do
|
33
|
+
converter = Converter.new
|
34
|
+
value = converter.transform_jyj5(31.5566,121.77887615)
|
35
|
+
value.should eq(0.0010982842446974356)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'method wgtochina_lb should correct' do
|
39
|
+
point = Point.new(31.208949, 121.533383)
|
40
|
+
offset_point = point.offset
|
41
|
+
offset_point.lat.should eq(31.2068310546875)
|
42
|
+
offset_point.lng.should eq(121.53770209418403)
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mars_geo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonny
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Mars geograpic lib
|
15
|
+
email:
|
16
|
+
- mars131@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/mars_geo.rb
|
27
|
+
- lib/mars_geo/converter.rb
|
28
|
+
- lib/mars_geo/point.rb
|
29
|
+
- lib/mars_geo/version.rb
|
30
|
+
- mars_geo.gemspec
|
31
|
+
- spec/converter_spec.rb
|
32
|
+
homepage: https://github.com/jonnyzheng/mars_geo
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.24
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Implementation of china geograpic encrypt which offset the position for display
|
56
|
+
correctly in chinese map service
|
57
|
+
test_files:
|
58
|
+
- spec/converter_spec.rb
|
59
|
+
has_rdoc:
|