Przeglądaj źródła

Lisatud 'maaamet_kataster'.

Ardo Kubjas 5 lat temu
rodzic
commit
6378c59965
3 zmienionych plików z 100 dodań i 0 usunięć
  1. 1 0
      NAMESPACE
  2. 67 0
      R/maaamet_kataster.R
  3. 32 0
      man/maaamet_kataster.Rd

+ 1 - 0
NAMESPACE

@@ -2,3 +2,4 @@
 
 export(inspire_grids)
 export(maaamet_kaardiruudud)
+export(maaamet_kataster)

+ 67 - 0
R/maaamet_kataster.R

@@ -0,0 +1,67 @@
+#' Maa-ameti katasriüksused
+#'
+#' Source: hhttps://geoportaal.maaamet.ee/docs/katastripiirid/paev/KATASTER_EESTI_SHP.zip. Andmed salvestatakse postgisi andmebaasi. Schema = 'inspire'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
+#' @param config list configuration file ruut::get_config()
+#' @param schema str database schema name. Deafult is 'inspire'
+#' @param url link ESRI Shapefile download url.
+#' @seealso [ruut::get_config()]
+#' @keywords postgis, maps, ESRI Shpfile
+#' @export
+#' @examples
+#' ## Not run:
+#' ##
+#' ## maaamet_kataster()
+#' ##
+#' ## End(**Not run**)
+maaamet_kataster <- function(config = NULL, schema = "maaamet",
+                             url = NULL) {
+  ans <- utils::askYesNo("Do you want to import maps into database?")
+  if (!ans) {
+    cat("\n------------------------\n")
+    cat("Kaardikihte ei lisatud.")
+    cat("\n------------------------\n")
+  }
+  if (ans) {
+    # Temp directory
+    tmp_dir <- tempdir()
+    # Download link
+    if (is.null(url)) url <- "https://geoportaal.maaamet.ee/docs/katastripiirid/paev"
+    # Estonia shapefile (ZIP archive)
+    map_shapefile <- "KATASTER_EESTI_SHP.zip"
+    # Download and save
+    saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
+    utils::download.file(
+      url = sprintf("%s/%s", url, map_shapefile),
+      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 = ".shp")
+    ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
+    tbl_names <- unlist(strsplit(x = ls, split = ".shp"))
+    # Export to postgis
+    if (is.null(config)) config <- ruut::get_config()
+    config$schema <- schema
+    # New schema
+    ruut::db_create_new_schema(conf = config)
+    # Multi layer
+    for (i in 1:length(tbl_names)) {
+      print(tbl_names[i])
+      config$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 = config,
+        id = "fid", crs_source = "EPSG:3301", crs_target = "EPSG:4326",
+        geometry_type = "POLYGON"
+      )
+      cat(sprintf(
+        "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
+        ls[i], config$schema, config$table
+      ))
+    }
+    # Delete temp directory
+    system(sprintf("rm -rf %s/*", tmp_dir))
+  }
+}

+ 32 - 0
man/maaamet_kataster.Rd

@@ -0,0 +1,32 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/maaamet_kataster.R
+\name{maaamet_kataster}
+\alias{maaamet_kataster}
+\title{Maa-ameti katasriüksused}
+\usage{
+maaamet_kataster(config = NULL, schema = "maaamet", url = NULL)
+}
+\arguments{
+\item{config}{list configuration file ruut::get_config()}
+
+\item{schema}{str database schema name. Deafult is 'inspire'}
+
+\item{url}{link ESRI Shapefile download url.}
+}
+\description{
+Source: hhttps://geoportaal.maaamet.ee/docs/katastripiirid/paev/KATASTER_EESTI_SHP.zip. Andmed salvestatakse postgisi andmebaasi. Schema = 'inspire'. Koniguratsiooni muutmiseks muuda konfiguratsiooni. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
+}
+\examples{
+## Not run:
+##
+## maaamet_kataster()
+##
+## End(**Not run**)
+}
+\seealso{
+\code{\link[ruut:get_config]{ruut::get_config()}}
+}
+\keyword{ESRI}
+\keyword{Shpfile}
+\keyword{maps,}
+\keyword{postgis,}