소스 검색

Vahekoopia.

Ardo Kubjas 5 년 전
부모
커밋
1dc0981216
3개의 변경된 파일118개의 추가작업 그리고 82개의 파일을 삭제
  1. 82 36
      R/pk_teisendame_jooni.R
  2. 7 6
      R/pk_teisendame_polygoone.R
  3. 29 40
      R/pk_teisendame_punkte.R

+ 82 - 36
R/pk_teisendame_jooni.R

@@ -18,47 +18,31 @@
 #' ruut::db_schema_tablenames(conf = conf)
 #' }
 pk_teisendame_jooni <- function(obj = NULL, conf = NULL) {
-  obj <- gsub(" ", "_", tolower(obj))
+  ## ------------- muutujad ja teisendused ---------------
+  vars <- ajutised_muutujad(pk = NULL, obj, conf)
+  obj <- vars$obj
+  piir <- vars$pk
+  conf <- vars$conf
   ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
-  if (is.null(conf)) {
-    conf <- ruut::get_config()
-    conf$schema <- obj
-  }
-  if (!any("piir" %in% ruut::db_schema_tablenames(conf = conf))) {
+  if (!any("a00_piir" %in% ruut::db_schema_tablenames(conf = conf))) {
     cat("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
     return(NULL)
   }
 
-  ## ------------- muutujad ja teisendused ---------------
-  conn <- ruut::db_connect(conf = conf)
-  try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
-  tmp_dir <- tempdir()
-  tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
-  # system(sprintf("touch %s", tmp_gpkg_file))
-  conf$gpkg_home <- tmp_dir
-  conf$gpkg_file <- obj
-  conf$gpkg_table <- "test_layer"
-  tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
-  tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  conf$gpkg_table <- "test_layer_2"
-  tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
-  tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  # write to gpkg
-  sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
-  # unlink(tmp_gpkg_file)
-
   ## ====================== teisendused =====================
+  conn <- ruut::db_connect(conf = conf)
   # Layers list
-  conf$schema <- obj
   layer_names <- ruut::db_schema_tablenames(conf = conf)
-  layer_names <- layer_names[!layer_names %in% c("bb2", "epk10t_grid", "epk02t_grid", "epk2t_grid")] # c("bb", "bb2", "bb_epk10t_grid", "bb_epk02t_grid", "bb_epk2t_grid", "bb_epk10t", "bb_epk02t")]
+  layer_names <- layer_names[!layer_names %in% c("a00_bb2", "a00_piir_bb", "bb2_epk10t_grid", "bb2_epk02t_grid", "bb2_epk2t_grid")]
+  layer_names <- c("a00_piir", layer_names[grepl("^data_", layer_names)])
   for (layer_name in layer_names) {
     try(ruumiline_obj <- sf::st_read(dsn = conn, layer = c(conf$schema, layer_name)))
     ## Kontlrollime kas geomeetriline objekt on polügoon.
     ## Arvutame sel juhul pindala.
     is_line <- any(grepl("line", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE))
     if (is_line) {
-      ## Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
+
+      ## 1. Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
       ## Peame need pärast ühendama
       # ruut::qgis_algorithm_search_by_word("Intersection")
       algorithm <- "native:intersection"
@@ -67,36 +51,98 @@ pk_teisendame_jooni <- function(obj = NULL, conf = NULL) {
         conf = conf, geometry_type = "MultiLineString", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
       )
-      conf$table <- sprintf("%s", "epk02t_grid")
+      conf$table <- sprintf("%s", "bb2_epk02t_grid")
       overlay <- ruut::construct_to_gpkg_output_postgres_str(
         conf = conf, geometry_type = "Polygon", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
       )
-      output <- tmp_gpkg_file_output # ajutine fail
+      output <- vars$tmp_gpkg_file_output_1 # ajutine fail
       str <- sprintf("{ 'INPUT' : '%s', 'INPUT_FIELDS' : ['fid'], 'OUTPUT' : '%s', 'OVERLAY' : '%s', 'OVERLAY_FIELDS' : [], 'OVERLAY_FIELDS_PREFIX' : '' }", input, output, overlay)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
-      ## Ühendame ruudus olevad pinnad
+
+      ## 2. Ühendame ruudus olevad pinnad
       # ruut::qgis_algorithm_search_by_word("Dissolve")
       algorithm <- "native:dissolve"
-      input <- tmp_gpkg_file_input # ajutine fail
-      output <- tmp_gpkg_file_output_2 # ajutine fail
+      input <- vars$tmp_gpkg_file_input_1 # ajutine fail
+      output <- vars$tmp_gpkg_file_output_2 # ajutine fail
       str <- sprintf("{ 'FIELD' : ['%s'], 'INPUT' : '%s', 'OUTPUT' : '%s' }", "id", input, output)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
-      ## Lisame joonte pikkused
+
+      ## 3. Lisame joonte pikkused
       # ruut::qgis_algorithm_search_by_word("attributes")
       algorithm <- "qgis:exportaddgeometrycolumns"
-      input <- tmp_gpkg_file_input_2 # ajutine fail
-      conf$table <- sprintf("%s_epk02t", layer_name)
+      input <- vars$tmp_gpkg_file_input_2 # ajutine fail
+      epk02t_table_name <- sprintf("epk02t_%s", gsub("^data_", "", layer_name))
+      conf$table <- epk02t_table_name
       output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
       str <- sprintf("{ 'CALC_METHOD' : 0, 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
+
+      ## ------------- TIFF ----------------
+      ## 4. Lisame pindade joonelementide pikkused
+      # ruut::qgis_algorithm_search_by_word("attributes")
+      algorithm <- "native:joinattributesbylocation"
+      conf$table <- "bb2_epk02t_grid"
+      input <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
+      conf$table <- epk02t_table_name
+      join <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiLineString", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
+      output <- vars$tmp_gpkg_file_output_1
+      str <- sprintf("{ 'DISCARD_NONMATCHING' : False, 'INPUT' : '%s', 'JOIN' : '%s', 'JOIN_FIELDS' : ['length'], 'METHOD' : 0, 'OUTPUT' : '%s', 'PREDICATE' : [1], 'PREFIX' : '' }", input, join, output)
+      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      system(cmd)
+
+      ## 5. Asendame NULL väärtustega 0.
+      algorithm <- "qgis:advancedpythonfieldcalculator"
+      input <- vars$tmp_gpkg_file_input_1 # ajutine fail
+      output <- vars$tmp_gpkg_file_output_2
+      cmd <- sprintf("qgis_process run %s --FIELD_NAME='value' --FIELD_TYPE=1 --FIELD_LENGTH=4 --FIELD_PRECISION=2 --GLOBAL=\"def getValue(x):
+    if not x:
+        value = 0
+    else:
+        value = round(x,1)
+    return value\" --FORMULA='value = getValue( <length> )' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
+      system(cmd)
+
+      ## 6. Eemaldame üleliigsed veerud
+      # ruut::qgis_algorithm_search_by_word("Drop ")
+      algorithm <- "native:deletecolumn"
+      input <- vars$tmp_gpkg_file_input_2
+      conf$table <- sprintf("grid_%s", gsub("^data_", "", layer_name))
+      output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "LineString", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "fid")
+      cmd <- sprintf("qgis_process run %s --COLUMN='length' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
+      system(cmd)
+
+      ## 7. tif salvestamine tmp kataloogi
+      grid_layer <- sf::read_sf(dsn = conn, layer = c(conf$schema, "bb2_epk02t_grid"))
+      # sf::st_crs(grid_layer) <- 3301
+      pk_attributes <- attributes(grid_layer$geom)
+      extent <- sprintf("%s,%s,%s,%s [EPSG:3301]", round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["xmax"] / 100)) * 100, round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["ymax"] / 100)) * 100)
+      # ruut::qgis_algorithm_search_by_word("Rasterize")
+      algorithm <- "gdal:rasterize"
+      # ruut::qgis_show_help(algorithm = algorithm)
+      input <- ruut::construct_to_gpkg_output_postgres_str(
+        conf = conf, geometry_type = "MultiLineString", srid = 3301,
+        checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
+      )
+      tif_file_name <- sprintf("tif_%s", gsub("^data_", "", layer_name))
+      output <- sprintf("%s/%s.tif", vars$tmp_dir, tif_file_name)
+      str <- sprintf("{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'value', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }", extent, input, output)
+      cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
+      system(cmd) # Salvestame kataloogi
+
+      ## 8. ----------- TIF TO POSTGIS -------------
+      (cmd <- sprintf(
+        "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
+        conf$password, output, conf$schema, tif_file_name, conf$user, conf$dbname, conf$host, conf$port
+      ))
+      system(cmd)
     }
   }
   ## Layers list
   conf$schema <- obj
   ruut::db_schema_tablenames(conf = conf)
-  sf::st_layers(dsn = tmp_gpkg_file)
+  sf::st_layers(dsn = vars$tmp_gpkg_file)
 }

