Ardo Kubjas 5 лет назад
Родитель
Сommit
6d3ca5c290
2 измененных файлов с 105 добавлено и 0 удалено
  1. 73 0
      R/osm_shp.R
  2. 32 0
      man/osm_shp.Rd

+ 73 - 0
R/osm_shp.R

@@ -0,0 +1,73 @@
+#' OSM Esri Shapefile
+#'
+#' Source:  \url{https://download.geofabrik.de/europe/} . Andmed salvestatakse postgisi andmebaasi. Schema = 'osm_shp'. Koniguratsiooni muutmiseks muuda konfiguratsiooni muutujat. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
+#' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
+#' @return No output.
+#' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()]
+#' @keywords postgis, maps, ESRI Shpfile, OSM
+#' @export
+#' @examples
+#' ## Not run:
+#' ##
+#' ## osm_shp()
+#' ##
+#' ## End(**Not run**)
+osm_shp <- function(conf = 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 <- "/tmp/osm_shp"
+    if (!dir.exists(tmp_dir)) {
+      dir.create(tmp_dir)
+    }
+    # Download link
+    url <- "https://download.geofabrik.de/europe"
+    # Estonia shapefile (ZIP archive)
+    map_shapefile <- "estonia-latest-free.shp.zip"
+    # Download and save
+    url_download <- sprintf("%s/%s", url, map_shapefile)
+    saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
+    if (!file.exists(saveTo)) {
+      utils::download.file(
+        url = url_download,
+        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 = ".dbf")
+    ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
+    tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
+    # Export to postgis
+    if (is.null(conf)) {
+      conf <- ruut::get_config()
+      conf$schema <- "osm_shp"
+    }
+    # New schema
+    ruut::db_create_new_schema(conf = conf)
+    # Multi layer
+    for (i in 1:length(tbl_names)) {
+      print(tbl_names[i])
+      conf$table <- gsub("gis_osm_", "", gsub("_free_1", "", 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 = conf,
+        id = "fid", crs_source = "EPSG:4326", crs_target = "EPSG:4326",
+        geometry_type = "PROMOTE_TO_MULTI"
+      )
+      cat(sprintf(
+        "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
+        ls[i], conf$schema, conf$table
+      ))
+    }
+    # Delete temp directory
+    # system(sprintf("rm -rf %s/*", tmp_dir))
+  }
+}

+ 32 - 0
man/osm_shp.Rd

@@ -0,0 +1,32 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/osm_shp.R
+\name{osm_shp}
+\alias{osm_shp}
+\title{OSM Esri Shapefile}
+\usage{
+osm_shp(conf = NULL)
+}
+\arguments{
+\item{conf}{A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.}
+}
+\value{
+No output.
+}
+\description{
+Source:  \url{https://download.geofabrik.de/europe/} . Andmed salvestatakse postgisi andmebaasi. Schema = 'osm_shp'. Koniguratsiooni muutmiseks muuda konfiguratsiooni muutujat. Muutujate vaikeväärtused on sellised, et ei ole vaja midagi muuta.
+}
+\examples{
+## Not run:
+##
+## osm_shp()
+##
+## End(**Not run**)
+}
+\seealso{
+\code{\link[ruut:get_config]{ruut::get_config()}}, \code{\link[ruut:copy_shp_to_db]{ruut::copy_shp_to_db()}}
+}
+\keyword{ESRI}
+\keyword{OSM}
+\keyword{Shpfile,}
+\keyword{maps,}
+\keyword{postgis,}