Skip to content

Commit

Permalink
#22 fix shapefile management
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Oct 17, 2019
1 parent 4106b38 commit 7fefe10
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
33 changes: 13 additions & 20 deletions R/geoflow_action_geosapi_publish_ogc_services.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ geosapi_publish_ogc_services <- function(entity, config, options){
#layername/sourcename
layername <- if(!is.null(entity$data$layername)) entity$data$layername else entity$identifiers$id
sourcename <- if(!is.null(datasource_name)) datasource_name else layername
basefilename <- paste0(entity$identifiers$id, "_", entity$data$uploadType,"_", layername)
filename <- paste0(basefilename, ".zip")

#shortcut for gs config
GS <- config$software$output$geoserver
Expand Down Expand Up @@ -68,30 +70,20 @@ geosapi_publish_ogc_services <- function(entity, config, options){
config$logger.warn(warnMsg)
}else{
uploaded <- FALSE
isSourceUrl <- regexpr("(http|https)[^([:blank:]|\\\"|<|&|#\n\r)]+", datasource_file) > 0
if(isSourceUrl){
warnMsg <- "Upload from URL: Upload will assume remote file is a zip archive!"
config$logger.warn(warnMsg)
filename <- file.path(getwd(), "data", paste0(datasource_name,".zip"))
download.file(datasource_file, destfile = filename)

config$logger.info("Upload from local file(s)")
filepath <- file.path(getwd(), "data", filename)
if(file.exists(filepath)){
config$logger.info(sprintf("Upload file '%s' [%s] to GeoServer...", filepath, entity$data$uploadType))
uploaded <- GS$uploadData(workspace, datastore, endpoint = "file", configure = "none", update = "overwrite",
filename = filename, extension = entity$data$uploadType, charset = "UTF-8")
unlink(filename)
}else{
config$logger.info("Upload from local file(s)")
srcFilename <- datasource_file
data.files <- list.files(path = dirname(srcFilename), pattern = paste0(datasource_name,".zip"))
if(length(data.files)>0){
filename <- file.path(dirname(srcFilename), data.files[1])
uploaded <- GS$uploadData(workspace, datastore, endpoint = "file", configure = "none", update = "overwrite",
filename = filename, extension = entity$data$uploadType, charset = "UTF-8",
contentType = if(entity$data$uploadType=="spatialite") "application/x-sqlite3" else "")
filename = filepath, extension = entity$data$uploadType, charset = "UTF-8",
contentType = if(entity$data$uploadType=="spatialite") "application/x-sqlite3" else "")
}else{
errMsg <- sprintf("Upload from local file(s): no zipped file found for source '%s' (%s)", srcFilename, datasource_name)
errMsg <- sprintf("Upload from local file(s): no zipped file found for source '%s' (%s)", filepath, sourcename)
config$logger.error(errMsg)
stop(errMsg)
}
}

if(uploaded){
infoMsg <- sprintf("Successful Geoserver upload for file '%s' (%s)", datasource_file, entity$data$uploadType)
config$logger.info(infoMsg)
Expand All @@ -112,7 +104,8 @@ geosapi_publish_ogc_services <- function(entity, config, options){
#build feature type
featureType <- GSFeatureType$new()
featureType$setName(layername)
featureType$setNativeName(sourcename)
nativename <- if(entity$data$upload) basefilename else sourcename
featureType$setNativeName(nativename)
featureType$setAbstract(entity$descriptions$abstract)
featureType$setTitle(entity$title)
featureType$setSrs(epsgCode)
Expand Down
22 changes: 14 additions & 8 deletions R/geoflow_entity.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ geoflow_entity <- R6Class("geoflow_entity",
#instead of the complete dataset

isSourceUrl <- regexpr("(http|https)[^([:blank:]|\\\"|<|&|#\n\r)]+", datasource_file) > 0
if(isSourceUrl){
if(isSourceUrl && is.null(self$data$features)){
#case where data is remote and there was no data enrichment in initWorkflow
warnMsg <- "Copying data from URL to Job data directory!"
config$logger.warn(warnMsg)
download.file(datasource_file, destfile = paste(basefilename, datasource_ext, sep="."))
}else{
if(is.null(self$data$features)){

config$logger.info("Copying data local file(s) to Job data directory!")
srcFilename <- datasource_file
data.files <- list.files(path = dirname(srcFilename), pattern = datasource_name)
Expand Down Expand Up @@ -217,6 +217,15 @@ geoflow_entity <- R6Class("geoflow_entity",
config$logger.warn(sprintf("Entity data features writer not implemented for type '%s'", self$data$uploadType))
}
)
#we also copy the source with its native name
#this is required eg for geosapi if we want to set-up filtered shapefile layers
#based on the same source shapefile
config$logger.info("For entities enriched with spatial data, copy the datasource")
file.copy(
from = file.path(config$wd, get_temp_directory(), paste0(datasource_name, ".", datasource_ext)),
to = getwd()
)

}
}
}
Expand Down Expand Up @@ -255,7 +264,7 @@ geoflow_entity <- R6Class("geoflow_entity",

layername <- if(!is.null(self$data$layername)) self$data$layername else self$identifiers$id

TEMP_DATA_DIR <- file.path(getwd(), "geoflow_temp_data")
TEMP_DATA_DIR <- file.path(getwd(), get_temp_directory())
if(!dir.exists(TEMP_DATA_DIR)){
config$logger.info("Create geoflow temporary data directory")
dir.create(TEMP_DATA_DIR)
Expand All @@ -270,7 +279,7 @@ geoflow_entity <- R6Class("geoflow_entity",
if(isSourceUrl){
warnMsg <- "Downloading remote data from URL to temporary geoflow temporary data directory!"
config$logger.warn(warnMsg)
download.file(datasource_file, destfile = trgFilename)
download.file(datasource_file, destfile = trgFilename, mode = "wb")
unzip(zipfile = trgFilename, exdir = TEMP_DATA_DIR, unzip = getOption("unzip"))
shpExists <- TRUE
}else{
Expand Down Expand Up @@ -317,7 +326,7 @@ geoflow_entity <- R6Class("geoflow_entity",
config$software$output$geoserver_config$parameters$url,
config$software$output$geoserver_config$properties$workspace,
layername, paste(self$spatial_extent,collapse=","),self$srid))
self$addRelation(new_thumbnail)
self$relations <- c(new_thumbnail, self$relation) #here we use native vector to put WMS as first thumbnail
#WMS
new_wms <- geoflow_relation$new()
new_wms$setKey("wms")
Expand Down Expand Up @@ -375,9 +384,6 @@ geoflow_entity <- R6Class("geoflow_entity",
config$logger.warn(sprintf("Metadata dynamic handling based on 'data' not implemented for type '%s'", self$data$uploadType))
}
)

#remove temp dir
unlink(TEMP_DATA_DIR, force = TRUE)
},

#getContacts
Expand Down

0 comments on commit 7fefe10

Please sign in to comment.