[GRASS-user] test if raster/vector exists (in Perl)

I have a Perl program that loops through the r.lake module, and captures output. In the program, there are a few instances where I would like to test if a given raster already exists. In perl, its something like this to test if a file exists … if (-e “filename”) { do stuff }.

is there a way to use this feature against a raster? Otherwise, I was going to do something like r.info and pipe the output to a file. then search that the file for some keyword indicating it exists or doesnt exist.

any other ideas or approaches?

thanks,
Mark

Try with ‘g.findfile element=cell file=<your_raster_name>’ in a Perl pipe. If the module returns a series of empty strings, your raster does not exist. I used it in Python in a way like this:

if os.popen(‘g.findfile element=cell file=%s’ % <raster_name>,’r’).readlines()[0][:-1].split(‘=’)[1]:

else:

although you use Perl I suppose it will be similar (I hope).

Cheers,

Jose A.

De: grassuser-bounces@grass.itc.it [mailto:grassuser-bounces@grass.itc.it] En nombre de M S
Enviado el: martes, 14 de agosto de 2007 18:42
Para: GRASS user list
Asunto: [POSIBLESPAM]: [GRASS-user] test if raster/vector exists (in Perl)

I have a Perl program that loops through the r.lake module, and captures output. In the program, there are a few instances where I would like to test if a given raster already exists. In perl, its something like this to test if a file exists … if (-e “filename”) { do stuff }.

is there a way to use this feature against a raster? Otherwise, I was going to do something like r.info and pipe the output to a file. then search that the file for some keyword indicating it exists or doesnt exist.

any other ideas or approaches?

thanks,
Mark

Mark wrote:

I have a Perl program that loops through the r.lake module, and
captures output. In the program, there are a few instances where I
would like to test if a given raster already exists. In perl, its
something like this to test if a file exists ... if (-e "filename") {
do stuff }.

José Antonio Ruiz Arias wrote:

Try with ‘g.findfile element=cell file=<your_raster_name>’ in a Perl
pipe. If the module returns a series of empty strings, your raster
does not exist.

You can also test g.findfile's return code, it returns 0 if the file was
found, 1 if not.

Hamish