Cookie Notice

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

2011/11/07

Even More Traveling, Even Less Sales

Here's some table descriptions from MySQL, from which you should be able to reverse engineer the table creation. State Capitals
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(10)     | NO   | PRI | NULL    | auto_increment |
| state     | varchar(25) | YES  |     | NULL    |                |
| st        | varchar(2)  | YES  |     | NULL    |                |
| city      | varchar(25) | YES  |     | NULL    |                |
| latitude  | float(12,6) | YES  |     | NULL    |                |
| longitude | float(12,6) | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+
Combinations - Connecting each one to each other
+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| id         | int(10) | NO   | PRI | NULL    | auto_increment |
| state_id_1 | int(10) | YES  |     | NULL    |                |
| state_id_2 | int(10) | YES  |     | NULL    |                |
+------------+---------+------+-----+---------+----------------+
Distances
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(10)     | NO   | PRI | NULL    | auto_increment |
| distance | float(12,6) | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
I'll say again, I think I made a mistake by not including distance in the combination table. I didn't write perl code to put the state capital information into the database. I copied it from a source and recrafted it into SQL by hand.
  1. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 01 , "Delaware" , "DE" , "Dover" ) ;  
  2. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 02 , "Pennsylvania" , "PA" , "Harrisburg" ) ;  
  3. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 03 , "New Jersey, NJ" , "Trenton" ) ;  
  4. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 04 , "Georgia" , "GA" , "Atlanta" ) ;  
  5. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 05 , "Connecticut" , "CT" , "Hartford" ) ;  
  6. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 06 , "Massachusetts" , "MA" , "Boston" ) ;  
  7. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 07 , "Maryland" , "MD" , "Annapolis" ) ;  
  8. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 08 , "South Carolina" , "SC" , "Columbia" ) ;  
  9. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 09 , "New Hampshire" , "NH" , "Concord" ) ;  
  10. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 10 , "Virginia" , "VA" , "Richmond" ) ;  
  11. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 11 , "New York" , "NY" , "Albany" ) ;  
  12. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 12 , "North Carolina" , "NC" , "Raleigh" ) ;  
  13. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 13 , "Rhode Island" , "RI" , "Providence" ) ;  
  14. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 14 , "Vermont" , "VT" , "Montpelier" ) ;  
  15. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 15 , "Kentucky" , "KY" , "Frankfort" ) ;  
  16. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 16 , "Tennessee" , "TN" , "Nashville" ) ;  
  17. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 17 , "Ohio" , "OH" , "Columbus" ) ;  
  18. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 18 , "Louisiana" , "LA" , "Baton Rouge" ) ;  
  19. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 19 , "Indiana" , "IN" , "Indianapolis" ) ;  
  20. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 20 , "Mississippi" , "MS" , "Jackson" ) ;  
  21. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 21 , "Illinois" , "IL" , "Springfield" ) ;  
  22. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 22 , "Alabama" , "AL" , "Montgomery" ) ;  
  23. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 23 , "Maine" , "ME" , "Augusta" ) ;  
  24. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 24 , "Missouri" , "MO" , "Jefferson City" ) ;  
  25. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 25 , "Arkansas" , "AR" , "Little Rock" ) ;  
  26. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 26 , "Michigan" , "MI" , "Lansing" ) ;  
  27. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 27 , "Florida" , "FL" , "Tallahassee" ) ;  
  28. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 28 , "Texas" , "TX" , "Austin" ) ;  
  29. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 29 , "Iowa" , "IA" , "Des Moines" ) ;  
  30. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 30 , "Wisconsin" , "WI" , "Madison" ) ;  
  31. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 31 , "California" , "CA" , "Sacramento" ) ;  
  32. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 32 , "Minnesota" , "MN" , "Saint Paul" ) ;  
  33. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 33 , "Oregon" , "OR" , "Salem" ) ;  
  34. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 34 , "Kansas" , "KS" , "Topeka" ) ;  
  35. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 35 , "West Virginia" , "WV" , "Charleston" ) ;  
  36. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 36 , "Nevada" , "NV" , "Carson City" ) ;  
  37. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 37 , "Nebraska" , "NE" , "Lincoln" ) ;  
  38. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 38 , "Colorado" , "CO" , "Denver" ) ;  
  39. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 39 , "North Dakota" , "ND" , "Bismarck" ) ;  
  40. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 40 , "South Dakota" , "SD" , "Pierre" ) ;  
  41. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 41 , "Montana" , "MT" , "Helena" ) ;  
  42. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 42 , "Washington" , "WA" , "Olympia" ) ;  
  43. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 43 , "Idaho" , "ID" , "Boise" ) ;  
  44. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 44 , "Wyoming" , "WY" , "Cheyenne" ) ;  
  45. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 45 , "Utah" , "UT" , "Salt Lake City" ) ;  
  46. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 46 , "Oklahoma" , "OK" , "Oklahoma City" ) ;  
  47. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 47 , "New Mexico" , "NM" , "Santa Fe" ) ;  
  48. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 48 , "Arizona" , "AZ" , "Phoenix" ) ;  
  49. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 49 , "Alaska" , "AK" , "Juneau" ) ;  
  50. INSERT INTO state_capitals ( id , state , st , city ) VALUES ( 50 , "Hawaii" , "HI" , "Honolulu" ) ;  
