MiKatt / openSTARS

open source implementation of the STARS ArcGIS toolbox
https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0239237
Other
46 stars 13 forks source link

Error in readVect, need to explicity call rgrass7::use_sp() #10

Closed alaindanet closed 4 years ago

alaindanet commented 5 years ago

Hi, readVECT produces an error when no explicit choice of methods is provided. From the documentation of rgrass7, it indicates that something goes wrong with the set up of .GRASS_CACHE.

I found a solution by explicity call the use of sp with use_sp.

I am not sure that it is an issue related to openSTARS but it seems to me worth to signal it.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.4.0, PROJ 5.2.0
library(openSTARS)
#> Le chargement a nécessité le package : data.table
#> 
#> Attachement du package : 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> Le chargement a nécessité le package : rgrass7
#> Le chargement a nécessité le package : XML
#> GRASS GIS interface loaded with GRASS version: GRASS 7.6.0 (2019)
#> and location: file532f5179315a
library(raster)
#> Le chargement a nécessité le package : sp
#> 
#> Attachement du package : 'raster'
#> The following object is masked from 'package:data.table':
#> 
#>     shift
#> The following object is masked from 'package:dplyr':
#> 
#>     select
#> The following object is masked from 'package:tidyr':
#> 
#>     extract
Sys.setenv(LANG = "en") 

dem_path <- system.file("extdata", "nc", "elev_ned_30m.tif", package = "openSTARS")
sites_path <- system.file("extdata", "nc", "sites_nc.shp", package = "openSTARS")
preds_path <- c(system.file("extdata", "nc", "landuse.shp", package =
    "openSTARS"), system.file("extdata", "nc", "pointsources.shp", package =
  "openSTARS"))
sites <- read_sf(sites_path)
obs_sites <- sample_n(sites, 10)
# Set dem path
initGRASS(gisBase = "/usr/lib/grass76/",
          home = tempdir(),
          override = TRUE)
#> gisdbase    /tmp/RtmpLRk95G 
#> location    file54d3a25622d 
#> mapset      file54d3af01b58 
#> rows        1 
#> columns     1 
#> north       1 
#> south       0 
#> west        0 
#> east        1 
#> nsres       1 
#> ewres       1 
#> projection  NA
setup_grass_environment(dem = dem_path)
#> Setting up GRASS Environment ...
import_data(dem = dem_path,
  sites = obs_sites,
)
#> Loading DEM into GRASS as dem ...
#> Loading sites into GRASS as sites_o ...
#> No streams available, skipping.

derive_streams()
#> Conditioning DEM ...
#> Load elevation map
#>    0%   3%   6%   9%  12%  15%  18%  21%  24%  27%  30%  33%  36%  39%  42%  45%  48%  51%  54%  57%  60%  63%  66%  69%  72%  75%  78%  81%  84%  87%  90%  93%  96%  99% 100%
#> Remove one cell extremas
#>    0%   3%   6%   9%  12%  15%  18%  21%  24%  27%  30%  33%  36%  39%  42%  45%  48%  51%  54%  57%  60%  63%  66%  69%  72%  75%  78%  81%  84%  87%  90%  93%  96%  99% 100%
#> Set edge points
#>    0%   3%   6%   9%  12%  15%  18%  21%  24%  27%  30%  33%  36%  39%  42%  45%  48%  51%  54%  57%  60%  63%  66%  69%  72%  75%  78%  81%  84%  87%  90%  93%  96%  99% 100%
#> A* Search...
#>    0%   2%   4%   6%   8%  10%  12%  14%  16%  18%  20%  22%  24%  26%  28%  30%  32%  34%  36%  38%  40%  42%  44%  46%  48%  50%  52%  54%  56%  58%  60%  62%  64%  66%  68%  70%  72%  74%  76%  78%  80%  82%  84%  86%  88%  90%  92%  94%  96%  98% 100%
#> Processing 3858 sinks
#>    1%   3%   5%   7%   9%  11%  13%  15%  17%  19%  21%  23%  25%  27%  29%  31%  33%  35%  37%  39%  41%  43%  45%  47%  49%  51%  53%  55%  57%  59%  61%  63%  65%  67%  69%  71%  73%  75%  77%  79%  81%  83%  85%  87%  89%  91%  93%  95%  97%  99% 100%
#> Write conditioned DEM
#>    0%   3%   6%   9%  12%  15%  18%  21%  24%  27%  30%  33%  36%  39%  42%  45%  48%  51%  54%  57%  60%  63%  66%  69%  72%  75%  78%  81%  84%  87%  90%  93%  96%  99% 100%
#> Deriving streams from DEM ...
#> Calculating stream topology ...
cj <- openSTARS::check_compl_junctions()
cj
#> [1] FALSE
if(cj){
  openSTARS::correct_compl_junctions()
}
calc_edges()
#> Calculating reach contributing area (RCA) ...
#> Calculating upstream catchment areas ...
calc_sites()
#> Preparing sites 'sites' ...
#> Snapping sites to streams ...
#> Error in readVECT(sites_map, type = "point", ignore.stderr = TRUE): either sp or sf must be chosen
traceback()
#> Pas d'historique des fonctions appelées ('traceback') disponible

rgrass7::use_sp()
calc_edges()
#> ATTENTION: La couche vectorielle <edges> existe déjà et va être écrasé
#> Calculating reach contributing area (RCA) ...
#> Calculating upstream catchment areas ...
calc_sites()
#> ATTENTION: La couche vectorielle <sites> existe déjà et va être écrasé
#> Preparing sites 'sites' ...
#> Snapping sites to streams ...
#> Setting pid and locID ...
#> ATTENTION: La couche vectorielle <sites> existe déjà et va être écrasé
#> Assigning netID and rid ...
#> Calculating upDist ...

Created on 2019-08-22 by the reprex package (v0.3.0)

MiKatt commented 5 years ago

Thanks for the notice. I updated the code of setup_grass_environment to include use_sp(). Please let me know if the problem still prevails.

alaindanet commented 5 years ago

You are welcome. Unfortunately, I think that I will not switch to the development version of the package in the coming days.