Wednesday, July 24, 2013

Calculating distances in meatspace

I'm working on an automated provisioning system for a very large VPN network. For each new VPN client, I need to select a headend site where VPN tunnels should land. The only data available is that which I can get from the sales and billing systems. This system offers me the zip code of the install site.

Using the zip code of the install site, and the known zip codes of my various head-end sites, I'm able to select the destination for the primary and secondary VPN tunnels.

It's not perfect (physical location often has little to do with network path), but it's better than nothing. I haven't decided how to handle non-US sites yet.

I'm using a database of US zip codes found here, and a very dirty perl script. The script grabs the latitude and longitude of two zip codes from the database, and prints the mileage between them as calculated using the Haversine formula for great circle distance.

It runs like this:


Christophers-MacBook-Pro:scripts chris$ zipdistance.pl 95134 60614
1837 miles
Christophers-MacBook-Pro:scripts chris$

The script:


#!/opt/local/bin/perl
use GIS::Distance;

my $dbfile="/Users/chris/Downloads/zipcode.csv";
my $lat1,$lon1,$lat2,$lon2;

sub usage{
  printf "Usage: $0 <zipcode> <zipcode>\n";
  exit;
}

if (@ARGV != 2) {usage;};
unless ($ARGV[0] =~ /[0-9]{5}/) {usage;}
unless ($ARGV[1] =~ /[0-9]{5}/) {usage;}

my @sorted = sort @ARGV;

open(DB, '<', $dbfile);

FIRST: while (<DB>) {
  if ($_ =~ /^.$sorted[0]/) {
    (my $trash,my $trash,my $trash,$lat1,$lon1)=split(",",$_);
    my $i;
    ($i) = $lat1 =~ /"([^"]*)"/; $lat1 = $i;
    ($i) = $lon1 =~ /"([^"]*)"/; $lon1 = $i;
    last FIRST;
  }
}

SECOND: while (<DB>) {
  if ($_ =~ /^.$sorted[1]/) {
    (my $trash,my $trash,my $trash,$lat2,$lon2)=split(",",$_);
    my $i;
    ($i) = $lat2 =~ /"([^"]*)"/; $lat2 = $i;
    ($i) = $lon2 =~ /"([^"]*)"/; $lon2 = $i;
    last SECOND;
  }
}

if ("$lat1" == "" || "$lat2" == "") {
  printf "Unknown distance\n";
  exit 1;
}

my $gis = GIS::Distance->new();
my $distance = $gis->distance( $lat1,$lon1 => $lat2,$lon2 );
printf ("%d miles\n",$distance->miles());




3 comments:

  1. Calculating distances in meatspace, very dirty perl scripts? So Chris, this is what you've been doing with your life? You may need more than just fragmentation. As an end user of CISCO, I only hope you don't claim it. And yes, it only makes sense to have a $20K router in a $5K HUD structure. Didn't you watch Coming to America 1988?? 706-593-9921 text me.

    ReplyDelete
  2. 01001101011000010111100101100010011001010010000001101001011001100010000001001001001000000111011101110010011010010111010001100101001000000111100101101111011101010010000001101001011011100010000001100010011010010110111001100001011100100111100100100000011000110110111101100100011001010010000001111001011011110111010100100000011101110110100101101100011011000010000001110100011001010111100001110100001000000110110101100101001011100010000000100000010010010010000001100110011010010110011101110101011100100110010101100100001000000111100101101111011101010010000001110111011011110111010101101100011001000010000001101000011000010111011001100101001000000111010001110010011000010110001101100101011001000010000001101101011110010010000001001001010100000010000001100001011001000110010001110010011001010111001101110011001000000110001001111001001000000110111001101111011101110010111000100000001000000101001101101111001000000110010001101001011001000010000001111001011011110111010100100000011001010111011001100101011100100010000001100111011001010111010000100000011101000110100001100101001000000111011101101001011100000110010101110010011100110010000001100110011010010111100001100101011001000010000001101111011011100010000001110100011010000110010100100000010101100110111101101100011101100110111100100000011011110111001000100000011000010111001001100101001000000111100101101111011101010010000001110010011010010110010001101001011011100110011100100000011000010111001001101111011101010110111001100100001000000110111101101110001000000110000100100000011011000110000101110111011011100010000001101101011011110111011101100101011100100010000001100101011011100110011101101001011011100110010100100000011110010110111101110101001000000111001001100101011000100111010101101001011011000111010000100000011101110110100101110100011010000010000000110101001100000011000000100000011011000110001001110011001000000110111101100110001000000111010001101111011100100111000101110101011001010011111100100000001000000100100001101111011100000110010100100000011110010110111101110101001000000110000101110010011001010010000001110111011001010110000101110010011010010110111001100111001000000111100101101111011101010111001000100000011010000110010101101100011011010110010101110100001000000111010001101000011001010111001101100101001000000110010001100001011110010111001100101110

    ReplyDelete
  3. Excellent technical info. This is fresh and new to me. Keep sharing more updates on VPN networks. Traffic Ticket Lawyer Fairfax VA

    ReplyDelete