[R-sig-Geo] How to test for a layer in GRASS GIS mapset using rgrass7

Christian Willmes c@willme@ @ending from uni-koeln@de
Tue Dec 4 16:42:33 CET 2018


Hello,

I am not sure if this is the correct list to ask this specific question 
about executing GRASS GIS from R Scripts.

My problem is how to determine if a layer exists in the current GRASS 
GIS mapset or not.

During my script I use r.mapcalc from GRASS GIS to do some expensive 
computation. So I want to store and reuse the resulting layer, wich is 
produced according to a variable value, in case I need this layer for 
the specific variable value again.

If I just omit the overwrite tag it does not work, becasue GRASS GIS 
stops the execution on this event.

Error in execGRASS("r.mapcalc", expression = expr) : The command:
r.mapcalc expression="rsl40 = GEBCO_2014_2D_4326 >= -40"
produced an error (1) during execution:
FEHLER: output map <rsl40> exists. To overwrite, use the --overwrite flag
Error in execGRASS("r.mapcalc", expression = expr) : The command:
r.mapcalc expression="land_NA_1_rsl40  =  if( rsl40 , 1 ,null())"
produced an error (1) during execution:
FEHLER: output map <land_NA_1_rsl40> exists. To overwrite, use the
         --overwrite flag

So, I tryed to check if a certain layer already exists in the mapset to 
test if the computation needs/can be executed or not.

Using the following:

if(execGRASS("g.findfile", element="cell", file=lyrname, mapset='"."')){
     return(FALSE)
   }else{
     return(TRUE)
   }

Here it stops, if the layer does not exist:

Fehler in execGRASS("g.findfile", element = "cell", file = lyrname, 
mapset = "\".\"") :
   The command:
g.findfile element=cell file=rsl40 mapset="."
produced an error (1) during execution:


Does anyone know a solution to this?

Thank you very much!

Best,
Christian


-- 
Dr. Christian Willmes	
AG GIS & Fernerkundung      | GIS & RS Group
Geographisches Institut     | Institute of Geography
Universität zu Köln         | University of Cologne
Tel.: +49 (0)221 470 6234
http://www.geographie.uni-koeln.de/14126.html
http://www.sfb806.de
http://crc806db.uni-koeln.de
http://publons.com/a/1316706/
http://orcid.org/0000-0002-5566-6542



More information about the R-sig-Geo mailing list