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

removed {rgdal} dependency for shapefile extraction. #62

Merged
merged 3 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 65 additions & 17 deletions R/ingest_globalfields.R
Original file line number Diff line number Diff line change
Expand Up @@ -1155,35 +1155,83 @@ extract_pointdata_allsites <- function(
# Extracts point data for a set of sites given by df_lonlat for a
# shapefile. df_lonlat requires columns sitename, lon, and lat.

extract_pointdata_allsites_shp <- function( dir, df_lonlat, layer ){
extract_pointdata_allsites_shp <- function(dir, df_lonlat, layer) {

# solves error, see https://stackoverflow.com/questions/75927165/error-in-wk-handle-wk-wkbwkb-s2-geography-writeroriented-oriented-loop-0
sf::sf_use_s2(FALSE)

# Load spatial data using sf
shp <- sf::st_read(dsn = dir, layer = layer)

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

# Create sf points object
pts <- sf::st_as_sf(df_clean, coords = c("lon", "lat"), crs = sf::st_crs(shp))

# Spatial join and data manipulation
df <- sf::st_join(pts, shp) |>
dplyr::select(-geometry) |>
dplyr::bind_cols(df_lonlat)

# Alternative fix:
# define variables
lon <- lat <- . <- NULL
# lon <- lat <- . <- NULL

sf::sf_use_s2(FALSE)
# sf::sf_use_s2(FALSE)

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

shp <- sf::st_read(dsn = dir, layer = layer)
pts <- sf::st_as_sf(
df_clean |>
dplyr::select(lon, lat),
coords = c("lon","lat"),
crs = sf::st_crs(shp)
)
df <- sf::st_join(pts, shp, left = TRUE) |>
as_tibble() |>
bind_cols(df_clean, .)
# df_clean <- df_lonlat |>
# ungroup() |>
# tidyr::drop_na(c(lon, lat))

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

return(df)
}

# 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)

# # create SpatialPoints object for sites
# 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 site 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)

# return(df)
# }

#' Implements a weather generator
#'
#' Implements a weather generator to simulate daily precipitation, given the monthly total and the number of days with rain for each month.
Expand Down
4 changes: 3 additions & 1 deletion vignettes/example.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,9 @@ df_wwf <- ingest(
source = "wwf",
dir = "~/data/biomes/wwf_ecoregions/official/",
settings = list(layer = "wwf_terr_ecos")
)
) |>
unnest(data) |>
select(sitename, lon, lat, BIOME_NAME)
```

The following provides the biome codes. This information is additionally added by the ingestr package in column `BIOME_NAME`:
Expand Down
Loading