.Rhistory 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. library(ruut)
  2. styler:::style_active_file()
  3. styler:::style_active_file()
  4. system("ogr2ogr --long-usage")
  5. styler:::style_active_file()
  6. styler:::style_active_file()
  7. library(ruut)
  8. #'
  9. #' INSPIRE grid map
  10. #'
  11. library(dplyr)
  12. library(ruut)
  13. # Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2
  14. # Temp directory
  15. tmp_dir <- tempdir()
  16. # Download link
  17. url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
  18. # Estonia shapefile (ZIP archive)
  19. map_shapefile <- "Estonia_shapefile.zip"
  20. # Download and sav
  21. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  22. download.file(
  23. url = download_url,
  24. destfile = saveTo, method = "curl", extra = "-L"
  25. )
  26. # Unzip
  27. unzip(saveTo, overwrite = T, exdir = tmp_dir)
  28. # List of files
  29. ls <- list.files(path = tmp_dir, pattern = ".shp")
  30. ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
  31. tbl_names <- strsplit(x = ls, split = ".shp") %>% unlist()
  32. # Export to postgis
  33. schema <- "inspire"
  34. i <- 1
  35. config <- get_config()
  36. config$schema <- schema
  37. # New schema
  38. db_create_new_schema(conf = config)
  39. library(dplyr)
  40. library(ruut)
  41. # Source: https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2
  42. # Temp directory
  43. tmp_dir <- tempdir()
  44. # Download link
  45. url <- "https://www.eea.europa.eu/data-and-maps/data/eea-reference-grids-2/gis-files/estonia-shapefile/download"
  46. # Estonia shapefile (ZIP archive)
  47. map_shapefile <- "Estonia_shapefile.zip"
  48. # Download and sav
  49. saveTo <- sprintf("%s/%s", tmp_dir, map_shapefile)
  50. download.file(
  51. url = url,
  52. destfile = saveTo, method = "curl", extra = "-L"
  53. )
  54. # Unzip
  55. unzip(saveTo, overwrite = T, exdir = tmp_dir)
  56. # List of files
  57. ls <- list.files(path = tmp_dir, pattern = ".shp")
  58. ls_long <- list.files(path = tmp_dir, pattern = ".shp", full.names = T)
  59. tbl_names <- strsplit(x = ls, split = ".shp") %>% unlist()
  60. # Export to postgis
  61. schema <- "inspire"
  62. i <- 1
  63. config <- get_config()
  64. config$schema <- schema
  65. # New schema
  66. db_create_new_schema(conf = config)
  67. print(tbl_names[i])
  68. config$table <- tbl_names[i]
  69. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  70. crs_source <- "-s_srs EPSG:3035"
  71. crs_target <- "-t_srs EPSG:4326"
  72. fid <- "fid"
  73. ruut::copy_shp_to_db(dir = tmp_dir, layer = tbl_names[i], conf = config,
  74. id = fid, crs_source = crs_source, crs_target = crs_target,
  75. geometry_type = 'POLYGON')
  76. dir = tmp_dir; layer = tbl_names[i]; conf = config;
  77. id = fid; crs_source = crs_source; crs_target = crs_target;
  78. geometry_type = 'POLYGON'
  79. length(list.files(path = dir, pattern = sprintf("%s*", layer)))
  80. length(list.files(path = dir, pattern = sprintf("%s*", layer))) > 0
  81. source <- sprintf('"%s" "%s"', dir, layer)
  82. source
  83. cmd <- sprintf(
  84. paste0(
  85. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  86. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  87. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  88. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  89. "%s %s -nlt %s"
  90. ),
  91. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  92. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  93. crs_source, crs_target, geometry_type
  94. )
  95. cmd
  96. system(cmd)
  97. dir = tmp_dir; layer = tbl_names[i]; conf = config;
  98. id = fid; crs_source = crs_source; crs_target = crs_target;
  99. geometry_type = 'PROMOTE_TO_MULTI'
  100. cmd <- sprintf(
  101. paste0(
  102. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  103. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  104. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  105. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  106. "%s %s -nlt %s"
  107. ),
  108. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  109. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  110. crs_source, crs_target, geometry_type
  111. )
  112. cmd
  113. ruut::db_create_new_schema(conf)
  114. system(cmd)
  115. cmd <- sprintf(
  116. paste0(
  117. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  118. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  119. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  120. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  121. "%s %s -nlt PROMOTE_TO_MULTI"
  122. ),
  123. config$dbname, config$host, config$port, config$user, config$password,
  124. config$sslmode, config$schema, source, fid, config$schema, config$table,
  125. crs_source, crs_target
  126. )
  127. cmd
  128. system(cmd)
  129. geometry_type = 'POLYGON'
  130. cmd <- sprintf(
  131. paste0(
  132. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  133. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  134. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  135. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  136. "%s %s -nlt %s"
  137. ),
  138. config$dbname, config$host, config$port, config$user, config$password,
  139. config$sslmode, config$schema, source, fid, config$schema, config$table,
  140. crs_source, crs_target, geometry_type
  141. )
  142. cmd
  143. system(cmd)
  144. system("ogr2ogr --long-usage")
  145. cmd <- sprintf(
  146. paste0(
  147. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  148. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  149. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  150. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  151. "-s_srs%s -t_srs -nlt %s"
  152. ),
  153. config$dbname, config$host, config$port, config$user, config$password,
  154. config$sslmode, config$schema, source, fid, config$schema, config$table,
  155. crs_source, crs_target, geometry_type
  156. )
  157. system(cmd)
  158. cmd <- sprintf(
  159. paste0(
  160. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  161. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  162. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  163. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  164. "%s %s -nlt %s"
  165. ),
  166. config$dbname, config$host, config$port, config$user, config$password,
  167. config$sslmode, config$schema, source, fid, config$schema, config$table,
  168. crs_source, crs_target, geometry_type
  169. )
  170. system(cmd)
  171. crs_target
  172. crs_source
  173. cmd
  174. cmd <- sprintf(
  175. paste0(
  176. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  177. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  178. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  179. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  180. "%s %s -nlt %s"
  181. ),
  182. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  183. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  184. crs_source, crs_target, geometry_type
  185. )
  186. cmd
  187. crs_target
  188. crs_source = "EPSG:4326"
  189. cmd <- sprintf(
  190. paste0(
  191. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  192. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  193. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  194. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  195. "%s %s -nlt %s"
  196. ),
  197. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  198. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  199. crs_source, crs_target, geometry_type
  200. )
  201. source('~/apps/R/packages/ruut/R/copy_shp_to_db.R', echo=TRUE)
  202. cmd
  203. cmd <- sprintf(
  204. paste0(
  205. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  206. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  207. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  208. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  209. "-s_srs %s -t_srs %s -nlt %s"
  210. ),
  211. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  212. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  213. crs_source, crs_target, geometry_type
  214. )
  215. copy_shp_to_db <- function(dir = NULL, layer = NULL, conf = NULL, id = "fid",
  216. crs_source = "EPSG:4326",
  217. crs_target = "EPSG:4326",
  218. geometry_type = "PROMOTE_TO_MULTI") {
  219. # Command 'ogr2ogr' help
  220. # system("ogr2ogr --long-usage")
  221. # Kontrollime kas etteantud kataloogis paiknevad failid
  222. if (length(list.files(path = dir, pattern = sprintf("%s*", layer))) > 0) {
  223. source <- sprintf('"%s" "%s"', dir, layer)
  224. cmd <- sprintf(
  225. paste0(
  226. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  227. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  228. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  229. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  230. "-s_srs %s -t_srs %s -nlt %s"
  231. ),
  232. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  233. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  234. crs_source, crs_target, geometry_type
  235. )
  236. ruut::db_create_new_schema(conf)
  237. Sys.sleep(1)
  238. system(cmd)
  239. } else {
  240. cat("\nEi leitud antud kataloogist faile.")
  241. return(NULL)
  242. }
  243. }
  244. ruut::copy_shp_to_db(dir = tmp_dir, layer = tbl_names[i], conf = config,
  245. id = fid, crs_source = crs_source, crs_target = crs_target,
  246. geometry_type = 'POLYGON')
  247. cmd <- sprintf(
  248. paste0(
  249. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  250. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  251. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  252. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  253. "-s_srs %s -t_srs %s -nlt %s"
  254. ),
  255. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode,
  256. conf$schema, x, id, conf$schema, conf$table, crs_source, crs_target,
  257. geometry_type
  258. )
  259. source <- sprintf('"%s" "%s"', dir, layer)
  260. cmd <- sprintf(
  261. paste0(
  262. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  263. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  264. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  265. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  266. "-s_srs %s -t_srs %s -nlt %s"
  267. ),
  268. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  269. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  270. crs_source, crs_target, geometry_type
  271. )
  272. cmd
  273. system(cmd)
  274. cmd <- sprintf(
  275. paste0(
  276. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  277. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  278. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  279. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  280. "%s %s -nlt %s"
  281. ),
  282. config$dbname, config$host, config$port, config$user, config$password,
  283. config$sslmode, config$schema, source, fid, config$schema, config$table,
  284. crs_source, crs_target, geometry_type
  285. )
  286. system(cmd)
  287. source <- sprintf('"%s" "%s"', tmp_dir, tbl_names[i])
  288. crs_source <- "EPSG:3035"
  289. crs_target <- "EPSG:4326"
  290. fid <- "fid"
  291. cmd <- sprintf(
  292. paste0(
  293. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  294. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  295. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  296. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  297. "-s_srs %s -t_srs %s -nlt %s"
  298. ),
  299. config$dbname, config$host, config$port, config$user, config$password,
  300. config$sslmode, config$schema, source, fid, config$schema, config$table,
  301. crs_source, crs_target, geometry_type
  302. )
  303. system(cmd)
  304. config
  305. conf
  306. config
  307. cmd <- sprintf(
  308. paste0(
  309. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  310. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  311. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  312. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  313. "-s_srs %s -t_srs %s -nlt %s"
  314. ),
  315. config$dbname, config$host, config$port, config$user, config$password,
  316. config$sslmode, config$schema, source, fid, config$schema, config$table,
  317. crs_source, crs_target, geometry_type
  318. )
  319. system(cmd)
  320. cmd <- sprintf(
  321. paste0(
  322. "ogr2ogr -progress --conf PG_USE_COPY YES -f PostgreSQL ",
  323. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  324. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  325. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  326. "-s_srs %s -t_srs %s -nlt %s"
  327. ),
  328. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  329. conf$sslmode, conf$schema, source, id, conf$schema, conf$table,
  330. crs_source, crs_target, geometry_type
  331. )
  332. system(cmd)
  333. cmd <- sprintf(
  334. paste0(
  335. "ogr2ogr -progress --config PG_USE_COPY YES -f PostgreSQL ",
  336. "PG:\" dbname='%s' host=%s port=%d user='%s' password='%s' ",
  337. "sslmode=%s active_schema=%s \" -lco DIM=2 %s -overwrite -nlt GEOMETRY ",
  338. "-lco GEOMETRY_NAME=geometry -lco FID=%s -nln %s.%s ",
  339. "-s_srs %s -t_srs %s -nlt %s"
  340. ),
  341. conf$dbname, conf$host, conf$port, conf$user, conf$password,
  342. conf$sslmode, conf$schema, source, fid, conf$schema, conf$table,
  343. crs_source, crs_target, geometry_type
  344. )
  345. system(cmd)
  346. styler:::style_active_file()
  347. library(ruut)
  348. styler:::style_active_file()
  349. styler:::style_active_file()
  350. library(ruut)
  351. styler:::style_active_file()
  352. styler:::style_active_file()
  353. library(ruut)
  354. styler:::style_active_file()
  355. library(ruut)
  356. styler:::style_active_file()
  357. styler:::style_active_file()
  358. library(ruut)
  359. styler:::style_active_file()
  360. styler:::style_active_file()
  361. styler:::style_active_file()
  362. styler:::style_active_file()
  363. library(ruut)
  364. conf <- ruut::get_config()
  365. conf
  366. styler:::style_active_file()
  367. library(ruut)
  368. library(ruut)
  369. conn <- ruut::db_connect()
  370. config <- get_config()
  371. config$schema <- "public"
  372. conf = config
  373. q <- sprintf("select t.table_name
  374. from information_schema.tables t
  375. where t.table_schema = '%s'
  376. and t.table_type = 'BASE TABLE'
  377. order by t.table_name;", conf$schema)
  378. conf$schema
  379. res <- DBI::dbGetQuery(conn, q)
  380. res
  381. res <- as.array(DBI::dbGetQuery(conn, q))
  382. res <- DBI::dbGetQuery(conn, q)
  383. res[,'table_name']
  384. styler:::style_active_file()
  385. library(ruut)
  386. config
  387. config$schema <- 'maaamet'
  388. db_schema_tablenames(conf = config)
  389. library(ruut)
  390. styler:::style_active_file()
  391. library(ruut)
  392. library(ruut)
  393. styler:::style_active_file()
  394. construct_qgis_output_result_to_beter_format <- function(str = "") {
  395. #' str - Processing algorithm… Algorithm '......' starting…
  396. #' Input parameters: -> kopeerin need loogilistes sulgudes
  397. #' olevad väärtused argumendi "str" väärtuseks.
  398. str <- "{ 'CREATEINDEX' : True, 'DATABASE' : 'Data', 'DROP_STRING_LENGTH' : False, 'ENCODING' : 'UTF-8', 'FORCE_SINGLEPART' : False, 'GEOMETRY_COLUMN' : 'geometry', 'INPUT' : '/data/gpkg/teed/teed_l.gpkg|layername=teed_l', 'LOWERCASE_NAMES' : True, 'OVERWRITE' : True, 'PRIMARY_KEY' : 'fid', 'SCHEMA' : 'data', 'TABLENAME' : 'mikihiir' }"
  399. str <- gsub("'", "", str)
  400. str <- gsub(":", "=", str)
  401. str <- gsub(" ", "", str)
  402. str <- gsub(",", "',", str)
  403. str <- gsub("=", "='", str)
  404. str <- gsub("'True'", "1", str)
  405. str <- gsub("'False'", "0", str)
  406. str <- gsub(",", ",\n", str)
  407. str <- gsub("[{]", "\nalgorithm=algorithm,\n", str)
  408. str <- gsub("[}]", "'\n", str)
  409. str <- gsub("layername='", "layername=", str)
  410. cat(str)
  411. str
  412. }
  413. str <- "{ 'CREATEINDEX' : True, 'DATABASE' : 'Data', 'DROP_STRING_LENGTH' : False, 'ENCODING' : 'UTF-8', 'FORCE_SINGLEPART' : False, 'GEOMETRY_COLUMN' : 'geometry', 'INPUT' : '/data/gpkg/teed/teed_l.gpkg|layername=teed_l', 'LOWERCASE_NAMES' : True, 'OVERWRITE' : True, 'PRIMARY_KEY' : 'fid', 'SCHEMA' : 'data', 'TABLENAME' : 'mikihiir' }"
  414. params <- construct_qgis_output_result_to_beter_format(str = str)
  415. styler:::style_active_file()
  416. styler:::style_active_file()
  417. library(ruut)
  418. library(ruut)
  419. styler:::style_active_file()
  420. library(ruut)
  421. library(ruut)
  422. library(ruut)
  423. styler:::style_active_file()
  424. library(ruut)
  425. library(ruut)
  426. library(ruut)
  427. library(ruut)
  428. styler:::style_active_file()
  429. construct_ogr2ogr_PG_connect_str()
  430. construct_ogr2ogr_PG_connect_str <- function(gpkg_home = "/tmp", gpkg_file = "test", gpkg_table = "layer_one") {
  431. dsn <- sprintf("%s/%s.gpkg", gpkg_home, gpkg_file)
  432. ogr <- sprintf("ogr:dbname='%s'", dsn)
  433. output <- sprintf('%s table=\"%s\" (geometry)', ogr, gpkg_table)
  434. output
  435. }
  436. construct_ogr2ogr_PG_connect_str()
  437. library(ruut)
  438. styler:::style_active_file()
  439. conf <- ruut::get_config()
  440. PG <- sprintf(
  441. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' password=\'%s\' sslmode=%s key=\'id\' srid=3301 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
  442. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode, conf$schema, conf$table
  443. )
  444. PG
  445. styler:::style_active_file()
  446. PG
  447. library(ruut)
  448. conf <- ruut::get_config()
  449. construct_to_gpkg_output_postgres_str <- function(conf = NULL) {
  450. if (is.null(conf)) conf <- ruut::get_config()
  451. PG <- sprintf(
  452. 'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' password=\'%s\' sslmode=%s key=\'id\' srid=3301 checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
  453. conf$dbname, conf$host, conf$port, conf$user, conf$password, conf$sslmode, conf$schema, conf$table
  454. )
  455. PG
  456. }
  457. construct_to_gpkg_output_postgres_str(conf =
  458. conf
  459. )
  460. conf <- ruut::get_config()
  461. conf
  462. library(ruut)
  463. conf <- ruut::get_config()
  464. conf
  465. conf <- ruut::get_config()
  466. construct_to_gpkg_output_file_str(conf = conf)
  467. construct_to_gpkg_output_file_str <- function(conf = conf) {
  468. dsn <- sprintf("%s/%s.gpkg", conf$gpkg_home, conf$gpkg_file)
  469. ogr <- sprintf("ogr:dbname='%s'", dsn)
  470. output <- sprintf('%s table=\"%s\" (geometry)', ogr, conf$gpkg_table)
  471. output
  472. }
  473. construct_to_gpkg_output_file_str(conf = conf)
  474. styler:::style_active_file()
  475. library(ruut)
  476. config <- get_config()
  477. llibrary(ruut)
  478. library(ruut)
  479. config <- get_config()
  480. config$schema <- "data"
  481. db_schema_tablenames(conf = config)
  482. conn <- ruut::db_connect()
  483. q <- sprintf("select t.table_name
  484. from information_schema.tables t
  485. where t.table_schema = '%s'
  486. and t.table_type = 'BASE TABLE'
  487. order by t.table_name;", conf$schema)
  488. res <- DBI::dbGetQuery(conn, q)
  489. conn <- ruut::db_connect()
  490. q <- sprintf("select t.table_name
  491. from information_schema.tables t
  492. where t.table_schema = '%s'
  493. and t.table_type = 'BASE TABLE'
  494. order by t.table_name;", conf$schema)
  495. conf$schema
  496. config <- get_config()
  497. config$schema <- "data"
  498. conf <- get_config()
  499. conf$schema <-
  500. "data"
  501. conn <- ruut::db_connect()
  502. q <- sprintf("select t.table_name
  503. from information_schema.tables t
  504. where t.table_schema = '%s'
  505. and t.table_type = 'BASE TABLE'
  506. order by t.table_name;", conf$schema)
  507. res <- DBI::dbGetQuery(conn, q)
  508. nrow(res)
  509. if(nrow(res)==0) NULL else res[, "table_name"]
  510. if(nrow(res)==0) NULL else res[, "table_name"]
  511. styler:::style_active_file()
  512. library(ruut)