]> git.street.me.uk Git - andy/viking.git/blame - tools/dem24k/dem24k.pl
[DOC] Add version and source to the man page.
[andy/viking.git] / tools / dem24k / dem24k.pl
CommitLineData
8c721f83
EB
1#!/usr/bin/perl
2
3%states = ( "AL" => "61087", "AK" => "61095", "AZ" => "61081",
4"AR" => "61091", "CA" => "61069", "CO" => "61076", "CT" => "61063",
5"DE" => "61073", "DC" => "61072", "FL" => "61093", "GA" => "61089",
6"HI" => "61094", "ID" => "61053", "IL" => "61071", "IL" => "61066",
7"IA" => "61058", "KS" => "61078", "KY" => "61077", "LA" => "61092",
8"ME" => "61048", "MD" => "61075", "MA" => "61059", "MI" => "61096",
9"MN" => "61055", "MS" => "61088", "MO" => "61080", "MT" => "61047",
10"NB" => "61060", "NV" => "61067", "NH" => "61057", "NJ" => "61065",
11"NM" => "61086", "NY" => "61061", "NC" => "61083", "ND" => "61049",
12"OH" => "61070", "OK" => "61082", "OR" => "61056", "PA" => "61062",
13"RI" => "61064", "SC" => "61090", "DS" => "61050", "TN" => "61084",
14"TX" => "61085", "UT" => "61068", "VT" => "61054", "VA" => "61079",
15"WA" => "61046", "WV" => "61074", "WI" => "61052", "WY" => "61051" );
16
17use FindBin qw($Bin);
18chdir($Bin);
19
20if ( $ARGV[2] ) { $BASEDIR = $ARGV[2]; $BASEDIR =~ s/([^A-Z0-9])/\\$1/g; }
21else { $BASEDIR = "~/.viking-maps/dem24k" }
22
23$tmpfile=`tempfile`;
24chop $tmpfile;
25
26# floor
27if ($lat<0) {$lat-=(1/8);}
28if ($lon<0) {$lon-=(1/8);}
29$lat=int($ARGV[0]*8)/8;
30$lon=int($ARGV[1]*8)/8;
31$format = sprintf("%.3f,%.3f", $lat,$lon);
32$line = `grep $format dem24k.dat`;
33chop($line);
34($bla, $state, $quad, $county) = split("\\|", $line);
35
36print "$format $quad $state $county\n";
37
38if (!$quad) { die "couldn't find correct quad" }
39if ( -f "$state/$county/$quad.dem" ) {
40 die "quad already exists";
41}
42
43$ilat = int($lat);
44$ilon = int($lon);
45
46system("mkdir -p $BASEDIR/$ilat/$ilon/");
47system("touch $BASEDIR/$ilat/$ilon/$format.dem");
48
49use WWW::Mechanize;
50
51$agent = WWW::Mechanize->new();
52
53$agent->agent_alias( 'Windows IE 6' );
54
55$agent->get("https://secure.geocomm.com/accounts/login.php?type=");
56$agent->field("username", "onelongpause");
57$agent->field("password", "andyoubegin");
58$agent->click();
59$agent->follow("continue");
60$agent->get("http://data.geocomm.com/catalog/US/".$states{$state}."/sublist.html");
61$agent->follow($county);
62$agent->follow("Digital Elevation Models");
63open(COUNTY, ">$tmpfile");
64print COUNTY $agent->content();
65close(COUNTY);
66
67# figure out form number
68$tmp = "egrep \"<b>$quad, $state \\([0-9a-z]+\\)<\\/b>\" $tmpfile -n|cut -d: -f1";
69$n = `$tmp`; #line num of <b>quad, ST</b>
70chop($n);
71$n = `head -n $n $tmpfile|grep "<form"|wc -l`;
72chop($n);
73$form = $n + 1;
74print $form;
75$agent->form($form);
76$agent->click();
77
78open(COUNTY, ">$tmpfile");
79print COUNTY $agent->content();
80close(COUNTY);
81
82open(COUNTY,"$tmpfile");
83while (<COUNTY>) {
84 if (/(http:\/\/download.geocomm.com\/images\/.*?\/geocomm-w-80.gif)/) {
85 system("wget --user-agent=\"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)\" $1 -O - > /dev/null ")
86 }
87 if (/(http:\/\/dl1.geocomm.com\/download\/.*?.DEM.SDTS.TAR.GZ)/) {
88 $tarball = $1;
89 }
90}
91
92system("rm $tmpfile");
93$tmpfile = $tmpfile . ".tgz";
94system("wget --user-agent=\"Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.3) Gecko/20061201 Firefox/2.0.0.3 (Ubuntu-feisty)\" $tarball -O $tmpfile");
95
96system("./repackage.sh $tmpfile $format");
97system("mkdir -p $BASEDIR/$ilat/$ilon");
98
99#TODO: make safe for all BASEDIR!!! escape!!!
100system("rm $BASEDIR/$ilat/$ilon/$format.dem");
101system("mv $format.dem $BASEDIR/$ilat/$ilon");
102
103system("rm $tmpfile");