|
|
@@ -0,0 +1,98 @@
|
|
|
+#' Teeregistri teejooned
|
|
|
+#'
|
|
|
+#' Allalaadimise link: \url{https://teeregister.mnt.ee/reet/shpreports?0}. Andmed tuleb alla laadida käsitsi ja paigaldada kataloogi './inst/tmp/teeregister_teed'. Teeregistri sõiduteede ja teeosade andmed on antud SHP-formaadis kaardikihitidena. Avaandmed teede kohta leiab: \url{https://avaandmed.eesti.ee/datasets/teeregister}, kus on link WFS kujul kättesaamiseks \url{https://teeregister-api.mnt.ee/teenus/wfs?request=GetCapabilities&service=WFS}. WMS kaardilingi leiab \url{https://kaart.maaamet.ee/wms/alus-geo?}.
|
|
|
+#' @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
|
|
|
+#' \dontrun{
|
|
|
+#'
|
|
|
+#' conf <- ruut::get_config()
|
|
|
+#' teeregister_teed(conf = conf)
|
|
|
+#' }
|
|
|
+teeregister_teed <- function(conf = NULL) {
|
|
|
+ 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 <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "teeregister_teed")
|
|
|
+ if (!dir.exists(tmp_dir)) {
|
|
|
+ dir.create(tmp_dir)
|
|
|
+ }
|
|
|
+ # Params
|
|
|
+ params <- c("muutee_soiduteed", "muutee_teeosad", "riigitee_soiduteed", "riigitee_teeosad")
|
|
|
+ # Export to postgis
|
|
|
+ if (is.null(conf)) {
|
|
|
+ conf <- ruut::get_config()
|
|
|
+ conf$schema <- "teeregister"
|
|
|
+ }
|
|
|
+ # New schema
|
|
|
+ ruut::db_create_new_schema(conf = conf)
|
|
|
+ for (j in 1:length(params)) {
|
|
|
+ # Shapefile (ZIP archive)
|
|
|
+ # Download and save
|
|
|
+ saveTo <- sprintf("%s/shp_%s.zip", tmp_dir, params[j])
|
|
|
+ # 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"))
|
|
|
+ # Multi layer
|
|
|
+ for (i in 1:length(tbl_names)) {
|
|
|
+ print(tbl_names[i])
|
|
|
+ table <- gsub("[^[:alnum:]]", "", gsub("[[:digit:]]", "", tbl_names[i]))
|
|
|
+ conf$table <- sprintf("tmp_%s", table)
|
|
|
+ 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:3301", crs_target = "EPSG:4326",
|
|
|
+ geometry_type = "MULTILINESTRING"
|
|
|
+ )
|
|
|
+ # ------- multilinestring to linestring -----------
|
|
|
+ # ruut::qgis_algorithm_search_by_word(str = "singleparts")
|
|
|
+ algorithm <- "native:multiparttosingleparts"
|
|
|
+ # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
|
|
|
+ # Run algorithm.
|
|
|
+ input <- sprintf(
|
|
|
+ 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' srid=4326 type=MultiLineString checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
|
|
|
+ conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password, conf$schema, conf$table
|
|
|
+ )
|
|
|
+ conf$table <- table
|
|
|
+ output <- sprintf(
|
|
|
+ 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' table=\"%s\".\"%s\" (geometry)',
|
|
|
+ conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password, conf$schema, conf$table
|
|
|
+ )
|
|
|
+ result <- qgisprocess::qgis_run_algorithm(
|
|
|
+ algorithm = algorithm,
|
|
|
+ INPUT = input,
|
|
|
+ OUTPUT = output,
|
|
|
+ .quiet = TRUE
|
|
|
+ )
|
|
|
+ # ------------ drop temp table ---------
|
|
|
+ # Connect to db
|
|
|
+ conn <- ruut::db_connect()
|
|
|
+ q <- sprintf("DROP TABLE IF EXISTS %s.tmp_%s CASCADE;", conf$schema, table)
|
|
|
+ cat("\n-----------------\n")
|
|
|
+ cat(q)
|
|
|
+ DBI::dbExecute(conn, q)
|
|
|
+
|
|
|
+
|
|
|
+ cat(sprintf(
|
|
|
+ "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
|
|
|
+ ls[i], conf$schema, conf$table
|
|
|
+ ))
|
|
|
+ }
|
|
|
+ # Delete unnecessary files.
|
|
|
+ system(sprintf("find %s -type f -not -name '*.zip' -print0 | xargs -0 rm --", tmp_dir))
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|