<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
  </head>
  <body bidimailui-charset-is-forced="true">
    <p><font size="+1">Hello </font>Loïc</p>
    <p><br>
    </p>
    <p>Let's keep the discussion on the list, so others can help and
      benefit from responses.</p>
    <p><br>
    </p>
    <p>BTW, you might want to post to the r-sig-geo list also. If you
      come across questions specific to R and spatial functions, that's
      the best place to ask.</p>
    <p><br>
    </p>
    <p>Below,inline, some guesses as to what is happening...<br>
    </p>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 07/07/2020 0:03, Loïc Valéry wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:AM5PR0601MB2692F8F4CFB20B3D5BF8CE8CD0690@AM5PR0601MB2692.eurprd06.prod.outlook.com">
      <pre class="moz-quote-pre" wrap="">Dear Micha,

As you expected, I should have kept a few thanks in stock ! ;-) because I have a new little problem with rgrass7 !
In fact, when I run the script that you kindly corrected me (as a reminder, our exchange of previous emails is below) , everything goes fine...the first time. If I run the script a second time, I get an error message from Windows and another one from R (see below).
For the script to work properly again I have to close and open R (or use the .rs.rstartR() command from RStudio) which is not very convenient because it stops the script.

I tried to fix this error but without success. That's why I'm contacting you, hoping you will have an idea on how to "reset" rgrass7 without having to restart a new session.

I thank you in advance for your answer and remain at your disposal if you need additional information to find the solution.
Kind regards,
Loïc


ERRORS WHEN I RUN THE SCRIPT FOR A SECOND TIME :

      - FROM WINDOWS : g.region.exe : the procedure entry point GEOSMakeValid_r could not be located in the dynamic link library C:\Program Files\GRASS GIS 7.8\extrabin\gdal300.dll

      - FROM R : 
