blog/content/post/2009-08-04-00001212.md

62 lines
4.4 KiB
Markdown
Raw Normal View History

2019-03-31 11:00:21 +00:00
---
title: 2地点間の緯度と経度から直線距離を計算する
author: kazu634
date: 2009-08-04
url: /2009/08/04/_1313/
wordtwit_post_info:
- 'O:8:"stdClass":13:{s:6:"manual";b:0;s:11:"tweet_times";i:1;s:5:"delay";i:0;s:7:"enabled";i:1;s:10:"separation";s:2:"60";s:7:"version";s:3:"3.7";s:14:"tweet_template";b:0;s:6:"status";i:2;s:6:"result";a:0:{}s:13:"tweet_counter";i:2;s:13:"tweet_log_ids";a:1:{i:0;i:4745;}s:9:"hash_tags";a:0:{}s:8:"accounts";a:1:{i:0;s:7:"kazu634";}}'
categories:
- Perl
---
<div class="section">
<p>
<a href="http://wadati.blog10.fc2.com/blog-entry-345.html" onclick="__gaTracker('send', 'event', 'outbound-article', 'http://wadati.blog10.fc2.com/blog-entry-345.html', '轍 ~自転車的なる日記~ 緯度・経度から距離を求める方法');" target="_blank">轍 ~自転車的なる日記~ 緯度・経度から距離を求める方法</a>」を参考にして、2地点間の距離を計算するスクリプトを作成しました。
</p>
<h4>
Perlソース
</h4>
<pre class="syntax-highlight">
<span class="synStatement">use strict</span>;
<span class="synStatement">use warnings</span>;
<span class="synStatement">use </span>Perl6::Say;
<span class="synStatement">use </span>Math::Trig;
<span class="synComment"># 戸塚駅:</span>
<span class="synComment"># - 東経: 139.5373496</span>
<span class="synComment"># - 北緯: 35.4037596</span>
<span class="synComment"># ルノアール鎌倉駅前店</span>
<span class="synComment"># - 東経: 139.5511142</span>
<span class="synComment"># - 北緯: 35.3194949</span>
<span class="synComment"># 戸塚駅からルノアール鎌倉駅前店までの距離を求める(メートル単位)</span>
say calc_distance(<span class="synConstant">139.5373496</span>, <span class="synConstant">35.4037596</span>, <span class="synConstant">139.5511142</span>, <span class="synConstant">35.3194949</span>);
<span class="synStatement">sub</span><span class="synIdentifier"> calc_distance </span>{
<span class="synComment"># 引数の取得</span>
<span class="synStatement">my</span> ( <span class="synIdentifier">$src_lng</span>, <span class="synIdentifier">$src_lat</span>, <span class="synIdentifier">$target_lng</span>, <span class="synIdentifier">$target_lat</span> ) = <span class="synIdentifier">@_</span>;
<span class="synComment"># 角度をラジアンに変換する</span>
<span class="synIdentifier">$src_lng</span> = deg2rad(<span class="synIdentifier">$src_lng</span>);
<span class="synIdentifier">$src_lat</span> = deg2rad(<span class="synIdentifier">$src_lat</span>);
<span class="synIdentifier">$target_lng</span> = deg2rad(<span class="synIdentifier">$target_lng</span>);
<span class="synIdentifier">$target_lat</span> = deg2rad(<span class="synIdentifier">$target_lat</span>);
<span class="synComment"># 距離を計算するための準備</span>
<span class="synStatement">my</span> <span class="synIdentifier">$lat</span> = <span class="synStatement">abs</span>(<span class="synIdentifier">$src_lat</span> - <span class="synIdentifier">$target_lat</span>);
<span class="synStatement">my</span> <span class="synIdentifier">$lng</span> = <span class="synStatement">abs</span>(<span class="synIdentifier">$src_lng</span> - <span class="synIdentifier">$target_lng</span>);
<span class="synComment"># x軸方向の変位</span>
<span class="synStatement">my</span> <span class="synIdentifier">$disp_lng</span> = <span class="synConstant">6378137</span> * <span class="synIdentifier">$lng</span> * <span class="synStatement">cos</span>(<span class="synIdentifier">$src_lat</span>);
<span class="synComment"># y軸方向の変位</span>
<span class="synStatement">my</span> <span class="synIdentifier">$disp_lat</span> = <span class="synConstant">6378137</span> * <span class="synIdentifier">$lat</span>;
<span class="synComment"># 距離</span>
<span class="synStatement">return</span> <span class="synStatement">sqrt</span>((<span class="synIdentifier">$disp_lng</span> ** <span class="synConstant">2</span>) + (<span class="synIdentifier">$disp_lat</span> ** <span class="synConstant">2</span>));
}
</pre>
<h4>
実行結果
</h4>
<pre class="syntax-highlight">
~/working/tmp_perl on simoom634 <span class="synStatement">[</span><span class="synConstant">504</span><span class="synStatement">]</span> $: perl temp.pl
<span class="synConstant">9463</span>.<span class="synConstant">08275991388</span>
</pre>
</div>