minu_teed_graaf.R 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #' Riigiteedevõrgu graafide loomine (nodes and links)
  2. #'
  3. #' ETAK (Eesti topograafia andmekogu) \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Eesti-topograafia-andmekogu/Laadi-ETAK-andmed-alla-p609.html} andmete põhjal
  4. #'
  5. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  6. #' @return No output.
  7. #' @seealso [ruut::get_config()], [ruut::copy_shp_to_db()], [estmap::minu_teed_graaf()], [estmap::minu_teed_muud_teed()], [estmap::minu_teed_eesti()]
  8. #' @keywords postgis
  9. #' @export
  10. #' @examples
  11. #' \dontrun{
  12. #'
  13. #' conf <- ruut::get_config()
  14. #' minu_teed_graaf(conf = conf)
  15. #' }
  16. minu_teed_graaf <- function(conf = NULL) {
  17. ## --------------------- muutujad ja teisendused -----------------------------
  18. # source("R/ajutised_muutujad.R")
  19. vars <- ajutised_muutujad(conf = conf)
  20. conf <- vars$conf
  21. # conf$schema <- "minu_teed"
  22. # conf$table <- "zzz"
  23. # output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom")
  24. # conf$schema <- "eesti"
  25. # conf$table <- "e_501_tee_j"
  26. # cat(paste(paste0('"', ruut::db_table_colnames(conf = conf)$column_name, '"'), collapse = ","))
  27. ## ------------------------- 1.1 Kõik teed osadena ---------------------------
  28. ## Kasutame ETAK andmestikku 'e_501_tee_j'.
  29. ## s.o tyyp <= 60.
  30. # ruut::qgis_algorithm_search_by_word("extract")
  31. algorithm <- "native:extractbyattribute"
  32. # ruut::qgis_show_help(algorithm = algorithm)
  33. conf$schema <- "eesti"
  34. conf$table <- "e_501_tee_j"
  35. input <- ruut::construct_to_gpkg_output_postgres_str(
  36. conf = conf, geometry_type = "MultiLineString", srid = 3301,
  37. checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
  38. )
  39. output <- vars$tmp_gpkg_file_output_1
  40. str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output)
  41. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  42. system(cmd)
  43. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  44. ## -------------------- 1.2 Teeme veergude valiku ----------------------------
  45. # cat(paste(paste0('--FIELDS=\'', ruut::db_table_colnames(conf = conf)$column_name, '\''), collapse = " "))
  46. # cat(paste(paste0('--COLUMN=\'', ruut::db_table_colnames(conf = conf)$column_name, '\''), collapse = " "))
  47. # ruut::qgis_algorithm_search_by_word("retain")
  48. # algorithm <- "native:deletecolumn"
  49. algorithm <- "native:retainfields"
  50. # ruut::qgis_show_help(algorithm = algorithm)
  51. input <- vars$tmp_gpkg_file_input_1
  52. output <- vars$tmp_gpkg_file_output_2
  53. cmd <- sprintf("qgis_process run %s --FIELDS='etak_id' --FIELDS='tyyp' --FIELDS='tyyp_t' --FIELDS='tee' --FIELDS='nimetus' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
  54. system(cmd)
  55. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  56. ## ----------------------- 1.3 Graafi genereerimine -----------------------------
  57. # ruut::qgis_algorithm_search_by_word("graph")
  58. algorithm <- "Networks:build_graph"
  59. # ruut::qgis_show_help(algorithm = algorithm)
  60. input <- vars$tmp_gpkg_file_input_2
  61. output <- vars$tmp_gpkg_file_output_3
  62. cmd <- sprintf("qgis_process run %s --IDENT=0 --NOEUDS='%s' --PREFIXE=\"\" --RESEAU='%s' --SENS=\"\" ", algorithm, output, input)
  63. cat(cmd)
  64. system(cmd)
  65. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  66. ## ------------------ 1.4 Copy temp gpkg file to postgis ---------------------
  67. conf$schema <- "minu_teed"
  68. conf$table <- "links"
  69. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  70. cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, vars$tmp_gpkg_file, vars$layer_2, "LINESTRING", conf$schema, conf$table)
  71. system(cmd)
  72. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  73. conf$schema <- "minu_teed"
  74. conf$table <- "nodes"
  75. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  76. cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, vars$tmp_gpkg_file, vars$layer_3, "POINT", conf$schema, conf$table)
  77. system(cmd)
  78. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  79. ## ------------------------- 2.1 Prepare GTFS --------------------------------
  80. # Download link
  81. url <- "http://www.peatus.ee/gtfs/"
  82. # Estonia shapefile (ZIP archive)
  83. map_shapefile <- "gtfs.zip"
  84. # Gtfs directory
  85. gtfs_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "gtfs")
  86. # Download and save
  87. url_download <- sprintf("%s%s", url, map_shapefile)
  88. saveTo <- sprintf("%s/%s", gtfs_dir, map_shapefile)
  89. if (!file.exists(saveTo)) {
  90. utils::download.file(
  91. url = url_download,
  92. destfile = saveTo, method = "curl", extra = "-L"
  93. )
  94. }
  95. # Unzip
  96. utils::unzip(saveTo, overwrite = T, exdir = gtfs_dir)
  97. # ruut::qgis_algorithm_search_by_word("prepare")
  98. algorithm <- "Networks:prepare\\ GTFS"
  99. # ruut::qgis_show_help(algorithm = algorithm)
  100. sub_dir <- "LI"
  101. cmd <- sprintf("qgis_process run %s --PREFIXE_RESEAU='%s' --REP_RESULTAT='%s' --SPLIT_FORMULA=\"[-8:]\" --REP_SOURCE='%s' --UIC=1 ", algorithm, sub_dir, gtfs_dir, gtfs_dir)
  102. cat(cmd)
  103. system(cmd)
  104. cat(sprintf("\n-------------\nLoodi andmebaas '%s/%s'.\n", gtfs_dir, sub_dir))
  105. ## ------------------------- 2.2 GTFS import --------------------------------
  106. # ruut::qgis_algorithm_search_by_word("gtfs")
  107. algorithm <- "Networks:gtfs_import"
  108. # ruut::qgis_show_help(algorithm = algorithm)
  109. # str <- "{ 'DEBUT_PERIODE' : QDate(2021, 3, 20), 'ENCODAGE' : 'utf_8_sig', 'FIN_PERIODE' : QDate(2021, 3, 20), 'PREFIXE' : 'IC', 'PROJ' : QgsCoordinateReferenceSystem('EPSG:2154'), 'REP_GTFS' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/LI', 'REP_SORTIE' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa', 'T1' : QTime(0, 0, 0), 'T2' : QTime(23, 59, 59) }"
  110. # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  111. cmd <- sprintf("qgis_process run %s --DEBUT_PERIODE='%s' --ENCODAGE='utf_8_sig' --FIN_PERIODE='%s' --PREFIXE='gtfs' --PROJ='EPSG:3301' --REP_GTFS='%s/%s' --REP_SORTIE='%s' --T1='00:00:00' --T2='23:59:59' ", algorithm, Sys.Date(), as.Date(Sys.Date()) + 60, gtfs_dir, sub_dir, vars$tmp_dir)
  112. cat(cmd)
  113. system(cmd)
  114. cat(sprintf("\n-------------\nLoodi shp failid kataloogi '%s'.\n", vars$tmp_dir))
  115. # Delete unnecessary files.
  116. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", gtfs_dir, map_shapefile))
  117. ## -------------------- 2.3 Copy shp files to postgis ------------------------
  118. # List of files
  119. ls <- list.files(path = vars$tmp_dir, pattern = ".shp")
  120. ls_long <- list.files(path = vars$tmp_dir, pattern = ".shp", full.names = T)
  121. tbl_names <- tools::file_path_sans_ext(list.files(path = vars$tmp_dir, pattern = "*.shp"))
  122. conf$schema <- "minu_teed"
  123. for (i in 1:length(tbl_names)) {
  124. geometry <- "LINESTRING"
  125. if (tbl_names[i] == "gtfs_stops") geometry <- "POINT"
  126. conf$table <- tbl_names[i]
  127. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  128. cmd <- sprintf("ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL %s -lco DIM=2 %s -overwrite -nlt %s -lco GEOMETRY_NAME=geom -lco FID=id -nln %s.%s -nlt PROMOTE_TO_MULTI", PG, ls_long[i], geometry, conf$schema, conf$table)
  129. system(cmd)
  130. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  131. }
  132. }