pk_lisame_polygoonid.R 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #' Piirkonnale andmebaasides olevate polügoonide lisamine
  2. #'
  3. #' Etteantud piirkonna geomeetrilise piirjoone ('piir') ja selle joone piirikasti ('bb') järele leitakse nende aladega kaetud polügoonid. Andmed salvestatakse postgis andmebaasi.
  4. #'
  5. #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja lisada.
  6. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  7. #' @return Uute andmebaasi kihtide 'piir_...' ja 'bb_...' loomine.
  8. #' @seealso [sf::st_read()], [sf::write_sf()],[sf::st_transform()],[ruut::pk_sellest_alustame_db_loomist()],[ruut::pk_lisame_ruudustikud()] ,[ruut::pk_lisame_polygoonid()],[ruut::pk_lisame_jooned()],[ruut::pk_lisame_punktid()],[ruut::pk_teisendame_polygoone()],[ruut::pk_teisendame_jooni()],[ruut::pk_teisendame_punkte()]
  9. #' @keywords postgis, boundary box, EPSG:3301
  10. #' @export
  11. #' @examples
  12. #' \dontrun{
  13. #'
  14. #' obj <- "marja"
  15. #' pk_lisame_polygoonid(obj = obj, conf = NULL)
  16. #'
  17. #' # Layers list.
  18. #' ruut::db_schema_tablenames(conf = conf)
  19. #' }
  20. pk_lisame_polygoonid <- function(obj = NULL, conf = NULL) {
  21. ## ------------- muutujad ja teisendused ---------------
  22. vars <- ajutised_muutujad(pk = NULL, obj, conf)
  23. obj <- vars$obj
  24. piir <- vars$pk
  25. conf <- vars$conf
  26. ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
  27. if (!any("a00_piir" %in% ruut::db_schema_tablenames(conf = conf))) {
  28. cat(
  29. "\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n"
  30. )
  31. return(NULL)
  32. }
  33. ## Algorithm
  34. # ruut::qgis_algorithm_search_by_word(str = "extract")
  35. # algorithm <- "native:extractbylocation"
  36. # cat(ruut::qgis_show_help(algorithm = algorithm))
  37. ## -------------------- Loop -----------------------
  38. intersect_layers <- c("a00_bb2") # c("piir", "bb", "bb2")
  39. andmed <-
  40. data.frame("schema" = character(0), "table" = character(0))
  41. andmed <-
  42. rbind(andmed,
  43. data.frame("schema" = "maaamet", "table" = "asustusyksus"))
  44. andmed <-
  45. rbind(andmed,
  46. data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
  47. andmed <-
  48. rbind(andmed,
  49. data.frame("schema" = "osm_shp", "table" = "buildings_a"))
  50. andmed <-
  51. rbind(andmed,
  52. data.frame("schema" = "osm_shp", "table" = "landuse_a"))
  53. andmed <-
  54. rbind(andmed,
  55. data.frame("schema" = "osm_shp", "table" = "natural_a"))
  56. andmed <-
  57. rbind(andmed, data.frame("schema" = "osm_shp", "table" = "places_a"))
  58. andmed <-
  59. rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pofw_a"))
  60. andmed <-
  61. rbind(andmed, data.frame("schema" = "osm_shp", "table" = "pois_a"))
  62. andmed <-
  63. rbind(andmed,
  64. data.frame("schema" = "osm_shp", "table" = "traffic_a"))
  65. andmed <-
  66. rbind(andmed,
  67. data.frame("schema" = "osm_shp", "table" = "transport_a"))
  68. andmed <-
  69. rbind(andmed, data.frame("schema" = "osm_shp", "table" = "water_a"))
  70. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_201_meri_a_dissolved"))
  71. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a_dissolved"))
  72. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a_dissolved"))
  73. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a_dissolved"))
  74. # andmed <- rbind(andmed, data.frame("schema" = "eesti", "table" = "e_306_margala_a_dissolved"))
  75. andmed <-
  76. rbind(andmed,
  77. data.frame("schema" = "eesti", "table" = "e_201_meri_a"))
  78. andmed <-
  79. rbind(andmed,
  80. data.frame("schema" = "eesti", "table" = "e_202_seisuveekogu_a"))
  81. andmed <-
  82. rbind(andmed,
  83. data.frame("schema" = "eesti", "table" = "e_203_vooluveekogu_a"))
  84. andmed <-
  85. rbind(andmed,
  86. data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_a"))
  87. andmed <-
  88. rbind(andmed,
  89. data.frame("schema" = "eesti", "table" = "e_301_muu_kolvik_ka"))
  90. andmed <-
  91. rbind(andmed, data.frame("schema" = "eesti", "table" = "e_302_ou_a"))
  92. andmed <-
  93. rbind(andmed,
  94. data.frame("schema" = "eesti", "table" = "e_303_haritav_maa_a"))
  95. andmed <-
  96. rbind(andmed,
  97. data.frame("schema" = "eesti", "table" = "e_304_lage_a"))
  98. andmed <-
  99. rbind(andmed,
  100. data.frame("schema" = "eesti", "table" = "e_305_puittaimestik_a"))
  101. andmed <-
  102. rbind(andmed,
  103. data.frame("schema" = "eesti", "table" = "e_306_margala_ka"))
  104. andmed <-
  105. rbind(andmed,
  106. data.frame("schema" = "eesti", "table" = "e_307_turbavali_a"))
  107. andmed <-
  108. rbind(andmed,
  109. data.frame("schema" = "eesti", "table" = "e_401_hoone_ka"))
  110. andmed <-
  111. rbind(andmed,
  112. data.frame("schema" = "eesti", "table" = "e_403_muu_rajatis_ka"))
  113. andmed <-
  114. rbind(andmed,
  115. data.frame("schema" = "eesti", "table" = "e_404_maaalune_hoone_ka"))
  116. andmed <-
  117. rbind(andmed,
  118. data.frame("schema" = "eesti", "table" = "e_501_tee_a"))
  119. andmed <-
  120. rbind(
  121. andmed,
  122. data.frame("schema" = "eesti", "table" = "e_505_liikluskorralduslik_rajatis_ka")
  123. )
  124. andmed <-
  125. rbind(andmed, data.frame("schema" = "eesti", "table" = "mullakaart"))
  126. ## Tabelid mis on juba andmebaasis ja mida ümber ei kirjutata.
  127. olemasolevad_tabelid <- ruut::db_schema_tablenames(conf = conf)
  128. for (intersect in intersect_layers) {
  129. conf$table <- intersect
  130. conf$schema <- obj
  131. intersect_layer <-
  132. ruut::construct_to_gpkg_output_postgres_str(
  133. conf = conf,
  134. geometry_type = "Polygon",
  135. srid = 3301,
  136. checkPrimaryKeyUnicity = TRUE,
  137. key = "id"
  138. )
  139. for (i in 1:nrow(andmed)) {
  140. cat("\n\n\n==========================================\n\n")
  141. cat(sprintf("Tabel: %s.%s", andmed[i, "schema"], andmed[i, "table"]))
  142. cat("\n==========================================\n\n")
  143. ## Kui olemasolev tabel eksisteerib andmebaasis, siis jätame arvutused
  144. ## selle tabeliga vahele.
  145. uus_tabel <- sprintf("data_a_%s_%s",
  146. strtrim(as.character(andmed$schema[i]), 2),
  147. andmed$table[i])
  148. if (uus_tabel %in% olemasolevad_tabelid) {
  149. cat("\nTabel on juba andmebaasis olemas.\n")
  150. next
  151. }
  152. ## Osadel tabelitel andmebaasisvõib olla geomeetria välja tähistus 'geometry'.
  153. geom <- "geom"
  154. # if (andmed$schema[i] %in% c("teeregister_wfs")) geom <- "geometry" else geom <- "geom"
  155. conf$table <- andmed$table[i]
  156. conf$schema <- andmed$schema[i]
  157. input <- ruut::construct_to_gpkg_output_postgres_str(
  158. conf = conf,
  159. geometry_type = "Polygon",
  160. srid = 3301,
  161. checkPrimaryKeyUnicity = TRUE,
  162. key = "id",
  163. geometry_field = geom
  164. )
  165. # Geomeetria parandamine
  166. # ruut::qgis_algorithm_search_by_word("fix")
  167. algorithm <- "native:fixgeometries"
  168. output <- vars$tmp_gpkg_file_output_1 # ajutine fail
  169. str <-
  170. sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  171. cmd <-
  172. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  173. system(cmd)
  174. # Ühisosa leidmine ja salvestamine ajutiseks failiks.
  175. algorithm <- "native:intersection"
  176. input <- vars$tmp_gpkg_file_input_1
  177. output <- vars$tmp_gpkg_file_output_2 # ajutine fail
  178. str <-
  179. paste0(
  180. "{ 'INPUT' : '",
  181. input,
  182. "', 'INPUT_FIELDS' : [], 'OUTPUT' : '",
  183. output,
  184. "', 'OVERLAY' : '",
  185. intersect_layer,
  186. "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }"
  187. )
  188. cmd <-
  189. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  190. cat(sprintf("\n%s\n\n", cmd))
  191. system(cmd)
  192. ## Andmebaasi tabeli nimi: 2 esimest tähte tuleb schema nimest millest on andmed võetud, siis '_a_' s.o 'area' mis tähistab polügoone.
  193. # Eemaldame üleliigsed veerud ja salvestame postgis andmebaasi.
  194. # ruut::qgis_algorithm_search_by_word("Drop")
  195. algorithm <- "native:deletecolumn"
  196. input <- vars$tmp_gpkg_file_input_2
  197. conf$table <-
  198. sprintf("data_a_%s_%s",
  199. strtrim(as.character(andmed$schema[i]), 2),
  200. andmed$table[i])
  201. conf$schema <- obj
  202. output <-
  203. ruut::construct_to_gpkg_output_postgres_str(
  204. conf = conf,
  205. geometry_field = "geom",
  206. geometry_type = NULL,
  207. srid = 3301,
  208. checkPrimaryKeyUnicity = FALSE,
  209. key = "id"
  210. )
  211. cmd <-
  212. sprintf(
  213. "qgis_process run %s --COLUMN='fid' --COLUMN='id_2' --COLUMN='left' --COLUMN='top' --COLUMN='right' --COLUMN='bottom' --INPUT='%s' --OUTPUT='%s' ",
  214. algorithm,
  215. input,
  216. output
  217. )
  218. system(cmd)
  219. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  220. if (andmed$table[i] == "landuse_a") {
  221. parent_table <-
  222. sprintf("data_a_%s_%s",
  223. strtrim(as.character(andmed$schema[i]), 2),
  224. andmed$table[i])
  225. conn <- ruut::db_connect(conf = conf)
  226. landuse_a <-
  227. unique(as.data.frame(sf::st_read(
  228. dsn = conn,
  229. layer = c(conf$schema, parent_table),
  230. as_tibble = T
  231. ))[, c("code", "fclass")])
  232. DBI::dbDisconnect(conn)
  233. for (k in 1:nrow(landuse_a)) {
  234. table_suffix <- landuse_a$fclass[k]
  235. conf$table <- parent_table
  236. conf$schema <- obj
  237. input <-
  238. ruut::construct_to_gpkg_output_postgres_str(
  239. conf = conf,
  240. geometry_type = "MultiPolygon",
  241. srid = 3301,
  242. checkPrimaryKeyUnicity = TRUE,
  243. key = "id"
  244. )
  245. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  246. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  247. output <-
  248. ruut::construct_to_gpkg_output_postgres_str(
  249. conf = conf,
  250. geometry_field = "geom",
  251. geometry_type = "MultiPolygon",
  252. srid = 3301,
  253. checkPrimaryKeyUnicity = FALSE,
  254. key = "id"
  255. )
  256. str <-
  257. paste0(
  258. "{ 'INPUT' : '",
  259. input,
  260. "', 'INTERSECT' : '",
  261. intersect_layer,
  262. "', 'OUTPUT' : '",
  263. output,
  264. "', FIELD : 'code', OPERATOR : 0, VALUE : '",
  265. landuse_a$code[k],
  266. "' }"
  267. )
  268. algorithm <- "native:extractbyattribute"
  269. cmd <-
  270. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  271. cat(sprintf("\n%s\n", cmd))
  272. system(cmd)
  273. }
  274. }
  275. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  276. if (andmed$table[i] == "e_401_hoone_ka") {
  277. parent_table <-
  278. sprintf("data_a_%s_%s",
  279. strtrim(as.character(andmed$schema[i]), 2),
  280. andmed$table[i])
  281. conn <- ruut::db_connect(conf = conf)
  282. alamobjektid <-
  283. unique(as.data.frame(sf::st_read(
  284. dsn = conn,
  285. layer = c(conf$schema, parent_table),
  286. as_tibble = T
  287. ))[, c("kood", "tyyp")])
  288. DBI::dbDisconnect(conn)
  289. for (k in 1:nrow(alamobjektid)) {
  290. table_suffix <- alamobjektid$tyyp[k]
  291. conf$table <- parent_table
  292. conf$schema <- obj
  293. input <-
  294. ruut::construct_to_gpkg_output_postgres_str(
  295. conf = conf,
  296. geometry_type = "MultiPolygon",
  297. srid = 3301,
  298. checkPrimaryKeyUnicity = TRUE,
  299. key = "id"
  300. )
  301. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  302. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  303. output <-
  304. ruut::construct_to_gpkg_output_postgres_str(
  305. conf = conf,
  306. geometry_field = "geom",
  307. geometry_type = "MultiPolygon",
  308. srid = 3301,
  309. checkPrimaryKeyUnicity = FALSE,
  310. key = "id"
  311. )
  312. str <-
  313. paste0(
  314. "{ 'INPUT' : '",
  315. input,
  316. "', 'INTERSECT' : '",
  317. intersect_layer,
  318. "', 'OUTPUT' : '",
  319. output,
  320. "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '",
  321. alamobjektid$tyyp[k],
  322. "' }"
  323. )
  324. algorithm <- "native:extractbyattribute"
  325. cmd <-
  326. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  327. cat(sprintf("\n%s\n", cmd))
  328. system(cmd)
  329. ## ----------- lisame tsentroidid -----------
  330. parent_table <-
  331. sprintf("data_a_%s_%s",
  332. strtrim(as.character(andmed$schema[i]), 2),
  333. andmed$table[i])
  334. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  335. conf$schema <- obj
  336. input <-
  337. ruut::construct_to_gpkg_output_postgres_str(
  338. conf = conf,
  339. geometry_type = "MultiPolygon",
  340. srid = 3301,
  341. checkPrimaryKeyUnicity = TRUE,
  342. key = "id"
  343. )
  344. conf$table <-
  345. sprintf(
  346. "data_p_%s_%s_%s",
  347. strtrim(as.character(andmed$schema[i]), 2),
  348. andmed$table[i],
  349. table_suffix
  350. )
  351. output <-
  352. ruut::construct_to_gpkg_output_postgres_str(
  353. conf = conf,
  354. geometry_field = "geom",
  355. geometry_type = "Point",
  356. srid = 3301,
  357. checkPrimaryKeyUnicity = FALSE,
  358. key = "id"
  359. )
  360. str <-
  361. sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }",
  362. input,
  363. output)
  364. # ruut::qgis_algorithm_search_by_word("centroid")
  365. algorithm <- "native:centroids"
  366. # ruut::qgis_show_help(algorithm = algorithm)
  367. cmd <-
  368. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  369. cat(sprintf("\n%s\n", cmd))
  370. system(cmd)
  371. }
  372. }
  373. }
  374. }
  375. ## Layers list
  376. conf$schema <- obj
  377. ruut::db_schema_tablenames(conf = conf)
  378. sf::st_layers(dsn = vars$tmp_gpkg_file)
  379. }