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

egib layers #102

Merged
merged 2 commits into from
Dec 11, 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
6 changes: 5 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ Authors@R: c(person(given = "Krzysztof",
comment = c(ORCID = "0000-0002-8281-4301")),
person("GUGiK",
role = "ctb",
comment = "source of the data"))
comment = "source of the data"),
person(given = "Grzegorz",
family = "Sapijaszko",
role = "ctb",
email = "[email protected]"))
Description: Automatic open data acquisition from resources of Polish Head Office
of Geodesy and Cartography ('Główny Urząd Geodezji i Kartografii')
(<https://www.gov.pl/web/gugik>).
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(DEM_request)
export(borders_download)
export(borders_get)
export(egib_download)
export(emuia_download)
export(geocodePL_get)
export(geodb_download)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ in `ortho_request()` and `DEM_request()`.

* Updated data sources in `borders_download()` function.

* added `egib_download()` function for downloading Land and Building Register layers.

# rgugik 0.4.1

* The list of communes has been updated (as of 02 January 2024).
Expand Down
143 changes: 143 additions & 0 deletions R/egib_download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#' @title Download Land and Building Register (EGiB) layers
#'
#' @param county County name in Polish. Check [`county_names()`] function.
#' @param TERYT County ID (4 characters).
#' @param layer Requested layer, `parcels` default. Other common layer is `buildings` (budynki).
#' You can check available layers by [`egib_layers()`] data set.
#' @param outdir name of the output directory where the data has to be stored,
#' current directory by default. If you don't want to download data, pass a `NULL` value.
#' @param ... any other parameter passed to [`sf::read_sf()`] function
#'
#' @return Simple feature data frame of objects for requested layer.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' parcels <- egib_download(county = "Świętochłowice", layer = "parcels", outdir = NULL) # 3.9 MB
#' egib_download(TERYT = c("2476", "2264"), layer = "buildings", outdir = ".") # 2.2 + 2.6 MB
#' }
#'
#' @references
#' The EGiB data (Ewidencja Gruntów i Budynków) consist of 2 primary layers:
#' cadastral data (parcels, "dzialki" in Polish) and buildings footprints (budynki).
#' The data is maintained on county level, which results in 380 units/different
#' sources of data. It may contain additional layers like points of detailed
#' horizontal and vertical geodetic control network (osnowa_pozioma
#' and osnowa_pionowa respectively).
#'
#' \url{https://www.geoportal.gov.pl/en/data/land-and-building-register-egib/}
#' \url{https://www.geoportal.gov.pl/en/data/detailed-control-network-database-bdsog/}

egib_download <- function(county = NULL, TERYT = NULL, layer = "parcels", outdir = ".", ...) {

layer_names = rgugik::egib_layers

if (is.null(county) && is.null(TERYT)) {
stop("'county' and 'TERYT' are empty")
}

if (!is.null(county) && !is.null(TERYT)) {
stop("use only one input")
}

if (!all(county %in% layer_names$NAME)) {
stop("incorrect county name")
}

if (!is.null(TERYT) && any(nchar(TERYT) != 4)) {
stop("incorrect TERYT")
}

if (length(layer) > 1L) {
stop("please provide only one layer at time")
}

switch(layer,
parcels = {
layer <- "dzialki" },
buildings = {
layer <- "budynki" },
{
layer <- layer
}
)

if (!is.null(county)) {
sel_vector <- layer_names[, "NAME"] %in% county
layer_names <- layer_names[sel_vector, ]
layer_names <- layer_names[match(county, layer_names$NAME),]

Check warning on line 69 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=69,col=64,[commas_linter] Commas should always have a space after.

if(any(duplicated(layer_names$NAME))) {

Check warning on line 71 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=71,col=7,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 71 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=71,col=8,[any_duplicated_linter] anyDuplicated(x, ...) > 0 is better than any(duplicated(x), ...).
dup <- which(duplicated(layer_names$NAME))

message("There are counties with the same name, both will be downloaded.")
message("If you would like to avoid it, please provide TERYT instead.")

layer_names[layer_names$NAME == layer_names[dup, "NAME"], 1:2]
}

} else {
sel_vector <- layer_names[, "TERYT"] %in% TERYT
layer_names = layer_names[sel_vector, ]
layer_names <- layer_names[match(TERYT, layer_names$TERYT),]

Check warning on line 83 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=83,col=64,[commas_linter] Commas should always have a space after.

}

if(nrow(layer_names > 1L) & is.null(outdir)) {

Check warning on line 87 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=87,col=5,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.

Check warning on line 87 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=87,col=29,[vector_logic_linter] Conditional expressions require scalar logical operators (&& and ||)
message("There is more than one county provided, however you didn't specified the output directory.")

Check warning on line 88 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=88,col=101,[line_length_linter] Lines should not be more than 100 characters. This line is 105 characters.
message("Only the first will be taken.")
layer_names <- layer_names[1, ]
}

for (k in seq_len(nrow(layer_names))) {
print(k)

TERYT <- layer_names[k, "TERYT"]
county_name <- layer_names[k, "NAME"]

layers <- layer_names[k, "LAYERS"] |>
strsplit(split = ", ") |>

Check warning on line 100 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=100,col=24,[fixed_regex_linter] This regular expression is static, i.e., its matches can be expressed as a fixed substring expression, which is faster to compute. Here, you can use ", " with fixed = TRUE.
as.list()
layers <- layers[[1]]

egib_url <- layer_names[k, "URL"]

if (all(is.na(layers))) {
a <- ""
if(!is.na(egib_url)) {

Check warning on line 108 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=108,col=9,[spaces_left_parentheses_linter] Place a space before left parenthesis, except in a function call.
a <- paste("You might try to check", egib_url, "using st_layers() directly.")
}
stop(paste0("there is no layers available for time being\n", a))

Check warning on line 111 in R/egib_download.R

View workflow job for this annotation

GitHub Actions / lint

file=R/egib_download.R,line=111,col=7,[condition_message_linter] Don't use paste0 to build stop strings. Instead use the fact that these functions build condition message strings from their input (using "" as a separator). For translatable strings, prefer using gettextf().
}

if(!any(grepl(layer, layers))) {
stop(paste("Can't find", layer, "in", paste0(unlist(layers), collapse = ", ")))
}

if(any(grepl(layer, layers))) {
layer <- layers[which(grepl(layer, layers))]
if(length(layer) > 1L) {
stop(paste0("There is more than 1 layer simillar to requested: ",
paste0(unlist(layer), collapse = ", "), ". Please specify."))
}
egib_url <- paste0("WFS:", egib_url)

message(paste("Downloading layer", layer, "for", county_name, "county. TERYT:", TERYT))

output <- tryGet(sf::read_sf(dsn = egib_url, layer = layer,
options = "CONSIDER_EPSG_AS_URN=YES", ...))

if (any(output %in% c("error", "warning"))) {
return(invisible("connection error"))
}

if(!is.null(outdir)) {
outfile <- paste0(outdir, "/", TERYT, ".gpkg")
message(paste("Writing the data to", outfile))
sf::write_sf(output, dsn = outfile, layer = layer, append = FALSE)
}
}
}
return(output)
}
14 changes: 14 additions & 0 deletions R/egib_layers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' @name egib_layers
#' @title Available urls and layers of Land and building register (EGiB)
#'
#' @description The data frame contains the names of counties,
#' their identifiers (TERYT, 4 characters), URL ofthe EGiB data (WFS)
#' and the names of available layers.
#'
#' @importFrom utils data
#'
#' @docType data
#' @keywords dataset EGiB
#' @examples
#' egib_layers
"egib_layers"
5 changes: 5 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ knitr::opts_chunk$set(
- [Register of Towns, Streets and Addresses](https://emuia.gugik.gov.pl)
- [State Register of Geographical Names](https://www.geoportal.gov.pl/pl/dane/panstwowy-rejestr-nazw-geograficznych-prng/)
- [State Register of Borders](https://www.geoportal.gov.pl/pl/dane/panstwowy-rejestr-granic-prg/)
- [Land and Building Registers](https://integracja.gugik.gov.pl/eziudp/index.php?zbior=egib)
- Location (geometry) of cadastral parcels using TERYT (parcel ID) or coordinates
- 3D models of buildings (LOD1, LOD2)
- Various digital elevation models as:
Expand All @@ -47,6 +48,7 @@ ds_pl = c("Ortofotomapa",
"Baza Danych Obiektów Ogólnogeograficznych",
"Baza Danych Obiektów Topograficznych",
"Ewidencja Miejscowości, Ulic i Adresów",
"Ewidencja Gruntów i Budynków",
"Państwowy Rejestr Nazw Geograficznych",
"Państwowy Rejestr Granic",
"Lokalizacja działek katastralnych",
Expand All @@ -57,6 +59,7 @@ ds_en = c("Orthophotomap",
"General Geographic Database",
"Topographic Database",
"Register of Towns, Streets and Addresses",
"Land and Building Register",
"State Register of Geographical Names",
"State Register of Borders",
"Location of cadastral parcels",
Expand All @@ -67,6 +70,7 @@ fun = c("`ortho_request()`, `tile_download()`",
"`geodb_download()`",
"`topodb_download()`",
"`emuia_download()`",
"`egib_download()`",
"`geonames_download()`",
"`borders_get()`, `borders_download()`",
"`parcel_get()`",
Expand All @@ -77,6 +81,7 @@ input = c("geometry",
"voivodeship",
"county",
"commune",
"county",
"type",
"type",
"parcel ID, coordinates",
Expand Down
Loading
Loading