Ver Fonte

Muudetud: ehak, kaardiruudud ja kataster.

Ardo Kubjas há 5 anos atrás
pai
commit
beda35451a
5 ficheiros alterados com 72 adições e 76 exclusões
  1. 26 28
      R/maaamet_ehak.R
  2. 27 30
      R/maaamet_kaardiruudud.R
  3. 9 8
      R/maaamet_kataster.R
  4. 5 5
      man/maaamet_ehak.Rd
  5. 5 5
      man/maaamet_kaardiruudud.Rd

+ 26 - 28
R/maaamet_ehak.R

@@ -7,28 +7,27 @@
 #' @keywords postgis, maps, ESRI Shpfile, OSM
 #' @export
 #' @examples
-#' ## Not run:
-#' ##
-#' ## maaamet_ehak()
-#' ##
-#' ## End(**Not run**)
+#' \dontrun{
+#'
+#' conf <- ruut::get_config()
+#' maaamet_ehak(conf = conf)
+#' }
 maaamet_ehak <- function(conf = NULL) {
-  ans <- utils::askYesNo("Do you want to import maps into database?")
-  if (!ans) {
+  ans <- utils::askYesNo("Do you want to import maps into database?", default = F)
+  if (!ans | is.na(ans)) {
     cat("\n------------------------\n")
     cat("Kaardikihte ei lisatud.")
     cat("\n------------------------\n")
+    return()
   }
   if (ans) {
-    # Download links
-    urls <- c(
-      "https://geoportaal.maaamet.ee/docs/haldus_asustus/maakond_shp.zip",
-      "https://geoportaal.maaamet.ee/docs/haldus_asustus/omavalitsus_shp.zip",
-      "https://geoportaal.maaamet.ee/docs/haldus_asustus/asustusyksus_shp.zip",
-      "https://geoportaal.maaamet.ee/docs/haldus_asustus/omavalitsus_valispiirini_shp.zip"
-    )
-    # Estonia shapefile (ZIP archive)
-    map_shapefile <- "maaamet_shapefile.zip"
+    # Temp directory
+    tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "maaamet_ehak")
+    if (!dir.exists(tmp_dir)) {
+      dir.create(tmp_dir)
+    }
+    # Params
+    params <- c("maakond", "omavalitsus", "asustusyksus", "omavalitsus_valispiirini")
     # Export to postgis
     if (is.null(conf)) {
       conf <- ruut::get_config()
@@ -36,18 +35,17 @@ maaamet_ehak <- function(conf = NULL) {
     }
     # New schema
     ruut::db_create_new_schema(conf = conf)
-    for (j in 1:length(urls)) {
-      # Temp directory
-      tmp_dir <- sprintf("/tmp/maaamet_ehak/%s", j)
-      if (!dir.exists(tmp_dir)) {
-        dir.create(tmp_dir, recursive = T)
-      }
+    for (j in 1:length(params)) {
+      # Shapefile (ZIP archive)
+      map_shapefile <- sprintf("%s.zip", params[j])
       # Download and save
       saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
-      utils::download.file(
-        url = urls[j],
-        destfile = saveTo, method = "curl", extra = "-L"
-      )
+      if (!file.exists(saveTo)) {
+        utils::download.file(
+          url = sprintf("https://geoportaal.maaamet.ee/docs/haldus_asustus/%s_shp.zip", params[j]),
+          destfile = saveTo, method = "curl", extra = "-L"
+        )
+      }
       # Unzip
       utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
       # List of files
@@ -70,8 +68,8 @@ maaamet_ehak <- function(conf = NULL) {
           ls[i], conf$schema, conf$table
         ))
       }
-      # Delete temp directory
-      # system(sprintf("rm -rf %s/*", tmp_dir))
+      # Delete unnecessary files.
+      system(sprintf("find %s -type f -not -name '*.zip' -print0 | xargs -0 rm --", tmp_dir))
     }
   }
 }

+ 27 - 30
R/maaamet_kaardiruudud.R

@@ -7,30 +7,27 @@
 #' @keywords postgis, maps, ESRI Shpfile, OSM
 #' @export
 #' @examples
