Created
February 24, 2019 04:11
-
-
Save komasaru/b0e55f3179df6a64f0a65c1fcfe924c8 to your computer and use it in GitHub Desktop.
Ruby script to convert WGS84(BLH) to ECEF coordinate.
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
#! /usr/local/bin/ruby | |
#--------------------------------------------------------------------------------- | |
# BLH -> ECEF 変換 | |
# : WGS84 の緯度(Beta)/経度(Lambda)/楕円体高(Height)を | |
# ECEF(Earth Centered Earth Fixed; 地球中心・地球固定直交座標系)座標に | |
# 変換する。 | |
# | |
# Date Author Version | |
# 2019.02.19 mk-mode.com 1.00 新規作成 | |
# | |
# Copyright(C) 2019 mk-mode.com All Rights Reserved. | |
#--------------------------------------------------------------------------------- | |
# | |
class ConvBlh2Ecef | |
USAGE = "[USAGE] ./blh2ecef.rb LATITUDE(BETA) LONGITUDE(LAMBDA) HEIGHT" | |
PI_180 = Math::PI / 180.0 | |
# WGS84 座標パラメータ | |
A = 6378137.0 # a(地球楕円体長半径(赤道面平均半径)) | |
ONE_F = 298.257223563 # 1 / f(地球楕円体扁平率=(a - b) / a) | |
B = A * (1.0 - 1.0 / ONE_F) # b(地球楕円体短半径) | |
E2 = (1.0 / ONE_F) * (2 - (1.0 / ONE_F)) | |
# e^2 = 2 * f - f * f | |
# = (a^2 - b^2) / a^2 | |
ED2 = E2 * A * A / (B * B) # e'^2= (a^2 - b^2) / b^2 | |
# Initialization | |
# * コマンドライン引数の取得 | |
def initialize | |
if ARGV.size < 3 | |
puts USAGE | |
exit | |
end | |
@blh = ARGV[0, 3].map(&:to_f) | |
end | |
# Execution | |
def exec | |
puts "BLH: LATITUDE(BETA) = %12.8f°" % @blh[0] | |
puts " LONGITUDE(LAMBDA) = %12.8f°" % @blh[1] | |
puts " HEIGHT = %7.3fm" % @blh[2] | |
ecef = blh2ecef(@blh) | |
puts "--->" | |
puts "ECEF: X = %12.3fm" % ecef[0] | |
puts " Y = %12.3fm" % ecef[1] | |
puts " Z = %12.3fm" % ecef[2] | |
rescue => e | |
msg = "[#{e.class}] #{e.message}\n" | |
e.backtrace.each { |tr| msg << "\t#{tr}\n" } | |
$stderr.puts msg | |
exit 1 | |
end | |
private | |
# BLH -> ECEF 変換 | |
# | |
# @param blh: BHL 座標 [latitude, longitude, height] | |
# @return ecef: ECEF 座標 [x, y, z] | |
def blh2ecef(blh) | |
lat, lon, ht = blh | |
n = lambda do |x| | |
s = Math.sin(x * PI_180) | |
A / Math.sqrt(1.0 - E2 * s * s) | |
end | |
x = (n.call(lat) + ht) \ | |
* Math.cos(lat * PI_180) \ | |
* Math.cos(lon * PI_180) | |
y = (n.call(lat) + ht) \ | |
* Math.cos(lat * PI_180) \ | |
* Math.sin(lon * PI_180) | |
z = (n.call(lat) * (1.0 - E2) + ht) \ | |
* Math.sin(lat * PI_180) | |
return [x, y, z] | |
rescue => e | |
raise | |
end | |
end | |
ConvBlh2Ecef.new.exec if __FILE__ == $0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment