Skip to content

Commit

Permalink
moved to sf for extracting data from shapefiles, avoiding abandoned r…
Browse files Browse the repository at this point in the history
…gdal
  • Loading branch information
stineb committed May 15, 2024
1 parent be9c203 commit 4bebf0b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Remotes:
bczernecki/climate
LazyData: true
ByteCompile: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
ggplot2,
rmarkdown,
Expand Down
31 changes: 14 additions & 17 deletions R/ingest_globalfields.R
Original file line number Diff line number Diff line change
Expand Up @@ -1158,27 +1158,24 @@ extract_pointdata_allsites <- function(
extract_pointdata_allsites_shp <- function( dir, df_lonlat, layer ){

# define variables
lon <-lat <- . <- NULL

shp <- rgdal::readOGR(dsn = dir, layer = layer)

geo.proj <- sp::proj4string(shp)
lon <- lat <- . <- NULL

sf::sf_use_s2(FALSE)

# create SpatialPoints object for plots
df_clean <- df_lonlat %>%
ungroup() %>%
dplyr::select(lon, lat) %>%
df_clean <- df_lonlat |>
ungroup() |>
dplyr::select(lon, lat) |>
tidyr::drop_na()

pts <- sp::SpatialPoints(df_clean, proj4string = sp::CRS(geo.proj))

# creates object that assigns each plot index to an ecoregion
df <- sp::over(pts, shp) %>%
as_tibble() %>%
bind_cols(df_clean, .) %>%
right_join(df_lonlat, by = c("lon", "lat")) %>%
dplyr::select(-lon, -lat)

shp <- sf::st_read(dsn = dir, layer = layer)
pts <- sf::st_as_sf(df_clean, coords = c("lon","lat"), crs = sf::st_crs(shp))
df <- sf::st_intersection(pts, shp) |>
as_tibble() |>
bind_cols(df_clean, .) |>
right_join(df_lonlat, by = c("lon", "lat"))
# dplyr::select(-lon, -lat)

return(df)
}

Expand Down

0 comments on commit 4bebf0b

Please sign in to comment.