diff --git a/NAMESPACE b/NAMESPACE index 74e7516c..02eadc8a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(executeWorkflow) export(executeWorkflowJob) export(extract_cell_components) export(extract_kvp) +export(extract_kvps) export(filter_sf_by_cqlfilter) export(geoflowLogger) export(geoflow_action) @@ -54,6 +55,7 @@ export(geoflow_validator_entity_TemporalCoverage) export(geoflow_validator_entity_Title) export(geoflow_validator_entity_Type) export(get_line_separator) +export(get_locales_from) export(get_union_bbox) export(initWorkflow) export(initWorkflowJob) diff --git a/R/geoflow_entity.R b/R/geoflow_entity.R index 942dc93f..a29cf21f 100644 --- a/R/geoflow_entity.R +++ b/R/geoflow_entity.R @@ -65,6 +65,8 @@ geoflow_entity <- R6Class("geoflow_entity", status = list(), #'@field resources entity resources resources = list(), + #'@field locales entity locales + locales = list(), #'@description Initializes an object of class \link{geoflow_entity} initialize = function(){ @@ -79,6 +81,13 @@ geoflow_entity <- R6Class("geoflow_entity", return(clazz_obj$getValidKeys()) }, + #'@description Adds locales to entity from kvp values + #'@param values values + addLocalesFromValues = function(values){ + locales <- unlist(lapply(names(attributes(values)), function(x){unlist(strsplit(x,"locale#"))[2]})) + self$locales <- unique(c(self$locales, locales)) + }, + #'@description Set an identifier given a key. Default key is "id", but others can be specified, eg "doi". #'@param key identifier key. Default is "id" #'@param id identifier value @@ -141,6 +150,7 @@ geoflow_entity <- R6Class("geoflow_entity", paste0(self$getAllowedKeyValuesFor("Title"), collapse=","))) } self$titles[[key]] <- title + self$addLocalesFromValues(title) }, #'@description Sets description diff --git a/R/geoflow_utils.R b/R/geoflow_utils.R index 096903ad..89eb3094 100644 --- a/R/geoflow_utils.R +++ b/R/geoflow_utils.R @@ -123,7 +123,79 @@ extract_kvp <- function(str){ return(value) }) - return(list(key = key, values = values)) + #locale management + locale = NULL + key_parts <- unlist(strsplit(key, "#")) + if(length(key_parts)>1){ + key <- key_parts[1] + locale <- key_parts[2] + } + + return(list(key = key, values = values, locale = locale)) +} + +#' @name extract_kvp +#' @aliases extract_kvp +#' @title extract_kvp +#' @description \code{extract_kvp} parses a string into a key value pair represented by +#' a \code{geoflow_kvp} object. +#' +#' @usage extract_kvps(strs) +#' +#' @param str a string as object of class \code{character} +#' @param collapse collapse by. Default is \code{NULL} +#' +#' @author Emmanuel Blondel, \email{emmanuel.blondel1@@gmail.com} +#' @export +#' +extract_kvps <- function(strs, collapse = NULL){ + kvps <- lapply(strs, function(str){ + kvp <- extract_kvp(str) + if(!is.null(collapse)) kvp$values <- list(paste0(kvp$values, collapse = collapse)) + return(kvp) + }) + keys <- unique(sapply(kvps, "[[", "key")) + kvps <- lapply(keys, function(key){ + kvps_for_key <- kvps[sapply(kvps, function(kvp){kvp$key == key})] + with_null_locale <- any(sapply(kvps_for_key, function(x){is.null(x$locale)})) + kvp_with_default_locale <- kvps_for_key[sapply(kvps_for_key, function(x){is.null(x$locale)})] + if(length(kvp_with_default_locale)>0){ + kvp_with_default_locale <- kvp_with_default_locale[[1]] + }else{ + #TODO support default language in geoflow + } + locale_values <- kvp_with_default_locale$values + if(length(locale_values)==1) locale_values <- locale_values[[1]] + kvp_with_locale <- kvps_for_key[sapply(kvps_for_key, function(x){!is.null(x$locale)})] + for(item in kvp_with_locale){ + attr(locale_values, paste0("locale#", toupper(item$locale))) <- item$values + } + + kvp_with_locales <- list(key = key, values = locale_values) + return(kvp_with_locales) + }) + + return(kvps) +} + +#'@name get_locales_from +#'@aliases get_locales_from +#'@title get_locales_from +#'@description Get locales from a property values set +#' +#'@usage get_locales_from(values) +#' +#'@param values values +#' +#'@export +get_locales_from <- function(values){ + if(is.null(attributes(values))) return(NULL) + locales <- lapply(attributes(values), function(x){ + if(is.list(x)) x <- x[[1]] + return(x) + }) + names(locales) <- sapply(names(attributes(values)), function(x){unlist(strsplit(x, "locale#"))[2]}) + return(locales) } #' @name str_to_posix diff --git a/inst/actions/geometa_create_iso_19115.R b/inst/actions/geometa_create_iso_19115.R index 8afe3f12..2196d325 100644 --- a/inst/actions/geometa_create_iso_19115.R +++ b/inst/actions/geometa_create_iso_19115.R @@ -113,6 +113,17 @@ function(action, entity, config){ md$setLanguage(entity$language) md$setDateStamp(Sys.time()) + #locales (i18n/i10n support) + if(length(entity$locales)){ + for(locale in entity$locales){ + a_locale <- ISOLocale$new() + a_locale$setId(locale) + a_locale$setLanguage(locale) + a_locale$setCharacterSet("utf8") + md$addLocale(a_locale) + } + } + if(!is.null(entity$data)) { md$setMetadataStandardName(switch(entity$data$spatialRepresentationType, "vector" = "ISO 19115:2003 Geographic information - Metadata", @@ -253,7 +264,7 @@ function(action, entity, config){ #citation now <- Sys.time() ct <- ISOCitation$new() - ct$setTitle(entity$titles[["title"]]) + ct$setTitle(entity$titles[["title"]], locales = geoflow::get_locales_from(entity$titles[["title"]])) if("alternative" %in% names(entity$titles)){ ct$setAlternateTitle(entity$titles[["alternative"]]) } diff --git a/inst/metadata/entity/entity_handler_df.R b/inst/metadata/entity/entity_handler_df.R index fee9073c..1719f7b6 100644 --- a/inst/metadata/entity/entity_handler_df.R +++ b/inst/metadata/entity/entity_handler_df.R @@ -110,14 +110,9 @@ handle_entities_df <- function(config, source){ } titles <- if(!is.na(src_title)) extract_cell_components(src_title) else list() if(length(titles)>0){ - if(length(titles)==1){ - kvp <- extract_kvp(titles) - entity$setTitle("title", paste(kvp$values, collapse=",")) - }else{ - for(title in titles){ - kvp <- extract_kvp(title) - entity$setTitle(kvp$key, paste(kvp$values, collapse=",")) - } + kvps <- extract_kvps(titles, collapse=",") + for(kvp in kvps){ + entity$setTitle(kvp$key, kvp$values) } } diff --git a/man/extract_kvp.Rd b/man/extract_kvp.Rd index 467be721..a86d0916 100644 --- a/man/extract_kvp.Rd +++ b/man/extract_kvp.Rd @@ -2,14 +2,22 @@ % Please edit documentation in R/geoflow_utils.R \name{extract_kvp} \alias{extract_kvp} +\alias{extract_kvps} \title{extract_kvp} \usage{ extract_kvp(str) + +extract_kvps(strs) } \arguments{ \item{str}{a string as object of class \code{character}} + +\item{collapse}{collapse by. Default is \code{NULL}} } \description{ +\code{extract_kvp} parses a string into a key value pair represented by +a \code{geoflow_kvp} object. + \code{extract_kvp} parses a string into a key value pair represented by a \code{geoflow_kvp} object. } diff --git a/man/geoflow_entity.Rd b/man/geoflow_entity.Rd index 1d70f528..ffe3f7d1 100644 --- a/man/geoflow_entity.Rd +++ b/man/geoflow_entity.Rd @@ -62,6 +62,8 @@ Emmanuel Blondel \item{\code{status}}{entity status} \item{\code{resources}}{entity resources} + +\item{\code{locales}}{entity locales} } \if{html}{\out{}} } @@ -70,6 +72,7 @@ Emmanuel Blondel \itemize{ \item \href{#method-geoflow_entity-new}{\code{geoflow_entity$new()}} \item \href{#method-geoflow_entity-getAllowedKeyValuesFor}{\code{geoflow_entity$getAllowedKeyValuesFor()}} +\item \href{#method-geoflow_entity-addLocalesFromValues}{\code{geoflow_entity$addLocalesFromValues()}} \item \href{#method-geoflow_entity-setIdentifier}{\code{geoflow_entity$setIdentifier()}} \item \href{#method-geoflow_entity-addDate}{\code{geoflow_entity$addDate()}} \item \href{#method-geoflow_entity-setLanguage}{\code{geoflow_entity$setLanguage()}} @@ -146,6 +149,23 @@ the list of valid keys for the field considered } } \if{html}{\out{
}} +\if{html}{\out{}} +\if{latex}{\out{\hypertarget{method-geoflow_entity-addLocalesFromValues}{}}} +\subsection{Method \code{addLocalesFromValues()}}{ +Adds locales to entity from kvp values +\subsection{Usage}{ +\if{html}{\out{
}}\preformatted{geoflow_entity$addLocalesFromValues(values)}\if{html}{\out{
}} +} + +\subsection{Arguments}{ +\if{html}{\out{
}} +\describe{ +\item{\code{values}}{values} +} +\if{html}{\out{
}} +} +} +\if{html}{\out{
}} \if{html}{\out{}} \if{latex}{\out{\hypertarget{method-geoflow_entity-setIdentifier}{}}} \subsection{Method \code{setIdentifier()}}{ diff --git a/man/get_locales_from.Rd b/man/get_locales_from.Rd new file mode 100644 index 00000000..1979a955 --- /dev/null +++ b/man/get_locales_from.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/geoflow_utils.R +\name{get_locales_from} +\alias{get_locales_from} +\title{get_locales_from} +\usage{ +get_locales_from(values) +} +\arguments{ +\item{values}{values} +} +\description{ +Get locales from a property values set +}