maatriksid.R 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. ## Maatriksid
  2. # 0 - intersect
  3. # 1 - contain
  4. # 2 - disjoint
  5. # 3 - equal
  6. # 4 - touch
  7. # 5 - overlap
  8. # 6 - are within
  9. # 7 - cross
  10. source("functions/delete_existing_variables.R")
  11. library(dplyr)
  12. library(sf)
  13. gpkg_home <- "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa"
  14. obj <- "marja"
  15. dsn <- sprintf("%s/%s.gpkg", gpkg_home, obj)
  16. conf <- ruut::get_config()
  17. conf$gpkg_home <- gpkg_home
  18. conf$gpkg_file <- obj
  19. ruut::gpkg_andmebaasi_kihtide_nimekiri(obj = obj, gpkg_home = gpkg_home)
  20. ## Kõik geomeetrilised objektid GPKG andmebaasist
  21. ## Loeme andmebaasist piiri ja piirikasti.
  22. # Layers list
  23. gpkg_info <- sf::st_layers(dsn = dsn)
  24. layer_names <- gpkg_info$name
  25. for (layer_name in layer_names) {
  26. cat(sprintf("\n%s", layer_name))
  27. assign(layer_name, sf::read_sf(dsn = dsn, layer = layer_name))
  28. }
  29. ## 0-maatriks
  30. # bb_epk02t_grid <- sf::read_sf(dsn = dsn, layer = "bb_epk02t_grid")
  31. rows <- sort(unique(bb_epk02t_grid$bottom), decreasing = T)
  32. cols <- sort(unique(bb_epk02t_grid$left))
  33. length(rows) * length(cols)
  34. ## Nullmaatriks
  35. (m.0 <- matrix(0L, nrow = length(rows), ncol = length(cols), byrow = F)) # 0-maatriks
  36. ## Ruudu id väärtustega maatriks
  37. (m.id <- matrix(bb_epk02t_grid$id, nrow = length(rows), ncol = length(cols), byrow = F))
  38. ## Ruudu vasakpoolse koordinaadi väärtustega maatriks
  39. (m.left <- matrix(bb_epk02t_grid$left, nrow = length(rows), ncol = length(cols), byrow = F))
  40. ## Ruudu parempoolse koordinaadi väärtustega maatriks
  41. (m.right <- matrix(bb_epk02t_grid$right, nrow = length(rows), ncol = length(cols), byrow = F))
  42. ## Ruudu ülemise koordinaadi väärtustega maatriks
  43. (m.top <- matrix(bb_epk02t_grid$top, nrow = length(rows), ncol = length(cols), byrow = F))
  44. ## Ruudu alumise koordinaadi väärtustega maatriks
  45. (m.bottom <- matrix(bb_epk02t_grid$bottom, nrow = length(rows), ncol = length(cols), byrow = F))
  46. ## ----------------------- TRUE/FALSE matrix ----------------------
  47. ## Kas alusruudustik sisaldab meie valitud kihti?
  48. x <- layer_names[11]
  49. x <- "piir"
  50. x <- "bb_e_401_hoone_ka_10"
  51. x <- "bb_muutee"
  52. x <- "bb_aadressandmed"
  53. for (x in layer_names) {
  54. ruumiline_obj <- get(x)
  55. y <- unlist(sf::st_intersects(ruumiline_obj, bb_epk02t_grid, sparse = TRUE))
  56. z <- rep(0, length(bb_epk02t_grid$id))
  57. z[y] <- 1
  58. assign(sprintf("m.%s", x), matrix(z, nrow = length(rows), ncol = length(cols), byrow = F))
  59. get(sprintf("m.%s", x))
  60. ruudustik <- bb_epk02t_grid
  61. ruudustik$value <- z
  62. ## plot to file
  63. png(filename = sprintf("tmp/img/matrix_true_false/%s.png", x))
  64. sf::st_geometry(ruudustik) %>% plot(main = x, sub = "True/False")
  65. sf::st_geometry(ruumiline_obj) %>% plot(add = T, border = "dark red", lwd = 1, col = "blue", pch = 16)
  66. text(x = (ruudustik$left + ruudustik$right) / 2, y = (ruudustik$bottom + ruudustik$top) / 2, labels = as.character(ruudustik$value))
  67. sf::st_geometry(ruudustik) %>% plot(add = T)
  68. dev.off()
  69. ## Punktide arv ruudus
  70. ## Kontlrollime kas geomeetriline objekt on punkt.
  71. (is_point <- any(grepl("point", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE)))
  72. if (is_point) {
  73. # ruut::qgis_algorithm_search_by_word("Count")
  74. algorithm <- "native:countpointsinpolygon"
  75. conf$gpkg_table <- sprintf("%s_numpoints", x)
  76. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F)
  77. conf$gpkg_table <- sprintf("%s", x)
  78. points <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  79. conf$gpkg_table <- sprintf("%s", "bb_epk02t_grid")
  80. polygons <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  81. str <- sprintf("{ 'CLASSFIELD' : '', 'FIELD' : 'value', 'OUTPUT' : '%s', 'POINTS' : '%s', 'POLYGONS' : '%s', 'WEIGHT' : '' }", output, points, polygons)
  82. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  83. system(cmd)
  84. }
  85. ## Kontlrollime kas geomeetriline objekt on polügoon.
  86. ## Arvutame sel juhul pindala.
  87. is_polygon <- any(grepl("polygon", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE))
  88. is_line <- any(grepl("line", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE))
  89. if (is_polygon || is_line) {
  90. ## Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
  91. ## Peame need pärast ühendama
  92. # ruut::qgis_algorithm_search_by_word("Intersection")
  93. algorithm <- "native:intersection"
  94. conf$gpkg_table <- sprintf("%s", x)
  95. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  96. conf$gpkg_table <- sprintf("%s", "test_layer")
  97. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F)
  98. conf$gpkg_table <- sprintf("%s", "bb_epk02t_grid")
  99. overlay <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  100. str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : ['fid'], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }", input, output, overlay)
  101. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  102. system(cmd)
  103. ## Ühendame ruudus olevad pinnad
  104. # ruut::qgis_algorithm_search_by_word("Dissolve")
  105. algorithm <- "native:dissolve"
  106. conf$gpkg_table <- sprintf("%s", "test_layer")
  107. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  108. conf$gpkg_table <- sprintf("%s", "test_layer_2")
  109. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F)
  110. str <- sprintf("{ 'FIELD' : ['%s'], 'INPUT' : '%s', 'OUTPUT' : '%s' }", "id", input, output)
  111. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  112. system(cmd)
  113. # processing.runalg('qgis:addfieldtoattributestable', input_layer, field_name, field_type, field_length, field_precision, output_layer)
  114. # processing.runalg('qgis:advancedpythonfieldcalculator', input_layer, field_name, field_type, field_length, field_precision, global, formula, output_layer)
  115. if (is_polygon) {
  116. ## Lisame pindade pindalad ja perimeetri
  117. # ruut::qgis_algorithm_search_by_word("attributes")
  118. algorithm <- "qgis:exportaddgeometrycolumns"
  119. conf$gpkg_table <- sprintf("%s", "test_layer_2")
  120. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  121. conf$gpkg_table <- sprintf("%s", "test_layer")
  122. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F)
  123. str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  124. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  125. system(cmd)
  126. ## Add field
  127. ## Arvutame pindala protsendi ruudust
  128. # ruut::qgis_algorithm_search_by_word("pythonfield")
  129. algorithm <- "qgis:advancedpythonfieldcalculator"
  130. conf$gpkg_table <- sprintf("%s", "test_layer")
  131. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  132. conf$gpkg_table <- sprintf("%s_area", x)
  133. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F)
  134. str <- sprintf("{ 'FIELD_NAME' : 'area_pc', 'FIELD_TYPE' : 1, 'FIELD_LENGTH' : 4, 'FIELD_PRECISION' : 2, 'GLOBAL' : '', 'FORMULA' : 'value = round($geom.area()/10001,4)', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  135. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  136. system(cmd)
  137. } else {
  138. ## Lisame joonte pikkused
  139. # ruut::qgis_algorithm_search_by_word("attributes")
  140. algorithm <- "qgis:exportaddgeometrycolumns"
  141. conf$gpkg_table <- sprintf("%s", "test_layer_2")
  142. input <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = T)
  143. conf$gpkg_table <- sprintf("%s_length", x)
  144. output <- ruut::construct_to_gpkg_output_file_str(conf = conf, geometry_field = "geom", is_input_str = F)
  145. str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
  146. cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
  147. system(cmd)
  148. }
  149. }
  150. }
  151. ## ----------------- Confusion matrix -------------------
  152. # \url{https://en.wikipedia.org/wiki/Confusion_matrix}
  153. ## ----------------- export matrix -------------------
  154. mat <- matrix(1:10, ncol = 2)
  155. rownames(mat) <- letters[1:5]
  156. colnames(mat) <- LETTERS[1:2]
  157. mat
  158. write.table(mat, file = "test.txt") # keeps the rownames
  159. read.table("test.txt", header = TRUE, row.names = 1) # says first column are rownames
  160. unlink("test.txt")
  161. write.table(mat, file = "test2.txt", row.names = FALSE) # drops the rownames
  162. read.table("test.txt", header = TRUE)
  163. unlink("test2.txt")