.Rhistory 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. ls <- list.files(path = tmp_dir, pattern = ".txt")
  2. ls_long <- list.files(path = tmp_dir, pattern = ".txt", full.names = T)
  3. tbl_names <- tools::file_path_sans_ext(list.files(path = tmp_dir, pattern = "*.txt"))
  4. # Export to postgis
  5. if (is.null(conf)) {
  6. conf <- ruut::get_config()
  7. conf$schema <- "xgtfs"
  8. conf$table <- ""
  9. }
  10. conf$table <- 'shapes'
  11. ruut::db_create_new_schema(conf = conf)
  12. # Postgresql string
  13. pg <- ruut::construct_ogr2ogr_PG_connect_str()
  14. # Connect to db
  15. conn <- ruut::db_connect()
  16. pg
  17. tbl_names
  18. i=8
  19. conf$table <- tbl_names[i]
  20. conf
  21. system(sprintf("mv %s %s/%s.csv", ls_long[i], tmp_dir, tbl_names[i]))
  22. shapes <- utils::read.csv(file = sprintf("%s/%s.csv", tmp_dir, tbl_names[i]))
  23. ## --------------- Points to SpatialLines -----------------
  24. cat("\n----------------\nMarsruutide geomeetria loomine\n")
  25. sl <- shapes %>%
  26. dplyr::arrange(shape_id, shape_pt_sequence) %>%
  27. sf::st_as_sf(coords = c("shape_pt_lon", "shape_pt_lat"), agr = "constant") %>%
  28. # dplyr::group_by(shape_id) %>%
  29. # dplyr::summarise() %>%
  30. sf::st_cast("POINT")
  31. sf::st_crs(sl) <- 4326
  32. sl <- shapes %>%
  33. dplyr::arrange(shape_id, shape_pt_sequence) %>%
  34. sf::st_as_sf(coords = c("shape_pt_lon", "shape_pt_lat"), agr = "constant") %>%
  35. # dplyr::group_by(shape_id) %>%
  36. # dplyr::summarise() %>%
  37. sf::st_cast("POINT")
  38. library(dplyr)
  39. sl <- shapes %>%
  40. dplyr::arrange(shape_id, shape_pt_sequence) %>%
  41. sf::st_as_sf(coords = c("shape_pt_lon", "shape_pt_lat"), agr = "constant") %>%
  42. # dplyr::group_by(shape_id) %>%
  43. # dplyr::summarise() %>%
  44. sf::st_cast("POINT")
  45. sf::st_crs(sl) <- 4326
  46. tabeli_suffix <- "_source"
  47. sf::st_write(
  48. obj = sl, dsn = conn, layer_options = c("GEOMETRY=AS_XY", "OVERWRITE=yes"),
  49. layer = sprintf("%s%s", conf$table, tabeli_suffix)
  50. )
  51. # Change schema
  52. q <- sprintf("
  53. drop table if exists %s.%s%s cascade; \
  54. ALTER TABLE %s%s SET SCHEMA %s", conf$schema, conf$table, tabeli_suffix, conf$table, tabeli_suffix, conf$schema)
  55. cat("\n----------------\nMuudame schema.\n")
  56. cat(q)
  57. DBI::dbExecute(conn, q)
  58. output <- sprintf(
  59. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' table=\"%s\".\"%s\" (geom)',
  60. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  61. conf$schema, conf$table
  62. )
  63. input <- sprintf(
  64. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' srid=4326 type=Point checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s_source\" (geometry)',
  65. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  66. conf$schema, conf$table
  67. )
  68. result <- qgisprocess::qgis_run_algorithm(
  69. algorithm = algorithm,
  70. CLOSE_PATH = 0,
  71. DATE_FORMAT = "",
  72. GROUP_FIELD = "shape_id",
  73. INPUT = input,
  74. ORDER_FIELD = "shape_pt_sequence",
  75. OUTPUT = output,
  76. .quiet = TRUE
  77. )
  78. # qgis_algorithm_search_by_word(str = "pointstopath")
  79. algorithm <- "qgis:pointstopath"
  80. # cat(qgis_show_help(algorithm = algorithm))
  81. # Run algorithm.
  82. ## !!!!!!!!!!!!!!!!!!!! NB! pead muutma pythoni skripti 188 rida.
  83. ## File "/usr/share/qgis/python/plugins/processing/algs/qgis/PointsToPaths.py", line 188, in processAlgorithm
  84. ## Panin sinna mingi faili, sest temp faili ei osanud python kirjutada ja pärast lugeda.
  85. output <- sprintf(
  86. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' table=\"%s\".\"%s\" (geom)',
  87. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  88. conf$schema, conf$table
  89. )
  90. input <- sprintf(
  91. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' srid=4326 type=Point checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s_source\" (geometry)',
  92. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  93. conf$schema, conf$table
  94. )
  95. result <- qgisprocess::qgis_run_algorithm(
  96. algorithm = algorithm,
  97. CLOSE_PATH = 0,
  98. DATE_FORMAT = "",
  99. GROUP_FIELD = "shape_id",
  100. INPUT = input,
  101. ORDER_FIELD = "shape_pt_sequence",
  102. OUTPUT = output,
  103. .quiet = TRUE
  104. )
  105. styler:::style_active_file()
  106. conf = NULL
  107. library(dplyr)
  108. # Temp directory
  109. tmp_dir <- "/tmp/gtfs"
  110. if (!dir.exists(tmp_dir)) {
  111. dir.create(tmp_dir)
  112. }
  113. # Download link
  114. url <- "http://www.peatus.ee/gtfs/"
  115. # Estonia shapefile (ZIP archive)
  116. map_shapefile <- "gtfs.zip"
  117. # Download and save
  118. url_download <- sprintf("%s%s", url, map_shapefile)
  119. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  120. if (!file.exists(saveTo)) {
  121. utils::download.file(
  122. url = url_download,
  123. destfile = saveTo, method = "curl", extra = "-L"
  124. )
  125. }
  126. # Unzip
  127. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  128. # List of files
  129. ls <- list.files(path = tmp_dir, pattern = ".txt")
  130. ls_long <- list.files(path = tmp_dir, pattern = ".txt", full.names = T)
  131. tbl_names <- tools::file_path_sans_ext(list.files(path = tmp_dir, pattern = "*.txt"))
  132. # Export to postgis
  133. if (is.null(conf)) {
  134. conf <- ruut::get_config()
  135. conf$schema <- "gtfs"
  136. conf$table <- ""
  137. }
  138. # Multi layer: paneme kõik csv failid üheks failiks.
  139. if (file.exists(saveTo)) {
  140. ## Export to postgis database.
  141. # New schema
  142. ruut::db_create_new_schema(conf = conf)
  143. # Postgresql string
  144. pg <- ruut::construct_ogr2ogr_PG_connect_str()
  145. # Connect to db
  146. conn <- ruut::db_connect()
  147. # ---------- Copy CSV!!! data to database ----------
  148. for (i in 1:length(tbl_names)) {
  149. # Tabeli nimi
  150. conf$table <- tbl_names[i]
  151. # ogr2ogr oskab csv faili lugeda juhul kui laiend on csv. Muudame.
  152. system(sprintf("mv %s %s/%s.csv", ls_long[i], tmp_dir, tbl_names[i]))
  153. # Laeme eraldi andmed shape muutujasse, et teede geomeetriat leida
  154. if (tbl_names[i] == "shapes") {
  155. shapes <- utils::read.csv(file = sprintf("%s/%s.csv", tmp_dir, tbl_names[i]))
  156. ## --------------- Points to SpatialLines -----------------
  157. cat("\n----------------\nMarsruutide geomeetria loomine\n")
  158. sl <- shapes %>%
  159. dplyr::arrange(shape_id, shape_pt_sequence) %>%
  160. sf::st_as_sf(coords = c("shape_pt_lon", "shape_pt_lat"), agr = "constant") %>%
  161. # dplyr::group_by(shape_id) %>%
  162. # dplyr::summarise() %>%
  163. sf::st_cast("POINT")
  164. sf::st_crs(sl) <- 4326
  165. # # Test plot
  166. # sl %>%
  167. # ggplot2::ggplot(ggplot2::aes(colour = shape_id)) +
  168. # ggplot2::geom_sf()
  169. # plot(sl)
  170. # Write to database
  171. tabeli_suffix <- "_source"
  172. sf::st_write(
  173. obj = sl, dsn = conn, layer_options = c("GEOMETRY=AS_XY", "OVERWRITE=yes"),
  174. layer = sprintf("%s%s", conf$table, tabeli_suffix)
  175. )
  176. # Change schema
  177. q <- sprintf("
  178. drop table if exists %s.%s%s cascade; \
  179. ALTER TABLE %s%s SET SCHEMA %s", conf$schema, conf$table, tabeli_suffix, conf$table, tabeli_suffix, conf$schema)
  180. cat("\n----------------\nMuudame schema.\n")
  181. cat(q)
  182. DBI::dbExecute(conn, q)
  183. # ----------- Point to path teisendus ------------
  184. # Algoritmi juhend!
  185. # qgis_algorithm_search_by_word(str = "pointstopath")
  186. algorithm <- "qgis:pointstopath"
  187. # cat(qgis_show_help(algorithm = algorithm))
  188. # Run algorithm.
  189. ## !!!!!!!!!!!!!!!!!!!! NB! pead muutma pythoni skripti 188 rida.
  190. ## File "/usr/share/qgis/python/plugins/processing/algs/qgis/PointsToPaths.py", line 188, in processAlgorithm
  191. ## Panin sinna mingi faili, sest temp faili ei osanud python kirjutada ja pärast lugeda.
  192. output <- sprintf(
  193. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' table=\"%s\".\"%s\" (geom)',
  194. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  195. conf$schema, conf$table
  196. )
  197. input <- sprintf(
  198. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' srid=4326 type=Point checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s_source\" (geometry)',
  199. conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
  200. conf$schema, conf$table
  201. )
  202. result <- qgisprocess::qgis_run_algorithm(
  203. algorithm = algorithm,
  204. CLOSE_PATH = 0,
  205. DATE_FORMAT = "",
  206. GROUP_FIELD = "shape_id",
  207. INPUT = input,
  208. ORDER_FIELD = "shape_pt_sequence",
  209. OUTPUT = output,
  210. .quiet = TRUE
  211. )
  212. } else {
  213. cmd <- sprintf(
  214. paste0(
  215. "ogr2ogr -f PostgreSQL ",
  216. "%s -lco SCHEMA=%s -lco OVERWRITE=yes -lco FID=%s -nln \"%s\" \"%s/%s.csv\""
  217. ), pg, conf$schema, "fid", conf$table, tmp_dir, tbl_names[i]
  218. )
  219. cat(cmd)
  220. cat("\n\n")
  221. system(cmd)
  222. }
  223. }
  224. # Lisame andmebaasi seosed
  225. q <- sprintf("
  226. ALTER TABLE %s.agency ADD CONSTRAINT agency_un UNIQUE (agency_id); \
  227. ALTER TABLE %s.calendar ADD CONSTRAINT calendar_un UNIQUE (service_id); \
  228. ALTER TABLE %s.fare_attributes ADD CONSTRAINT fare_attributes_un UNIQUE (fare_id); \
  229. ALTER TABLE %s.routes ADD CONSTRAINT routes_un UNIQUE (route_id); \
  230. ALTER TABLE %s.shapes ADD CONSTRAINT shapes_un UNIQUE (shape_id); \
  231. ALTER TABLE %s.stops ADD CONSTRAINT stops_un UNIQUE (stop_id); \
  232. ALTER TABLE %s.trips ADD CONSTRAINT trips_un UNIQUE (trip_id); \
  233. ALTER TABLE %s.fare_rules ADD CONSTRAINT fare_rules_fk FOREIGN KEY (fare_id) REFERENCES %s.fare_attributes(fare_id); \
  234. ALTER TABLE %s.fare_attributes ADD CONSTRAINT fare_attributes_fk FOREIGN KEY (agency_id) REFERENCES %s.agency(agency_id); \
  235. ALTER TABLE %s.routes ADD CONSTRAINT routes_fk FOREIGN KEY (agency_id) REFERENCES %s.agency(agency_id); \
  236. ALTER TABLE %s.calendar_dates ADD CONSTRAINT calendar_dates_fk FOREIGN KEY (service_id) REFERENCES %s.calendar(service_id); \
  237. ALTER TABLE %s.fare_rules ADD CONSTRAINT fare_rules_route_fk FOREIGN KEY (route_id) REFERENCES %s.routes(route_id); \
  238. ALTER TABLE %s.stop_times ADD CONSTRAINT stop_times_stops_fk FOREIGN KEY (stop_id) REFERENCES %s.stops(stop_id); \
  239. ALTER TABLE %s.stop_times ADD CONSTRAINT stop_times_trips_fk FOREIGN KEY (trip_id) REFERENCES %s.trips(trip_id); \
  240. ALTER TABLE %s.trips ADD CONSTRAINT trips_calendar_fk FOREIGN KEY (service_id) REFERENCES %s.calendar(service_id); \
  241. ALTER TABLE %s.trips ADD CONSTRAINT trips_routes_fk FOREIGN KEY (route_id) REFERENCES %s.routes(route_id);", conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema, conf$schema)
  242. cat(q)
  243. DBI::dbExecute(conn, q)
  244. # ogr2ogr help.
  245. # system("ogr2ogr --long-usage")
  246. # ogrinfo help.
  247. # system("ogrinfo --help-general")
  248. # pg <- ruut::construct_ogr2ogr_PG_connect_str()
  249. # system(sprintf("ogrinfo %s xgtfs.trips ", pg))
  250. # system(sprintf("ogrinfo %s xgtfs.trips ", pg))
  251. }
  252. # Delete temp directory
  253. # system(sprintf("rm -rf %s/*.csv", tmp_dir))
  254. }
  255. library(estmap)
  256. library(estmap)
  257. urls <- c("19-1.IBehaviorListener.2-=", "19-1.IBehaviorListener.2-")
  258. url_download <- "https://teeregister.mnt.ee/reet/shpreports?"
  259. url_download <- "https://teeregister.mnt.ee/reet/shpreports"
  260. urls <- c("19-1.IBehaviorListener.2-=", "19-1.IBehaviorListener.2-")
  261. i <- 1
  262. url <- sprintf("%s?%s", url_download, urls[i])
  263. cat(url)
  264. url_download <- "https://teeregister.mnt.ee/reet/shpreports"
  265. urls <- c("0-9.IBehaviorListener.0-RIIGITEE_SOIDUTEED", "19-1.IBehaviorListener.2-", "0-2.IBehaviorListener.2-")
  266. i <- 1
  267. url <- sprintf("%s?%s", url_download, urls[i])
  268. cat(url)
  269. url_download <- "https://teeregister.mnt.ee/reet/shpreports"
  270. urls <- c("0-5.IBehaviorListener.0-:idc", "19-1.IBehaviorListener.2-", "0-2.IBehaviorListener.2-")
  271. i <- 1
  272. url <- sprintf("%s?%s", url_download, urls[i])
  273. cat(url)
  274. system.file("inst/testimiseks/","test_1.R",package = "estmap")
  275. system.file("testimiseks/","test_1.R",package = "estmap")
  276. system.file("testimiseks","test_1.R",package = "estmap")
  277. library(estmap)
  278. tmp_dir <- system.file("tmp","gtfs",package = "estmap")
  279. tmp_dir
  280. system.file("tmp","gtfs",package = "estmap")
  281. tmp_dir <- system.file("tmp","Untitled Document.txt",package = "estmap")
  282. tmp_dir
  283. tmp_dir <- system.file("tmp/gtfs","Untitled Document.txt",package = "estmap")
  284. tmp_dir
  285. tmp_dir <- system.file("gtfs","Untitled Document.txt",package = "estmap")
  286. tmp_dir <- system.file("tmp","gtfs/Untitled Document.txt",package = "estmap")
  287. tmp_dir
  288. tmp_dir <- system.file("tmp","Untitled.txt",package = "estmap")
  289. tmp_dir
  290. tmp_dir <- system.file("undelited","undelited.txt",package = "estmap")
  291. tmp_dir
  292. tmp_dir <- system.file("undelited","undelited.txt",package = "estmap")
  293. tmp_dir
  294. system.file("undelited","test_1.R",package = "estmap")
  295. system.file("undelited","undelited.txt",package = "estmap")
  296. system.file("undelited","Undelited.txt",package = "estmap")
  297. system.file("undelited","Undelited.txt",package = "estmap")
  298. system.file("untitled","test_1.R",package = "estmap")
  299. system.file("untitled","test_1.R",package = "estmap")
  300. system.file("untitled","test_1.R",package = "estmap")
  301. system.file("untitled","test_1.R",package = "estmap")
  302. library(estmap)
  303. system.file("undelited","Undelited.txt",package = "estmap")
  304. system.file("untitled","Undelited.txt",package = "estmap")
  305. system.file("untitled","Untitled.txt",package = "estmap")
  306. system.file("tmp","Untitled.txt",package = "estmap")
  307. system.file("tmp/gtfs","Untitled.txt",package = "estmap")
  308. system.file("tmp/gtfs","Untitled.txt",package = "estmap")
  309. system.file("DESCRIPTION",package = "estmap")
  310. system.file("DESCRIPTION",package = "estmap")
  311. system.file(package = "estmap")
  312. tmp_dir <- sprintf("%s", system.file(package = "estmap"))
  313. tmp_dir
  314. tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "gtfs")
  315. tmp_dir
  316. library(estmap)
  317. library(estmap)
  318. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  319. if (!ans | is.na(ans)) {
  320. cat("\n------------------------\n")
  321. cat("Kaardikihte ei lisatud.")
  322. cat("\n------------------------\n")
  323. return()
  324. }
  325. !ans | is.na(ans)
  326. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  327. ans
  328. !ans | is.na(ans)
  329. is.na(ans)
  330. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  331. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  332. ans
  333. is.na(ans)
  334. !ans | is.na(ans)
  335. ans
  336. ans <- NA
  337. ans
  338. ans <- NA
  339. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  340. cat("\n------------------------\n")
  341. cat("Kaardikihte ei lisatud.")
  342. cat("\n------------------------\n")
  343. return()
  344. }
  345. if (!ans | is.na(ans)) {
  346. cat("\n------------------------\n")
  347. cat("Kaardikihte ei lisatud.")
  348. cat("\n------------------------\n")
  349. return()
  350. }
  351. if (!ans | is.na(ans)) {
  352. cat("\n------------------------\n")
  353. cat("Kaardikihte ei lisatud.")
  354. cat("\n------------------------\n")
  355. # return()
  356. }
  357. ans <- NA
  358. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  359. cat("\n------------------------\n")
  360. cat("Kaardikihte ei lisatud.")
  361. cat("\n------------------------\n")
  362. # return()
  363. }
  364. ans <- NA
  365. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  366. ans
  367. ans <- NA
  368. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  369. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  370. ans
  371. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  372. ans
  373. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  374. ans
  375. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  376. ans
  377. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  378. ans
  379. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  380. ans
  381. ans <- utils::askYesNo("Do you want to import gtfs data into database?")
  382. ans <- utils::askYesNo("Do you want to import gtfs data into database?", default = F)
  383. ans
  384. ans <- utils::askYesNo("Do you want to import gtfs data into database?", default = F)
  385. ans
  386. styler:::style_active_file()
  387. styler:::style_active_file()
  388. library(estmap)
  389. library(estmap)
  390. library(estmap)
  391. conf = NULL
  392. # Temp directory
  393. tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "osm_shp")
  394. if (!dir.exists(tmp_dir)) {
  395. dir.create(tmp_dir)
  396. }
  397. # Download link
  398. url <- "https://download.geofabrik.de/europe"
  399. # Estonia shapefile (ZIP archive)
  400. map_shapefile <- "estonia-latest-free.shp.zip"
  401. # Download and save
  402. url_download <- sprintf("%s/%s", url, map_shapefile)
  403. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  404. if (!file.exists(saveTo)) {
  405. utils::download.file(
  406. url = url_download,
  407. destfile = saveTo, method = "curl", extra = "-L"
  408. )
  409. }
  410. # Unzip
  411. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  412. # List of files
  413. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  414. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  415. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  416. # Export to postgis
  417. if (is.null(conf)) {
  418. conf <- ruut::get_config()
  419. conf$schema <- "osm_shp"
  420. }
  421. # New schema
  422. ruut::db_create_new_schema(conf = conf)
  423. tbl_names
  424. conf = NULL
  425. # Temp directory
  426. tmp_dir <- sprintf("%s/tmp/%s", system.file(package = "estmap"), "osm_shp")
  427. if (!dir.exists(tmp_dir)) {
  428. dir.create(tmp_dir)
  429. }
  430. # Download link
  431. url <- "https://download.geofabrik.de/europe"
  432. # Estonia shapefile (ZIP archive)
  433. map_shapefile <- "estonia-latest-free.shp.zip"
  434. # Download and save
  435. url_download <- sprintf("%s/%s", url, map_shapefile)
  436. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  437. file.exists(saveTo)
  438. if (!file.exists(saveTo)) {
  439. utils::download.file(
  440. url = url_download,
  441. destfile = saveTo, method = "curl", extra = "-L"
  442. )
  443. }
  444. # Unzip
  445. utils::unzip(saveTo, overwrite = T, exdir = tmp_dir)
  446. # List of files
  447. ls <- list.files(path = tmp_dir, pattern = ".dbf")
  448. ls_long <- list.files(path = tmp_dir, pattern = ".dbf", full.names = T)
  449. tbl_names <- unlist(strsplit(x = ls, split = ".dbf"))
  450. # Export to postgis
  451. if (is.null(conf)) {
  452. conf <- ruut::get_config()
  453. conf$schema <- "osm_shp"
  454. }
  455. # New schema
  456. ruut::db_create_new_schema(conf = conf)
  457. map_shapefile
  458. # Multi layer
  459. for (i in 1:length(tbl_names)) {
  460. print(tbl_names[i])
  461. conf$table <- gsub("gis_osm_", "", gsub("_free_1", "", tbl_names[i]))
  462. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  463. ## Export to postgis database.
  464. ruut::copy_shp_to_db(
  465. dir = tmp_dir, layer = tbl_names[i], conf = conf,
  466. id = "fid", crs_source = "EPSG:4326", crs_target = "EPSG:4326",
  467. geometry_type = "PROMOTE_TO_MULTI"
  468. )
  469. cat(sprintf(
  470. "\nShp fail %s kopeeriti POSTGIS andmebaasi %s.%s\n\n",
  471. ls[i], conf$schema, conf$table
  472. ))
  473. }
  474. # Delete unnecessary files.
  475. system(sprintf("find %s -type f -not -name '%s' -print0 | xargs -0 rm --", tmp_dir, map_shapefile))
  476. styler:::style_active_file()
  477. styler:::style_active_file()
  478. library(estmap)
  479. conf = NULL
  480. # Export to postgis
  481. if (is.null(conf)) {
  482. conf <- ruut::get_config()
  483. conf$schema <- "teeregister_wfs"
  484. }
  485. ruut::db_create_new_schema(conf = conf)
  486. ## Teeregistri avaandmete kihtide lugemine
  487. wfs_list <- loe_teeregistri_wfs_kihid()
  488. layer_list <- wfs_list[, "layer"]
  489. wfs_list <- loe_teeregistri_wfs_kihid()
  490. wfs_list <- estmap:::loe_teeregistri_wfs_kihid()
  491. wfs_list
  492. layer_list
  493. layer_list <- wfs_list[, "layer"]
  494. layer_list
  495. installed_tables <- ruut::db_schema_tablenames(conf = conf)
  496. installed_tables
  497. layer_list
  498. i=1
  499. wfs_layer <- layer_list[i]
  500. conf$table <- gsub("ms:", "", wfs_layer)
  501. conf$table
  502. conf$table <- tolower(gsub("ms:", "", wfs_layer))
  503. conf$table
  504. conf$table %in% installed_tables
  505. !conf$table %in% installed_tables
  506. styler:::style_active_file()
  507. i=58
  508. wfs_layer <- layer_list[i]
  509. conf$table <- tolower(gsub("ms:", "", wfs_layer))
  510. !conf$table %in% installed_tables
  511. library(estmap)
  512. teeregister_wfs()