pk_lisame_polygoonid.R 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. for (k in 1:nrow(landuse_a)) {
  220. table_suffix <- landuse_a$fclass[k]
  221. conf$table <- parent_table
  222. conf$schema <- obj
  223. input <-
  224. ruut::construct_to_gpkg_output_postgres_str(
  225. conf = conf,
  226. geometry_type = "MultiPolygon",
  227. srid = 3301,
  228. checkPrimaryKeyUnicity = TRUE,
  229. key = "id"
  230. )
  231. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  232. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  233. output <-
  234. ruut::construct_to_gpkg_output_postgres_str(
  235. conf = conf,
  236. geometry_field = "geom",
  237. geometry_type = "MultiPolygon",
  238. srid = 3301,
  239. checkPrimaryKeyUnicity = FALSE,
  240. key = "id"
  241. )
  242. str <-
  243. paste0(
  244. "{ 'INPUT' : '",
  245. input,
  246. "', 'INTERSECT' : '",
  247. intersect_layer,
  248. "', 'OUTPUT' : '",
  249. output,
  250. "', FIELD : 'code', OPERATOR : 0, VALUE : '",
  251. landuse_a$code[k],
  252. "' }"
  253. )
  254. algorithm <- "native:extractbyattribute"
  255. cmd <-
  256. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  257. cat(sprintf("\n%s\n", cmd))
  258. system(cmd)
  259. }
  260. }
  261. ## Filtreerime mõned andmetabelid eraldi alamkihtideks
  262. if (andmed$table[i] == "e_401_hoone_ka") {
  263. parent_table <-
  264. sprintf("data_a_%s_%s",
  265. strtrim(as.character(andmed$schema[i]), 2),
  266. andmed$table[i])
  267. conn <- ruut::db_connect(conf = conf)
  268. alamobjektid <-
  269. unique(as.data.frame(sf::st_read(
  270. dsn = conn,
  271. layer = c(conf$schema, parent_table),
  272. as_tibble = T
  273. ))[, c("kood", "tyyp")])
  274. for (k in 1:nrow(alamobjektid)) {
  275. table_suffix <- alamobjektid$tyyp[k]
  276. conf$table <- parent_table
  277. conf$schema <- obj
  278. input <-
  279. ruut::construct_to_gpkg_output_postgres_str(
  280. conf = conf,
  281. geometry_type = "MultiPolygon",
  282. srid = 3301,
  283. checkPrimaryKeyUnicity = TRUE,
  284. key = "id"
  285. )
  286. # sf::st_read(dsn = conn, layer = c(conf$schema, parent_table))
  287. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  288. output <-
  289. ruut::construct_to_gpkg_output_postgres_str(
  290. conf = conf,
  291. geometry_field = "geom",
  292. geometry_type = "MultiPolygon",
  293. srid = 3301,
  294. checkPrimaryKeyUnicity = FALSE,
  295. key = "id"
  296. )
  297. str <-
  298. paste0(
  299. "{ 'INPUT' : '",
  300. input,
  301. "', 'INTERSECT' : '",
  302. intersect_layer,
  303. "', 'OUTPUT' : '",
  304. output,
  305. "', FIELD : 'tyyp', OPERATOR : 0, VALUE : '",
  306. alamobjektid$tyyp[k],
  307. "' }"
  308. )
  309. algorithm <- "native:extractbyattribute"
  310. cmd <-
  311. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  312. cat(sprintf("\n%s\n", cmd))
  313. system(cmd)
  314. ## ----------- lisame tsentroidid -----------
  315. parent_table <-
  316. sprintf("data_a_%s_%s",
  317. strtrim(as.character(andmed$schema[i]), 2),
  318. andmed$table[i])
  319. conf$table <- sprintf("%s_%s", parent_table, table_suffix)
  320. conf$schema <- obj
  321. input <-
  322. ruut::construct_to_gpkg_output_postgres_str(
  323. conf = conf,
  324. geometry_type = "MultiPolygon",
  325. srid = 3301,
  326. checkPrimaryKeyUnicity = TRUE,
  327. key = "id"
  328. )
  329. conf$table <-
  330. sprintf(
  331. "data_p_%s_%s_%s",
  332. strtrim(as.character(andmed$schema[i]), 2),
  333. andmed$table[i],
  334. table_suffix
  335. )
  336. output <-
  337. ruut::construct_to_gpkg_output_postgres_str(
  338. conf = conf,
  339. geometry_field = "geom",
  340. geometry_type = "Point",
  341. srid = 3301,
  342. checkPrimaryKeyUnicity = FALSE,
  343. key = "id"
  344. )
  345. str <-
  346. sprintf("{ 'ALL_PARTS' : False, 'INPUT' : '%s', 'OUTPUT' : '%s' }",
  347. input,
  348. output)
  349. # ruut::qgis_algorithm_search_by_word("centroid")
  350. algorithm <- "native:centroids"
  351. # ruut::qgis_show_help(algorithm = algorithm)
  352. cmd <-
  353. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  354. cat(sprintf("\n%s\n", cmd))
  355. system(cmd)
  356. }
  357. }
  358. }
  359. }
  360. ## Layers list
  361. conf$schema <- obj
  362. ruut::db_schema_tablenames(conf = conf)
  363. sf::st_layers(dsn = vars$tmp_gpkg_file)
  364. }