inspire_grids.R 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #' INSPIRE kaardiruudud
  2. #'
  3. #' Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2
  4. #' @examples
  5. #' ## Not run:
  6. #' ##
  7. #' ## inspire_grids()
  8. #' ##
  9. #' ## End(**Not run**)
  10. inspire_grids <- function() {
  11. ans <- utils::askYesNo("Do you want to use askYesNo?")
  12. if (!ans) {
  13. cat("\n------------------------\n")
  14. cat("Kaardikihte ei lisatud.")
  15. cat("\n------------------------\n")
  16. }
  17. if (ans) {
  18. # Temp directory
  19. tmp_dir <- tempdir()
  20. # Download link
  21. url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
  22. # Estonia shapefile (ZIP archive)
  23. map_shapefile <- "Estonia_shapefile.zip"
  24. # Download and save
  25. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  26. utils::download.file(
  27. url = url,
  28. destfile = saveTo, method = "curl", extra = "-L"
  29. )
  30. # Unzip
  31. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  32. # List of files
  33. ls <- list.files(path = tmp_dir, pattern = ".shp")
  34. ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
  35. tbl_names <- unlist(strsplit(x = ls, split = ".shp"))
  36. # Export to postgis
  37. schema <- "inspire"
  38. config <- ruut::get_config()
  39. config$schema <- schema
  40. # New schema
  41. ruut::db_create_new_schema(conf = config)
  42. # Multy layer
  43. for (i in 1:length(tbl_names)) {
  44. print(tbl_names[i])
  45. config$table <- tbl_names[i]
  46. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  47. ## Export to postgis database.
  48. ruut::copy_shp_to_db(
  49. dir = tmp_dir, layer = tbl_names[i], conf = config,
  50. id = "fid", crs_source = "EPSG:3035", crs_target = "EPSG:4326",
  51. geometry_type = "POLYGON"
  52. )
  53. cat(sprintf(
  54. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  55. ls[i], config$schema, config$table
  56. ))
  57. }
  58. }
  59. }