St_union function taking a long time to run (R)

38 Views Asked by At

I am having trouble with a line of my code. I am trying to run st_union from the "sf" package on two sf objects, a shapefile that has been transformed into an sf object (EPSG:4326) and occurrence data that is also a sf object. I want my two sf objects to union as 1 sf object with the the number of occurrences ("summarize occurrences) of each "genus" in "occ_sf" to be grouped with the name of the county("FYLKESNAVN") found in the shapefile. I am not sure if the runtime is due to the amount of data I have in occ_sf, in which I have 900 000 occurrences. Or with the number of genera in the table.

When I ran these two with st_join it ran pretty quickly. When I ran them with st_intersection it was taking a really long time, I had to manually terminate the run (multiple times). I think st_union is the more relevant function for the output I want. I have updated my packages and deleted the .RDATA file from my laptop, and I have also updated R to 4.3.3 "Angel Food Cake" to try to run this faster. But it's still running really slowly. I have also tried running a progress bar with the "progress" package but I could not get it to run the status of the st_union function.

Any advice or new ideas? This is my code:

# Perform the spatial union of shp2 and occ_sf
library(sf)
library(dplyr)
shp2_occsf_union <-  st_union(shp2, occ_sf)
class(shp2_occsf_union)
View(shp2_occsf_union)

# Group data
grouped_union <- shp2_occsf_union %%
  group_by(FYLKESNAVN, genus)
  summarize(occurrences = n())

# View the result
View(grouped_union)

# Plot the union
library("ggplot2")
ggplot() +
  geom_sf(data = shp2) +
  geom_sf(data = grouped_union, aes(color = occurrences), size = 0.25) +
  geom_sf(data = shp) +
  theme_classic()
0

There are 0 best solutions below