inspire_grids.R 2.0 KB

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