Ardo Kubjas преди 5 години
родител
ревизия
5ae42e2162
променени са 2 файла, в които са добавени 106 реда и са изтрити 15 реда
  1. 7 5
      00_algandmed.R
  2. 99 10
      gpkg/02_piirkonnale_hoonete_lisamine.R

+ 7 - 5
00_algandmed.R

@@ -1,7 +1,9 @@
 #' Andmebaasi loomine ja ühendamine
 #'
-
-str <- ""
+# Processing algorithm…
+# Algorithm 'Intersection' starting…
+# Input parameters:
+str <- "{ 'INPUT' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/valga.gpkg|layername=landuse_a', 'INPUT_FIELDS' : [], 'OUTPUT' : 'TEMPORARY_OUTPUT', 'OVERLAY' : '/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/valga.gpkg|layername=piir', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }"
 ruut::construct_qgis_output_result_to_beter_format(str = str)
 
 source("functions/delete_existing_variables.R")
@@ -37,12 +39,12 @@ for (i in 1:length(objektid)) {
   ## Muutujad: pk - piirkond
   obj <- objektid[i]
   pk <- pk_piir(obj = obj)
-  pk <- sf::st_transform(pk, sf::st_crs('EPSG:3301'))
-  st_crs(pk)
+  pk <- sf::st_transform(pk, sf::st_crs("EPSG:3301"))
+  # sf::st_crs(pk)
 
   sf::st_geometry(pk) %>% plot()
   gpkg_home <- "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa"
-  piirkonnale_ruudustike_lisamine(obj = obj, pk = pk, gpkg_home = gpkg_home)
+  # piirkonnale_ruudustike_lisamine(obj = obj, pk = pk, gpkg_home = gpkg_home)
   piirkonnale_hoonete_lisamine(obj = obj, pk = pk, gpkg_home = gpkg_home)
 }
 

+ 99 - 10
gpkg/02_piirkonnale_hoonete_lisamine.R

@@ -6,7 +6,10 @@
 #' @param pk Piirkonna geomeetriline joon.
 #' @param gpkg_home path Salvestatavate GPKG faili asukoht.
 #'
+#' @examples
+#' \dontrun{
 #'
+#' }
 piirkonnale_hoonete_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tmp") {
   if (is.null(pk) || !sf::st_is_valid(pk)) {
     cat("\nPalun kontrolli geomeetrilise kujundi õigsust.\n")
@@ -22,20 +25,68 @@ piirkonnale_hoonete_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tm
   input_layer <- sprintf("%s|layername=%s", dsn, input_layer_name)
   tmp_gpkg_file <- tempfile(fileext = ".gpkg")
 
-  ## -------------- andmed, mis jäävad piiri sisse -------------------
-  ## ------------------------- algandmed -----------------------------
+  ## ----------------- piiri sisse jäävad polügoonid -------------------
+  ## --------------------------- algandmed -----------------------------
   andmed.df <- data.frame("schema" = character(0), "table" = character(0))
   andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "buildings_a"))
+  # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
+  j <- 1
+  for (j in 1:nrow(andmed.df)) {
+    output_layer_name <- as.character(andmed.df$table[j])
+    input <- sprintf(
+      'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
+      conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
+      andmed.df$schema[j], andmed.df$table[j]
+    )
+    # ruut::qgis_algorithm_search_by_word(str = "extract")
+    algorithm <- "native:extractbylocation"
+    # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
+    result <- qgisprocess::qgis_run_algorithm(
+      algorithm = algorithm,
+      INPUT = input,
+      INTERSECT = input_layer,
+      OUTPUT = tmp_gpkg_file,
+      PREDICATE = c(0) # c(0, 1)
+      # .quiet = TRUE
+    )
+    result
+    # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
+    system(sprintf("ogr2ogr -f GPKG -overwrite  %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
+  }
+
+  ## -------------- piiri -30 meetrise puhvri sisse jäävad polügoonid -------------
+  ## --------------------------------- algandmed ---------------------------------
+  andmed.df <- data.frame("schema" = character(0), "table" = character(0))
   andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
   andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
   # andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "aadressandmed")) # POINT
   andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "asustusyksus"))
   andmed.df <- rbind(andmed.df, data.frame("schema" = "maaamet", "table" = "shp_katastriyksus"))
