| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #' INSPIRE kaardiruudud
- #'
- #' Source: \url{https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2}. Andmed salvestatakse postgisi andmebaasi. Schema = 'inspire'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
- #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
- #' @return No output.
- #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()]
- #' @keywords postgis, maps, ESRI Shpfile, OSM
- #' @export
- #' @examples
- #' \dontrun{
- #'
- #' conf <- ruut::get_config()
- #' inspire_grids(conf = conf)
- #' }
- inspire_grids <- function(conf = NULL) {
- ans <- utils::askYesNo("Do you want to import INSPIRE grids into database?", default = F)
- if (!ans | is.na(ans)) {
- cat("\n------------------------\n")
- cat("Kaardikihte ei lisatud.")
- cat("\n------------------------\n")
- return()
- }
- if (ans) {
- # Temp directory
- tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "inspire")
- if (!dir.exists(tmp_dir)) {
- dir.create(tmp_dir)
- }
- # Download link
- url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
- # Estonia shapefile (ZIP archive)
- map_shapefile <- "Estonia_shapefile.zip"
- # Download and save
- url_download <- sprintf("%s/%s", url, "")
- saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
- if (!file.exists(saveTo)) {
- utils::download.file(
- url = url_download,
- destfile = saveTo, method = "curl", extra = "-L"
- )
- }
- # Unzip
- utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
- # List of files
- ls <- list.files(path = tmp_dir, pattern = ".dbf")
- ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
- tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
- # Export to postgis
- if (is.null(conf)) conf <- ruut::get_config()
- conf$schema <- "inspire"
- # New schema
- ruut::db_create_new_schema(conf = conf)
- # Multi layer
- for (i in 1:length(tbl_names)) {
- print(tbl_names[i])
- conf$table <- tbl_names[i]
- source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
- ## Export to postgis database.
- ruut::copy_shp_to_db(
- dir = tmp_dir, layer = tbl_names[i], conf = conf,
- id = "fid", crs_source = "EPSG:3035", crs_target = "EPSG:4326",
- geometry_type = "POLYGON"
- )
- cat(sprintf(
- "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
- ls[i], conf$schema, conf$table
- ))
- }
- # Delete unnecessary files.
- system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
- }
- }
|