.Rhistory 18 KB

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