0

Anyone have a hint in how to use geos inside Rcpp functions (I'm building an R package)? Actually, I'm looking for a better approach then this:

Rcpp::Environment rgeos_env = Environment::namespace_env("rgeos");
Rcpp::Function Dist = rgeos_env["gDistance"];

Contextualizing, I want to calculate distances between spatial polygons, but I do that a lot of times (Monte Carlo Tests) and I want to improve the speed of my code.

lcgodoy
  • 763
  • 5
  • 14
  • You will not gain any speed up calling an _R_ function from _Rcpp_. This is because you have to _stop_ the _C++_ execution, pass objects back to _R_, run the _R_ function, export the results back into _C++_, and then finish the computation. Overall, there is a lot more overhead for little gain. – coatless Apr 08 '18 at 23:53
  • Have you tried using the `sf` package which uses `geos` to do spatial calculations? – SymbolixAU Apr 09 '18 at 00:03
  • @SymbolixAU, I tried last year, but it was slower than what I had. Thank you for the suggestion. – lcgodoy Apr 09 '18 at 00:25
  • It is a _qualitative_ duplicate in the sense that you cannot possibly envision Monte Carlo replications around _calling an R function_ from C++ and somehow betting on the magic pony to make it faster than R. – Dirk Eddelbuettel Apr 09 '18 at 00:32

1 Answers1

1

I see two possible approaches to use geos without going back to R from C++:

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75