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

In import_data, pred_sites expect character #6

Closed alaindanet closed 5 years ago

alaindanet commented 5 years ago

Hi,

thank you so much so for your package!

I am trying to interpolate environmental data through kriging with observation sites and prediction sites.

While the import_data() function works well without prediction sites, it fails with it (an sf object passed in the pred_sites argument).

You will see below that the error suggests that import_data() is expecting a character vector while the documentation indicates that pred_sites argument is supposed to handle sf object.

Would you have a workaround to handle that ?

library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.1.2, PROJ 4.9.3
library(openSTARS)
#> Loading required package: data.table
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> Loading required package: rgrass7
#> Loading required package: sp
#> Loading required package: XML
#> GRASS GIS interface loaded with GRASS version: (GRASS not running)
library(raster)
#> 
#> Attaching 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)
pred_sites <- sample_n(sites, 10)
# Set dem path
initGRASS(gisBase = "/usr/lib/grass72/",
          home = tempdir(),
          override = TRUE)
#> gisdbase    /tmp/RtmpHxYuWW 
#> location    file390023cdf604 
#> mapset      file390019a2d15b 
#> 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,
  pred_sites = pred_sites 
)
#> Loading DEM into GRASS as dem ...
#> Loading sites into GRASS as sites_o ...
#> Error in FUN(X[[i]], ...): a character vector argument expected
traceback()
#> No traceback available

Created on 2019-06-03 by the reprex package (v0.2.0.9000).

alaindanet commented 5 years ago

Well it works if I pass a path to a shapefile to pred_sites argument. I guess that the handling of the sf object has not been yet implemented.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.1.2, PROJ 4.9.3
library(openSTARS)
#> Loading required package: data.table
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> Loading required package: rgrass7
#> Loading required package: sp
#> Loading required package: XML
#> GRASS GIS interface loaded with GRASS version: GRASS 7.2.0 (2016)
#> and location: file38304f5d0677
library(raster)
#> 
#> Attaching 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)
pred_sites <- sample_n(sites, 10)
# Set dem path
initGRASS(gisBase = "/usr/lib/grass72/",
          home = tempdir(),
          override = TRUE)
#> gisdbase    /tmp/Rtmpcq95kL 
#> location    file560c12f6f773 
#> mapset      file560c20119975 
#> 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,
  pred_sites = pred_sites 
)
#> Loading DEM into GRASS as dem ...
#> Loading sites into GRASS as sites_o ...
#> Error in FUN(X[[i]], ...): a character vector argument expected
traceback()
#> No traceback available

import_data(dem = dem_path,
  sites = obs_sites,
  pred_sites = preds_path
)
#> Loading DEM into GRASS as dem ...
#> WARNING: Raster map <dem> already exists and will be overwritten
#> Loading sites into GRASS as sites_o ...
#> Loading preditions sites into GRASS as landuse_o, pointsources_o ...
#> No streams available, skipping.

Created on 2019-06-03 by the reprex package (v0.2.0.9000).

MiKatt commented 5 years ago

Hi, sorry for the late reply; I was travelling. I am currently updating a few function. Could you please try to use the development version of the package from GitHub using:

devtools::install_github("MiKatt/openSTARS", ref = "dev")

This should have sf as an import option for prediction sites.

alaindanet commented 5 years ago

Sorry, I was in vacation. Unfortunately, It still does not work.

library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.1.2, PROJ 4.9.3
devtools::install_github("MiKatt/openSTARS", ref = "dev")
#> Skipping install of 'openSTARS' from a github remote, the SHA1 (a90ad2c8) has not changed since last install.
#>   Use `force = TRUE` to force installation
library(openSTARS)
#> Loading required package: data.table
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> Loading required package: rgrass7
#> Loading required package: sp
#> Loading required package: XML
#> GRASS GIS interface loaded with GRASS version: GRASS 7.2.0 (2016)
#> and location: file2f591f3c8e04
library(raster)
#> 
#> Attaching 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)
pred_sites <- sample_n(sites, 10)
# Set dem path
initGRASS(gisBase = "/usr/lib/grass72/",
          home = tempdir(),
          override = TRUE)
#> gisdbase    /tmp/RtmpUuIPWE 
#> location    file227433888738 
#> mapset      file22745e42ea67 
#> 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,
  pred_sites = pred_sites
)
#> Loading DEM into GRASS as 'dem' ...
#> WARNING: Over-riding projection check
#> Loading sites into GRASS as 'sites_o' ...
#> Error in FUN(X[[i]], ...): a character vector argument expected

Created on 2019-07-04 by the reprex package (v0.2.0.9000).

MiKatt commented 5 years ago

Sorry for that. There was an error in the import_data function when using sf objects that I (hopefully) corrected now. Would you please try again?

alaindanet commented 5 years ago

It works! Thank you very much!

library(tidyverse)
library(sf)
#> Linking to GEOS 3.7.1, GDAL 2.1.2, PROJ 4.9.3
devtools::install_github("MiKatt/openSTARS", ref = "dev")
#> Downloading GitHub repo MiKatt/openSTARS@dev
#> from URL https://api.github.com/repos/MiKatt/openSTARS/zipball/dev
#> Installing openSTARS
#> '/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore  \
#>   --quiet CMD INSTALL  \
#>   '/tmp/RtmpJUpp3k/devtools73f172169bc9/MiKatt-openSTARS-9c273e2'  \
#>   --library='/home/alain/R/x86_64-pc-linux-gnu-library/3.4'  \
#>   --install-tests
#> 
library(openSTARS)
#> Loading required package: data.table
#> 
#> Attaching package: 'data.table'
#> The following objects are masked from 'package:dplyr':
#> 
#>     between, first, last
#> The following object is masked from 'package:purrr':
#> 
#>     transpose
#> Loading required package: rgrass7
#> Loading required package: sp
#> Loading required package: XML
#> GRASS GIS interface loaded with GRASS version: (GRASS not running)
library(raster)
#> 
#> Attaching 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)
pred_sites <- sample_n(sites, 10)
# Set dem path
initGRASS(gisBase = "/usr/lib/grass72/",
          home = tempdir(),
          override = TRUE)
#> gisdbase    /tmp/RtmpJUpp3k 
#> location    file73f19879c9d 
#> mapset      file73f13c8e8288 
#> 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,
  pred_sites = pred_sites 
)
#> Loading DEM into GRASS as 'dem' ...
#> WARNING: Over-riding projection check
#> Loading sites into GRASS as 'sites_o' ...
#> Loading preditions sites into GRASS as 'pred_sites_o' ...
#> No streams available, skipping.
traceback()
#> No traceback available

Created on 2019-07-05 by the reprex package (v0.2.0.9000).