pk_sellest_alustame_db_loomist.R 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. # Eemaldame üleliigsed veerud
  88. # ruut::qgis_algorithm_search_by_word("Drop")
  89. algorithm <- "native:deletecolumn"
  90. input <- vars$tmp_gpkg_file_input_1
  91. conf$table <- "a00_piir"
  92. output <-
  93. ruut::construct_to_gpkg_output_postgres_str(
  94. conf = conf,
  95. geometry_field = "geom",
  96. geometry_type = "Polygon",
  97. srid = 3301,
  98. checkPrimaryKeyUnicity = FALSE,
  99. key = "id"
  100. )
  101. cmd <-
  102. sprintf(
  103. "qgis_process run %s --COLUMN='fid' --INPUT='%s' --OUTPUT='%s' ",
  104. algorithm,
  105. input,
  106. output
  107. )
  108. system(cmd)
  109. ## 2. Piirikasti salvestamine
  110. ## konstrueerime EXTENT, HSPACING ja VSPACING väärtused piirikasti konstrueerimiseks.
  111. pk_attributes <- attributes(pk$geom)
  112. extent <-
  113. sprintf(
  114. "%s,%s,%s,%s [EPSG:3301]",
  115. round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
  116. ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
  117. round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
  118. ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
  119. )
  120. hspacing <-
  121. (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
  122. vspacing <-
  123. (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
  124. conf$table <- "a00_piir_bb"
  125. output <-
  126. ruut::construct_to_gpkg_output_postgres_str(
  127. conf = conf,
  128. geometry_field = "geom",
  129. geometry_type = NULL,
  130. srid = 3301,
  131. checkPrimaryKeyUnicity = FALSE,
  132. key = "id"
  133. )
  134. # ruut::qgis_algorithm_search_by_word(str = "grid")
  135. algorithm <- "native:creategrid"
  136. # cat(ruut::qgis_show_help(algorithm = algorithm))
  137. cmd <-
  138. sprintf(
  139. "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
  140. algorithm,
  141. extent,
  142. hspacing,
  143. vspacing,
  144. output
  145. )
  146. cat(sprintf("\n%s\n", cmd))
  147. system(cmd)
  148. ## 3. epk2t piirikasti konstrueerimine ja salvestamine s.t kuhu mahuvad sisse
  149. ## meie piirkonna epk2t (1x1km) ruudud.
  150. ## 3.1 Leiame piirikastiga seotud epk2t ruudud
  151. # ruut::qgis_algorithm_search_by_word(str = "extract")
  152. algorithm <- "native:extractbylocation"
  153. # cat(ruut::qgis_show_help(algorithm = algorithm))
  154. conf$table <- "epk2t_grid"
  155. conf$schema <- "maaamet"
  156. input <- ruut::construct_to_gpkg_output_postgres_str(
  157. conf = conf,
  158. geometry_type = "Polygon",
  159. srid = 3301,
  160. checkPrimaryKeyUnicity = TRUE,
  161. key = "id",
  162. geometry_field = "geom"
  163. )
  164. conf$table <- "a00_piir_bb"
  165. conf$schema <- obj
  166. intersect <-
  167. ruut::construct_to_gpkg_output_postgres_str(
  168. conf = conf,
  169. geometry_type = "Polygon",
  170. srid = 3301,
  171. checkPrimaryKeyUnicity = TRUE,
  172. key = "id"
  173. )
  174. output <- vars$tmp_gpkg_file_output_1 # ajutine fail
  175. str <-
  176. sprintf(
  177. "{ 'INPUT' : '%s', 'INTERSECT' : '%s', 'OUTPUT' : '%s', 'PREDICATE' : [5,6] }",
  178. input,
  179. intersect,
  180. output
  181. )
  182. cmd <-
  183. ruut::construct_qgis_output_result_to_better_format(str, algorithm)
  184. system(cmd)
  185. ## 3.2 Ajutise faili abil epk2t piirikasti konstrueerimine
  186. test_layer_obj <-
  187. sf::read_sf(dsn = vars$tmp_gpkg_file, layer = "test_layer_1")
  188. pk_attributes <- attributes(test_layer_obj$geom)
  189. extent <-
  190. sprintf(
  191. "%s,%s,%s,%s [EPSG:3301]",
  192. round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100,
  193. ceiling((pk_attributes$bbox["xmax"] / 100)) * 100,
  194. round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100,
  195. ceiling((pk_attributes$bbox["ymax"] / 100)) * 100
  196. )
  197. hspacing <-
  198. (ceiling((pk_attributes$bbox["xmax"] / 100)) * 100) - (round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100)
  199. vspacing <-
  200. (ceiling((pk_attributes$bbox["ymax"] / 100)) * 100) - (round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100)
  201. conf$table <- "a00_bb2"
  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. # ruut::qgis_algorithm_search_by_word(str = "grid")
  212. algorithm <- "native:creategrid"
  213. # cat(ruut::qgis_show_help(algorithm = algorithm))
  214. cmd <-
  215. sprintf(
  216. "qgis_process run %s --CRS=\"EPSG:3301\" --EXTENT=\"%s\" --HOVERLAY=0 --HSPACING=%s --TYPE=2 --VOVERLAY=0 --VSPACING=%s --OUTPUT=\"%s\"",
  217. algorithm,
  218. extent,
  219. hspacing,
  220. vspacing,
  221. output
  222. )
  223. cat(sprintf("\n%s\n", cmd))
  224. system(cmd)
  225. ## Layers list
  226. conf$schema <- obj
  227. ruut::db_schema_tablenames(conf = conf)
  228. sf::st_layers(dsn = vars$tmp_gpkg_file)
  229. }