From b71f28071894809c380517659d8b6bab97ca815d Mon Sep 17 00:00:00 2001 From: MGousseff Date: Thu, 5 Oct 2023 09:36:33 +0200 Subject: [PATCH] Fixed importLCZvect : drop = TRUE did'nt keep all the column. Also fixed a deprecated tidyr syntax. --- R/importLCZvect.R | 34 ++++++++++++++---------------- inst/tinytest/test_importLCZvect.R | 9 +++++++- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/R/importLCZvect.R b/R/importLCZvect.R index 730f8fe..6d2b443 100644 --- a/R/importLCZvect.R +++ b/R/importLCZvect.R @@ -51,7 +51,9 @@ importLCZvect<-function(dirPath, file="rsu_lcz.geojson", output="sfFile", column colErr<-c("It seems that some of the columns you try to import do not exist in the source file, are you sure you meant ", paste(badCol)," ?") - if (prod(inCol)==0){ stop(colErr) } else { sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,colonnes] } + if (prod(inCol)==0){ stop(colErr) } else { + if (drop== TRUE) {sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,colonnes] } else {sfFile<-sf::st_read(dsn=fileName,quiet=!verbose)[,]} + } # if typeLevels is empty if (length(typeLevels)==1){ @@ -60,20 +62,16 @@ importLCZvect<-function(dirPath, file="rsu_lcz.geojson", output="sfFile", column } # if typeLevels is not specified it will be set to default and we need to capture this later - typeLevelsDefault<-c("1"="1","2"="2","3"="3","4"="4","5"="5","6"="6","7"="7","8"="8", - "9"="9","10"="10","101"="101","102"="102","103"="103","104"="104", - "105"="105","106"="106","107"="107","101"="11","102"="12","103"="13","104"="14", - "105"="15", "106"="16","107"="17") + # typeLevelsDefault<-c("1"="1","2"="2","3"="3","4"="4","5"="5","6"="6","7"="7","8"="8", + # "9"="9","10"="10","101"="101","102"="102","103"="103","104"="104", + # "105"="105","106"="106","107"="107","101"="11","102"="12","103"="13","104"="14", + # "105"="15", "106"="16","107"="17") # Select columns from original file - if (column!=""){ - if(drop==T){sfFile<-subset(sfFile,select=colonnes)} - - - prov<-as.character(unique((st_drop_geometry(subset(sfFile,select=column,drop=T))))) %>% as.character - names(prov)<-prov - - if( prod(prov%in%typeLevels)==0 ){ - if (verbose==TRUE){ +if (column!=""){ + prov<-as.character(unique((st_drop_geometry(subset(sfFile,select=column,drop=T))))) %>% as.character + names(prov)<-prov + if( prod(prov%in%typeLevels)==0 ) { + if (verbose==TRUE){ print("levels in typeLevels are : ") print(typeLevels) print("levels in original data set are ") @@ -83,7 +81,7 @@ importLCZvect<-function(dirPath, file="rsu_lcz.geojson", output="sfFile", column Some geoms have been dropped,this could seriously alter your analysis, please check the levels or enter an empty string as typeLevels") } - if( sum(prov%in%typeLevels)==0 ){ + if( sum(prov%in%typeLevels)==0 ){ stop( paste0("none of the levels present in ",column, " is covered by the levels you specified.", @@ -92,7 +90,6 @@ importLCZvect<-function(dirPath, file="rsu_lcz.geojson", output="sfFile", column " must contain LCZ types in a standard format")) } - sfFile <- sfFile%>% mutate(!!column:=fct_recode(factor(subset(sfFile,select=column,drop=T),levels=typeLevels),!!!typeLevels)) %>% @@ -104,8 +101,9 @@ importLCZvect<-function(dirPath, file="rsu_lcz.geojson", output="sfFile", column #sfFile <- sfFile%>% mutate(!!column:=fct_recode(subset(sfFile,select=column,drop=T),!!!typeLevels)) if(output=="sfFile"){return(sfFile)} else { - if(output=="bBox"){bBox<-st_bbox(sfFile,crs=st_crs(sfFile)) %>% st_as_sfc - return(bBox)} + if(output=="bBox"){ + bBox<-st_bbox(sfFile,crs=st_crs(sfFile)) %>% st_as_sfc + return(bBox) } else { stop("Output must be sfFile to return geoms and LCZ or bBox to return the bounding box")} diff --git a/inst/tinytest/test_importLCZvect.R b/inst/tinytest/test_importLCZvect.R index 4d18238..7efec9c 100644 --- a/inst/tinytest/test_importLCZvect.R +++ b/inst/tinytest/test_importLCZvect.R @@ -98,4 +98,11 @@ expect_warning(importLCZvect(dirPath=paste0(system.file("extdata", package = "lc "9"="9","10"="10","101"="101","102"="102","103"="103","104"="104", "105"="105","106"="106","101"="11","102"="12","103"="13","104"="14", "105"="15", "106"="16","107"="17"),drop=T), - "The levels you specified with the typeLevels argument don't cover the LCZ values") \ No newline at end of file + "The levels you specified with the typeLevels argument don't cover the LCZ values") + + +# test if the drop argument allows to keep or drop comun other than specified +test<-importLCZvect(dirPath=paste0( + system.file("extdata", package = "lczexplore"),"/bdtopo_2_2/Redon"),file="rsu_lcz.geojson", + column="LCZ_PRIMARY",geomID="ID_RSU",confid="LCZ_UNIQUENESS_VALUE",drop=FALSE) +expect_equal("LCZ_SECONDARY"%in%names(test),TRUE) \ No newline at end of file