Created
November 28, 2014 07:43
-
-
Save Youngv/f91dc1a12a120f476f53 to your computer and use it in GitHub Desktop.
图吧坐标转WGS84坐标
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def transform_coordinate | |
begin | |
wgLon,wgLat = mapBar2WGS84(self.lon,self.lat) | |
gdlat,gdlon = transform(wgLat, wgLon) | |
self.lat,self.lon = gdlat.round(6),gdlon.round(6) | |
self.coordinate_updated = 1 | |
self.save | |
rescue => e | |
Rails.logger.error e | |
Rails.logger.error "id:#{self._id},coordinate:#{self.coordinate}" | |
end | |
end | |
def mapBar2WGS84(lon, lat) | |
# 图吧坐标转WGS84坐标 | |
# 参数: | |
# lon - 经度 | |
# lat - 纬度 | |
# 返回值: | |
# 经度, 纬度 | |
lon = lon * 100000 % 36000000.0 | |
lat = lat * 100000 % 36000000.0 | |
lon1 = -(((Math.cos(lat / 100000)) * (lon / 18000)) + ((Math.sin(lon / 100000)) * (lat / 9000))) + lon | |
lat1 = -(((Math.sin(lat / 100000)) * (lon / 18000)) + ((Math.cos(lon / 100000)) * (lat / 9000))) + lat | |
lon2 = -(((Math.cos(lat1 / 100000)) * (lon1 / 18000)) + ((Math.sin(lon1 / 100000)) * (lat1 / 9000))) + lon + ((lon > 0) ? 1 : -1) | |
lat2 = -(((Math.sin(lat1 / 100000)) * (lon1 / 18000)) + ((Math.cos(lon1 / 100000)) * (lat1 / 9000))) + lat + ((lat > 0) ? 1 : -1) | |
return lon2 / 100000.0, lat2 / 100000.0 | |
end | |
# World Geodetic System ==> Mars Geodetic System | |
def transform(wgLat, wgLon) | |
### | |
# transform(latitude,longitude) , WGS84 | |
# return (latitude,longitude) , GCJ02 | |
### | |
a = 6378245.0 | |
ee = 0.00669342162296594323 | |
if outOfChina(wgLat, wgLon) | |
mgLat = wgLat | |
mgLon = wgLon | |
return | |
end | |
dLat = transformLat(wgLon - 105.0, wgLat - 35.0) | |
dLon = transformLon(wgLon - 105.0, wgLat - 35.0) | |
radLat = wgLat / 180.0 * Math::PI | |
magic = Math.sin(radLat) | |
magic = 1 - ee * magic * magic | |
sqrtMagic = Math.sqrt(magic) | |
dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * Math::PI) | |
dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * Math::PI) | |
mgLat = wgLat + dLat | |
mgLon = wgLon + dLon | |
return mgLat,mgLon | |
end | |
def outOfChina(lat, lon) | |
if (lon < 72.004 or lon > 137.8347) | |
return true | |
end | |
if (lat < 0.8293 or lat > 55.8271) | |
return true | |
end | |
return false | |
end | |
def transformLat(x, y) | |
ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(x.abs) | |
ret += (20.0 * Math.sin(6.0 * x * Math::PI) + 20.0 * Math.sin(2.0 * x * Math::PI)) * 2.0 / 3.0 | |
ret += (20.0 * Math.sin(y * Math::PI) + 40.0 * Math.sin(y / 3.0 * Math::PI)) * 2.0 / 3.0 | |
ret += (160.0 * Math.sin(y / 12.0 * Math::PI) + 320 * Math.sin(y * Math::PI / 30.0)) * 2.0 / 3.0 | |
return ret | |
end | |
def transformLon(x, y) | |
ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(x.abs) | |
ret += (20.0 * Math.sin(6.0 * x * Math::PI) + 20.0 * Math.sin(2.0 * x * Math::PI)) * 2.0 / 3.0 | |
ret += (20.0 * Math.sin(x * Math::PI) + 40.0 * Math.sin(x / 3.0 * Math::PI)) * 2.0 / 3.0 | |
ret += (150.0 * Math.sin(x / 12.0 * Math::PI) + 300.0 * Math.sin(x / 30.0 * Math::PI)) * 2.0 / 3.0 | |
return ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment