From 556328e98d479725cc68f03d4f0ac510aa31ab22 Mon Sep 17 00:00:00 2001 From: Bennici Date: Fri, 9 Jul 2021 09:49:36 +0200 Subject: [PATCH] #43 Manage feature info response convertion to dataframe for xml format --- R/WMSLayer.R | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/R/WMSLayer.R b/R/WMSLayer.R index 966675a..79a067c 100644 --- a/R/WMSLayer.R +++ b/R/WMSLayer.R @@ -300,13 +300,30 @@ WMSLayer <- R6Class("WMSLayer", saveXML(obj, destfile) }else if(destext == "json"){ write(obj, destfile) - } + # }else if(destext == "xml"){ + # write(obj, destfile) + # } ftFeatures <- NULL if(destext == "xml"){ + xml<- xmlParse(obj) rootname <- xmlName(xmlChildren(xml)[[1]]) if(rootname == "FeatureInfoResponse"){ - #TODO build sf(s) based on a FeatureInfoResponse xml (eg. case of Thredds data servers) + xml_list <- xmlToList(xml) + if("FIELDS" %in% names(xml_list)){ + variables = lapply(xml_list[["FIELDS"]], function(x){ + outvar <- x + name <- names(x) + attr(outvar, "description") <- name + return(outvar) + }) + ftFeatures<-data.frame(variables) + }else{ + #THREDDS STRUCTURE + ftFeatures<-data.frame(xml_list,stringsAsFactors=FALSE) + names(ftFeatures)<-gsub("^.*\\.","",names(ftFeatures)) + ftFeatures<-sf::st_as_sf(ftFeatures,coords= c("longitude","latitude")) + } }else{ ftFeatures <- sf::st_read(destfile, quiet = TRUE) }