Commit 409352e6 authored by Romulo Pereira Goncalves's avatar Romulo Pereira Goncalves
Browse files

When results is NULL, we should create an empty SpatialPolygonsDataframe. To...

When results is NULL, we should create an empty SpatialPolygonsDataframe. To save empty SpatialTemporalDataframes we need to use sf, since it supports the empty Geometries.
parent 5487f73a
......@@ -107,10 +107,14 @@ saveSamplePoints <-
}
result <- do.call(rbind, collect)
res <- raster::extract(dummy_raster, result)
if (length(which(is.na(res))) > 0) {
result <- result[-which(is.na(res)), ]
if (!is.null(result)) {
res <- raster::extract(dummy_raster, result)
if (length(which(is.na(res))) > 0) {
result <- result[-which(is.na(res)), ]
}
} else {
result <-
sp::SpatialPolygonsDataFrame(Sr = sp::SpatialPolygons(list()), data = data.frame())
}
raster::crs(result) <- raster::crs(dummy_raster)
......@@ -120,22 +124,22 @@ saveSamplePoints <-
if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
}
rgdal::writeOGR(
obj = result,
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
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
append = FALSE
)
} else {
rgdal::writeOGR(
obj = result,
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
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
append = FALSE
)
}
}
......@@ -193,10 +197,14 @@ writeOutSamples <- function(in_path, step, class_name, output_format = c("shp",
}
result <- do.call(rbind, collect)
res <- raster::extract(dummy_sample, result)
if (length(which(is.na(res))) > 0) {
result <- result[-which(is.na(res)), ]
if (!is.null(result)) {
res <- raster::extract(dummy_sample, result)
if (length(which(is.na(res))) > 0) {
result <- result[-which(is.na(res)), ]
}
} else {
result <-
sp::SpatialPolygonsDataFrame(Sr = sp::SpatialPolygons(list()), data = data.frame())
}
raster::crs(result) <- raster::crs(dummy_sample)
output_format <- match.arg(output_format)
......@@ -205,22 +213,22 @@ writeOutSamples <- function(in_path, step, class_name, output_format = c("shp",
if (nrow(result) > 0) {
result <- sp::spTransform(result, sp::CRS("+init=epsg:4326"))
}
rgdal::writeOGR(
obj = result,
layer = paste("sel_SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "sel_SamplePoints_step_", step, "_", class_name, ".geojson", sep = ""),
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
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
append = FALSE
)
} else {
rgdal::writeOGR(
obj = result,
layer = paste("sel_SamplePoints_step_", step, "_", class_name, sep = ""),
dsn = paste(in_path, "sel_SamplePoints_step_", step, "_", class_name, ".shp", sep = ""),
sf::st_write(
obj = sf::st_as_sf(x = result, crs = 4326),
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
append = FALSE
)
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment