| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- #' Riigiteedevõrgu graafide loomine (nodes and links)
- #'
- #' ETAK (Eesti topograafia andmekogu) \url{https://geoportaal.maaamet.ee/est/Ruumiandmed/Eesti-topograafia-andmekogu/Laadi-ETAK-andmed-alla-p609.html} andmete põhjal
- #'
- #' @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()], [estmap::minu_teed_graaf()], [estmap::minu_teed_muud_teed()], [estmap::minu_teed_eesti()]
- #' @keywords postgis
- #' @export
- #' @examples
- #' \dontrun{
- #'
- #' conf <- ruut::get_config()
- #' minu_teed_graaf(conf = conf)
- #' }
- minu_teed_graaf <- function(conf = NULL) {
- ## --------------------- muutujad ja teisendused -----------------------------
- # source("R/ajutised_muutujad.R")
- vars <- ajutised_muutujad(conf = conf)
- conf <- vars$conf
- # conf$schema <- "minu_teed"
- # conf$table <- "zzz"
- # output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom")
- # conf$schema <- "eesti"
- # conf$table <- "e_501_tee_j"
- # cat(paste(paste0('"', ruut::db_table_colnames(conf = conf)$column_name, '"'), collapse = ","))
- ## ------------------------- 1.1 Kõik teed osadena ---------------------------
- ## Kasutame ETAK andmestikku 'e_501_tee_j'.
- ## s.o tyyp <= 60.
- # ruut::qgis_algorithm_search_by_word("extract")
- algorithm <- "native:extractbyattribute"
- # ruut::qgis_show_help(algorithm = algorithm)
- conf$schema <- "eesti"
- conf$table <- "e_501_tee_j"
- input <- ruut::construct_to_gpkg_output_postgres_str(
- conf = conf, geometry_type = "MultiLineString", srid = 3301,
- checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
- )
- output <- vars$tmp_gpkg_file_output_1
- str <- sprintf("{ 'FIELD' : 'tyyp', 'INPUT' : '%s', 'OPERATOR' : 5, 'OUTPUT' : '%s', 'VALUE' : '60' }", input, output)
- cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## -------------------- 1.2 Teeme veergude valiku ----------------------------
- # cat(paste(paste0('--FIELDS=\'', ruut::db_table_colnames(conf = conf)$column_name, '\''), collapse = " "))
- # cat(paste(paste0('--COLUMN=\'', ruut::db_table_colnames(conf = conf)$column_name, '\''), collapse = " "))
- # ruut::qgis_algorithm_search_by_word("retain")
- # algorithm <- "native:deletecolumn"
- algorithm <- "native:retainfields"
- # ruut::qgis_show_help(algorithm = algorithm)
- input <- vars$tmp_gpkg_file_input_1
- output <- vars$tmp_gpkg_file_output_2
- 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)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## ------------------ 1.2 Copy temp gpkg file to postgis ---------------------
- conf$schema <- "minu_teed"
- conf$table <- "teedevork"
- PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
- 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)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
- ## ----------------------- 1.3 Graafi genereerimine -----------------------------
- # ruut::qgis_algorithm_search_by_word("graph")
- algorithm <- "Networks:build_graph"
- # ruut::qgis_show_help(algorithm = algorithm)
- input <- vars$tmp_gpkg_file_input_2
- output <- vars$tmp_gpkg_file_output_3
- cmd <- sprintf("qgis_process run %s --IDENT=0 --NOEUDS='%s' --PREFIXE=\"\" --RESEAU='%s' --SENS=\"\" ", algorithm, output, input)
- cat(cmd)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", output))
- ## ------------------ 1.4 Copy temp gpkg file to postgis ---------------------
- conf$schema <- "minu_teed"
- conf$table <- "links"
- PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
- 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)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
- conf$schema <- "minu_teed"
- conf$table <- "nodes"
- PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
- 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)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
- ## ------------------------- 2.1 Prepare GTFS --------------------------------
- # Download link
- url <- "http://www.peatus.ee/gtfs/"
- # Estonia shapefile (ZIP archive)
- map_shapefile <- "gtfs.zip"
- # Gtfs directory
- gtfs_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "gtfs")
- # Download and save
- url_download <- sprintf("%s%s", url, map_shapefile)
- saveTo <- sprintf("%s/%s", gtfs_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 = gtfs_dir)
- # ruut::qgis_algorithm_search_by_word("prepare")
- algorithm <- "Networks:prepare\\ GTFS"
- # ruut::qgis_show_help(algorithm = algorithm)
- sub_dir <- "LI"
- 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)
- cat(cmd)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s/%s'.\n", gtfs_dir, sub_dir))
- ## ------------------------- 2.2 GTFS import --------------------------------
- # ruut::qgis_algorithm_search_by_word("gtfs")
- algorithm <- "Networks:gtfs_import"
- # ruut::qgis_show_help(algorithm = algorithm)
- # 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) }"
- # cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
- 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)
- cat(cmd)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi shp failid kataloogi '%s'.\n", vars$tmp_dir))
- # Delete unnecessary files.
- system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", gtfs_dir, map_shapefile))
- ## -------------------- 2.3 Copy shp files to postgis ------------------------
- # List of files
- ls <- list.files(path = vars$tmp_dir, pattern = ".shp")
- ls_long <- list.files(path = vars$tmp_dir, pattern = ".shp", full.names = T)
- tbl_names <- tools::file_path_sans_ext(list.files(path = vars$tmp_dir, pattern = "*.shp"))
- conf$schema <- "minu_teed"
- for (i in 1:length(tbl_names)) {
- geometry <- "LINESTRING"
- if (tbl_names[i] == "gtfs_stops") geometry <- "POINT"
- conf$table <- tbl_names[i]
- PG <- ruut::construct_ogr2ogr_PG_connect_str(conf = conf)
- 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)
- system(cmd)
- cat(sprintf("\n-------------\nLoodi andmebaas '%s'.\n", conf$table))
- }
- }
|