Skip to content

Commit

Permalink
Fixed importLCZvect : drop = TRUE did'nt keep all the column. Also fi…
Browse files Browse the repository at this point in the history
…xed a deprecated tidyr syntax.
  • Loading branch information
MGousseff committed Oct 5, 2023
1 parent ca6c8ee commit b71f280
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
34 changes: 16 additions & 18 deletions R/importLCZvect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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 ")
Expand All @@ -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.",
Expand All @@ -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)) %>%
Expand All @@ -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")}

Expand Down
9 changes: 8 additions & 1 deletion inst/tinytest/test_importLCZvect.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
"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)

0 comments on commit b71f280

Please sign in to comment.