Lesson 1, Topic 5
In Progress

Mapping Slums – Visualization

Lesson Progress
0% Complete

After getting good results from validating your data, your instructor will teach you how to create and visualize the raster stack in R – Studio. You will import and visualize the previously shown raster data set and learn how to create the stack from it. This is important, because in the next step your raster stack and your previously calculated random forest model are going to be combined in order to get a final result, showing the prediction of a prospective slum settlement.

You can find the code which is used for this topic below.

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Code used for this topic
############################################




##########Create Raster stack_Random forestReg######

#Load required Raster
Conflict2Eq <- raster("cova_mooc/Conflict2_Eqdist.tif")
DEM1<- raster("cova_mooc/DEM1.tif")
GovtbuildCS <- raster("cova_mooc/Govtbuild_CSTdist.tif")
MainRoadEQd <- raster("cova_mooc/Main_Road_EQdist.tif")
Slope1 <- raster("cova_mooc/Slope1.tif")
SocioeconID <- raster("cova_mooc/Socioecon_IDW.tif")
WatelineEQd <- raster("cova_mooc/Wate_line_EQdist.tif")
churchCSTdi <- raster("cova_mooc/church_CSTdist.tif")
climaterisk <- raster("cova_mooc/climate_risk.tif")
dumpCSTdist <- raster("cova_mooc/dump_CSTdist.tif")
factIndCSTd <- raster("cova_mooc/fact_Ind_CSTdist.tif")
hzdindex <- raster("cova_mooc/hzd_index.tif")
marktCSTdis <- raster("cova_mooc/markt_CSTdist.tif")
mosqueCSTdi <- raster("cova_mooc/mosque_CSTdist.tif")
pubschCSTdi <- raster("cova_mooc/pub_sch_CSTdist.tif")
pubwaterCST <- raster("cova_mooc/pub_water_CSTdist.tif")
smallsettCS <- raster("cova_mooc/smallsett_CSTdist.tif")

smallsettCS
pubwaterCST

#####Stack raster#####
stack_Cova = stack(Conflict2Eq, DEM1, GovtbuildCS, MainRoadEQd, Slope1, SocioeconID, WatelineEQd, churchCSTdi, climaterisk, dumpCSTdist, factIndCSTd, hzdindex, marktCSTdis, mosqueCSTdi, pubschCSTdi, pubwaterCST, smallsettCS)


#####plot stack to visualizee########
plot(stack_Cova)

#save
file.output <- paste0(output_folder,"/Image_stack.tif")
writeRaster(stack_Cova,file.output,datatype="FLT4S", format = "GTiff", overwrite=TRUE)

Fitting the Model

This step starts with a short reconsideration of what happened in the last view steps. The aim of this part of teh process is to fit your model into the process of calculating a final outcome. Therefore you will have to apply the previously exported data from the random forrest model and the raster stack you’ve just created and combine them. You’ll learn how to to import the neceessary data into R-Studio to finally calculate an output that shows the prediction of slum settlement in our study area of Lagos. Also for a faster processing you will be taught how you can split the stack into blocks sizes.

At the end of the lecture you will be able to visualize your result in R-Studio but for a better visualization you will learn how to edit it in Q-GIS as well- which will be the final step of this course.

You can find the code which is used for this topic below.

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Code used in this example
######################Fit Random forest###############

######### INITAL PARAMETERS ############

#inputs
input_model <- "Result_mooc/RF_model.rds"
input_stack <- "Result_mooc/Image_stack.tif"


#others
number_cores <- 6


########## PREPARE DATA ############

#####read stack
sta.vars <- stack(input_stack)

######read model
log.mod <- readRDS(input_model)

####Visualize data#####
varImpPlot(log.mod)
log.mod$importance
print(sta.vars)
plot(sta.vars)

#######add names
log.vars <- rownames(log.mod$importance)
names(sta.vars) <- log.vars

######split stack into block for faster processing
img.blocks <- blockSize(sta.vars)

######temp data
csv.fol <- paste0(output_folder,"/csv");dir.create(csv.fol,showWarnings=F)

########## PROCESSING... ############

#turn on cores to run parallel each  bricks
detectCores()
cl<-makeCluster(number_cores)
registerDoParallel(cl)


#start parallel
foreach(i=1:img.blocks$n,.packages=c("raster","randomForest")) %dopar% {
  #get data
  data.input <- as.data.frame(getValues(sta.vars,row=img.blocks$row[i],nrows=img.blocks$nrows[i]))
  #apply model
  data.input <- predict(log.mod,data.input,type = "prob")
  data.input <- data.input[,2]
  #save outputs
  output.name <- paste0(csv.fol,'/pred_',i,'.csv')
  write.csv(data.input,output.name)
}
stopCluster(cl)



########## READ AND JOIN ############
#get the csv data
csv.files <- mixedsort(list.files(csv.fol,full.names=T,pattern=".csv"))
csv.values <- lapply(csv.files,function(x){
  x.val <- read.csv(x)[,2]
  return(x.val)
})
#add to raster
template <- sta.vars[[1]]
values(template) <- unlist(csv.values)
names(template) <- "prediction"

####Visulae predicted map######
plot(template)


#save
output.name <- paste0(output_folder,"/prediction_RF.tif")
writeRaster(template,output.name,overwrite=TRUE)

Visualization of the Model

In the last step of this lecture you’ll learn how to finally visualize your model in Q-GIS in order to get a meaningful final outcome from all the previous steps of this mini – MOOC. There will be different methods, how to visualize the probability of a prospective slum settlement explained to you.

YouTube

By loading the video, you agree to YouTube’s privacy policy.
Learn more

Load video

Click the ‘Submit’ Button to rate this topic.