Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize sfc affine transformation #32

Closed
lambdamoses opened this issue Feb 29, 2024 · 1 comment
Closed

Optimize sfc affine transformation #32

lambdamoses opened this issue Feb 29, 2024 · 1 comment

Comments

@lambdamoses
Copy link
Collaborator

The code using the binary arithmetic operators on sfc is slow when the number of geometries is large, which is bad news when there're hundreds of thousands of cells:

g$geometry <- g$geometry * mult + add

From profiling, the slow part is the mapply called in the sf package, which is essentially an R loop that is not parallelized:

https://github.com/r-spatial/sf/blob/bd1940f68dd7607b7e9098ccc32c99e273c30a60/R/arith.R#L160

Try optimizing it by using st_coordinates() to get the coordinates, directly apply matrix multiplication for the linear transformation, and then use df2sf() (now using the really fast sfheaders package behind the scene) to turn it back to sf. Before going back to sf, I wonder if the sweep() step used to apply the translation is slow. I also wonder if this optimization issue should actually be opened in the sf repo instead of here.

@lambdamoses
Copy link
Collaborator Author

Right now the speedup would not work for GEOMETRY which has a mixture of different geometry types because st_coordinates would not work. I'm waiting for official performance improvements in sf itself as in r-spatial/sf#2376

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant