Skip to content

Commit

Permalink
Merge pull request #62 from stineb/master
Browse files Browse the repository at this point in the history
removed {rgdal} dependency for shapefile extraction.
  • Loading branch information
stineb authored Jun 17, 2024
2 parents 41f5a6a + 6a45eee commit 1709852
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
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

0 comments on commit 1709852

Please sign in to comment.