# Link to GRASS GIS software v.7.8.3
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">initGRASS(gisBase ="C:/Program Files/GRASS GIS 7.8", 
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">+           home="temp/GRASS_tmp", use_g.dirseps.exe=F, 
+           gisDbase="temp/GRASS_tmp", mapset="PERMANENT", 
+           remove_GISRC=T, override=T)
Error in if (is.na(projstr)) uprojargs <- projstr else uprojargs <- paste(unique(unlist(strsplit(projstr,  : 
  l'argument est de longueur nulle
De plus : There were 50 or more warnings (use warnings() to see the first 50)</pre>
    </blockquote>
    <p><br>
    </p>
    <p>I'm not sure, but I don't think you want to rerun<tt> initGRASS()
      </tt>a second time. In your script, you do initGRASS only once,
      then if you need to perform several analyses, do them in the same
      (running) GRASS session.</p>
    <p><br>
    </p>
    <blockquote type="cite"
cite="mid:AM5PR0601MB2692F8F4CFB20B3D5BF8CE8CD0690@AM5PR0601MB2692.eurprd06.prod.outlook.com">
      <pre class="moz-quote-pre" wrap="">
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">
# Specifying the projection reference for the GRASS working environment
p4str<-sp::proj4string(seg_poly)                                                    
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">Warning message:
In sp::proj4string(seg_poly) :
  CRS object has comment, which is lost in output
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">
execGRASS("g.proj", flags = "c", proj4 = p4str)                                    
rgrass7::use_sp()

# Converting the 'sp' object into a GRASS-readable file format (here, 'vec1.shp')
writeVECT(seg_poly,"vec1",v.in.ogr_flags=c("o", "overwrite"), 
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">+           driver="ESRI Shapefile")</pre>
    </blockquote>
    <p><br>
    </p>
    <p>The driver name is "ESRI_Shapefile" (note the underscore).
      Perhaps that is the problem here?</p>
    <p><br>
    </p>
    <blockquote type="cite"
cite="mid:AM5PR0601MB2692F8F4CFB20B3D5BF8CE8CD0690@AM5PR0601MB2692.eurprd06.prod.outlook.com">
      <pre class="moz-quote-pre" wrap="">
Error in writeVECT(seg_poly, "vec1", v.in.ogr_flags = c("o", "overwrite"),  : 
  driver %in% candDrivers is not TRUE
De plus : Warning message:
In system(syscmd, intern = intern, ignore.stderr = ignore.stderr,  :
  l'exécution de la commande 'v.in.ogr.exe -f' renvoie un statut 313
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">

# Run the 'v.generalize' function                                                  
execGRASS("v.generalize",flag=c("overwrite"),
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">+           parameters=list(input="vec1",
+                           output="GRASS_smooth_seg_poly",
+                           error="GRASS_smooth_seg_poly_error",
+                           method="distance_weighting",
+                           threshold=1))
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">

# Converting the shapefile 'GRASS_smooth_seg_poly.shp' into a R-readable object 
# (here, a SpatialPolygonDataFrame named 'smooth_seg_poly')
smooth_seg_poly<-readVECT("GRASS_smooth_seg_poly", with_prj=T, 
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">+                           driver="ESRI Shapefile")</pre>
    </blockquote>
    <p><br>
    </p>
    <p>Same here, the driver name for shapefile is wrong.</p>
    <p><br>
    </p>
    <p>I would also add that both R and GRASS have switched to the
      better Geopackage format for exchanging spatial data. You might
      want to try, the driver name is "GPKG"<br>
    </p>
    <p><br>
    </p>
    <p>See details about Geopackage here:</p>
    <p><a href="https://www.gis-blog.com/geopackage-vs-shapefile/">https://www.gis-blog.com/geopackage-vs-shapefile/</a></p>
    <p>and in French here:</p>
    <p><a
href="https://www.sigterritoires.fr/index.php/le-format-geopackage-et-qgis-3/">https://www.sigterritoires.fr/index.php/le-format-geopackage-et-qgis-3/</a></p>
    <p><br>
    </p>
    <p>Best, Micha</p>
    <p><br>
    </p>
    <p><br>
    </p>
    <blockquote type="cite"
cite="mid:AM5PR0601MB2692F8F4CFB20B3D5BF8CE8CD0690@AM5PR0601MB2692.eurprd06.prod.outlook.com">
      <pre class="moz-quote-pre" wrap="">
Error in .read_vect_non_plugin(vname = vname, layer = layer, type = type,  : 
  driver %in% candDrivers is not TRUE
De plus : Warning message:
In system(syscmd, intern = intern, ignore.stderr = ignore.stderr,  :
  l'exécution de la commande 'v.in.ogr.exe -f' renvoie un statut 313






PREVIOUS EMAILS: 

De : Micha Silver <a class="moz-txt-link-rfc2396E" href="mailto:tsvibar@gmail.com"><tsvibar@gmail.com></a>
Envoyé : mardi 9 juin 2020 23:08
À : Loïc Valéry <a class="moz-txt-link-rfc2396E" href="mailto:lvalery@outlook.fr"><lvalery@outlook.fr></a>
Objet : Re: [R] Need help using GRASS within R - problem with CRS using the 'v.generalize' command 
 

On 09/06/2020 16:22, Loïc Valéry wrote:
Dear Micha,

A thousand thanks for your help. It works now!
Whoa, you'd better save some thanks for the next question. ;-)

For your information and following your remark, I also updated rgdal.
Thanks again for taking the time to answer me.
Kind regards
Loïc

De : Micha Silver <a class="moz-txt-link-rfc2396E" href="mailto:tsvibar@gmail.com"><tsvibar@gmail.com></a>
Envoyé : mardi 9 juin 2020 08:44
À : Loïc Valéry <a class="moz-txt-link-rfc2396E" href="mailto:lvalery@outlook.fr"><lvalery@outlook.fr></a>; <a class="moz-txt-link-abbreviated" href="mailto:r-help@R-project.org">r-help@R-project.org</a> <a class="moz-txt-link-rfc2396E" href="mailto:r-help@R-project.org"><r-help@R-project.org></a>
Objet : Re: [R] Need help using GRASS within R - problem with CRS using the 'v.generalize' command 
 
Hello


On 08/06/2020 19:52, Loïc Valéry wrote:
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">Dear all,

First of all, this is my first message on the list. Therefore, please be indulgent if my message is not perfectly formatted as it should be.

I am currently encountering a difficulty with GRASS 7.8 within R when using the 'v.generalize' command to smooth the contour of polygons after a segmentation step.

I tried two different ways to "call" GRASS:

            1 - using the RQGIS3 package
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
My first suggestion: no need for getting QGIS involved here. That's an 
extra layer of complication that seems unnecessary, given your goal of 
using the GRASS module, v.generalize.


</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">            2 - using the rgrass7 package

The first method returns an error message (i.e. "proj_create_from_database: Cannot find proj.db") and therefore does not produce any result.
The second method results in a layer of smoothed polygons but the projection reference (i.e. CRS) is lost whereas the input layer has one.

Since in both cases the problem seems to be the same (i.e. GRASS fails to access the projection information), I thought it was interesting to deal with these two cases simultaneously. So, below you will find two small examples - each dealing with one of the two procedures - in order to clarify the problem.

1 - Example using the RQGIS3 package :

</pre>
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap="">setwd("D:/test")
library(sp)
library(rgdal)
</pre>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">rgdal: version: 1.4-8, (SVN revision 845)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
Path to GDAL shared files: C:/Users/toto/Documents/R/win-library/3.6/rgdal/gdal
GDAL binary built with GEOS: TRUE
Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
Path to PROJ.4 shared files: C:/Users/toto/Documents/R/win-library/3.6/rgdal/proj
Linking to sp version: 1.4-1
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">

Please note that you have older versions of GDAL (2.2.3 here) and PROJ.4 
(4.9 here). These are currently being replaced by GDAL 3.0 and PROJ 6.3. 
You might (should) want to follow the discussion the the r-sig-geo maillist:


<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/pipermail/r-sig-geo/2020-June/028165.html">https://stat.ethz.ch/pipermail/r-sig-geo/2020-June/028165.html</a>


....... (skipped all the discussion regarding RQGIS3 as I think it's not 
relevant)
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">

2 - EXAMPLE USING THE RGRASS7 PACKAGE

seg_poly = a SpatialPolygonsDataFrame with CRS (cf. below) :

</pre>
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap="">setwd("D:/test")
library(rgrass7)

# characteristics of the SpatialPolygonsDataFrame 'seg_poly' : CRS does exist
seg_poly
</pre>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">class       : SpatialPolygonsDataFrame
features    : 31
extent      : 477371.3, 477397.6, 5631995, 5632020  (xmin, xmax, ymin, ymax)
crs         : +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs
variables   : 1
names       : Seg_ID
min values  :      1
max values  :     31
Warning message:
In proj4string(x) : CRS object has comment, which is lost in output
</pre>
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap=""># initialization of GRASS 7.8 from R
initGRASS(gisBase ="C:/Program Files/GRASS GIS 7.8", home="temp/GRASS",gisDbase="temp/GRASS", use_g.dirseps.exe=F,remove_GISRC=T, override=T)
</pre>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">gisdbase    temp/GRASS
location    file19685026c56
mapset      file196829fa7141
rows        1
columns     1
north       1
south       0
west        0
east        1
nsres       1
ewres       1
projection  NA

I suspect that the problem comes from 'projection NA' when initializing GRASS (cf. just above)
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">

What you need to do here is setup the CRS of your new location. 
Typically, you would run initGRASS and point to a *previously created*  
LOCATION, with CRS already defined. In this case, since you are creating 
a new location, you must define it's coordinate system. (GRASS is very 
"picky" about that).

Here's a GIS Stackexchange post that explains:

<a class="moz-txt-link-freetext" href="https://gis.stackexchange.com/questions/183032/create-a-new-grass-database-in-r-with-crs-projection">https://gis.stackexchange.com/questions/183032/create-a-new-grass-database-in-r-with-crs-projection</a>


You can derive the full proj4 string from your sp object with:


p4str = sp::proj4string(seg_poly)


Then use that to set the project parameters for the new LOCATION, with

execGRASS("g.proj", flags = "c", proj4 = p4str) Now you should be able 
to continue with...


</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap="">execGRASS("v.generalize",flag=c("overwrite"),parameters=list(input="vec1",
</pre>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">+                                                              output="GRASS_smooth_seg_poly",
+                                                              error="GRASS_smooth_seg_poly_error",
+                                                              method="distance_weighting",
+                                                              threshold=1))



As a novice in the field, I would be very grateful for your help.
I remain at your entire disposal for any further information you may need to help me in finding a solution to this problem.

Yours sincerely,
Loïc
______________________________________________
<a class="moz-txt-link-abbreviated" href="mailto:R-help@r-project.org">R-help@r-project.org</a> mailing list -- To UNSUBSCRIBE and more, see
<a class="moz-txt-link-freetext" href="https://stat.ethz.ch/mailman/listinfo/r-help">https://stat.ethz.ch/mailman/listinfo/r-help</a>
PLEASE do read the posting guide <a class="moz-txt-link-freetext" href="http://www.R-project.org/posting-guide.html">http://www.R-project.org/posting-guide.html</a>
and provide commented, minimal, self-contained, reproducible code.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
</pre>
    </blockquote>
    <pre class="moz-signature" cols="72">-- 
Micha Silver
Ben Gurion Univ.
Sde Boker, Remote Sensing Lab
cell: +972-523-665918
<a class="moz-txt-link-freetext" href="https://orcid.org/0000-0002-1128-1325">https://orcid.org/0000-0002-1128-1325</a></pre>
  </body>
</html>