pk_lisame_polygoonid.R 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. for (intersect in intersect_layers) {
  127. conf$table <- intersect
  128. conf$schema <- obj
  129. intersect_layer <-
  130. ruut::construct_to_gpkg_output_postgres_str(
  131. conf = conf,
  132. geometry_type = "Polygon",
  133. srid = 3301,
  134. checkPrimaryKeyUnicity = TRUE,
  135. key = "id"
  136. )
  137. for (i in 1:nrow(andmed)) {
  138. if (andmed$schema[i] %in% c("teeregister_wfs"))
  139. geom <- "geometry"
  140. else
  141. geom <- "geom"
  142. conf$table <- andmed$table[i]
  143. conf$schema <- andmed$schema[i]
  144. input <- ruut::construct_to_gpkg_output_postgres_str(
  145. conf = conf,
  146. geometry_type = "Polygon",
  147. srid = 3301,
  148. checkPrimaryKeyUnicity = TRUE,
  149. key = "id",
  150. geometry_field = geom
  151. )
  152. # Geomeetria parandamine
  153. # ruut::qgis_algorithm_search_by_word("fix")
  154. algorithm <- "native:fixgeometries"
  155. output <- vars$tmp_gpkg_file_output_1 # ajutine fail
  156. str <-
  157. sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  158. cmd <-
  159. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  160. system(cmd)
  161. # Ühisosa leidmine ja salvestamine ajutiseks failiks.
  162. algorithm <- "native:intersection"
  163. input <- vars$tmp_gpkg_file_input_1
  164. output <- vars$tmp_gpkg_file_output_2 # ajutine fail
  165. str <-
  166. paste0(
  167. "{ 'INPUT' : '",
  168. input,
  169. "', 'INPUT_FIELDS' : [], 'OUTPUT' : '",
  170. output,
  171. "', 'OVERLAY' : '",
  172. intersect_layer,
  173. "', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }"
  174. )
  175. cmd <-
  176. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  177. cat(sprintf("\n%s\n\n", cmd))
  178. system(cmd)
  179. ## 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.
  180. # Eemaldame üleliigsed veerud ja salvestame postgis andmebaasi.
  181. # ruut::qgis_algorithm_search_by_word("Drop")
  182. algorithm <- "native:deletecolumn"
  183. input <- vars$tmp_gpkg_file_input_2
  184. conf$table <-
  185. sprintf("data_a_%s_%s",
  186. strtrim(as.character(andmed$schema[i]), 2),
  187. andmed$table[i])
  188. conf$schema <- obj
  189. output <-
  190. ruut::construct_to_gpkg_output_postgres_str(
  191. conf = conf,
  192. geometry_field = "geom",
  193. geometry_type = NULL,
  194. srid = 3301,
  195. checkPrimaryKeyUnicity = FALSE,
  196. key = "id"
  197. )
  198. cmd <-
  199. sprintf(
  200. "qgis_process run %s --COLUMN='fid' --COLUMN='id_2' --COLUMN='left' --COLUMN='top' --COLUMN='right' --COLUMN='bottom' --INPUT='%s' --OUTPUT='%s' ",
  201. algorithm,
  202. input,
  203. output
  204. )
  205. system(cmd)
  206. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  207. if (andmed$table[i] == "landuse_a") {
  208. parent_table <-
  209. sprintf("data_a_%s_%s",
  210. strtrim(as.character(andmed$schema[i]), 2),
  211. andmed$table[i])
  212. conn <- ruut::db_connect(conf = conf)
  213. landuse_a <-
  214. unique(as.data.frame(sf::st_read(
  215. dsn = conn,
  216. layer = c(conf$schema, parent_table),
  217. as_tibble = T
  218. ))[, c("code", "fclass")])
  219. DBI::dbDisconnect(conn)
  220. for (k in 1:nrow(landuse_a)) {
  221. table_suffix <- landuse_a$fclass[k]
  222. conf$table <- parent_table
  223. conf$schema <- obj
  224. input <-
  225. ruut::construct_to_gpkg_output_postgres_str(
  226. conf = conf,
  227. geometry_type = "MultiPolygon",
  228. srid = 3301,
  229. checkPrimaryKeyUnicity = TRUE,
  230. key = "id"
  231. )
  232. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  233. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  234. output <-
  235. ruut::construct_to_gpkg_output_postgres_str(
  236. conf = conf,
  237. geometry_field = "geom",
  238. geometry_type = "MultiPolygon",
  239. srid = 3301,
  240. checkPrimaryKeyUnicity = FALSE,
  241. key = "id"
  242. )
  243. str <-
  244. paste0(
  245. "{ 'INPUT' : '",
  246. input,
  247. "', 'INTERSECT' : '",
  248. intersect_layer,
  249. "', 'OUTPUT' : '",
  250. output,
  251. "', FIELD : 'code', OPERATOR : 0, VALUE : '",
  252. landuse_a$code[k],
  253. "' }"
  254. )
  255. algorithm <- "native:extractbyattribute"
  256. cmd <-
  257. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  258. cat(sprintf("\n%s\n", cmd))
  259. system(cmd)
  260. }
  261. }
  262. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  263. if (andmed$table[i] == "e_401_hoone_ka") {
  264. parent_table <-
  265. sprintf("data_a_%s_%s",
  266. strtrim(as.character(andmed$schema[i]), 2),
  267. andmed$table[i])
  268. conn <- ruut::db_connect(conf = conf)
  269. alamobjektid <-
  270. unique(as.data.frame(sf::st_read(
  271. dsn = conn,
  272. layer = c(conf$schema, parent_table),
  273. as_tibble = T
  274. ))[, c("kood", "tyyp")])
  275. DBI::dbDisconnect(conn)
  276. for (k in 1:nrow(alamobjektid)) {
  277. table_suffix <- alamobjektid$tyyp[k]
  278. conf$table <- parent_table
  279. conf$schema <- obj
  280. input <-
  281. ruut::construct_to_gpkg_output_postgres_str(
  282. conf = conf,
  283. geometry_type = "MultiPolygon",
  284. srid = 3301,
  285. checkPrimaryKeyUnicity = TRUE,
  286. key = "id"
  287. )
  288. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  289. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  290. output <-
  291. ruut::construct_to_gpkg_output_postgres_str(
  292. conf = conf,
  293. geometry_field = "geom",
  294. geometry_type = "MultiPolygon",
  295. srid = 3301,
  296. checkPrimaryKeyUnicity = FALSE,
  297. key = "id"
  298. )
  299. str <-
  300. paste0(
  301. "{ 'INPUT' : '",
  302. input,
  303. "', 'INTERSECT' : '",
  304. intersect_layer,
  305. "', 'OUTPUT' : '",
  306. output,
  307. "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '",
  308. alamobjektid$tyyp[k],
  309. "' }"
  310. )
  311. algorithm <- "native:extractbyattribute"
  312. cmd <-
  313. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  314. cat(sprintf("\n%s\n", cmd))
  315. system(cmd)
  316. ## ----------- lisame tsentroidid -----------
  317. parent_table <-
  318. sprintf("data_a_%s_%s",
  319. strtrim(as.character(andmed$schema[i]), 2),
  320. andmed$table[i])
  321. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  322. conf$schema <- obj
  323. input <-
  324. ruut::construct_to_gpkg_output_postgres_str(
  325. conf = conf,
  326. geometry_type = "MultiPolygon",
  327. srid = 3301,
  328. checkPrimaryKeyUnicity = TRUE,
  329. key = "id"
  330. )
  331. conf$table <-
  332. sprintf(
  333. "data_p_%s_%s_%s",
  334. strtrim(as.character(andmed$schema[i]), 2),
  335. andmed$table[i],
  336. table_suffix
  337. )
  338. output <-
  339. ruut::construct_to_gpkg_output_postgres_str(
  340. conf = conf,
  341. geometry_field = "geom",
  342. geometry_type = "Point",
  343. srid = 3301,
  344. checkPrimaryKeyUnicity = FALSE,
  345. key = "id"
  346. )
  347. str <-
  348. sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }",
  349. input,
  350. output)
  351. # ruut::qgis_algorithm_search_by_word("centroid")
  352. algorithm <- "native:centroids"
  353. # ruut::qgis_show_help(algorithm = algorithm)
  354. cmd <-
  355. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  356. cat(sprintf("\n%s\n", cmd))
  357. system(cmd)
  358. }
  359. }
  360. }
  361. }
  362. ## Layers list
  363. conf$schema <- obj
  364. ruut::db_schema_tablenames(conf = conf)
  365. sf::st_layers(dsn = vars$tmp_gpkg_file)
  366. }