+  ## Esmalt leiame piirile uhverjoone.
+  # ruut::qgis_algorithm_search_by_word(str = "buffer")
+  algorithm <- "native:buffer"
+  # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
+  result <- qgisprocess::qgis_run_algorithm(
+    algorithm = algorithm,
+    DISSOLVE = 0,
+    DISTANCE = -30,
+    END_CAP_STYLE = 2,
+    INPUT = "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/valga.gpkg|layername=piir",
+    JOIN_STYLE = 2,
+    MITER_LIMIT = 2,
+    OUTPUT = tmp_gpkg_file,
+    SEGMENTS = 5
+    # .quiet = TRUE
+  )
+  result
+  piir_buffer <- sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT"))
+  # st_geometry(pk) %>% plot()
+  # st_geometry(piir_buffer) %>% plot(add=T, col = 'red')
   j <- 1
   for (j in 1:nrow(andmed.df)) {
-    output_layer_name <- andmed.df$table[j]
+    output_layer_name <- as.character(andmed.df$table[j])
     input <- sprintf(
-      'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=3301 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
+      'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
       conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
       andmed.df$schema[j], andmed.df$table[j]
     )
@@ -45,16 +96,54 @@ piirkonnale_hoonete_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tm
     result <- qgisprocess::qgis_run_algorithm(
       algorithm = algorithm,
       INPUT = input,
-      INTERSECT = input_layer,
+      INTERSECT = piir_buffer,
       OUTPUT = tmp_gpkg_file,
-      PREDICATE = c(0)  # c(0, 1)
+      PREDICATE = c(0, 1)
       # .quiet = TRUE
     )
     result
-    assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
-    system(sprintf("ogr2ogr -f GPKG -overwrite  %s %s -nln %s", dsn, tmp_gpkg_file, output_layer_name))
+    # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
+    system(sprintf("ogr2ogr -f GPKG -overwrite  %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
   }
 
+  ## ----------------- piiriga lõigatud polügoonid -------------------
+  ## --------------------------- algandmed ---------------------------
+  andmed.df <- data.frame("schema" = character(0), "table" = character(0))
+  andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "landuse_a"))
+  andmed.df <- rbind(andmed.df, data.frame("schema" = "osm_shp", "table" = "water_a"))
+  j <- 1
+  for (j in 1:nrow(andmed.df)) {
+    output_layer_name <- as.character(andmed.df$table[j])
+    input <- sprintf(
+      'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
+      conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
+      andmed.df$schema[j], andmed.df$table[j]
+    )
+    # ruut::qgis_algorithm_search_by_word(str = "intersect")
+    algorithm <- "native:intersection"
+    # cat(qgisprocess::qgis_show_help(algorithm = algorithm))
+    result <- qgisprocess::qgis_run_algorithm(
+      algorithm = algorithm,
+      INPUT = input,
+      INPUT_FIELDS = "",
+      OVERLAY = sprintf(
+        "/data/gpkg/artiklid/artikkel_210127_valga_matsalu_lahemaa/%s.gpkg|layername=piir",
+        obj
+      ),
+      OVERLAY_FIELDS = "",
+      OVERLAY_FIELDS_PREFIX = "",
+      OUTPUT = tmp_gpkg_file
+      # .quiet = TRUE
+    )
+    result
+    # assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
+    system(sprintf("ogr2ogr -f GPKG -overwrite  %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
+  }
+
+
+
+
+
   # ## ------------ andmed, mis jäävad piirikasti sisse ----------------
   # ## ------------------------- algandmed -----------------------------
   # andmed.df <- data.frame("schema" = character(0), "table" = character(0))
@@ -68,7 +157,7 @@ piirkonnale_hoonete_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tm
   # for (j in 1:nrow(andmed.df)) {
   #   output_layer_name <- andmed.df$table[j]
   #   input <- sprintf(
-  #     'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=3301 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
+  #     'postgres://dbname=\'%s\' host=%s port=%s user=\'%s\' sslmode=%s password=\'%s\' key=\'fid\' srid=4326 type=Polygon checkPrimaryKeyUnicity=\'1\' table=\"%s\".\"%s\" (geometry)',
   #     conf$dbname, conf$host, conf$port, conf$user, conf$sslmode, conf$password,
   #     andmed.df$schema[j], andmed.df$table[j]
   #   )
@@ -85,7 +174,7 @@ piirkonnale_hoonete_lisamine <- function(obj = NULL, pk = NULL, gpkg_home = "/tm
   #   )
   #   result
   #   assign(as.character(andmed.df$table[j]), sf::read_sf(qgisprocess::qgis_output(result, "OUTPUT")))
-  #   system(sprintf("ogr2ogr -f GPKG -overwrite  %s %s -nln %s", dsn, tmp_gpkg_file, output_layer_name))
+  #   system(sprintf("ogr2ogr -f GPKG -overwrite  %s %s -nln %s -t_srs \"EPSG:3301\"", dsn, tmp_gpkg_file, output_layer_name))
   # }
 
   ## ---------------------- vaata layer'id ----------------------