save_files.r 4.18 KB
Newer Older
1
2
3
4
5
6
#' Save a run
#'
#' Saves the run object for a step
#'
#' @param outPath output path
#' @param step step number
7
#' @param run1 the object to be saved
8
9
#'
#' @export
10
11
save_run <- function(outPath, step, run1) {
  save(run1, file = paste(outPath, paste("Run", step, sep = ""), sep = ""))
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
}

#' Save Tif
#'
#' Saves the classification result into a tiff.
#'
#' @param outPath output path
#' @param step step number
#' @param classNames the names of the classes
#' @param index index of the class
#' @param raster the raster values
#' @param overwrite overwrite files or not
#'
#' @export
save_class_tiff <- function(outPath, step, classNames, index, raster, overwrite) {
  raster::writeRaster(
    raster,
    filename = paste(outPath,
                     paste("step_",
                           step,
                           paste("_", classNames[index], sep = ""),
                           ".tif",
                           sep = ""),
                     sep = ""),
    format = "GTiff",
    overwrite = overwrite)
}

#' Save KML
#'
#' Saves the classification result into a KML file.
#'
#' @param outPath output path
#' @param step step number
#' @param raster the raster values
#' @param overwrite overwrite files or not
#'
#' @export
save_kml <- function(outPath, step, raster, overwrite) {
  kml <- raster::projectRaster(raster,
                               crs = "+proj=longlat +datum=WGS84",
                               method = 'ngb')
  
55
  raster::KML(kml, paste(outPath, paste("step_", step, sep = ""), sep = ""), overwrite = overwrite, zip = '')
56
}
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133


#' Save Sample Points
#'
#' Saves the reference sample points
#'
#' @param in_path file path (character) for results of habitat type sampling and probability mapping (same as outPath from function multi_Class_Sampling)
#' @param step step number (numeric)
#' @param class_name name (character) of habitat type for which samples should be selected
#' @param output_format format (character) of output; whether shp (default) or geojson
#' @param ref_samples list of reference sample points
#' @param ref_switch vector with switch values
#' @param dummy_raster raster with classified pixels marked as 1
#'
#' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
#' 1) Point Shape/GeoJSON represents pixel that belong to selected habitat type and can be used as reference for further model building.
#'    ESRI shapefiles have the same CRS as the input raster. GeoJSON files are in the standard CRS of GeoJSON (EPSG:4326).
#'
#'
#' @export
saveSamplePoints <-
  function(in_path,
           step,
           class_name,
           output_format = c("shp", "geojson"),
           ref_samples,
           ref_switch,
           dummy_raster) {
    collect <- list()
    j <- 0
    
    ###extract only class samples
    for (i in 1:length(ref_samples)) {
      if (length(dim(ref_samples[[i]])) != 0)
      {
        if (is.na(ref_switch[i]) == F) {
          j = j + 1
          collect[[j]] <-
            ref_samples[[i]][which(ref_samples[[i]]@data == 1), ]
        } else
        {
          j = j + 1
          collect[[j]] <-
            ref_samples[[i]][which(ref_samples[[i]]@data == 2), ]
        }
      }
    }
    
    result <- do.call(rbind, collect)
    
    res <- raster::extract(dummy_raster, result)
    if (length(which(is.na(res))) > 0) {
      res <- result[-which(is.na(res)), ]
    }
    raster::crs(res) <- raster::crs(dummy_raster)
    output_format <- match.arg(output_format)
    if (output_format == "geojson") {
      res <- sp::spTransform(res, sp::CRS("+init=epsg:4326"))
      rgdal::writeOGR(
        obj = res,
        layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
        dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""),
        driver = "GeoJSON",
        check_exists = TRUE,
        overwrite_layer = TRUE
      ) 
    } else {
      rgdal::writeOGR(
        obj = res,
        layer = paste("SamplePoints_step_", step, "_", class_name, sep = ""),
        dsn = paste(in_path, "SamplePoints_step_", step, "_", class_name, ".shp", sep = ""),
        driver = "ESRI Shapefile",
        check_exists = TRUE,
        overwrite_layer = TRUE
      )
    }
}