inbo / n2khab

R package with preprocessing functions and standard reference data for Flemish Natura 2000 (N2K) habitat (HAB) analyses
https://inbo.github.io/n2khab
GNU General Public License v3.0
2 stars 1 forks source link

read_watersurfaces(): make fix_geom handling a bit more efficient #185

Closed florisvdh closed 3 months ago

florisvdh commented 3 months ago

@cecileherr pointed out in #184 that applying the optional count of invalid polygons in read_habitatmap(fix_geom = TRUE) is very slow. It was copied from read_watersurfaces(), where timing was still acceptable.

It seems that I calculated the polygon validity twice. Reducing this to a single calculation already gives some speedup, even though this will not be acceptable yet in case of read_habitatmap().

> # using n2khab 0.10.1
>
> system.time(n2khab::read_watersurfaces(fix_geom = TRUE) |> invisible())
Fixed 9 invalid or corrupt geometries.
   user  system elapsed 
 21.675   1.443   7.712 
> system.time(n2khab::read_watersurfaces(fix_geom = TRUE) |> invisible())
Fixed 9 invalid or corrupt geometries.
   user  system elapsed 
  6.377   0.031   6.408 
>
> # using n2khab@876df05
>
> devtools::load_all(".")
ℹ Loading n2khab
Attaching n2khab 0.10.1.9000.
Will use sf 1.0-16.
Will use terra 1.7-78.
> system.time(read_watersurfaces(fix_geom = TRUE) |> invisible())
Fixed 9 invalid or corrupt geometries.
   user  system elapsed 
  5.454   0.034   5.488 
> system.time(read_watersurfaces(fix_geom = TRUE) |> invisible())
Fixed 9 invalid or corrupt geometries.
   user  system elapsed 
  5.147   0.010   5.157 
> system.time(read_watersurfaces(fix_geom = TRUE) |> invisible())
Fixed 9 invalid or corrupt geometries.
   user  system elapsed 
  5.143   0.051   5.193 
> system.time(read_watersurfaces() |> invisible())
   user  system elapsed 
  1.188   0.012   1.203 

Restarting R session...

> # using n2khab 0.10.1
>
> system.time(n2khab::read_watersurfaces(fix_geom = TRUE) |> invisible())
Fixed 9 invalid or corrupt geometries.
   user  system elapsed 
  8.179   0.135   8.298