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_habitatdata to version 2023 #184

Open
wants to merge 4 commits into
base: dev_nextrelease
Choose a base branch
from
Open
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
151 changes: 101 additions & 50 deletions R/read_habitatdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' }
#'
#' The data source \code{habitatmap_stdized} is the processed version
#' of the raw data source \code{habitatmap} (De Saeger et al., 2020).
#' of the raw data source \code{habitatmap} (De Saeger et al., 2023).
#' Every polygon in the \code{habitatmap} can consist of maximum 5
#' different types. This information is stored in the
#' columns 'HAB1', HAB2',..., 'HAB5' of the attribute table. The
Expand Down Expand Up @@ -111,13 +111,13 @@
#' @references
#'
#' \itemize{
#' \item De Saeger, S., Guelinckx, R., Oosterlynck, P., De Bruyn, A.,
#' Debusschere, K.,
#' Dhaluin, P., Erens, R., Hendrickx, P., Hennebel, D., Jacobs, I., Kumpen, M.,
#' Opdebeeck, J., Spanhove, T., Tamsyn, W., Van Oost, F., Van Dam, G.,
#' Van Hove, M., Wils, C., Paelinckx, D. (2020). Biologische Waarderingskaart
#' en Natura 2000 Habitatkaart, uitgave 2020. (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 35). Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.18840851}.
#' \item De Saeger S., Dhaluin P., Erens R., Guelinckx G., Hennebel D.,
#' Jacobs I., Kumpen M., Van Oost F., Spanhove T., Leyssen A., Oosterlynck P.,
#' Van Dam G., Van Hove M., Wils C. (red.) (2023).
#' Biologische Waarderingskaart en Natura 2000 Habitatkaart, uitgave 2023.
#' (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 31).
#' Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.96375305}.
#' \item De Saeger, S., Oosterlynck, P. & Paelinckx, D. (2017). The Biological
#' Valuation Map (BVM): a field-driven survey of land cover and vegetation in
#' the Flemish Region of Belgium. Documents phytosociologiques - Actes du
Expand Down Expand Up @@ -160,6 +160,7 @@ read_habitatmap_stdized <-
"20_processed/habitatmap_stdized/habitatmap_stdized.gpkg"
),
version = c(
"habitatmap_stdized_2023_v1",
"habitatmap_stdized_2020_v1",
"habitatmap_stdized_2018_v2",
"habitatmap_stdized_2018_v1"
Expand Down Expand Up @@ -306,19 +307,18 @@ read_habitatmap_stdized <-
#'
#' @references
#' \itemize{
#' \item Leyssen A., Scheers K., Smeekens V., Wils C., Packet J., De Knijf G. &
#' Denys L. (2020).
#' Watervlakken versie 1.1: polygonenkaart van stilstaand water in Vlaanderen.
#' Uitgave 2020. Rapporten van het Instituut voor Natuur- en Bosonderzoek 2020
#' (40). Instituut voor Natuur en Bosonderzoek, Brussel.
#' \doi{10.21436/inbor.19088385}.
#' \item De Saeger, S., Guelinckx, R., Oosterlynck, P., De Bruyn, A., Debusschere, K.,
#' Dhaluin, P., Erens, R., Hendrickx, P., Hennebel, D., Jacobs, I., Kumpen, M.,
#' Op De Beeck, J., Spanhove, T., Tamsyn, W., Van Oost, F., Van Dam, G.,
#' Van Hove, M., Wils, C., Paelinckx, D. (2020). Biologische Waarderingskaart
#' en Natura 2000 Habitatkaart, uitgave 2020. (Rapporten van het Instituut voor
#' Natuur- en Bosonderzoek; Nr. 35). Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.18840851}.
#' \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. Rapporten van het Instituut voor Natuur- en Bosonderzoek 2022
#' (31). Instituut voor Natuur en Bosonderzoek, Brussel.
#' \doi{10.21436/inbor.87014272}.
#' #' \item De Saeger S., Dhaluin P., Erens R., Guelinckx G., Hennebel D.,
#' Jacobs I., Kumpen M., Van Oost F., Spanhove T., Leyssen A., Oosterlynck P.,
#' Van Dam G., Van Hove M., Wils C. (red.) (2023).
#' Biologische Waarderingskaart en Natura 2000 Habitatkaart, uitgave 2023.
#' (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 31).
#' Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.96375305}.
#' }
#'
#' @examples
Expand Down Expand Up @@ -359,6 +359,7 @@ read_watersurfaces_hab <-
),
interpreted = FALSE,
version = c(
"watersurfaces_hab_v5",
"watersurfaces_hab_v4",
"watersurfaces_hab_v3",
"watersurfaces_hab_v2",
Expand Down Expand Up @@ -404,8 +405,8 @@ read_watersurfaces_hab <-

watersurfaces_types <- watersurfaces_types %>%
mutate(
polygon_id = factor(.data$polygon_id, levels = levels(watersurfaces_polygons$polygon_id)),
certain = .data$certain == 1,
polygon_id = factor(polygon_id, levels = levels(watersurfaces_polygons$polygon_id)),
certain = certain == 1,
type = factor(.data$type,
levels = levels(types$type)
)
Expand Down Expand Up @@ -581,7 +582,9 @@ read_watersurfaces <-
function(file = NULL,
extended = FALSE,
fix_geom = FALSE,
version = c("watersurfaces_v1.2", "watersurfaces_v1.1", "watersurfaces_v1.0")) {
version = c("watersurfaces_v1.2",
"watersurfaces_v1.1",
"watersurfaces_v1.0")) {
version <- match.arg(version)
assert_that(is.flag(extended), noNA(extended))
assert_that(is.flag(fix_geom), noNA(fix_geom))
Expand Down Expand Up @@ -867,7 +870,7 @@ read_watersurfaces <-

#' Return the data source \code{habitatmap} as an \code{sf} multipolygon layer
#'
#' Returns the raw data source \code{habitatmap} (De Saeger et al., 2020)
#' Returns the raw data source \code{habitatmap} (De Saeger et al., 2023)
#' as a standardized \code{sf} multipolygon layer
#' (tidyverse-styled, internationalized) in the Belgian Lambert 72 CRS
#' (EPSG-code \href{https://epsg.io/31370}{31370}).
Expand All @@ -879,6 +882,14 @@ read_watersurfaces <-
#' requires the corresponding version of the processed data source
#' \code{habitatmap_stdized} to be present in its default location inside the
#' \code{n2khab_data} folder.
#' @param fix_geom Logical.
#' Should invalid or corrupt geometries be fixed in the resulting \code{sf}
#' object in order to make them valid?
#' This prevents potential problems in geospatial operations, but beware that
#' fixed geometries are different from the original ones.
#' \code{\link[sf:st_make_valid]{sf::st_make_valid()}} is used to fix
#' geometries (with GEOS as backend).
#' Defaults to \code{FALSE}.
#'
#' @inheritParams read_habitatmap_stdized
#'
Expand All @@ -891,13 +902,13 @@ read_watersurfaces <-
#' @references
#'
#' \itemize{
#' \item De Saeger, S., Guelinckx, R., Oosterlynck, P., De Bruyn, A.,
#' Debusschere, K.,
#' Dhaluin, P., Erens, R., Hendrickx, P., Hennebel, D., Jacobs, I., Kumpen, M.,
#' Opdebeeck, J., Spanhove, T., Tamsyn, W., Van Oost, F., Van Dam, G.,
#' Van Hove, M., Wils, C., Paelinckx, D. (2020). Biologische Waarderingskaart
#' en Natura 2000 Habitatkaart, uitgave 2020. (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 35). Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.18840851}.
#' \item De Saeger S., Dhaluin P., Erens R., Guelinckx G., Hennebel D.,
#' Jacobs I., Kumpen M., Van Oost F., Spanhove T., Leyssen A., Oosterlynck P.,
#' Van Dam G., Van Hove M., Wils C. (red.) (2023).
#' Biologische Waarderingskaart en Natura 2000 Habitatkaart, uitgave 2023.
#' (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 31).
#' Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.96375305}.
#' \item De Saeger, S., Oosterlynck, P. & Paelinckx, D. (2017). The Biological
#' Valuation Map (BVM): a field-driven survey of land cover and vegetation in
#' the Flemish Region of Belgium. Documents phytosociologiques - Actes du
Expand All @@ -916,6 +927,12 @@ read_watersurfaces <-
#'
#' hm <- read_habitatmap()
#' hm
#'
#' hm_valid <- read_habitatmap(fix_geom = TRUE)
#' hm_valid
#'
#' all(sf::st_is_valid(hm))
#' all(sf::st_is_valid(hm_valid))
#' }
#'
#' @export
Expand All @@ -925,6 +942,8 @@ read_watersurfaces <-
#' noNA
#' @importFrom sf
#' read_sf
#' st_is_valid
#' st_make_valid
#' st_crs<-
#' @importFrom rlang .data
#' @importFrom dplyr
Expand All @@ -937,12 +956,15 @@ read_watersurfaces <-
read_habitatmap <-
function(file = file.path(locate_n2khab_data(), "10_raw/habitatmap"),
filter_hab = FALSE,
fix_geom = FALSE,
version = c(
"habitatmap_2023",
"habitatmap_2020",
"habitatmap_2018"
)) {
assert_that(file.exists(file))
assert_that(is.flag(filter_hab), noNA(filter_hab))
assert_that(is.flag(fix_geom), noNA(fix_geom))
version <- match.arg(version)

if (filter_hab) {
Expand All @@ -952,7 +974,9 @@ read_habitatmap <-
"20_processed/habitatmap_stdized/habitatmap_stdized.gpkg"
))

if (version == "habitatmap_2020") {
if (version == "habitatmap_2023") {
xxh64sum_habitatmap_stdized_expected <- "5c32f9b5d74eac23"
} else if (version == "habitatmap_2020") {
xxh64sum_habitatmap_stdized_expected <- "3109c26f0a27a0f3"
} else {
xxh64sum_habitatmap_stdized_expected <- c("b80f469f33636c8b", "8e9c4e09f5f67c3e")
Expand Down Expand Up @@ -1027,6 +1051,18 @@ read_habitatmap <-
mutate(polygon_id = factor(.data$polygon_id, levels = hab_stdized$polygon_id))
}

if (fix_geom) {
# temporarily dropped because st_is_valid takes too long
# n_invalid <- sum(
# !st_is_valid(habitatmap) | is.na(st_is_valid(habitatmap))
# )
# if (n_invalid > 0) {
habitatmap <- st_make_valid(habitatmap)
# message("Fixed ", n_invalid, " invalid or corrupt geometries.")
message("Fixed invalid or corrupt geometries.")
# }
Comment on lines +1055 to +1063
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See PR #185 for some inspiration regarding more efficiency, although with current timings such solution would not come near to a pleasant experience.

}

suppressWarnings(st_crs(habitatmap) <- 31370)

return(habitatmap)
Expand All @@ -1051,7 +1087,7 @@ read_habitatmap <-
#' \code{habitatmap_terr} is the further interpreted, terrestrial part of
#' \code{habitatmap_stdized} (see \code{\link{read_habitatmap_stdized}}),
#' which, in turn, is derived from the raw data source \code{habitatmap}
#' (De Saeger et al., 2020).
#' (De Saeger et al., 2023).
#' By default, occurrences of type \code{7220} are dropped because a more
#' reliable data source is available for this habitat type (see \code{drop_7220}
#' argument).
Expand Down Expand Up @@ -1156,13 +1192,13 @@ read_habitatmap <-
#' @references
#'
#' \itemize{
#' \item De Saeger, S., Guelinckx, R., Oosterlynck, P., De Bruyn, A.,
#' Debusschere, K.,
#' Dhaluin, P., Erens, R., Hendrickx, P., Hennebel, D., Jacobs, I., Kumpen, M.,
#' Opdebeeck, J., Spanhove, T., Tamsyn, W., Van Oost, F., Van Dam, G.,
#' Van Hove, M., Wils, C., Paelinckx, D. (2020). Biologische Waarderingskaart
#' en Natura 2000 Habitatkaart, uitgave 2020. (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 35). Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.18840851}.
#' \item De Saeger S., Dhaluin P., Erens R., Guelinckx G., Hennebel D.,
#' Jacobs I., Kumpen M., Van Oost F., Spanhove T., Leyssen A., Oosterlynck P.,
#' Van Dam G., Van Hove M., Wils C. (red.) (2023).
#' Biologische Waarderingskaart en Natura 2000 Habitatkaart, uitgave 2023.
#' (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 31).
#' Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.96375305}.
#' \item De Saeger, S., Oosterlynck, P. & Paelinckx, D. (2017). The Biological
#' Valuation Map (BVM): a field-driven survey of land cover and vegetation in
#' the Flemish Region of Belgium. Documents phytosociologiques - Actes du
Expand Down Expand Up @@ -1211,6 +1247,8 @@ read_habitatmap_terr <-
keep_aq_types = TRUE,
drop_7220 = TRUE,
version = c(
"habitatmap_terr_2023_v1",
"habitatmap_terr_2020_v2",
"habitatmap_terr_2020_v1",
"habitatmap_terr_2018_v2",
"habitatmap_terr_2018_v1"
Expand Down Expand Up @@ -1324,13 +1362,16 @@ read_habitatmap_terr <-
#' Return the data source \code{habitatstreams} as an \code{sf} linestring
#' layer or as a list
#'
#' Returns the raw data source \code{habitatstreams} (Leyssen et al., 2020)
#' as an \code{sf} linestring
#' Returns the raw data source \code{habitatstreams}(section 'habitat 3260' from
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#' Returns the raw data source \code{habitatstreams}(section 'habitat 3260' from
#' Returns the raw data source \code{habitatstreams} (section 'habitat 3260' from

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and rerun devtools::document() to update Rd file

#' De Saeger et al., 2023) as an \code{sf} linestring
#' layer or as a list of two objects: the \code{sf} object (CRS:
#' Belgian Lambert 72 (EPSG-code \href{https://epsg.io/31370}{31370}))
#' plus a data frame
#' with textual explanation of the values of the \code{source_id} variable.
#'
#' For more information about this map, see also the documentation of the
#' previous version (Leyssen et al., 2020).
#'
#' @param source_text Logical, defaults to \code{FALSE}.
#' If \code{TRUE}, a list is returned (see \emph{Value}).
#'
Expand All @@ -1348,13 +1389,23 @@ read_habitatmap_terr <-
#' }
#'
#' @references
#' Leyssen A., Smeekens V., Denys L. (2020). Indicatieve situering van het
#'
#' \itemize{
#' \item De Saeger S., Dhaluin P., Erens R., Guelinckx G., Hennebel D.,
#' Jacobs I., Kumpen M., Van Oost F., Spanhove T., Leyssen A., Oosterlynck P.,
#' Van Dam G., Van Hove M., Wils C. (red.) (2023).
#' Biologische Waarderingskaart en Natura 2000 Habitatkaart, uitgave 2023.
#' (Rapporten van het Instituut voor Natuur- en Bosonderzoek; Nr. 31).
#' Instituut voor Natuur- en Bosonderzoek (INBO).
#' \doi{10.21436/inbor.96375305}.
#' \item Leyssen A., Smeekens V., Denys L. (2020). Indicatieve situering van het
#' Natura 2000 habitattype 3260. Submontane en laaglandrivieren met vegetaties
#' behorend tot het \emph{Ranunculion fluitantis} en het
#' \emph{Callitricho-Batrachion}.
#' Uitgave 2020 (versie 1.7). Rapporten van het Instituut voor Natuur- en
#' Bosonderzoek 2020 (34). Research Institute for Nature and Forest, Brussels.
#' \doi{10.21436/inbor.18903609}.
#' }
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -1417,8 +1468,8 @@ read_habitatstreams <-
lines <-
habitatstreams %>%
select(
river_name = .data$NAAM,
source_id = .data$BRON
river_name = NAAM,
source_id = BRON
) %>%
mutate(
river_name = factor(
Expand All @@ -1427,15 +1478,15 @@ read_habitatstreams <-
replacement = "\\1\\U\\2",
str_replace(
str_to_title(
str_squish(.data$river_name)
str_squish(river_name)
),
pattern = "Ij",
replacement = "IJ"
),
perl = TRUE
)
),
source_id = factor(.data$source_id),
source_id = factor(source_id),
type = "3260" %>%
factor(levels = read_types() %>%
.$type %>%
Expand All @@ -1457,8 +1508,8 @@ read_habitatstreams <-
) %>%
mutate(
source_id = factor(.data$source_id,
levels = lines %>% .$source_id %>%
levels()
levels = lines %>% .$source_id %>%
levels()
),
source_text = fct_reorder(
.data$source_text,
Expand Down
Loading
Loading