inspire_grids.R 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #' INSPIRE kaardiruudud
  2. #'
  3. #' 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.
  4. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  5. #' @return No output.
  6. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()]
  7. #' @keywords postgis, maps, ESRI Shpfile, OSM
  8. #' @export
  9. #' @examples
  10. #' \dontrun{
  11. #'
  12. #' conf <- ruut::get_config()
  13. #' inspire_grids(conf = conf)
  14. #' }
  15. inspire_grids <- function(conf = NULL) {
  16. ans <- utils::askYesNo("Do you want to import INSPIRE grids into database?", default = F)
  17. if (!ans | is.na(ans)) {
  18. cat("\n------------------------\n")
  19. cat("Kaardikihte ei lisatud.")
  20. cat("\n------------------------\n")
  21. return()
  22. }
  23. if (ans) {
  24. # Temp directory
  25. tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "inspire")
  26. if (!dir.exists(tmp_dir)) {
  27. dir.create(tmp_dir)
  28. }
  29. # Download link
  30. url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
  31. # Estonia shapefile (ZIP archive)
  32. map_shapefile <- "Estonia_shapefile.zip"
  33. # Download and save
  34. url_download <- sprintf("%s/%s", url, "")
  35. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  36. if (!file.exists(saveTo)) {
  37. utils::download.file(
  38. url = url_download,
  39. destfile = saveTo, method = "curl", extra = "-L"
  40. )
  41. }
  42. # Unzip
  43. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  44. # List of files
  45. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  46. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  47. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  48. # Export to postgis
  49. if (is.null(conf)) conf <- ruut::get_config()
  50. conf$schema <- "inspire"
  51. # New schema
  52. ruut::db_create_new_schema(conf = conf)
  53. # Multi layer
  54. for (i in 1:length(tbl_names)) {
  55. print(tbl_names[i])
  56. conf$table <- tbl_names[i]
  57. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  58. ## Export to postgis database.
  59. ruut::copy_shp_to_db(
  60. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  61. id = "fid", crs_source = "EPSG:3035", crs_target = "EPSG:3301",
  62. geometry_type = "POLYGON"
  63. )
  64. cat(sprintf(
  65. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  66. ls[i], conf$schema, conf$table
  67. ))
  68. }
  69. # Delete unnecessary files.
  70. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
  71. }
  72. }