-#' ## Not run:
-#' ##
-#' ## maaamet_kaardiruudud()
-#' ##
-#' ## End(**Not run**)
+#' \dontrun{
+#'
+#' conf <- ruut::get_config()
+#' maaamet_kaardiruudud(conf = conf)
+#' }
 maaamet_kaardiruudud <- function(conf = NULL) {
-  ans <- utils::askYesNo("Do you want to import maps into database?")
-  if (!ans) {
+  ans <- utils::askYesNo("Do you want to import maps into database?", default = F)
+  if (!ans | is.na(ans)) {
     cat("\n------------------------\n")
     cat("Kaardikihte ei lisatud.")
     cat("\n------------------------\n")
+    return()
   }
   if (ans) {
-    # Download links
-    urls <- c(
-      "https://geoportaal.maaamet.ee/docs/pohikaart/epk2T_SHP.zip",
-      "https://geoportaal.maaamet.ee/docs/pohikaart/epk10T_SHP.zip",
-      "https://geoportaal.maaamet.ee/docs/pohikaart/epk20T_shp.zip",
-      "https://geoportaal.maaamet.ee/docs/pohikaart/epk50T_shp.zip",
-      "https://geoportaal.maaamet.ee/docs/pohikaart/epk100T_shp.zip",
-      "https://geoportaal.maaamet.ee/docs/pohikaart/epk200T_shp.zip"
-    )
-    # Estonia shapefile (ZIP archive)
-    map_shapefile <- "maaamet_shapefile.zip"
+    # Temp directory
+    tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "maaamet_epk")
+    if (!dir.exists(tmp_dir)) {
+      dir.create(tmp_dir)
+    }
+    # Params
+    params <- c("epk2T_SHP", "epk10T_SHP", "epk20T_shp", "epk50T_shp", "epk100T_shp", "epk200T_shp")
     # Export to postgis
     if (is.null(conf)) {
       conf <- ruut::get_config()
@@ -38,18 +35,18 @@ maaamet_kaardiruudud <- function(conf = NULL) {
     }
     # New schema
     ruut::db_create_new_schema(conf = conf)
-    for (j in 1:length(urls)) {
-      # Temp directory
-      tmp_dir <- sprintf("/tmp/maaamet_epk/%s", j)
-      if (!dir.exists(tmp_dir)) {
-        dir.create(tmp_dir, recursive = T)
-      }
+    for (j in 1:length(params)) {
+      # Shapefile (ZIP archive)
+      param_split <- strsplit(params[j], "_")[[1]][1]
+      map_shapefile <- sprintf("%s.zip", param_split)
       # Download and save
       saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
-      utils::download.file(
-        url = urls[j],
-        destfile = saveTo, method = "curl", extra = "-L"
-      )
+      if (!file.exists(saveTo)) {
+        utils::download.file(
+          url = sprintf("https://geoportaal.maaamet.ee/docs/pohikaart/%s.zip", params[j]),
+          destfile = saveTo, method = "curl", extra = "-L"
+        )
+      }
       # Unzip
       utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
       # List of files
@@ -72,8 +69,8 @@ maaamet_kaardiruudud <- function(conf = NULL) {
           ls[i], conf$schema, conf$table
         ))
       }
-      # Delete temp directory
-      # system(sprintf("rm -rf %s/*", tmp_dir))
+      # Delete unnecessary files.
+      system(sprintf("find %s -type f -not -name '*.zip' -print0 | xargs -0 rm --", tmp_dir))
     }
   }
 }

+ 9 - 8
R/maaamet_kataster.R

@@ -7,21 +7,22 @@
 #' @keywords postgis, maps, ESRI Shpfile, OSM
 #' @export
 #' @examples
-#' ## Not run:
-#' ##
-#' ## maaamet_kataster()
-#' ##
-#' ## End(**Not run**)
+#' \dontrun{
+#'
+#' conf <- ruut::get_config()
+#' ## maaamet_kataster(conf = conf)
+#' }
 maaamet_kataster <- function(conf = NULL) {
-  ans <- utils::askYesNo("Do you want to import maps into database?")
-  if (!ans) {
+  ans <- utils::askYesNo("Do you want to import maps 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 <- "/tmp/maaamet_kataster"
+    tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "maaamet_kataster")
     if (!dir.exists(tmp_dir)) {
       dir.create(tmp_dir)
     }

+ 5 - 5
man/maaamet_ehak.Rd

@@ -16,11 +16,11 @@ No output.
 Source: \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Haldus-ja-asustusjaotus-p119.html} . Andmed salvestatakse postgisi andmebaasi. Schema = 'maaamet'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
 }
 \examples{
-## Not run:
-##
-## maaamet_ehak()
-##
-## End(**Not run**)
+\dontrun{
+
+conf <- ruut::get_config()
+maaamet_ehak(conf = conf)
+}
 }
 \seealso{
 \code{\link[ruut:get_config]{ruut::get_config()}}, \code{\link[ruut:copy_shp_to_db]{ruut::copy_shp_to_db()}}

+ 5 - 5
man/maaamet_kaardiruudud.Rd

@@ -16,11 +16,11 @@ No output.
 Source: \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Kaardilehtede-susteemid-p224.html}. Andmed salvestatakse postgisi andmebaasi. Schema = 'maaamet'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
 }
 \examples{
-## Not run:
-##
-## maaamet_kaardiruudud()
-##
-## End(**Not run**)
+\dontrun{
+
+conf <- ruut::get_config()
+maaamet_kaardiruudud(conf = conf)
+}
 }
 \seealso{
 \code{\link[ruut:get_config]{ruut::get_config()}}, \code{\link[ruut:copy_shp_to_db]{ruut::copy_shp_to_db()}}