Jesus Loves GRASS

Technical blog about GRASS/GIS,open source geoinformatics and MAPSERVER.

Friday, December 01, 2006

Downloading the SRTM V3.0 using a PERL script

GeoTiff is the best format to download because it runs very well with GDAL and GRASS, the tiles are in 5º by 5º. I am interrested in area between 35ºE -> 100º E; 30ºN -> 60ºN, this is a huge square from the western med. till the middle of China. The main objective is only the Aral Sea, but since I need to catch all the NIS countries the area has to be relatively big (Kazakhstan is HUGE!!!)

From the map I need all the tiles from x=44 y=6 till x=56 y=1 (left bottom till rigth top) this gives a big amount of clicks/downloads !!!! Using the approach of lets spend 99% of the time making a script that will do 100% of the job in 1% of the time, I decided to make a simple PERL scrip.

I am no PERL expert, I normally program in C/C++ so I am not familiar with PERL, but it was relatively simple because I had a book around the house called "Perl for C programmers" and the code lines for the FTP access where more or less copied from the explanation of how the Net::FTP module works.

The tile files have a structure like srtm_X_Y.zip so the script asks for the minimal and maximal number for x and y, after it makes the URL string and opens an FTP connection to the italian server. The script only has the debug dumping of the FTP connection.

Just copy/paste the next files to a file and run it:

#!/usr/bin/perl
#The script generates the URL of the files to download
#then it opens an FTP connection a gets the file
#simple, clean without much programming advances and it works
#note: the file code is something like this srtm_X_Y.zip
use strict;
use warnings;
use Net::FTP;
#variable declaration
#specific varibles, change here according to needs
my $ftp_url="ftplmu.jrc.it";
my $ftp_pwd="/pub/SRTM_v3/SRTM_Data_GeoTiff";
my $ftp_file;
my $ftp;
#input variables
my $max_x;
my $min_x;
my $max_y;
my $min_y;
#other variables
my $temp_min_y;
my $formated_number_y;
my $formated_number_x;
my $file_url;
#user input
print "Enter tile number of maximum long. (x): ";
$max_x=<STDIN>;
chomp($max_x);
print "Enter tile number of minimum long. (x): ";
$min_x=<STDIN>;
chomp($min_x);
print "Enter tile number of maximum lat. (y): ";
$max_y=<STDIN>;
chomp($max_y);
print "Enter tile number of minimum lat. (y): ";
$min_y=<STDIN>;
chomp($min_y);
#for loop to generate the url and open the FTP connection
$temp_min_y=$min_y;
for ( ;$min_x<=$max_x; $min_x++) { #some nice C/C++ sytanx $formated_number_x=sprintf("%02d",$min_x); $min_y=$temp_min_y; for( ;$min_y<=$max_y; $min_y++) { $formated_number_y=sprintf("%02d",$min_y); $ftp_file="srtm_".$formated_number_x."_".$formated_number_y.".zip"; #creating the FTP object and getting the file $ftp = Net::FTP->new($ftp_url, Debug=>1, Passive=>1) or die "It is not connecting: $@";
$ftp->login("anonymous");
$ftp->cwd($ftp_pwd);
$ftp->message;
$ftp->binary;
$ftp->get($ftp_file) or die "Get failed ", $ftp->message;
$ftp->quit;
}
}

2 Comments:

At 22/11/07 12:50 AM, Anonymous Anonymous said...

Hi!

Thanks for the script!

Is the final "}" character really necessary?

I am no expert but I got an error when trying to run the script...

Cheers,

Nikos.

 
At 26/11/07 8:11 AM, Blogger Mister Jesus said...

Yes the "}" is necessary

When copy/past the scrip from the post the "#" used for the comments get mixed,with the actual code lines causing an error. If this was your error please remove the comment lines.

If the error was something else could you post the error ???

 

Post a Comment

<< Home