-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexportFeatures.R
77 lines (67 loc) · 2.57 KB
/
exportFeatures.R
1
2
3
4
5
6
7
8
9
10
11
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#' @name exportFeatures
#' @title exportFeatures
#' @description Export sp object to well-known formats
#'
#' @param features the sp object to export
#' @param outputFormat the format of the output file, by default "SHP"
#' @param tozip object of class "logical" indicating if a zip should be created.
#' Default is FALSE.
#' @param file.path the base path where to export the features
#' @param file.name the name of the output file
#'
#' @note only supported for GML and SHP
#'
#' @author Emmanuel Blondel \email{emmanuel.blondel1@@gmail.com}
#' Norbert Billet \email{norbert.billet@@ird.fr}
#'
exportFeatures <- function(features, outputFormat = "SHP", tozip = FALSE,
file.path = NULL, file.name = NULL){
uuid <- UUIDgenerate()
if(is.null(file.name)){
file.name <- uuid
}
if(is.null(file.path)){
file.path <- file.path(tempdir(),uuid)
dir.create(file.path)
}
output <- NULL
#manage output formats
if(outputFormat == "SHP"){
gdalIconv <- .Call("RGDAL_CPL_RECODE_ICONV", PACKAGE="rgdal")
if(gdalIconv) setCPLConfigOption("SHAPE_ENCODING", NULL)
writeOGR(features, file.path, file.name, driver="ESRI Shapefile", overwrite_layer=T)
writeEncFile <- function(file.path, file.name, extension, encoding = "UTF-8"){
encFile <- file(paste(paste(file.path, file.name, sep="/"), extension, sep="."))
writeLines(encoding, encFile, sep="")
unlink(encFile)
}
writeEncFile(file.path, file.name, "cst")
writeEncFile(file.path, file.name, "cpg")
shapefiles <- list.files(file.path, pattern = file.name, full.names=TRUE)
if(tozip){
outputFile<-paste(file.path,"/",file.name,".zip",sep="")
zip(zipfile=outputFile, flags="-r9Xj", files=shapefiles) # requires R_ZIPCMD to be set in linux OS.
unlink(shapefiles)
output <- outputFile
}else{
output <- shapefiles
}
}else if(outputFormat == "GML"){
outputFile <- paste(file.path, "/", file.name, ".gml", sep="")
writeOGR(features, dsn = outputFile, layer = file.name, driver="GML", encoding = "UTF-8")
if(tozip){
outputZipFile <- paste(file.path, "/", file.name, ".zip", sep="")
zip(zipfile=outputZipFile, flags="-r9Xj", files=outputFile) # requires R_ZIPCMD to be set in linux OS.
unlink(outputFile)
output <- outputZipFile
}else{
output <- outputFile
}
}else{
stop("Unsupported output format")
}
if (length(list.files(file.path, pattern = file.name, full.names = TRUE)) == 0) {
stop("Error when creating ", outputFormat)
}
return(output)
}