Skip to content

Commit

Permalink
summariseRSUs, fixed with dplyr::summarise
Browse files Browse the repository at this point in the history
  • Loading branch information
MGousseff committed Jan 16, 2025
1 parent 2cbb61d commit bb8c20e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
57 changes: 57 additions & 0 deletions R/plotSummarisedRSUs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
plotSummarisedRSUs<-function(longSf, workflows = c("osm", "bdt", "iau", "wudapt"), plot = TRUE, locations = NULL, graphPath = ""){
print(locations)
colorMap<-c("#8b0101","#cc0200","#fc0001","#be4c03","#ff6602","#ff9856",
"#fbed08","#bcbcba","#ffcca7","#57555a","#006700","#05aa05",
"#648423","#bbdb7a","#010101","#fdf6ae","#6d67fd", NA)
names(colorMap)<-as.character(c(1:10,101:107, NA))
etiquettes<-c("LCZ 1: Compact high-rise","LCZ 2: Compact mid-rise","LCZ 3: Compact low-rise",
"LCZ 4: Open high-rise","LCZ 5: Open mid-rise","LCZ 6: Open low-rise",
"LCZ 7: Lightweight low-rise","LCZ 8: Large low-rise",
"LCZ 9: Sparsely built","LCZ 10: Heavy industry",
"LCZ A: Dense trees", "LCZ B: Scattered trees",
"LCZ C: Bush,scrub","LCZ D: Low plants",
"LCZ E: Bare rock or paved","LCZ F: Bare soil or sand","LCZ G: Water", "ghostwhite")


summarisedRSUs<-matrix(ncol=6, nrow=0) %>% as.data.frame
names(summarisedRSUs)<-c("lcz", "numberRSUs", "meanArea", "numberRSUsClust", "meanAreaClust", "wf")
if(is.null(locations) | length(locations)==0){
locations<-unique(longSf$location)
}

for (wfi in workflows){
print(wfi)
sfIn<-longSf[longSf$location%in%locations & longSf$wf==wfi,]
dfOut<-summariseRSUs(sfIn, column = "lcz_primary" )
dfOut$wf<-wfi
summarisedRSUs<-rbind(summarisedRSUs, dfOut)
}

summarisedRSUs$lcz<-factor(summarisedRSUs$lcz,
levels = c(as.character(1:10),as.character(101:107)))

summarisedRSUs$meanArea<-round(summarisedRSUs$meanArea)
summarisedRSUs$meanAreaClust<-round(summarisedRSUs$meanAreaClust)
summarisedRSUs<-summarisedRSUs %>% arrange(wf,lcz)


#
outPlot<-ggplot() +
# geom_point(data = summarisedRSUs, aes(x=numberRSUs, y = meanArea, color = lcz), size = 2) +
geom_point(data = summarisedRSUs, aes(x=numberRSUsClust, y = meanAreaClust, color = lcz), size = 2) +
scale_color_manual(values=colorMap, breaks = names(colorMap), labels = etiquettes, na.value = "ghostwhite")+
facet_wrap(vars(wf))
if(plot){print(outPlot)}

if(!is.null(graphPath) && length(graphPath)==1 && nchar(graphPath)>1){
if(substr(graphPath, nchar(graphPath), nchar(graphPath))!="/"){graphPath<-paste0(graphPath, "/")}
graphName<-paste0(graphPath, paste0(locations, collapse = "_"), ".png")
ggsave(graphName, outPlot)
}

output<-list(outPlot=outPlot, summarisedRSUs=summarisedRSUs)
return(output)

}

# plotSummarisedRSUs(longSf = allLocAllWfs)
19 changes: 16 additions & 3 deletions R/summariseRSUs.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
#' with same level of LCZ which touch each other
#' @export
#' @examples
summariseRSUs<-function(sf, column ){
summariseRSUs<-function(sf, column = "lcz_primary" ){
# sf<-sf %>% mutate(area=st_area(geometry)) %>% st_drop_geometry() %>% as.data.frame

unClustered <-sf %>% group_by(.data[[column]]) %>%
summarise(numberRSUs=n(), meanArea = round(mean(st_area(geometry)), digits = 0)) %>% st_drop_geometry()

clustered <-sf %>% group_by(.data[[column]]) %>% summarise() %>% ungroup %>%
clustered <-sf %>% group_by(.data[[column]]) %>% summarize() %>% ungroup %>%
st_cast("MULTIPOLYGON") %>% st_cast("POLYGON") %>% group_by(.data[[column]]) %>%
summarise(numberRSUsClust=n(), meanAreaClust = round(mean(st_area(geometry)), digits = 0)) %>% st_drop_geometry
output <- full_join(unClustered,clustered, by = column)
Expand All @@ -23,4 +23,17 @@ summariseRSUs<-function(sf, column ){
}



summariseRSUs<-function (sf, column)
{
unClustered <- sf %>% group_by(.data[[column]]) %>% dplyr::summarise(numberRSUs = n(),
meanArea = round(mean(st_area(geometry)), digits = 0)) %>%
st_drop_geometry()
clustered <- sf %>% group_by(.data[[column]]) %>% dplyr::summarise() %>%
ungroup %>% st_cast("MULTIPOLYGON") %>% st_cast("POLYGON") %>%
group_by(.data[[column]]) %>% dplyr::summarise(numberRSUsClust = n(),
meanAreaClust = round(mean(st_area(geometry)), digits = 0)) %>%
st_drop_geometry
output <- full_join(unClustered, clustered, by = column)
names(output)[1] <- "lcz"
return(output)
}
2 changes: 1 addition & 1 deletion inst/tinytest/test_summariseRSUs.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ showLCZ(test, column="lcz_primary")+geom_sf_text(aes(label = round(drop_units(ar


library(sf)
allLocAllWfs<-read_sf("/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/newDataTree/AllLocAllWorkflows.fgb")
allLocAllWfs<-read_sf("/home/gousseff/Documents/3_data/data_article_LCZ_diff_algos/newDataTree/allLocAllWfs.fgb")
# summary(allLocAllWfs)
RSUsSummarisedAllLoc<-matrix(ncol=6, nrow=0) %>% as.data.frame
names(RSUsSummarisedAllLoc)<-c("lcz", "numberRSUs", "meanArea", "numberRSUsClust", "meanAreaClust", "wf")
Expand Down

0 comments on commit bb8c20e

Please sign in to comment.