+ 7 - 6
R/pk_teisendame_polygoone.R

@@ -30,11 +30,12 @@ pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
   }
 
   ## ====================== teisendused =====================
+  conn <- ruut::db_connect(conf = conf)
   # Layers list
   layer_names <- ruut::db_schema_tablenames(conf = conf)
   layer_names <- layer_names[!layer_names %in% c("a00_bb2", "a00_piir_bb", "bb2_epk10t_grid", "bb2_epk02t_grid", "bb2_epk2t_grid")]
+  layer_names <- c("a00_piir", layer_names[grepl("^data_", layer_names)])
   for (layer_name in layer_names) {
-    conn <- ruut::db_connect(conf = conf)
     try(ruumiline_obj <- sf::st_read(dsn = conn, layer = c(conf$schema, layer_name)))
     ## Kontlrollime kas geomeetriline objekt on polügoon.
     ## Arvutame sel juhul pindala.
@@ -86,12 +87,12 @@ pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
       epk02t_table_name <- sprintf("epk02t_%s", gsub("^data_", "", layer_name))
       conf$table <- epk02t_table_name
       output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
-      str <- sprintf("{ 'FIELD_NAME' : 'area_pc', 'FIELD_TYPE' : 1, 'FIELD_LENGTH' : 4, 'FIELD_PRECISION' : 2, 'GLOBAL' : '', 'FORMULA' : 'value = round($geom.area()/10000,4)', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
+      str <- sprintf("{ 'FIELD_NAME' : 'area_pc', 'FIELD_TYPE' : 1, 'FIELD_LENGTH' : 4, 'FIELD_PRECISION' : 2, 'GLOBAL' : '', 'FORMULA' : 'value = round($geom.area()/10000,3)', 'INPUT' : '%s', 'OUTPUT' : '%s' }", input, output)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
       ## ------------- TIFF ----------------
-      ## 5. Lisame pindade pindalad ja perimeetri
+      ## 5. Lisame pindade pindalad ja perimeetri ruutudele
       # ruut::qgis_algorithm_search_by_word("attributes")
       algorithm <- "native:joinattributesbylocation"
       conf$table <- "bb2_epk02t_grid"
@@ -99,7 +100,7 @@ pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
       conf$table <- epk02t_table_name
       join <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "MultiPolygon", srid = 3301, checkPrimaryKeyUnicity = TRUE, key = "id")
       output <- vars$tmp_gpkg_file_output_1
-      str <- sprintf("{ 'DISCARD_NONMATCHING' : False, 'INPUT' : '%s', 'JOIN' : '%s', 'JOIN_FIELDS' : ['area_pc'], 'METHOD' : 0, 'OUTPUT' : '%s', 'PREDICATE' : [0], 'PREFIX' : '' }", input, join, output)
+      str <- sprintf("{ 'DISCARD_NONMATCHING' : False, 'INPUT' : '%s', 'JOIN' : '%s', 'JOIN_FIELDS' : ['area_pc'], 'METHOD' : 0, 'OUTPUT' : '%s', 'PREDICATE' : [1], 'PREFIX' : '' }", input, join, output)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
 
@@ -111,7 +112,7 @@ pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
     if not x:
         value = 0
     else:
-        value = x * 100
+        value = x
     return value\" --FORMULA='value = getValue( <area_pc> )' --INPUT='%s' --OUTPUT='%s' ", algorithm, input, output)
       system(cmd)
 
