minu_teed_graaf.R 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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.2 Copy temp gpkg file to postgis ---------------------
  57. conf$schema <- "minu_teed"
  58. conf$table <- "teedevork"
  59. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  60. 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)
  61. system(cmd)
  62. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  63. ## ----------------------- 1.3 Graafi genereerimine -----------------------------
  64. # ruut::qgis_algorithm_search_by_word("graph")
  65. algorithm <- "Networks:build_graph"
  66. # ruut::qgis_show_help(algorithm = algorithm)
  67. input <- vars$tmp_gpkg_file_input_2
  68. output <- vars$tmp_gpkg_file_output_3
  69. cmd <- sprintf("qgis_process run %s --IDENT=0 --NOEUDS='%s' --PREFIXE=\"\" --RESEAU='%s' --SENS=\"\" ", algorithm, output, input)
  70. cat(cmd)
  71. system(cmd)
  72. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
  73. ## ------------------ 1.4 Copy temp gpkg file to postgis ---------------------
  74. conf$schema <- "minu_teed"
  75. conf$table <- "links"
  76. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  77. 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)
  78. system(cmd)
  79. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  80. conf$schema <- "minu_teed"
  81. conf$table <- "nodes"
  82. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  83. 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)
  84. system(cmd)
  85. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  86. ## ------------------------- 2.1 Prepare GTFS --------------------------------
  87. # Download link
  88. url <- "http://www.peatus.ee/gtfs/"
  89. # Estonia shapefile (ZIP archive)
  90. map_shapefile <- "gtfs.zip"
  91. # Gtfs directory
  92. gtfs_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "gtfs")
  93. # Download and save
  94. url_download <- sprintf("%s%s", url, map_shapefile)
  95. saveTo <- sprintf("%s/%s", gtfs_dir, map_shapefile)
  96. if (!file.exists(saveTo)) {
  97. utils::download.file(
  98. url = url_download,
  99. destfile = saveTo, method = "curl", extra = "-L"
  100. )
  101. }
  102. # Unzip
  103. utils::unzip(saveTo, overwrite = T, exdir = gtfs_dir)
  104. # ruut::qgis_algorithm_search_by_word("prepare")
  105. algorithm <- "Networks:prepare\\ GTFS"
  106. # ruut::qgis_show_help(algorithm = algorithm)
  107. sub_dir <- "LI"
  108. 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)
  109. cat(cmd)
  110. system(cmd)
  111. cat(sprintf("\n-------------\nLoodi andmebaas '%s/%s'.\n", gtfs_dir, sub_dir))
  112. ## ------------------------- 2.2 GTFS import --------------------------------
  113. # ruut::qgis_algorithm_search_by_word("gtfs")
  114. algorithm <- "Networks:gtfs_import"
  115. # ruut::qgis_show_help(algorithm = algorithm)
  116. # 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) }"
  117. # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  118. 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)
  119. cat(cmd)
  120. system(cmd)
  121. cat(sprintf("\n-------------\nLoodi shp failid kataloogi '%s'.\n", vars$tmp_dir))
  122. # Delete unnecessary files.
  123. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", gtfs_dir, map_shapefile))
  124. ## -------------------- 2.3 Copy shp files to postgis ------------------------
  125. # List of files
  126. ls <- list.files(path = vars$tmp_dir, pattern = ".shp")
  127. ls_long <- list.files(path = vars$tmp_dir, pattern = ".shp", full.names = T)
  128. tbl_names <- tools::file_path_sans_ext(list.files(path = vars$tmp_dir, pattern = "*.shp"))
  129. conf$schema <- "minu_teed"
  130. for (i in 1:length(tbl_names)) {
  131. geometry <- "LINESTRING"
  132. if (tbl_names[i] == "gtfs_stops") geometry <- "POINT"
  133. conf$table <- tbl_names[i]
  134. PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
  135. 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)
  136. system(cmd)
  137. cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
  138. }
  139. }