The latitudes and longitudes were also hand-crafted.
  1. UPDATE state_capitals SET latitude="32.361538", longitude="-86.279118" where state = "Alabama" ;  
  2. UPDATE state_capitals SET latitude="58.301935", longitude="-134.419740" where state = "Alaska" ;  
  3. UPDATE state_capitals SET latitude="33.448457", longitude="-112.073844" where state = "Arizona" ;  
  4. UPDATE state_capitals SET latitude="34.736009", longitude="-92.331122" where state = "Arkansas" ;  
  5. UPDATE state_capitals SET latitude="38.555605", longitude="-121.468926" where state = "California" ;  
  6. UPDATE state_capitals SET latitude="39.7391667", longitude="-104.984167" where state = "Colorado" ;  
  7. UPDATE state_capitals SET latitude="41.767", longitude="-72.677" where state = "Connecticut" ;  
  8. UPDATE state_capitals SET latitude="39.161921", longitude="-75.526755" where state = "Delaware" ;  
  9. UPDATE state_capitals SET latitude="30.4518", longitude="-84.27277" where state = "Florida" ;  
  10. UPDATE state_capitals SET latitude="33.76", longitude="-84.39" where state = "Georgia" ;  
  11. UPDATE state_capitals SET latitude="21.30895", longitude="-157.826182" where state = "Hawaii" ;  
  12. UPDATE state_capitals SET latitude="43.613739", longitude="-116.237651" where state = "Idaho" ;  
  13. UPDATE state_capitals SET latitude="39.783250", longitude="-89.650373" where state = "Illinois" ;  
  14. UPDATE state_capitals SET latitude="39.790942", longitude="-86.147685" where state = "Indiana" ;  
  15. UPDATE state_capitals SET latitude="41.590939", longitude="-93.620866" where state = "Iowa" ;  
  16. UPDATE state_capitals SET latitude="39.04", longitude="-95.69" where state = "Kansas" ;  
  17. UPDATE state_capitals SET latitude="38.197274", longitude="-84.86311" where state = "Kentucky" ;  
  18. UPDATE state_capitals SET latitude="30.45809", longitude="-91.140229" where state = "Louisiana" ;  
  19. UPDATE state_capitals SET latitude="44.323535", longitude="-69.765261" where state = "Maine" ;  
  20. UPDATE state_capitals SET latitude="38.972945", longitude="-76.501157" where state = "Maryland" ;  
  21. UPDATE state_capitals SET latitude="42.2352", longitude="-71.0275" where state = "Massachusetts" ;  
  22. UPDATE state_capitals SET latitude="42.7335", longitude="-84.5467" where state = "Michigan" ;  
  23. UPDATE state_capitals SET latitude="44.95", longitude="-93.094" where state = "Minnesota" ;  
  24. UPDATE state_capitals SET latitude="32.320", longitude="-90.207" where state = "Mississippi" ;  
  25. UPDATE state_capitals SET latitude="38.572954", longitude="-92.189283" where state = "Missouri" ;  
  26. UPDATE state_capitals SET latitude="46.595805", longitude="-112.027031" where state = "Montana" ;  
  27. UPDATE state_capitals SET latitude="40.809868", longitude="-96.675345" where state = "Nebraska" ;  
  28. UPDATE state_capitals SET latitude="39.160949", longitude="-119.753877" where state = "Nevada" ;  
  29. UPDATE state_capitals SET latitude="43.220093", longitude="-71.549127" where state = "New Hampshire" ;  
  30. UPDATE state_capitals SET latitude="40.221741", longitude="-74.756138" where state = "New Jersey" ;  
  31. UPDATE state_capitals SET latitude="35.667231", longitude="-105.964575" where state = "New Mexico" ;  
  32. UPDATE state_capitals SET latitude="42.659829", longitude="-73.781339" where state = "New York" ;  
  33. UPDATE state_capitals SET latitude="35.771", longitude="-78.638" where state = "North Carolina" ;  
  34. UPDATE state_capitals SET latitude="48.813343", longitude="-100.779004" where state = "North Dakota" ;  
  35. UPDATE state_capitals SET latitude="39.962245", longitude="-83.000647" where state = "Ohio" ;  
  36. UPDATE state_capitals SET latitude="35.482309", longitude="-97.534994" where state = "Oklahoma" ;  
  37. UPDATE state_capitals SET latitude="44.931109", longitude="-123.029159" where state = "Oregon" ;  
  38. UPDATE state_capitals SET latitude="40.269789", longitude="-76.875613" where state = "Pennsylvania" ;  
  39. UPDATE state_capitals SET latitude="41.82355", longitude="-71.422132" where state = "Rhode Island" ;  
  40. UPDATE state_capitals SET latitude="34.000", longitude="-81.035" where state = "South Carolina" ;  
  41. UPDATE state_capitals SET latitude="44.367966", longitude="-100.336378" where state = "South Dakota" ;  
  42. UPDATE state_capitals SET latitude="36.165", longitude="-86.784" where state = "Tennessee" ;  
  43. UPDATE state_capitals SET latitude="30.266667", longitude="-97.75" where state = "Texas" ;  
  44. UPDATE state_capitals SET latitude="40.7547", longitude="-111.892622" where state = "Utah" ;  
  45. UPDATE state_capitals SET latitude="44.26639", longitude="-72.57194" where state = "Vermont" ;  
  46. UPDATE state_capitals SET latitude="37.54", longitude="-77.46" where state = "Virginia" ;  
  47. UPDATE state_capitals SET latitude="47.042418", longitude="-122.893077" where state = "Washington" ;  
  48. UPDATE state_capitals SET latitude="38.349497", longitude="-81.633294" where state = "West Virginia" ;  
  49. UPDATE state_capitals SET latitude="43.074722", longitude="-89.384444" where state = "Wisconsin" ;  
  50. UPDATE state_capitals SET latitude="41.145548", longitude="-104.802042" where state = "Wyoming" ;  