@@ -144,7 +145,7 @@ pk_teisendame_polygoone <- function(obj = NULL, conf = NULL) {
 
       ## 9. ----------- TIF TO POSTGIS -------------
       (cmd <- sprintf(
-        "export PGPASSWORD=%s && raster2pgsql -d -I %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
+        "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
         conf$password, output, conf$schema, tif_file_name, conf$user, conf$dbname, conf$host, conf$port
       ))
       system(cmd)

+ 29 - 40
R/pk_teisendame_punkte.R

@@ -18,46 +18,32 @@
 #' ruut::db_schema_tablenames(conf = conf)
 #' }
 pk_teisendame_punkte <- function(obj = NULL, conf = NULL) {
-  obj <- gsub(" ", "_", tolower(obj))
+  ## ------------- muutujad ja teisendused ---------------
+  vars <- ajutised_muutujad(pk = NULL, obj, conf)
+  obj <- vars$obj
+  piir <- vars$pk
+  conf <- vars$conf
   ## Konfiguratsiooni muutujale väärtuste omistamine, kui seda pole antud.
-  if (is.null(conf)) {
-    conf <- ruut::get_config()
-    conf$schema <- obj
-  }
-  if (!any("piir" %in% ruut::db_schema_tablenames(conf = conf))) {
+  if (!any("a00_piir" %in% ruut::db_schema_tablenames(conf = conf))) {
     cat("\nAndmebaas loomata. Palun funktsiooniga ruut::pk_sellest_alustame_db_loomist() andmebaasi loomist.\n")
     return(NULL)
   }
 
-  ## ------------- muutujad ja teisendused ---------------
-  conn <- ruut::db_connect(conf = conf)
-  try(piir <- sf::st_read(dsn = conn, layer = c(conf$schema, "piir")))
-  tmp_dir <- tempdir()
-  tmp_gpkg_file <- sprintf("%s/%s.gpkg", tmp_dir, obj)
-  # system(sprintf("touch %s", tmp_gpkg_file))
-  conf$gpkg_home <- tmp_dir
-  conf$gpkg_file <- obj
-  conf$gpkg_table <- "test_layer"
-  tmp_gpkg_file_output <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
-  tmp_gpkg_file_input <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  conf$gpkg_table <- "test_layer_2"
-  tmp_gpkg_file_output_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = F)
-  tmp_gpkg_file_input_2 <- ruut::construct_to_gpkg_output_file_str(conf = conf, is_input_str = T)
-  # write to gpkg
-  sf::write_sf(piir, dsn = tmp_gpkg_file, layer = "piir", driver = "gpkg", fid_column_name = "id", delete_dsn = T)
-  # unlink(tmp_gpkg_file)
-
   ## ====================== teisendused =====================
+  conn <- ruut::db_connect(conf = conf)
   # Layers list
-  conf$schema <- obj
   layer_names <- ruut::db_schema_tablenames(conf = conf)
-  layer_names <- layer_names[!layer_names %in% c("bb2", "epk10t_grid", "epk02t_grid", "epk2t_grid")] # c("bb", "bb2", "bb_epk10t_grid", "bb_epk02t_grid", "bb_epk2t_grid", "bb_epk10t", "bb_epk02t")]
+  layer_names <- layer_names[!layer_names %in% c("a00_bb2", "a00_piir_bb", "bb2_epk10t_grid", "bb2_epk02t_grid", "bb2_epk2t_grid")]
+  layer_names <- c("a00_piir", layer_names[grepl("^data_", layer_names)])
   for (layer_name in layer_names) {
     try(ruumiline_obj <- sf::st_read(dsn = conn, layer = c(conf$schema, layer_name)))
     ## ------------- Punktide arv ruudus ---------------
     ## Kontlrollime kas geomeetriline objekt on punkt.
     (is_point <- any(grepl("point", tolower(attributes(ruumiline_obj$geom)$class), fixed = TRUE)))
     if (is_point) {
+
+      ## 1. Lõikame objekti ruudistikga tükkideks. Ühte ruutu võib jääda mitu tükki.
+      ## Peame need pärast ühendama
       # ruut::qgis_algorithm_search_by_word("Count")
       algorithm <- "native:countpointsinpolygon"
       conf$table <- sprintf("%s", layer_name)
@@ -65,38 +51,41 @@ pk_teisendame_punkte <- function(obj = NULL, conf = NULL) {
         conf = conf, geometry_type = "Point", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
       )
-      conf$table <- sprintf("%s", "epk02t_grid")
+      conf$table <- sprintf("%s", "bb2_epk02t_grid")
       polygons <- ruut::construct_to_gpkg_output_postgres_str(
         conf = conf, geometry_type = "MultiPolygon", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
       )
-      conf$table <- sprintf("%s_epk02t", layer_name)
-      output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Polygon", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
-      str <- sprintf("{ 'CLASSFIELD' : '', 'FIELD' : 'numpoints', 'OUTPUT' : '%s', 'POINTS' : '%s', 'POLYGONS' : '%s', 'WEIGHT' : '' }", output, points, polygons)
+      epk02t_table_name <- sprintf("epk02t_%s", gsub("^data_", "", layer_name))
+      conf$table <- epk02t_table_name
+      output <- ruut::construct_to_gpkg_output_postgres_str(conf = conf, geometry_field = "geom", geometry_type = "Point", srid = 3301, checkPrimaryKeyUnicity = FALSE, key = "id")
+      str <- sprintf("{ 'CLASSFIELD' : '', 'FIELD' : 'value', 'OUTPUT' : '%s', 'POINTS' : '%s', 'POLYGONS' : '%s', 'WEIGHT' : '' }", output, points, polygons)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd)
-      ## ------------- TIFF ----------------
-      grid_layer <- sf::read_sf(dsn = conn, layer = c(conf$schema, "epk02t_grid"))
+
+      ## 2. ------------- TIFF ----------------
+      grid_layer <- sf::read_sf(dsn = conn, layer = c(conf$schema, "bb2_epk02t_grid"))
       # sf::st_crs(grid_layer) <- 3301
       pk_attributes <- attributes(grid_layer$geom)
       extent <- sprintf("%s,%s,%s,%s [EPSG:3301]", round((pk_attributes$bbox["xmin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["xmax"] / 100)) * 100, round((pk_attributes$bbox["ymin"] / 100), digits = 0) * 100, ceiling((pk_attributes$bbox["ymax"] / 100)) * 100)
       # ruut::qgis_algorithm_search_by_word("Rasterize")
       algorithm <- "gdal:rasterize"
       # ruut::qgis_show_help(algorithm = algorithm)
-      conf$table <- sprintf("%s_epk02t", layer_name)
+      conf$table <- epk02t_table_name
       input <- ruut::construct_to_gpkg_output_postgres_str(
         conf = conf, geometry_type = "MultiPolygon", srid = 3301,
         checkPrimaryKeyUnicity = TRUE, key = "id", geometry_field = "geom"
       )
-      output <- sprintf("%s/%s.tif", tmp_dir, layer_name)
-      str <- sprintf("{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'numpoints', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }", extent, input, output)
+      tif_file_name <- sprintf("tif_%s", gsub("^data_", "", layer_name))
+      output <- sprintf("%s/%s.tif", vars$tmp_dir, tif_file_name)
+      str <- sprintf("{ 'BURN' : 0, 'DATA_TYPE' : 5, 'EXTENT' : '%s', 'EXTRA' : '-a_srs epsg:3301', 'FIELD' : 'value', 'HEIGHT' : 100, 'INIT' : None, 'INPUT' : '%s', 'INVERT' : False, 'NODATA' : -1, 'OPTIONS' : '', 'OUTPUT' : '%s', 'UNITS' : 1, 'WIDTH' : 100 }", extent, input, output)
       cmd <- ruut::construct_qgis_output_result_to_better_format(str = str, algorithm = algorithm)
       system(cmd) # Salvestame kataloogi
-      ## ----------- TIF TO POSTGIS -------------
-      conf$schema <- obj
+
+      ## 3. ----------- TIF TO POSTGIS -------------
       (cmd <- sprintf(
-        "export PGPASSWORD=%s && raster2pgsql -I %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
-        conf$password, output, conf$schema, layer_name, conf$user, conf$dbname, conf$host, conf$port
+        "export PGPASSWORD=%s && raster2pgsql -s 3301 -d -I -C -M %s %s.%s_tif | psql -U %s -d %s -h %s -p %s",
+        conf$password, output, conf$schema, tif_file_name, conf$user, conf$dbname, conf$host, conf$port
       ))
       system(cmd)
     }
@@ -104,5 +93,5 @@ pk_teisendame_punkte <- function(obj = NULL, conf = NULL) {
   ## Layers list
   conf$schema <- obj
   ruut::db_schema_tablenames(conf = conf)
-  sf::st_layers(dsn = tmp_gpkg_file)
+  sf::st_layers(dsn = vars$tmp_gpkg_file)
 }