diff --git a/DESCRIPTION b/DESCRIPTION index 1bee91a..8ea97f8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/R/geoflow_action.R b/R/geoflow_action.R index ce91558..97d42d7 100644 --- a/R/geoflow_action.R +++ b/R/geoflow_action.R @@ -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"), @@ -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"), diff --git a/inst/actions/geonode4R_publish_ogc_services.R b/inst/actions/geonode4R_publish_ogc_services.R new file mode 100644 index 0000000..e87f815 --- /dev/null +++ b/inst/actions/geonode4R_publish_ogc_services.R @@ -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) + } + } + } + + } + +} \ No newline at end of file diff --git a/inst/extdata/workflows/config_metadata_gsheets_sdi_geonode_shp.json b/inst/extdata/workflows/config_metadata_gsheets_sdi_geonode_shp.json new file mode 100644 index 0000000..91a0ac6 --- /dev/null +++ b/inst/extdata/workflows/config_metadata_gsheets_sdi_geonode_shp.json @@ -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 + } + ] +} diff --git a/tests/testthat/test_config_metadata_gsheets_sdi_geonode_shp.R b/tests/testthat/test_config_metadata_gsheets_sdi_geonode_shp.R new file mode 100644 index 0000000..523eb00 --- /dev/null +++ b/tests/testthat/test_config_metadata_gsheets_sdi_geonode_shp.R @@ -0,0 +1,50 @@ +# test_config_metadata_gsheets_geonode_shp.R +# Author: Emmanuel Blondel +# +# 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 +}) diff --git a/tests/testthat/test_config_metadata_gsheets_sdi_geoserver_shp.R b/tests/testthat/test_config_metadata_gsheets_sdi_geoserver_shp.R index 68b1f7f..b8df4eb 100644 --- a/tests/testthat/test_config_metadata_gsheets_sdi_geoserver_shp.R +++ b/tests/testthat/test_config_metadata_gsheets_sdi_geoserver_shp.R @@ -1,7 +1,7 @@ -# test_config_metadata_gsheets_geoserver_geotiff.R +# test_config_metadata_gsheets_geoserver_shp.R # Author: Emmanuel Blondel # -# 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)