coordconver 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 +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/coordconver.gemspec +24 -0
- data/lib/coordconver.rb +88 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2ba40d344954b440a5ec94a15ba90152193b2daa
|
4
|
+
data.tar.gz: 126d11aa9acc71ba85e21e7ae598c449be8eab8e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0e3aac5ab08cef9809cdd0e5175ff307e9e3b1839dbe0ef17235ae0eafe49c8b3effc70593d5b45594b430bcb27b1a00d76d845c3900a4b554bcb8afc2cb2625
|
7
|
+
data.tar.gz: cdfbc0355a34858fa1e8940d4982be65cf7b417e6be952a023508582311d31a1c6ad2853d38017d468c04c5ff866005791e1f1e0a8e8dc38ea67abdcacf20f0e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 chenwei
|
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,72 @@
|
|
1
|
+
# 坐标转换/Coordconver
|
2
|
+
提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换。
|
3
|
+
|
4
|
+
|
5
|
+
## 坐标系说明
|
6
|
+
### 地球坐标 (WGS84)
|
7
|
+
|
8
|
+
- 国际标准,从 GPS 设备中取出的数据的坐标系
|
9
|
+
|
10
|
+
- 国际地图提供商使用的坐标系
|
11
|
+
|
12
|
+
### 火星坐标 (GCJ-02)也叫国测局坐标系
|
13
|
+
- 中国标准,从国行移动设备中定位获取的坐标数据使用这个坐标系
|
14
|
+
|
15
|
+
- 国家规定: 国内出版的各种地图系统(包括电子形式),必须至少采用GCJ-02对地理位置进行首次加密。
|
16
|
+
|
17
|
+
###百度坐标 (BD-09)
|
18
|
+
- 百度标准,百度 SDK,百度地图,Geocoding 使用
|
19
|
+
|
20
|
+
## 开发过程需要注意的事
|
21
|
+
- 从设备获取经纬度(GPS)坐标
|
22
|
+
|
23
|
+
如果使用的是百度sdk那么可以获得百度坐标(bd09)或者火星坐标(GCJ02),默认是bd09
|
24
|
+
如果使用的是ios的原生定位库,那么获得的坐标是WGS84
|
25
|
+
如果使用的是高德sdk,那么获取的坐标是GCJ02
|
26
|
+
|
27
|
+
- 互联网在线地图使用的坐标系
|
28
|
+
|
29
|
+
火星坐标系:
|
30
|
+
iOS 地图(其实是高德)
|
31
|
+
Google国内地图(.cn域名下)
|
32
|
+
搜搜、阿里云、高德地图、腾讯
|
33
|
+
百度坐标系:
|
34
|
+
当然只有百度地图
|
35
|
+
WGS84坐标系:
|
36
|
+
国际标准,谷歌国外地图、osm地图等国外的地图一般都是这个
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'coordconver'
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install coordconver
|
53
|
+
|
54
|
+
## Usage
|
55
|
+
|
56
|
+
TODO: Write usage instructions here
|
57
|
+
|
58
|
+
## Development
|
59
|
+
|
60
|
+
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.
|
61
|
+
|
62
|
+
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).
|
63
|
+
|
64
|
+
## Contributing
|
65
|
+
|
66
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cheenwe/coordconver.
|
67
|
+
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
72
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "coordconver"
|
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
|
data/bin/setup
ADDED
data/coordconver.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'coordconver'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "coordconver"
|
8
|
+
spec.version = Coordconver::VERSION
|
9
|
+
spec.authors = ["chenwei"]
|
10
|
+
spec.email = ["cheenwe@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{坐标转换/Coordconver}
|
13
|
+
spec.description = %q{提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换.}
|
14
|
+
spec.homepage = "https://github.com/cheenwe/coordconver"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
data/lib/coordconver.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
class Coordconver
|
2
|
+
VERSION = "0.1.0"
|
3
|
+
|
4
|
+
# 定义一些常量
|
5
|
+
X_PI = 3.14159265358979324 * 3000.0 / 180.0
|
6
|
+
PI = 3.1415926535897932384626
|
7
|
+
AA = 6378245.0 #a
|
8
|
+
EE = 0.00669342162296594323 #e
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
def transformlat(lng, lat)
|
13
|
+
|
14
|
+
ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt((lng).abs);
|
15
|
+
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
|
16
|
+
ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
|
17
|
+
ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
|
18
|
+
return ret
|
19
|
+
end
|
20
|
+
|
21
|
+
def transformlng(lng, lat)
|
22
|
+
ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt((lng).abs);
|
23
|
+
ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
|
24
|
+
ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
|
25
|
+
ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
|
30
|
+
# 即 百度 转 谷歌、高德
|
31
|
+
def bd09_gcj02(bd_lon, bd_lat)
|
32
|
+
x = bd_lon - 0.0065;
|
33
|
+
y = bd_lat - 0.006;
|
34
|
+
z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
|
35
|
+
theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
|
36
|
+
gg_lng = z * Math.cos(theta);
|
37
|
+
gg_lat = z * Math.sin(theta);
|
38
|
+
return [gg_lng, gg_lat]
|
39
|
+
end
|
40
|
+
|
41
|
+
# 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
|
42
|
+
# 即谷歌、高德 转 百度
|
43
|
+
def gcj02_bd09(lng, lat)
|
44
|
+
z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * X_PI);
|
45
|
+
theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * X_PI);
|
46
|
+
bd_lng = z * Math.cos(theta) + 0.0065;
|
47
|
+
bd_lat = z * Math.sin(theta) + 0.006;
|
48
|
+
return [bd_lng, bd_lat]
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
# WGS84转GCj02
|
53
|
+
def wgs84_gcj02(lng, lat)
|
54
|
+
dlat = transformlat(lng - 105.0, lat - 35.0);
|
55
|
+
dlng = transformlng(lng - 105.0, lat - 35.0);
|
56
|
+
radlat = lat / 180.0 * PI;
|
57
|
+
magic = Math.sin(radlat);
|
58
|
+
magic = 1 - EE * magic * magic;
|
59
|
+
sqrtmagic = Math.sqrt(magic);
|
60
|
+
dlat = (dlat * 180.0) / ((AA * (1 - EE)) / (magic * sqrtmagic) * PI);
|
61
|
+
dlng = (dlng * 180.0) / (AA / sqrtmagic * Math.cos(radlat) * PI);
|
62
|
+
mglat = lat + dlat;
|
63
|
+
mglng = lng + dlng;
|
64
|
+
return [mglng, mglat]
|
65
|
+
end
|
66
|
+
|
67
|
+
# GCJ02 转换为 WGS84
|
68
|
+
def gcj02_wgs84(lng, lat)
|
69
|
+
dlat = transformlat(lng - 105.0, lat - 35.0);
|
70
|
+
dlng = transformlng(lng - 105.0, lat - 35.0);
|
71
|
+
radlat = lat / 180.0 * PI;
|
72
|
+
magic = Math.sin(radlat);
|
73
|
+
magic = 1 - EE * magic * magic;
|
74
|
+
sqrtmagic = Math.sqrt(magic);
|
75
|
+
dlat = (dlat * 180.0) / ((AA * (1 - EE)) / (magic * sqrtmagic) * PI);
|
76
|
+
dlng = (dlng * 180.0) / (AA / sqrtmagic * Math.cos(radlat) * PI);
|
77
|
+
mglat = lat + dlat;
|
78
|
+
mglng = lng + dlng;
|
79
|
+
return [lng * 2 - mglng, lat * 2 - mglat]
|
80
|
+
end
|
81
|
+
|
82
|
+
# WGS84 转换 BD08
|
83
|
+
def wgs84_bd09(lng, lat)
|
84
|
+
loca = wgs84_gcj02(lng, lat)
|
85
|
+
gcj02_bd09(loca[0], loca[1])
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coordconver
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- chenwei
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-09 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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
|
+
description: 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换.
|
42
|
+
email:
|
43
|
+
- cheenwe@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- coordconver.gemspec
|
56
|
+
- lib/coordconver.rb
|
57
|
+
homepage: https://github.com/cheenwe/coordconver
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.6.1
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: 坐标转换/Coordconver
|
81
|
+
test_files: []
|
82
|
+
has_rdoc:
|