pk_sellest_alustame_db_loomist.R 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #' Piirkonna postgis andmebaasi loomine
  2. #'
  3. #' Funktsioon loob piirkonna geojoonest ja nimest postgis andmebaasi. Samas lisatakse faili piir ja piiri piirkast. Ülejäänud funktsioonid kasutavad selle funktsiooni poolt loodud objekti nime ja piiri ega piirikasti pole vaja enam teiste funktsioonide rakendamisel lisada.
  4. #'
  5. #' @importFrom magrittr %>%
  6. #' @importFrom rlang .data
  7. #' @param pk Piirkonna geomeetriline joon.
  8. #' @param obj str Objekti nimi. Edaspidi on oluline ainult see nimi. Piirkonna geomeetrilist joont ei ole vaja edaspidi lisada.
  9. #' @param conf A list() of configuration variables. Default values \code{\link[ruut]{get_config}}.
  10. #' @return Andmebaasi loomine koos kihtidega 'piir' ja 'bb' (boundary box, piirikast).
  11. #' @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()]
  12. #' @keywords postgis, boundary box, EPSG:3301
  13. #' @export
  14. #' @examples
  15. #' \dontrun{
  16. #'
  17. #' # read geojson from string:
  18. #' geojson_txt <- paste('{"type":"MultiPoint","coordinates":[[658300,6474800],
  19. #' [658300,6475000],[658700,6476000],[659600,6475000],[659600,6474800],
  20. #' [658300,6474800]]}')
  21. #' x <- sf::read_sf(geojson_txt) %>% sf::st_cast("POLYGON")
  22. #' sf::st_crs(x) <- 3301
  23. #' x
  24. #' sf::st_geometry(x) %>% graphics::plot()
  25. #'
  26. #' obj <- "marja"
  27. #' pk_sellest_alustame_db_loomist(pk = x, obj = obj)
  28. #'
  29. #' # Layers list.
  30. #' ruut::db_schema_tablenames(conf = conf)
  31. #' }
  32. pk_sellest_alustame_db_loomist <-
  33. function(pk = NULL,
  34. obj = NULL,
  35. conf = NULL) {
  36. ## ----------- argumentide vastavuse kontroll -----------
  37. if (is.null(pk) || !sf::st_is_valid(pk)) {
  38. cat("\nPalun kontrolli geomeetrilise kujundi \u00F%igsust.\n")
  39. # return(NULL)
  40. }
  41. if (is.null(obj)) {
  42. cat("\nPalun sisesta objekti nimi.\n")
  43. # return(NULL)
  44. }
  45. vars <- ajutised_muutujad(pk, obj, conf)
  46. obj <- vars$obj
  47. pk <- vars$pk
  48. conf <- vars$conf
  49. # attributes(pk)$names <- attributes(pk)$sf_column <- "geom"
  50. ## --------------------- programmi osa ------------------------
  51. ## 1. Piirjoone salvestamine
  52. pk <- sf::st_transform(pk, 3301)
  53. # Write to gpkg and check validity
  54. sf::write_sf(
  55. pk,
  56. dsn = vars$tmp_gpkg_file,
  57. layer = vars$conf$gpkg_table,
  58. driver = "gpkg",
  59. fid_column_name = "fid",
  60. delete_dsn = T
  61. )
  62. # Geomeetria parandamine
  63. # ruut::qgis_algorithm_search_by_word("fix")
  64. algorithm <- "native:fixgeometries"
  65. input <- vars$tmp_gpkg_file_input_1 # ajutine fail
  66. output <- vars$tmp_gpkg_file_output_2 # ajutine fail
  67. str <-
  68. sprintf("{ 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  69. cmd <-
  70. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  71. system(cmd)
  72. ## Multipart to singlepart
  73. # ruut::qgis_algorithm_search_by_word("singlepart")
  74. algorithm <- "native:multiparttosingleparts"
  75. input <- vars$tmp_gpkg_file_input_2 # ajutine fail
  76. output <- vars$tmp_gpkg_file_output_1 # ajutine fail
  77. str <- sprintf("{
  78. 'INPUT' : '%s',
  79. 'OUTPUT' : '%s'
  80. }", input, output)
  81. cmd <-
  82. ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  83. system(cmd)
  84. # Write to postgis
  85. ruut::db_create_new_schema(conf = conf)
  86. # conn <- ruut::db_connect(conf = conf)
  87. # DBI::dbDisconnect(conn)
  88. # Eemaldame üleliigsed veerud
  89. # ruut::qgis_algorithm_search_by_word("Drop")
  90. algorithm <- "native:deletecolumn"
  91. input <- vars$tmp_gpkg_file_input_1
  92. conf$table <- "a00_piir"
  93. output <-
  94. ruut::construct_to_gpkg_output_postgres_str(
  95. conf = conf,
  96. geometry_field = "geom",
  97. geometry_type = "Polygon",
  98. srid = 3301,
  99. checkPrimaryKeyUnicity = FALSE,
  100. key = "id"
  101. )
  102. cmd <-
  103. sprintf(
  104. "qgis_process run %s --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ",
  105. algorithm,
  106. input,
  107. output
  108. )
  109. system(cmd)
  110. ## 2. Piirikasti salvestamine
  111. ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
  112. pk_attributes <- attributes(pk$geom)
  113. extent <-
  114. sprintf(
  115. "%s,%s,%s,%s [EPSG:3301]",
  116. round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
  117. ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
  118. round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
  119. ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
  120. )
  121. hspacing <-
  122. (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
  123. vspacing <-
  124. (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
  125. conf$table <- "a00_piir_bb"
  126. output <-
  127. ruut::construct_to_gpkg_output_postgres_str(
  128. conf = conf,
  129. geometry_field = "geom",
  130. geometry_type = NULL,
  131. srid = 3301,
  132. checkPrimaryKeyUnicity = FALSE,
  133. key = "id"
  134. )
  135. # ruut::qgis_algorithm_search_by_word(str = "grid")
  136. algorithm <- "native:creategrid"
  137. # cat(ruut::qgis_show_help(algorithm = algorithm))
  138. cmd <-
  139. sprintf(
  140. "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
  141. algorithm,
  142. extent,
  143. hspacing,
  144. vspacing,
  145. output
  146. )
  147. cat(sprintf("\n%s\n", cmd))
  148. system(cmd)
  149. ## 3. epk2t piirikasti konstrueerimine ja salvestamine s.t kuhu mahuvad sisse
  150. ## meie piirkonna epk2t (1x1km) ruudud.
  151. ## 3.1 Leiame piirikastiga seotud epk2t ruudud
  152. # ruut::qgis_algorithm_search_by_word(str = "extract")
  153. algorithm <- "native:extractbylocation"
  154. # cat(ruut::qgis_show_help(algorithm = algorithm))
  155. conf$table <- "epk2t_grid"
  156. conf$schema <- "maaamet"
  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. conf$table <- "a00_piir_bb"
  166. conf$schema <- obj
  167. intersect <-
  168. ruut::construct_to_gpkg_output_postgres_str(
  169. conf = conf,
  170. geometry_type = "Polygon",
  171. srid = 3301,
  172. checkPrimaryKeyUnicity = TRUE,
  173. key = "id"
  174. )
  175. output <- vars$tmp_gpkg_file_output_1 # ajutine fail
  176. str <-
  177. sprintf(
  178. "{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }",
  179. input,
  180. intersect,
  181. output
  182. )
  183. cmd <-
  184. ruut::construct_qgis_output_result_to_better_format(str, algorithm)
  185. system(cmd)
  186. ## 3.2 Ajutise faili abil epk2t piirikasti konstrueerimine
  187. test_layer_obj <-
  188. sf::read_sf(dsn = vars$tmp_gpkg_file, layer = "test_layer_1")
  189. pk_attributes <- attributes(test_layer_obj$geom)
  190. extent <-
  191. sprintf(
  192. "%s,%s,%s,%s [EPSG:3301]",
  193. round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
  194. ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
  195. round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
  196. ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
  197. )
  198. hspacing <-
  199. (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
  200. vspacing <-
  201. (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
  202. conf$table <- "a00_bb2"
  203. output <-
  204. ruut::construct_to_gpkg_output_postgres_str(
  205. conf = conf,
  206. geometry_field = "geom",
  207. geometry_type = NULL,
  208. srid = 3301,
  209. checkPrimaryKeyUnicity = FALSE,
  210. key = "id"
  211. )
  212. # ruut::qgis_algorithm_search_by_word(str = "grid")
  213. algorithm <- "native:creategrid"
  214. # cat(ruut::qgis_show_help(algorithm = algorithm))
  215. cmd <-
  216. sprintf(
  217. "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
  218. algorithm,
  219. extent,
  220. hspacing,
  221. vspacing,
  222. output
  223. )
  224. cat(sprintf("\n%s\n", cmd))
  225. system(cmd)
  226. ## Layers list
  227. conf$schema <- obj
  228. ruut::db_schema_tablenames(conf = conf)
  229. sf::st_layers(dsn = vars$tmp_gpkg_file)
  230. }