Skip to content
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
39 changes: 26 additions & 13 deletions R/create_geo_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,48 @@ add_spatial_information <- function(df_bp, sf_liegenschaften) {
#'
#' Generates an interactive Leaflet map from spatial building permit data.
#' Each polygon includes a popup with details such as publication number,
#' publication date, entry deadline, address, and overlapping permits.
#' publication date, entry deadline, address, and overlapping permits. The
#' default observation period is set to 20 days, as this is the timeframe during
#' which objections to the project can be submitted.
#'
#' @param sf_bp_geo An `sf` object containing spatial building permit data,
#' including #' attributes such as `publicationNumber`, `entryDeadline`,
#' including attributes such as `publicationNumber`, `entryDeadline`,
#' `address`, and `url`.
#' @param days_of_data Integer or Character. If a numeric value is provided, it
#' specifies the number of days in the past from which to retrieve publications.
#' If set to `"all"`, all publications will be displayed starting from January 2025.
#'
#' @param start_date A character date in the format "yyyy-mm-dd" representing the
#' first day for which data should be included.
#' first day for which data should be included.
#' @param end_date A character date in the format "yyyy-mm-dd" representing the
#' the last day for which data should be included.
#' @return A Leaflet map widget displaying the building permit polygons and
#' associated information.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # creating a map of the last 20 days
#' create_map(sf_bp_geo)
#'
#' # customize period of retrieval
#' sf_bp_geo |>
#' create_map(start_date = "20205-01-01", end_date = "2025-07-15")
#' }
create_map <- function(sf_bp_geo, days_of_data = 20) {

if (days_of_data != "all"){
start_date = as.character(Sys.Date()-days_of_data)
} else {
start_date = "2025-01-01"
create_map <- function(sf_bp_geo,
start_date = as.character(Sys.Date()-20),
end_date = as.character(Sys.Date())) {

if (start_date < "2025-01-01") {
cli::cli_warn("There are no geo-referenced building permit applications before 2025-01-01.")
}

sf_bp_geo <- sf_bp_geo |>
dplyr::filter(publicationDate >= start_date)
dplyr::filter(publicationDate >= start_date & publicationDate <= end_date)

if (nrow(sf_bp_geo) == 0) {
cli::cli_abort("There are no building permit application for this period.")
}


####
intersected_poly <- sf_bp_geo |>
sf::st_intersects()
Expand Down
19 changes: 14 additions & 5 deletions R/download_liegenschaften_layer.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ get_geo_info <- function(df_bp, sf_liegenschaften) {
#' get_liegenschaften_layer("data", "your.email@example.com", "Monday")
#' }
get_liegenschaften_layer <- function(file_destination, email_address, retrieval_day) {
sf_file <- paste0(

current_liegenschaften <- paste0(
file_destination,
"/AV_MOpublic-_Liegenschaften_-OGD/AVZH_LIEGENSCHAFTEN_F.shp"
)

if (weekdays(Sys.Date()) == retrieval_day | !file.exists(sf_file)) {
if (weekdays(Sys.Date()) == retrieval_day | !file.exists(current_liegenschaften)) {
dir.create(file_destination)

url <- get_giszh_api_download_url(404, email_address)
Expand All @@ -72,10 +72,19 @@ get_liegenschaften_layer <- function(file_destination, email_address, retrieval_
unzip(zip_file, exdir = file_destination)
}

sf_liegenschaf <- sf::read_sf(sf_file) |>
sf_current_liegenschaf <- sf::read_sf(current_liegenschaften) |>
dplyr::rename_with(tolower)

sf_projected_liegenschaf <- sf::read_sf(
paste0(file_destination,
"/AV_MOpublic-_Liegenschaften_-OGD/AVZH_LIEGENSCHAFTEN_PROJ_F.shp")
) |>
dplyr::rename_with(tolower)

return(sf_liegenschaf)
sf_liegenschaften <- rbind(sf_current_liegenschaf,
sf_projected_liegenschaf)

return(sf_current_liegenschaf)
}


Expand Down
25 changes: 19 additions & 6 deletions man/create_map.Rd

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

3 changes: 2 additions & 1 deletion tests/testthat/test-create_geo_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ testthat::test_that("a map is create", {
crs = 2056
)

map <- sf_map_test |> create_map(days_of_data = "all")
map <- sf_map_test |> create_map(start_date = "2025-01-01",
end_date = "2025-07-15")

testthat::expect_equal(inherits(map, "leaflet"), TRUE)
})
Loading