Skip to content

Commit

Permalink
Merge pull request #27 from MGousseff/MDemuzaereReview3
Browse files Browse the repository at this point in the history
M demuzaere review3
  • Loading branch information
MGousseff authored Oct 25, 2023
2 parents 91a1625 + 6e4209e commit 9506a71
Show file tree
Hide file tree
Showing 40 changed files with 561 additions and 118,111 deletions.
5 changes: 4 additions & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@
.github$
^CITATION\.cff$
^\\.gitignore$
.gitignore$
.gitignore$
^CODE_OF_CONDUCT\.md$
^CONTRIBUTING\.md$
^bdtopo_2_2_osm\.csv$
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Package: lczexplore
Title: lczexplore
Version: 0.0.1.0002
Version: 0.0.1.0003
Authors@R: c(
person("Matthieu", "Gousseff", , "[email protected]", role = c("aut", "cre")),
person(, "Centre National de la Recherche Scientifique, Lab-Sticc", role = "cph", comment = c(ORCID = "0000-0002-7106-2677"))
)
Description: This lczexplore package automatize the comparison of sets of local climate zone classifications. It was developed thanks to the project PAENDORA2 (Pour la gestion du confort estival : Données, Outils et Recherche-Action) (2022 -2025), funded by ADEME.
Description: This lczexplore package automatize the comparison of sets of local climate zone classifications. It was developed thanks to the project PAENDORA2 (Pour la gestion du confort estival : Donnees, Outils et Recherche-Action) (2022 -2025), funded by ADEME.
License: LGPL (>= 3)
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Expand Down
20 changes: 11 additions & 9 deletions R/confidSensib.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#' @param inputDf is an R file with geom IDs, (LCZ) classifications and
#' a confidence value granted for the (LCZ) classifications values of each geom. Ignored if filePath is not empty.
#' @param filePath is the path to a csv file containing geom IDs, LCZ classifications and
#' a confidence value granted for the LCZ value of each geom.
#' a confidence value granted for the LCZ value of each geom. Set it to "" if inputDf is specified (recommanded)
#' @param nPoints is the number of points (quantiles) of confidence for which
#' the average agreement between classifications will be computed
#' @param wf1 is the name of the workflow used to produce the first LCZ
Expand All @@ -27,14 +27,16 @@
#' @export
#'
#' @examples
#' mainPath<-system.file("extdata", package = "lczexplore")
#' testSourceFact<-read.csv(paste0(mainPath,"/bdtopo_2_2_osm.csv"),
#' sep=";",header=TRUE,stringsAsFactors = TRUE)
#' confidSensib(inputDf=testSourceFact, filePath="",
#' # creation of the comparison data on which to perform the analysis
#' redonCompare<-compareLCZ(sf1=redonBDT, wf1="bdt", geomID1 = "ID_RSU", column1 ="LCZ_PRIMARY",
#' confid1 = "LCZ_UNIQUENESS_VALUE",
#' sf2=redonOSM, wf2="osm",geomID2 = "ID_RSU", column2="LCZ_PRIMARY",
#' confid2 ="LCZ_UNIQUENESS_VALUE", exwrite=FALSE, plot=FALSE, saveG="")
#' confidSensib(inputDf=redonCompare$data,
#' nPoints=5, wf1="bdtopo_2_2", wf2="osm",
#' geomID1="ID_RSU", column1="LCZ_PRIMARY", confid1="LCZ_UNIQUENESS_VALUE",
#' geomID2="ID_RSU.1",column2="LCZ_PRIMARY.1", confid2="LCZ_UNIQUENESS_VALUE.1",
#' sep=";", repr="standard", plot=TRUE, saveG=mainPath)
#' sep=";", repr="standard", plot=TRUE, saveG="")
confidSensib<-function(inputDf="", filePath="", nPoints=5,
wf1="bdtopo_2_2", wf2="osm",
geomID1="ID_RSU", column1="LCZ_PRIMARY", confid1="LCZ_UNIQUENESS_VALUE",
Expand Down Expand Up @@ -74,7 +76,7 @@ confidSensib<-function(inputDf="", filePath="", nPoints=5,
# between LCZ classifications

echIntConf<-subset(echInt,!is.na(echInt$confidMin))
# print("séparation");(print(nrow(echIntNoconf)));(print(nrow(echIntConf)));(print(nrow(echInt)))


#############################################################################################
# All LCZ levels treated together
Expand Down Expand Up @@ -163,7 +165,7 @@ confidSensib<-function(inputDf="", filePath="", nPoints=5,
}

if(saveG!=""){
plotName<-paste0(saveG,"/GeneralUniquenessSensib.jpg")
plotName<-paste0(saveG,"/GeneralUniquenessSensib.png")
png(filename = plotName,width=1200,height=900)
print(allLCZ$ctPlot)
dev.off()
Expand Down Expand Up @@ -216,7 +218,7 @@ typeLevels<-unique(echIntConf[,column1]) %>% as.vector
}

if(saveG!=""){
plotName<-paste0(saveG,"/byLCZUniquenessSensib.jpg")
plotName<-paste0(saveG,"/byLCZUniquenessSensib.png")
png(filename = plotName,width=1200,height=900)
print(byLCZPLot)
dev.off()
Expand Down
123 changes: 63 additions & 60 deletions R/importLCZraster.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#' Imports the LCZ classifications produced on raster maps, mainly by the WUDAPT algorithm.
#' For now, the function import from the european tiff
#' produced by WUDAPT. Users can DOWNLOAD the WUDAPT tiff file
#' at the following url : https://figshare.com/articles/dataset/European_LCZ_map/13322450
#' A future version may include the world data once a strategy is defined to deal with CRS.
#' Imports the LCZ classifications produced on raster maps.
#' For now, the european tif map produced by WUDAP is used as a reference example.
#' Users can DOWNLOAD the WUDAPT tif file at the following url :
#' https://figshare.com/articles/dataset/European_LCZ_map/13322450
#'
#' @param dirPath is the path to the directory where the
#' @param fileName is the name of the raster file (tif or geotif), by default \'EU_LCZ_map.tif\' .
#' Will be useful when other zones will be added
#' @param column indicates the name of the column which will contain the LCZ in the output file
#' @param typeLevels indicates a named vector of the unique values contained in column,
#' @param zone set to europe by default, may include world once a strategy is defined
#' @param bBox bBox is the bounding box needed to crop the raster tiff file.
#' It can be produced by the importLCZvect function if one has a vect map o the same zone,
#' it can be a set of coordinates. It can either be of class bBox or of class sfc.
#' @return an sf file containing the geom and LCZ levels from the raster within the bBox bounding box
#' @param fileName is the name of the raster file (tif or geotif), by default \'EU_LCZ_map.tif\'.
#' @param LCZband defines the band of the raster file which contains the LCZ types.
#' It can be a number or a band name (default = 1)
#' @param LCZcolumn the name of the column of the output sf File in which the LCZ types will be stored
#' (default = "LCZ")
#' @param confidenceBand the band of the raster files which contains a measure of confidence.
#' Can be a number or a band name (default = "", that is no measure of confidence provided)
#' @param confidenceColumn the name of the column of the output sf File in which the confidence values will be stored
#' (default = "", no confidence value specified)
#' @param typeLevels indicates a named vector of the unique values contained in LCZcolumn,
#' @param bBox bBox is the bounding box needed to crop the raster file.
#' It can be produced bu the importLCZvect function. It can either be of class bBox or of class sfc
#' @return an sf file containing the geom and LCZ levels from theraster tiff within the bBox bounding box
#' @import sf dplyr forcats
#' @importFrom terra crop
#' @importFrom terra rast
Expand All @@ -35,54 +38,56 @@
#' # fileName="redonWudapt.tif",bBox=redonBbox)
#'
#' # another way to get the bounding box when one doesn't want
#' # to compare to a vector map is to enter it's coordinates
#' # and feed them to st_bbox() of the sf package.
#' #to compare to a vector map is to enter it's coordinates
#' # and feed them to st_bbox() of the sf package.
#'
#' # the following example can only be executed when user has downloaded
#' # CONUS-wide LCZ map and Training Areas on WUDAPT website
#' # sanDiegobBoxCoord<-st_sf(a=1:2, geom=st_sfc(
#' # st_point(c(-117.175198,32.707289)),
#' # st_point(c(-117.112198,32.750900)),crs = 4326
#' #))
#' #sanDiegoBbox<-st_bbox(sanDiegobBoxCoord)
#' #sanDiegoWudapt<-importLCZraster(
#' #dirPath="path_of_the_tiff",
#' #fileName="CONUS_LCZ_map_NLCD_v1.0_epsg4326.tif",
#' #column="CONUS_LCZ_map_NLCD_v1.0_epsg4326"
#' # ,bBox=sanDiegoBbox)
#' #showLCZ(sanDiegoWudapt, column="CONUS_LCZ_map_NLCD_v1.0_epsg4326")
importLCZraster<-function(dirPath,zone="europe",bBox,fileName="EU_LCZ_map.tif", column='EU_LCZ_map',
#' # imporLCZraster also allows to choose which band of a raster file to import
#' redonWudapt<-importLCZraster(system.file("extdata", package = "lczexplore"),
#' fileName="redonWudapt.tif",bBox=redonBbox, LCZband=1, LCZcolumn='EU_LCZ_map')
importLCZraster<-function(dirPath, bBox, fileName="EU_LCZ_map.tif", LCZband=1, LCZcolumn='EU_LCZ_map',
confidenceBand = "", confidenceColumn = "confidence",

typeLevels=c("1"="1","2"="2","3"="3","4"="4","5"="5","6"="6","7"="7","8"="8",
"9"="9","10"="10","101"="11","102"="12","103"="13","104"="14",
"105"="15", "106"="16","107"="17")){
# internal import function used in two loops
effectiveImport<-function(fileName,bBox){
sfFile<-rast(fileName)
effectiveImport<-function(fileName,bBox,LCZband,confidenceBand,LCZcolumn, confidenceColumn){

if(confidenceBand=="") {
lyrs=list(LCZband)
sfFile<-do.call(rast, list(x=fileName, lyrs=lyrs))
} else if (confidenceBand!="") {
lyrs=list(LCZband,confidenceBand)
sfFile<-do.call(rast, list(x=fileName, lyrs=lyrs))
}

if (sum(class(bBox)%in%c("sfc_POLYGON","sfc" ))==0) {
bBox<-st_as_sfc(bBox)
}
bBox<-st_transform(bBox,st_crs(sfFile,proj=T))
bBox<-st_transform(bBox, st_crs(sfFile,proj=TRUE))


cropTry<-try(sfFile %>% crop(bBox))
if(is(cropTry,"try-error")){stop("The bounding box doesn't intersect with the Wudapt tiff : \n
maybe it's out of the Europe zone covered by this package. \n Future versions may include other zones.")}
if(is(cropTry,"try-error")) {
stop("The bounding box doesn't intersect with the provided raster file.")}
else{
sfFile<-sfFile %>% crop(bBox) %>% as.polygons(dissolve=F) %>%
sfFile<-sfFile %>% crop(bBox) %>% as.polygons(dissolve=FALSE) %>%
st_as_sf() %>% st_intersection(bBox)

if (length(typeLevels==1)){
typeLevels<-c("1"="1","2"="2","3"="3","4"="4","5"="5","6"="6","7"="7","8"="8",
"9"="9","10"="10","101"="11","102"="12","103"="13","104"="14",
"105"="15", "106"="16","107"="17")}
# typeLevels2<-as.character(c(1:10,101:107))

sfFile<-sfFile%>%
mutate(!!column:=fct_recode(factor(subset(sfFile,select=column,drop=T),levels=typeLevels),

names(sfFile)[names(sfFile)==LCZband]<-LCZcolumn
names(sfFile)[names(sfFile)==confidenceBand]<-confidenceColumn
print(str(sfFile))
sfFile<-sfFile%>% mutate(!!LCZcolumn:=fct_recode(factor(subset(sfFile,select=LCZcolumn,drop=T),levels=typeLevels),
!!!typeLevels)) %>%
drop_na(column)
drop_na(LCZcolumn)

cat(levels(subset(sfFile,select=column,drop=T)))
cat(levels(subset(sfFile,select=LCZcolumn,drop=T)))
#plot(sfFile)
sfFile
}
Expand All @@ -95,27 +100,25 @@ importLCZraster<-function(dirPath,zone="europe",bBox,fileName="EU_LCZ_map.tif",
else{
fileName<-paste0(dirPath,"/",fileName)
print(fileName)
if (!file.exists(fileName)){
choice<-readline(prompt="The wudapt Europe map tiff file doesn't exist in the specified directory. \n
If you want lczexplore to try and download it type 1, else type 2 to exit and get the tiff map by yourself")
print("choice: "); print(choice)
if (choice==1){
url<-"https://figshare.com/ndownloader/files/35069446"
fetchTry<-try(download.file(url=url,method="auto",destfile=fileName))
if(is(fetchTry,"try-error")){
stop("The file couldn't be downloaded, maybe try to dowload another way.")
} else {
sfFile<-effectiveImport(fileName = fileName,bBox=bBox)
sfFile
if (!file.exists(fileName)) {
stop("The raster file doesn't exist in the specified directory")
} else { # if the user specifies the number of the layer and not its name
if (is.numeric(LCZband) | is.numeric(confidenceBand)){
sfFile<-rast(fileName)
layerNames <- names(sfFile)
if (is.numeric(LCZband)) { LCZband<-layerNames[LCZband] }
if (is.numeric(confidenceBand)) { confidenceBand<-layerNames[confidenceBand] }
}
}
}
else{
sfFile<-effectiveImport(fileName = fileName,bBox=bBox)
sfFile
}

sfFile<-effectiveImport(fileName = fileName, bBox = bBox,
LCZband = LCZband, LCZcolumn = LCZcolumn,
confidenceBand = confidenceBand,
confidenceColumn = confidenceColumn)
sfFile
}
}


sfFile$geomID<-row.names(sfFile)
return(sfFile)

}
2 changes: 1 addition & 1 deletion R/produceAnalysis.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#'
#' @examples
#' produceAnalysis(location="Redon", outDir=paste0(system.file(package="lczexplore"),"/tinytest"),
#' wf1="bdtopo_2_2", wf2="osm", refYear1="2022", refYear2="2022", repr="standard", saveG=location)
#' wf1="bdtopo_2_2", wf2="osm", refYear1="2022", refYear2="2022", repr="standard", saveG="")
produceAnalysis<-function(location="Redon",
outDir=getwd(),
wf1="bdtopo_2_2",
Expand Down
Empty file added R/shinyGC/Rplots.pdf
Empty file.
Loading

0 comments on commit 9506a71

Please sign in to comment.