Skip to content

Commit

Permalink
#293 base action
Browse files Browse the repository at this point in the history
  • Loading branch information
eblondel committed Apr 3, 2023
1 parent db7daf0 commit 4861559
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ License: MIT + file LICENSE
URL: https://github.com/r-geoflow/geoflow
BugReports: https://github.com/r-geoflow/geoflow
LazyLoad: yes
RoxygenNote: 7.2.1
RoxygenNote: 7.2.3
12 changes: 11 additions & 1 deletion R/geoflow_action.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ register_actions <- function(){
geoflow_action$new(
id = "geosapi-publish-ogc-services",
types = list("Data upload", "Data publication", "Metadata publication"),
def = "Publish vector data to GeoServer OGC web-services (WMS/WFS/WCS)",
def = "Publish data to GeoServer OGC web-services (WMS/WFS/WCS)",
target = NA,
target_dir = NA,
packages = list("geosapi"),
Expand All @@ -338,6 +338,16 @@ register_actions <- function(){
),
fun = source(system.file("actions", "geosapi_publish_ogc_services.R", package = "geoflow"))$value
),
geoflow_action$new(
id = "geonode4R-publish-ogc-services",
types = list("Data upload", "Data publication", "Metadata publication"),
def = "Publish data to GeoNode OGC web-services (WMS/WFS/WCS)",
target = NA,
target_dir = NA,
packages = list("geonode4R"),
available_options = list(),
fun = source(system.file("actions", "geonode4R_publish_ogc_services.R", package = "geoflow"))$value
),
geoflow_action$new(
id = "zen4R-deposit-record",
types = list("Data upload", "Data publication", "Metadata publication", "DOI assignment"),
Expand Down
81 changes: 81 additions & 0 deletions inst/actions/geonode4R_publish_ogc_services.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
function(action, entity, config){

if(!requireNamespace("geonode4R", quietly = TRUE)){
stop("The 'geonode4R-publish-ogc-services' action requires the 'geonode4R' package")
}

#shortcut for geonode config
GEONODE_CONFIG <- config$software$output$geonode_config
GEONODE <- config$software$output$geonode
if(is.null(GEONODE)){
errMsg <- "This action requires a GeoNode software to be declared in the configuration"
config$logger.error(errMsg)
stop(errMsg)
}

data_objects <- list()
if(is.null(entity$data$dir)){
data_objects <- list(entity$data)
}else{
data_objects <- entity$data$getData()
}

if(length(data_objects)>0) for(data_object in data_objects){

#datasource
datasource <- data_object$uploadSource[[1]]
datasource_name <- NULL
datasource_file <- NULL
if(!is.null(datasource)){
datasource_name <- unlist(strsplit(datasource, "\\."))[1]
datasource_file <- attr(datasource, "uri")
attributes(datasource) <- NULL
}else{
if(data_object$upload){
errMsg <- sprintf("Upload source is missing!")
stop(errMsg)
}
}

#layername/sourcename
layername <- if(!is.null(data_object$layername)) data_object$layername else entity$identifiers$id

#upload
#-------------------------------------------------------------------------------------------------
if(data_object$upload){

config$logger.info("Upload mode is set to true")
if(startsWith(data_object$uploadType,"db") || data_object$uploadType == "other"){
warnMsg <- "Skipping upload: Upload mode is only valid for types 'shp', 'spatialite' or 'h2'"
config$logger.warn(warnMsg)
}else{
uploaded <- FALSE
config$logger.info("Upload from local file(s)")
filepath <- file.path(getwd(), "data", datasource)
config$logger.info(sprintf("File to upload to GeoNode: %s", filepath))
if(file.exists(filepath)){
config$logger.info(sprintf("Upload file '%s' [%s] to GeoNode...", filepath, data_object$uploadType))
files = list.files(path = "data", pattern = datasource_name, full.names = TRUE)
files = files[!endsWith(files, ".zip")]
created = GEONODE$upload(files)
uploaded = created$success
}else{
errMsg <- sprintf("Upload from local file(s): no zipped file found for source '%s' (%s)", filepath, datasource)
config$logger.error(errMsg)
stop(errMsg)
}

if(uploaded){
infoMsg <- sprintf("Successful GeoNode upload for file '%s' (%s)", datasource_file, data_object$uploadType)
config$logger.info(infoMsg)
}else{
errMsg <- "Error during GeoNode file upload. Aborting 'geonode4R' action!"
config$logger.error(errMsg)
stop(errMsg)
}
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"profile": {
"id": "my-geonode-shp-workflow",
"name": "My GeoNode shp workflow",
"project": "Test geoflow project to publish shapefile in GeoNode",
"organization": "My organization",
"logos": [],
"mode": "entity",
"options": {
"line_separator": "_\n"
}
},
"metadata": {
"entities": [
{
"handler": "gsheet",
"source": "https://docs.google.com/spreadsheets/d/1H4tNcAzZRadVKf2BZ1C_m7DTAe22nkkXr55HCOjmLn0/edit?usp=sharing"
}
],
"contacts" : [
{
"handler": "gsheet",
"source": "https://docs.google.com/spreadsheets/d/1BqlXwA2fKiRuozNAQhBb_PbQVSPTCfl8_Q9rfM8E2ws/edit?usp=sharing"
}
]
},
"software": [
{
"id": "my-geonode",
"type": "output",
"software_type": "geonode",
"parameters": {
"url": "{{GEONODE_URL}}",
"user": "{{GEONODE_USER}}",
"pwd": "{{GEONODE_PASSWORD}}",
"logger": "DEBUG"
}
}
],
"actions": [
{
"id": "geonode4R-publish-ogc-services",
"run": true
}
]
}
50 changes: 50 additions & 0 deletions tests/testthat/test_config_metadata_gsheets_sdi_geonode_shp.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# test_config_metadata_gsheets_geonode_shp.R
# Author: Emmanuel Blondel <[email protected]>
#
# Description: Integration tests for config_metadata_gsheets_geonode_shp.json workflow
#=======================
require(geoflow, quietly = TRUE)
require(testthat)

cfg_file = system.file("extdata/workflows/config_metadata_gsheets_sdi_geonode_shp.json", package = "geoflow")

#init
test_that("init",{
CFG <- geoflow::initWorkflow(cfg_file)
expect_is(CFG$metadata$content, "list")
expect_equal(length(CFG$metadata$content), 2L)
expect_equal(names(CFG$metadata$content), c("contacts", "entities"))
expect_equal(length(CFG$metadata$content$contacts), 3L)
expect_equal(length(CFG$getContacts()), 3L)
expect_equal(length(CFG$metadata$content$entities), 1L)
expect_equal(length(CFG$getEntities()), 1L)
expect_equal(length(CFG$actions), 1L)
expect_equal(length(CFG$software), 2L)
expect_equal(names(CFG$software), c("input", "output"))
expect_equal(length(CFG$software$input), 0L)
expect_equal(length(CFG$software$output), 2L)
})

#debug
test_that("debug",{
DEBUG <- geoflow::debugWorkflow(cfg_file, entityIndex = 1, dir = ".")
expect_equal(names(DEBUG), c("config", "entity"))
expect_is(DEBUG$config, "list")
expect_is(DEBUG$entity, "geoflow_entity")
})

#execute
test_that("execute",{
EXEC <- geoflow::executeWorkflow(cfg_file, dir = ".")
expect_true(dir.exists(EXEC))
expect_true(file.exists(file.path(EXEC, "job.json")))
expect_true(file.exists(file.path(EXEC, "job-logs.txt")))
expect_true(file.exists(file.path(EXEC, "config_copyof_contacts_1.csv")))
expect_true(file.exists(file.path(EXEC, "config_copyof_entities_1.csv")))
expect_true(dir.exists(file.path(EXEC, "entities")))
entity_dirs <- list.dirs(path = file.path(EXEC, "entities"), full.names = F,recursive = F)
expect_true(all(entity_dirs %in% c("my-geoflow-shapefile-record1")))
config <- geoflow::initWorkflow(cfg_file)
GEONODE <- config$software$output$geonode
#TODO
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# test_config_metadata_gsheets_geoserver_geotiff.R
# test_config_metadata_gsheets_geoserver_shp.R
# Author: Emmanuel Blondel <[email protected]>
#
# Description: Integration tests for config_metadata_gsheets_geoserver_geotiff.json workflow
# Description: Integration tests for config_metadata_gsheets_geoserver_shp.json workflow
#=======================
require(geoflow, quietly = TRUE)
require(testthat)
Expand Down

0 comments on commit 4861559

Please sign in to comment.