Sadly, the ‘gridsample’ package has been removed from the R CRAN repository R CRAN, and the archived version fails to install. However, it is working up to the flowing step without the ‘gridsample’ package.
library(raster)
population_raster <- raster("RWA_ppp_2010_adj_v2.tif")
plot(population_raster)
A population raster and a shapefile RWAshp defining the various strata in the census. Here, the strata shapefile consists of the 30 districts.
load("C:/Users/nasif/Desktop/test/RWAshp.rda")
strata_raster <- rasterize(RWAshp,population_raster,field = "STL.2")
plot(strata_raster)
To classify urban and rural areas, a raster will be created based on population density. Urban areas will be defined by selecting the densest cells.
total_pop <- cellStats(population_raster, stat = "sum")
urban_pop_value <- total_pop * .16
pop_df <- data.frame(index = 1:length(population_raster[]),
pop = population_raster[])
pop_df <- pop_df[!is.na(pop_df$pop), ]
pop_df <- pop_df[order(pop_df$pop,decreasing = T), ]
pop_df$cumulative_pop <- cumsum(pop_df$pop)
pop_df$urban <- 0
pop_df$urban[which(pop_df$cumulative_pop <= urban_pop_value)] <- 1
urban_raster <- population_raster >= min(subset(pop_df,urban == 1)$pop)
plot(urban_raster)