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

Update read_watersurfaces for version 2024 #191

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: n2khab
Title: Providing Preprocessed Reference Data for Flemish Natura 2000 Habitat Analyses
Version: 0.11.0
Version: 0.11.0.9000
Authors@R: c(
person("Floris", "Vanderhaeghe", email = "floris.vanderhaeghe@inbo.be", role = c("aut", "cre"), comment = c(ORCID = "0000-0002-6378-6229")),
person("Toon", "Westra", email = "toon.westra@inbo.be", role = c("aut"), comment = c(ORCID = "0000-0003-2478-9459")),
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ importFrom(dplyr,mutate)
importFrom(dplyr,mutate_at)
importFrom(dplyr,mutate_if)
importFrom(dplyr,n)
importFrom(dplyr,na_if)
importFrom(dplyr,pull)
importFrom(dplyr,recode)
importFrom(dplyr,relocate)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# n2khab (development version)

# n2khab 0.11.0 (2024-10-03)

## Support for new data source versions
Expand Down
139 changes: 96 additions & 43 deletions R/read_habitatdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ read_watersurfaces_hab <-

#' Return the data source \code{watersurfaces} as an \code{sf} polygon layer
#'
#' Returns the raw data source \code{watersurfaces} (Scheers et al., 2022)
#' Returns the raw data source \code{watersurfaces} (Leyssen et al., 2024)
#' as a standardized \code{sf} polygon layer
#' (tidyverse-styled, internationalized) in the Belgian Lambert 72 CRS
#' (EPSG-code \href{https://epsg.io/31370}{31370}).
Expand All @@ -462,7 +462,7 @@ read_watersurfaces_hab <-
#' is the version corresponding to the \code{file} (note that the \code{version}
#' defaults to the latest version).
#'
#' See Scheers et al. (2022) for an elaborate explanation of the data source
#' See Leyssen et al. (2024) for an elaborate explanation of the data source
#' and its attributes.
#'
#' @param file Optional string. An absolute or relative file path of
Expand Down Expand Up @@ -499,19 +499,23 @@ read_watersurfaces_hab <-
#' \item \code{polygon_id}: code of the polygon;
#' \item \code{wfd_code}: optional; Flemish code of the water body with
#' regard to the Water Framework Directive (WFD);
#' \item \code{hyla_code}: optional; code of the watersurface according to the
#' Flemish working group 'Hyla', a working group on amphibians & reptiles;
#' \item \code{hyla_code}: optional (only v1.2 and earlier); code of the
#' watersurface according to the Flemish working group 'Hyla', a working group
#' on amphibians & reptiles;
#' \item \code{name}: name of the watersurface;
#' \item \code{area_name}: name of the area;
#' \item \code{wfd_type}: type code according to the Flemish WFD typology
#' (Denys, 2009);
#' \item \code{wfd_type_alternative}: alternative type code according to the
#' Flemish WFD typology, in case there is a gradient between different types
#' (only version 2024);
#' \item \code{wfd_type_certain}: Logical.
#' Is there high confidence about the \code{wfd_type} determination?
#' \item \code{depth_class}: class of water depth;
#' \item \code{connectivity}: connectivity class;
#' \item \code{usage}: usage class;
#' \item \code{water_level_management}: water-level management class (not in
#' older versions).
#' \item \code{water_level_management}: water-level management class (only
#' since v1.2).
#' }
#'
#' @family functions involved in processing the 'watersurfaces' data source
Expand All @@ -524,10 +528,10 @@ read_watersurfaces_hab <-
#' wateren in Vlaanderen.
#' Rapporten van het Instituut voor Natuur- en Bosonderzoek INBO.R.2009.34.
#' Instituut voor Natuur- en Bosonderzoek, Brussel.
#' \item Scheers K., Smeekens V., Wils C., Packet J., Leyssen A. & Denys L.
#' (2022). Watervlakken versie 1.2: Polygonenkaart van stilstaand water in
#' Vlaanderen. Uitgave 2022. Instituut voor Natuur- en Bosonderzoek.
#' \doi{10.21436/inbor.87014272}.
#' \item Leyssen A., Scheers K., Packet J., Van Hecke F., Wils C. (2024).
#' Watervlakken 2024: Polygonenkaart van stilstaand water in
#' Vlaanderen. Uitgave 2024. Instituut voor Natuur- en Bosonderzoek.
#' \doi{10.21436/inbor.114075267}.
#' }
#'
#' @examples
Expand Down Expand Up @@ -566,8 +570,7 @@ read_watersurfaces_hab <-
#' across
#' arrange
#' mutate
#' mutate_at
#' mutate_if
#' na_if
#' rename
#' select
#' left_join
Expand All @@ -582,7 +585,8 @@ read_watersurfaces <-
function(file = NULL,
extended = FALSE,
fix_geom = FALSE,
version = c("watersurfaces_v1.2",
version = c("watersurfaces_v2024",
"watersurfaces_v1.2",
"watersurfaces_v1.1",
"watersurfaces_v1.0")) {
version <- match.arg(version)
Expand Down Expand Up @@ -631,11 +635,9 @@ read_watersurfaces <-
)

wfd_typetransl <- read_sf(file, layer = "LktKRWTYPE") %>%
mutate_if(., is.character,
.funs = function(x) {
return(`Encoding<-`(x, "UTF-8"))
}
) %>%
mutate(across(where(is.character),
~ return(`Encoding<-`(.x, "UTF-8"))
)) %>%
mutate(across(c(.data$Code), as.factor)) %>%
rename(
wfd_type = .data$Code,
Expand Down Expand Up @@ -664,6 +666,7 @@ read_watersurfaces <-
"C", "circumneutraal",
"Cb", "circumneutraal, sterk gebufferd",
"CbFe", "circumneutraal, sterk gebufferd, ijzerrijk",
"CFe", "circumneutraal, ijzerrijk",
"Czb", "circumneutraal, zwak gebufferd",
"Z", "zuur",
"Zm", "zwak zuur",
Expand All @@ -676,6 +679,21 @@ read_watersurfaces <-
)
}

if (version == "watersurfaces_v2024") {
wfd_type_alttransl <- data.frame(wfd_type = "-", wfd_type_name = "geen ander watertype") %>%
bind_rows(wfd_typetransl) %>%
bind_rows(wfd_typetransl %>%
mutate(wfd_type = paste0("(",wfd_type,")"),
wfd_type_name = paste(wfd_type_name, "(weinig waarschijnlijk)"))) %>%
rename(wfd_type_alt_name = wfd_type_name,
wfd_type_alternative = wfd_type) %>%
mutate(
wfd_type_alternative = factor(.data$wfd_type_alternative,
levels = .$wfd_type_alternative
)
)
}

if (fix_geom) {
validities <- st_is_valid(watersurfaces)
n_invalid <- sum(!validities | is.na(validities))
Expand All @@ -691,18 +709,25 @@ read_watersurfaces <-
watersurfaces %>%
{
if (version == "watersurfaces_v1.2") {
rename(., water_level_management = .data$PEILBEHEER)
rename(., water_level_management = .data$PEILBEHEER,
hyla_code = .data$HYLAC)
} else if (version == "watersurfaces_v2024") {
rename(.,
wfd_type_alternative = .data$KRWTYPEA,
water_level_management = .data$PEILBEHEER) %>%
mutate(across(where(is.character), ~na_if(., "")))
} else {
.
rename(., hyla_code = .data$HYLAC)
}
} %>%
select(
polygon_id = .data$WVLC,
wfd_code = .data$WTRLICHC,
hyla_code = .data$HYLAC,
matches("^hyla_code$"),
name = .data$NAAM,
area_name = .data$GEBIED,
wfd_type = .data$KRWTYPE,
matches("^wfd_type_alternative$"),
wfd_type_certain = .data$KRWTYPES,
depth_class = .data$DIEPKL,
connectivity = .data$CONNECT,
Expand All @@ -729,23 +754,29 @@ read_watersurfaces <-
factor(
levels =
levels(wfd_typetransl$wfd_type)
),
hyla_code = ifelse(.data$hyla_code == 0,
NA,
.data$hyla_code
)
)
) %>%
mutate(across(
matches("^wfd_type_alternative$"),
~ factor(.,
levels =
levels(wfd_type_alttransl$wfd_type_alternative)
)
)) %>%
mutate(across(
matches("^hyla_code$"),
~ ifelse(.data$hyla_code == 0,
NA,
.data$hyla_code)
)) %>%
arrange(.data$polygon_id)

if (version == "watersurfaces_v1.0") {
watersurfaces <-
watersurfaces %>%
mutate_at(
.vars = c("wfd_code", "name"),
.funs = function(x) {
ifelse(x == "<Null>", NA, x)
}
) %>%
mutate(across(c("wfd_code", "name"),
~ ifelse(.x == "<Null>", NA, .x)
)) %>%
mutate(wfd_type_certain = ifelse(is.na(.data$wfd_type_certain),
na_lgl,
.data$wfd_type_certain %in%
Expand Down Expand Up @@ -777,11 +808,9 @@ read_watersurfaces <-
if (extended) {
if (version == "watersurfaces_v1.1") {
connectivitytransl <- read_sf(file, layer = "LktCONNECT") %>%
mutate_if(., is.character,
.funs = function(x) {
return(`Encoding<-`(x, "UTF-8"))
}
) %>%
mutate(across(where(is.character),
~ return(`Encoding<-`(.x, "UTF-8"))
)) %>%
mutate(across(c(.data$Code), as.factor)) %>%
rename(
connectivity = .data$Code,
Expand Down Expand Up @@ -823,8 +852,23 @@ read_watersurfaces <-
to = wfd_typetransl$wfd_type_name
)
) %>%
# following match is only partial in case of v1.2
left_join(connectivitytransl, by = "connectivity") %>%
{
if (version == "watersurfaces_v2024") {
left_join(., wfd_type_alttransl, by = "wfd_type_alternative") %>%
mutate(
wfd_type_alt_name =
.data$wfd_type_alternative %>%
mapvalues(
from = wfd_type_alttransl$wfd_type_alternative,
to = wfd_type_alttransl$wfd_type_alt_name
)
)
} else {
.
}
} %>%
#following match is only partial in case of v1.2
left_join(., connectivitytransl, by = "connectivity") %>%
mutate(
connectivity_name =
.data$connectivity %>%
Expand All @@ -834,10 +878,19 @@ read_watersurfaces <-
)
) %>%
select(
1:6,
.data$wfd_type_name,
7:9,
.data$connectivity_name,
polygon_id,
wfd_code,
matches("^hyla_code$"),
name,
area_name,
wfd_type,
wfd_type_name,
matches("^wfd_type_alternative$"),
matches("^wfd_type_alt_name$"),
wfd_type_certain,
depth_class,
connectivity,
connectivity_name,
everything()
)
}
Expand Down
27 changes: 16 additions & 11 deletions man/read_watersurfaces.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading