How to test for a layer in GRASS GIS mapset using rgrass7

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to test for a layer in GRASS GIS mapset using rgrass7

Christian Willmes
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

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: How to test for a layer in GRASS GIS mapset using rgrass7

Rich Shepard
On Tue, 4 Dec 2018, Christian Willmes wrote:

> 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.

Christian,

   Yes, grass wants the --o flag to overwrite an existing file. You can learn
what raster maps exist using 'g.list type=rast | grep <mapname>'.

HTH,

Rich

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: How to test for a layer in GRASS GIS mapset using rgrass7

Roger Bivand
Administrator
In reply to this post by Christian Willmes
On Tue, 4 Dec 2018, Christian Willmes wrote:

> 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)
>   }
You need to wrap the test in try() if it may not succeed (in nc):

oo <- try(execGRASS("g.findfile", element="cell", file="slope",
       mapset="'.'", intern=TRUE), silent=TRUE)
ifelse(class(oo) == "try-error", FALSE, TRUE)

and use intern to capture the output if the file exists. However, you can,
as Rich says, simply overwrite existing files if you wish.

>
> Here it stops, if the layer does not exist:
>

Note that your R script does not stop, simply g.findfile has returned 1
rather than 0 on exit.

Hope this clarifies,

Roger


> 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
>
>
>
--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: [hidden email]
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: How to test for a layer in GRASS GIS mapset using rgrass7

Micha Silver
In reply to this post by Christian Willmes

On 12/4/18 5:42 PM, Christian Willmes wrote:
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:


I think that the mapset parameter requires the actual mapset name, *not* the current working directory.




Does anyone know a solution to this?

Thank you very much!

Best,
Christian


-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: How to test for a layer in GRASS GIS mapset using rgrass7

Christian Willmes
In reply to this post by Roger Bivand

Am 04.12.18 um 20:55 schrieb Roger Bivand:
> You need to wrap the test in try() if it may not succeed (in nc):
>
> oo <- try(execGRASS("g.findfile", element="cell", file="slope",
>       mapset="'.'", intern=TRUE), silent=TRUE)
> ifelse(class(oo) == "try-error", FALSE, TRUE)
>
> and use intern to capture the output if the file exists.

Thank you very much, this was exactly what solved the issue.

Best regards,
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

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo