33# ' @param x Earth Engine table (ee$FeatureCollection) to be converted into a sf
44# ' object.
55# ' @param dsn Character. Output filename; in case \code{dsn} is missing
6- # ' \code{ee_as_sf} will create a temporary file.
6+ # ' \code{ee_as_sf} will create a shapefile file in tmp() directory .
77# ' @param crs Integer or character. coordinate reference system
88# ' for the EE table. If is NULL, \code{ee_as_sf} will take the CRS of
99# ' the first element.
@@ -101,7 +101,7 @@ ee_as_sf <- function(x,
101101 sp_eeobjects <- ee_get_spatial_objects(' Table' )
102102
103103 if (missing(dsn )) {
104- dsn <- paste0(tempfile()," .geojson " )
104+ dsn <- paste0(tempfile()," .shp " )
105105 }
106106
107107 if (! any(class(x ) %in% sp_eeobjects )) {
@@ -195,12 +195,21 @@ ee_as_sf <- function(x,
195195 file_name <- paste0(table_id , " _" , time_format )
196196
197197 # table to drive
198+ table_format <- ee_get_table_format(dsn )
199+ if (is.na(table_format )) {
200+ stop(
201+ ' sf_as_ee(..., via = \" drive\" ), only support the ' ,
202+ ' following output format: "CSV", "GeoJSON", "KML", "KMZ", "SHP"' ,
203+ ' . Use ee_table_to_drive and ee_drive_to_local to save in a TFRecord format.'
204+ )
205+ }
206+
198207 table_task <- ee_table_to_drive(
199208 collection = x_fc ,
200209 description = ee_description ,
201210 folder = container ,
202211 fileNamePrefix = file_name ,
203- fileFormat = " GeoJSON " ,
212+ fileFormat = table_format ,
204213 selectors = selectors
205214 )
206215
@@ -227,7 +236,12 @@ ee_as_sf <- function(x,
227236 overwrite = overwrite ,
228237 consider = ' all'
229238 )
230- local_sf <- sf :: read_sf(dsn , quiet = TRUE )
239+
240+ if (table_format == " CSV" ) {
241+ return (read.csv(dsn , stringsAsFactors = FALSE ))
242+ } else {
243+ local_sf <- sf :: read_sf(dsn , quiet = TRUE )
244+ }
231245 } else if (via == ' gcs' ) {
232246 # Creating name for temporal file; just for either drive or gcs
233247 time_format <- format(Sys.time(), " %Y-%m-%d-%H:%M:%S" )
@@ -245,13 +259,22 @@ ee_as_sf <- function(x,
245259
246260 file_name <- paste0(table_id , " _" , time_format )
247261
248- # table to drive
262+ # table to gcs
263+ table_format <- ee_get_table_format(dsn )
264+ if (is.na(table_format )) {
265+ stop(
266+ ' sf_as_ee(..., via = \" gcs\" ), only support the ' ,
267+ ' following output format: "CSV", "GeoJSON", "KML", "KMZ", "SHP"' ,
268+ ' . Use ee_table_to_drive and ee_drive_to_local to save in a TFRecord format.'
269+ )
270+ }
271+
249272 table_task <- ee_table_to_gcs(
250273 collection = x_fc ,
251274 description = ee_description ,
252275 bucket = container ,
253276 fileNamePrefix = file_name ,
254- fileFormat = " GeoJSON " ,
277+ fileFormat = table_format ,
255278 selectors = selectors
256279 )
257280
@@ -271,7 +294,11 @@ ee_as_sf <- function(x,
271294 stop(table_task $ status()$ error_message )
272295 }
273296 ee_gcs_to_local(task = table_task ,dsn = dsn , overwrite = overwrite )
274- local_sf <- sf :: read_sf(dsn , quiet = TRUE )
297+ if (table_format == " CSV" ) {
298+ return (read.csv(dsn , stringsAsFactors = FALSE ))
299+ } else {
300+ local_sf <- sf :: read_sf(dsn , quiet = TRUE )
301+ }
275302 } else {
276303 stop(" via argument invalid." )
277304 }
@@ -313,7 +340,32 @@ ee_fc_to_sf_getInfo <- function(x_fc, dsn, maxFeatures, overwrite = TRUE) {
313340 if (missing(dsn )) {
314341 x_sf
315342 } else {
316- sf :: write_sf(x_sf , dsn , delete_dsn = overwrite , quiet = TRUE )
343+ suppressWarnings(
344+ sf :: write_sf(x_sf , dsn , delete_dsn = overwrite , quiet = TRUE )
345+ )
317346 x_sf
318347 }
319348}
349+
350+ # ' Sync sf and ee drivers
351+ # ' @noRd
352+ ee_get_table_format <- function (dsn ) {
353+ table_format <- tolower(sub(" .*([.*])" , " \\ 1" , basename(dsn )))
354+ if (length(table_format ) != 1 ) {
355+ stop(" dns must be a single-length character" )
356+ }
357+
358+ if (table_format == " .shp" ) {
359+ " SHP"
360+ } else if (table_format == " .geojson" ) {
361+ " GeoJSON"
362+ } else if (table_format == " .kml" ) {
363+ " KML"
364+ } else if (table_format == " .kmz" ) {
365+ " KMZ"
366+ } else if (table_format == " .csv" ) {
367+ " CSV"
368+ } else {
369+ NA
370+ }
371+ }
0 commit comments