The distances themselves were generated mathematically, with the help of Google and Wikipedia to find the how-to.
  1. #!/usr/bin/perl  
  2.   
  3. use 5.010 ;  
  4. use strict ;  
  5. use warnings ;  
  6. use Data::Dumper ;  
  7. use DBI ;  
  8.   
  9. use lib '/home/jacoby/lib' ;  
  10. use MyDB 'db_connect' ;  
  11.   
  12. use subs qw{ get_combos get_states set_distance } ;  
  13.   
  14. my $pi = atan2( 1, 1 ) * 4 ;  
  15. my $states = get_states() ;  
  16. my $combos = get_combos() ;  
  17.   
  18. for my $combo ( (sort { $a<=>$b } keys %$combos ) ) {  
  19.     my $c_obj = $combos->{$combo} ;  
  20.     my ( $state_1 , $state_2 ) =  sort { $a <=> $b } $c_obj->{ state_id_1 } , $c_obj->{ state_id_2 } ;  
  21.     my $obj_s1 = $states->{ $state_1 } ;  
  22.     my $obj_s2 = $states->{ $state_2 } ;  
  23.     my $dist = haversine(  
  24.             $obj_s1->{ latitude } , $obj_s1->{ longitude } ,  
  25.             $obj_s2->{ latitude } , $obj_s2->{ longitude } ) ;  
  26.     say $combo ;  
  27.     say join ' - ' ,  
  28.     ( join ', ' , $obj_s1->{ city } , $obj_s1->{ state } ) ,  
  29.     ( join ', ' , $obj_s2->{ city } , $obj_s2->{ state } ) ;  
  30.     say join "\t" , '' , $dist . ' miles';  
  31.     set_distance( $combo , $dist ) ;  
  32.     }  
  33.   
  34. sub get_states {  
  35.     my $dbh = db_connect() ;  
  36.     my $sql = 'SELECT * from state_capitals ORDER BY id' ;  
  37.     my $states = $dbh->selectall_hashref( $sql , 'id' ) or croak $dbh->errstr;  
  38.     return $states ;  
  39.     }  
  40. sub get_combos {  
  41.     my $dbh = db_connect() ;  
  42.     my $sql = 'SELECT * from combinations ORDER BY id' ;  
  43.     my $combos = $dbh->selectall_hashref( $sql , 'id' ) or croak $dbh->errstr;  
  44.     return $combos ;  
  45.     }  
  46. sub set_distance {  
  47.     my ( $combo , $dist ) = @_ ;  
  48.     my $dbh = db_connect() ;  
  49.     my $sql = "INSERT INTO distances ( id , distance ) VALUES ( $combo , $dist ) " ;  
  50.     say $sql ;  
  51.     $dbh->do$sql ) or croak $dbh->errstr;  
  52.     }  
  53.   
  54. sub haversine {  
  55.     my ( $lat1$lon1$lat2$lon2 ) = @_ ;  
  56.   
  57.     my $theta = $lon1 - $lon2 ;  
  58.     my $dist =  
  59.         sin( deg2rad( $lat1 ) ) *  
  60.         sin( deg2rad( $lat2 ) ) +  
  61.         cos( deg2rad( $lat1 ) ) *  
  62.         cos( deg2rad( $lat2 ) ) *  
  63.         cos( deg2rad( $theta ) ) ;  
  64.   
  65.     $dist = acos( $dist ) ;  
  66.     $dist = rad2deg( $dist ) ;  
  67.     $dist = $dist * 60 * 1.1515 ;  
  68.     return sprintf '%5.2f' , $dist ;  
  69.     }  
  70.   
  71. sub acos {  
  72.     my ( $rad ) = @_ ;  
  73.     my $ret = atan2sqrt( 1 - $rad**2 ), $rad ) ;  
  74.     return $ret ;  
  75.     }  
  76.   
  77. sub deg2rad {  
  78.     my ( $deg ) = @_ ;  
  79.     return ( $deg * $pi / 180 ) ;  
  80.     }  
  81.   
  82. sub rad2deg {  
  83.     my ( $rad ) = @_ ;  
  84.     return ( $rad * 180 / $pi ) ;  
  85.     }  

No comments:

Post a Comment