Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2011/02/18

Capital Idea 2 - So Far Away

Rather than query Google Maps to get driving distances between capitals, I found latitude and longitude and use these functions to determine the distance, then ran it against all thousand-some possible distances, so it is ultimately a hash check rather than a calculation to find a distance.

  1. sub haversine {  
  2.     my ( $lat1$lon1$lat2$lon2 ) = @_ ;  
  3.   
  4.     my $theta = $lon1 - $lon2 ;  
  5.     my $dist =  
  6.         sin( deg2rad( $lat1 ) ) *  
  7.         sin( deg2rad( $lat2 ) ) +  
  8.         cos( deg2rad( $lat1 ) ) *  
  9.         cos( deg2rad( $lat2 ) ) *  
  10.         cos( deg2rad( $theta ) ) ;  
  11.   
  12.     $dist = acos( $dist ) ;  
  13.     $dist = rad2deg( $dist ) ;  
  14.     $dist = $dist * 60 * 1.1515 ;  
  15.     return $dist ;  
  16.     }  
  17.   
  18. sub acos {  
  19.     my ( $rad ) = @_ ;  
  20.     my $ret = atan2sqrt( 1 - $rad**2 ), $rad ) ;  
  21.     return $ret ;  
  22.     }  
  23.   
  24. sub deg2rad {  
  25.     my ( $deg ) = @_ ;  
  26.     return ( $deg * $pi / 180 ) ;  
  27.     }  
  28.   
  29. sub rad2deg {  
  30.     my ( $rad ) = @_ ;  
  31.     return ( $rad * 180 / $pi ) ;  
  32.     }  

2 comments:

  1. Why not use Geo::Distance? And if speed is an issue, there's an XS version as well.

    ReplyDelete
  2. Entirely because I had not heard of it before. Searching "Perl distance latitude longitude" did not bring it up to the top. I figured that, within reason, the basic algorithm would be simple enough for even a barely-got-through-the-math programmer like me.

    Since there are a known and finite number of distances to be determined, it's easier to compute them once and store them than to recompute them on the fly. I have two DB tables, one holding the length of each line and one holding the two end points of each line, which I import as a hash, so I have all the data I need going in. I just have to find a better than the run-until-the-sun-goes-cold algorithm I'm using now.

    ReplyDelete