save_files.r 7.63 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


#' 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
69
#' @param num_models number of models used for the classification of a habitat
70
#' @param dummy_raster raster with probabilities for each pixel
71
72
#'
#' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
73
#' 1) Point Shape/GeoJSON represents the pixels which were used to train the models for that habitat.
74
75
76
77
78
79
80
81
82
83
84
#'    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,
85
           num_models,
86
87
88
           dummy_raster) {
    collect <- list()
    j <- 0
89
90
91
    dummy_raster[dummy_raster == num_models] <- NA
    dummy_raster[dummy_raster > num_models] <- 1

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    ###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) {
113
      result <- result[-which(is.na(res)), ]
114
    }
115
    raster::crs(result) <- raster::crs(dummy_raster)
116

117
118
    output_format <- match.arg(output_format)
    if (output_format == "geojson") {
119
      # Only transform is there points to be saved.
120
121
      if (nrow(result) > 0) {
        result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
122
      }
123
      rgdal::writeOGR(
124
        obj = result,
125
126
127
128
129
130
131
132
        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(
133
        obj = result,
134
135
136
137
138
139
140
        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
      )
    }
141
142
143
  }


144
#' Selected Sample Collection for Habitat Types
145
#'
146
147
148
#' Writes out a set of samples (SpatialPointsDataFrame) into ESRI shapefiles or a GeoJSON file for a selected habitat type.
#' Each point represents a valid sample location that identifies the selected habitat type. Only those points are selected
#' which lay in the habitat type selected by the user's input threshold.
149
150
151
152
153
154
155
#'
#' @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
#'
#' @return ESRI shapefiles/GeoJSON with name: SamplePoints_step_classname.shp/SamplePoints_step_classname.geojson
156
#' 1) Point Shape/GeoJSON represents the pixels which belong to selected habitat type and can be used as reference for further model building.
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#'    ESRI shapefiles have the same CRS as the input raster. GeoJSON files are in the standard CRS of GeoJSON (EPSG:4326).
#'
#'
#' @export

###write out selected samples
writeOutSamples <- function(in_path, step, class_name, output_format = c("shp", "geojson")) {
  run1 <- get(load(paste(in_path, "Run", step, sep = "")))
  load(paste(in_path, "threshold_step_", step, sep = ""))
  dummy_sample <-
    raster::raster(paste(in_path, "step_", step, "_", class_name, ".tif", sep =
                           ""))
  
  length_threshold <- length(threshold)
  thres <- threshold[length_threshold]
  dummy_sample[dummy_sample < thres] <- NA
  dummy_sample[dummy_sample >= thres] <- 1
  
  collect <- list()
  j <- 0
  
  ###extract only class samples
  for (i in 1:length(run1@ref_samples)) {
    if (length(dim(run1@ref_samples[[i]])) != 0)
    {
      if (is.na(run1@switch[i]) == F) {
        j = j + 1
        collect[[j]] <-
          run1@ref_samples[[i]][which(run1@ref_samples[[i]]@data == 1), ]
      } else
      {
        j = j + 1
        collect[[j]] <-
          run1@ref_samples[[i]][which(run1@ref_samples[[i]]@data == 2), ]
      }
    }
  }
  
  result <- do.call(rbind, collect)
  
Johannes Knoch's avatar
Johannes Knoch committed
197
  res <- raster::extract(dummy_sample, result)
198
  if (length(which(is.na(res))) > 0) {
199
    result <- result[-which(is.na(res)), ]
200
  }
Johannes Knoch's avatar
Johannes Knoch committed
201
  raster::crs(result) <- raster::crs(dummy_sample)
202
203
  output_format <- match.arg(output_format)
  if (output_format == "geojson") {
204
205
206
207
    # Only transform is there points to be saved.
    if (nrow(result) > 0) {
      result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
    }
208
    rgdal::writeOGR(
209
      obj = result,
210
211
      layer = paste("sel_SamplePoints_step_", step, "_", class_name, sep = ""),
      dsn = paste(in_path, "sel_SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""),
212
213
214
215
216
217
      driver = "GeoJSON",
      check_exists = TRUE,
      overwrite_layer = TRUE
    ) 
  } else {
    rgdal::writeOGR(
218
      obj = result,
219
220
      layer = paste("sel_SamplePoints_step_", step, "_", class_name, sep = ""),
      dsn = paste(in_path, "sel_SamplePoints_step_", step, "_", class_name, ".shp", sep = ""),
221
222
223
224
225
      driver = "ESRI Shapefile",
      check_exists = TRUE,
      overwrite_layer = TRUE
    )
  }
226
}