rm(list=ls())
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.3
library(sf)
## Warning: package 'sf' was built under R version 4.1.3
## Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(gridExtra)
library(knitr)
## Warning: package 'knitr' was built under R version 4.1.3
setwd ("F:/Slum Mapping/")


# Required data for all slums (data source: https://www.arcgis.com/home/item.html?id=4678466156d142478606baf3d6ace6d6)

dhaka_map<- read_sf("dhaka_administrative_boundaries/dhaka_administrative_boundaries.shp")

bd_map <- read_sf("BD_Admin03_11072020/BD_Admin03_UPAZILA_AND_DHAKA_CITY.shp") 

dcc_map <- dplyr::filter(bd_map, bd_map$CC_NAME %in% c("DCC South", "DCC North"))
dcc_map$slum <- NA
#dcc_map$slum[dcc_map$THA_UPA_NA=="Mirpur" & dcc_map$UNI_WAR_NA=="W-11"] <- "Kallayanpur slum"
dcc_map$slum[dcc_map$THA_UPA_NA=="Mirpur" & dcc_map$UNI_WAR_NA=="W-11" | dcc_map$THA_UPA_NA=="Darus_Salam" & dcc_map$UNI_WAR_NA=="W-10"] <- "Kallayanpur slum"
dcc_map$slum[dcc_map$THA_UPA_NA=="Kadamtali" & dcc_map$UNI_WAR_NA=="W-58"] <- "Shyampur slum"
dcc_map$slum[dcc_map$THA_UPA_NA=="Jatrabari" & dcc_map$UNI_WAR_NA=="W-49"] <- "Dholpur slum"

slum <- dplyr::filter(dcc_map, dcc_map$slum %in% c("Dholpur slum", "Kallayanpur slum", "Shyampur slum"))
slum <- slum[-c(5),]
slum_all3 <- ggplot()+
    ggspatial::annotation_map_tile(zoom = 12)+
    geom_sf(data = dcc_map, fill="white",alpha=.9)+
    geom_sf(data=slum, aes(fill=slum))+ 
    geom_sf_text(data=slum, aes(label=UNI_WAR_NA), check_overlap = FALSE, size=3, color="black")+
    labs(title="Selected slums in Dhaka City Corporation (DCC)",x="Longitude",y="Lattitude") +
    guides(fill=guide_legend(title="Legends"))+
    theme(legend.title = element_text(face = "bold"))+
    theme(axis.title.x = element_text(face = "bold"))+
    theme(axis.title.y = element_text(face = "bold"))+
    ggspatial::annotation_north_arrow(location = "bl", which_north = "true", pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in")) + 
    theme(plot.background = element_rect(color = "black", size = 2)) +
    theme(axis.line = element_line(color = "black", size = 1), 
          axis.ticks = element_line(color = "black", size = 1), 
          axis.text = element_text(color = "black", size = 11))+
    theme(panel.border = element_rect(colour = "black", fill=NA, size=1))
          
    
#show(slum_all3)



# Inset map

slum_all_inset <- ggplot()+
    geom_sf(data=bd_map, fill="white", size=0.2)+
    geom_sf(data=dcc_map, fill=NA, color="blue", size=1.2)+ 
    annotate(geom = "text", x=91,y=26.5, label="Bangladesh", size = 5, color="black")+
    theme_void()  # to skip x, y axis 
    

gg_inset_all_map <- cowplot::ggdraw()+
    cowplot::draw_plot(slum_all3) +
    cowplot::draw_plot(slum_all_inset, x=0.50, y=0.55, width=.35, height=0.35)
## Zoom: 12
#show(gg_inset_all_map)

ggsave(
  "selected_slum.png",
  plot = last_plot(),
  device = "png",
  path = "F:/Slum Mapping/ggsave",
  #scale = 1,
  width = 40,
  height = 20,
  units = "cm",
  dpi = 300,
  limitsize = TRUE,
  bg = NULL,
  )

# Shyampur slum

sh_slum_map <- dplyr::filter(dcc_map, dcc_map$slum %in% c("Shyampur slum"))

sh_slum <- readr::read_csv("shyampur_slum.csv")
## Rows: 93 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): facilitiy, details
## dbl (2): lng, lat
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
sh_slum$facilitiy <- as.factor(sh_slum$facilitiy)

slum_sh_inset <- ggplot()+
    geom_sf(data=dcc_map, fill="white", size=0.2)+
    geom_sf(data=sh_slum_map, fill=NA, color="blue", size=1.2)+ 
    theme_void()

slum_sh <- ggplot(sh_slum_map)+
    ggspatial::annotation_map_tile(zoom = 14)+
    geom_sf(fill="white", alpha=.9)+ 
    labs(title="Health related facilities in Shyampur slum in Dhaka City Corporation (DCC) South",x="Longitude",y="Lattitude") +
    geom_point(data=sh_slum, aes(x=lng, y=lat, color= facilitiy), size=5) +
    annotate(geom = "text", x=90.442,y=23.692, label="Administrative location", size = 5, color="red")+
    annotate(geom = "text", x=90.442,y=23.691, label="Thana: Kadamtali \nWard: W-58", size = 5, color="black")+
    geom_sf_text(data = sh_slum_map, aes(label=UNI_WAR_NA), size=4, color= "black")+
    scale_color_manual(values = c("Drinking water collection point" = "#56B4E9",
                                  "Eviction prone area" = "#009E73", 
                                  "Fire prone area" = "#F0E442", 
                                  "Flood prone area" = "#0072B2",
                                  "Mosque or temple" = "#CC79A7", 
                                  "Pharmacy with(out) doctor" = "#661100", 
                                  "Place for recreation or amusement park" = "#44AA99",  
                                  "School or learning centre" = "#882255", 
                                  "Toilet and washing point" = "#FF0000", 
                                  "Unsafe zone (narrow lane)" = "#6699CC", 
                                  "Waste disposal point" = "#888888"))+
    guides(color=guide_legend(title="Legends"))+
    theme(legend.title = element_text(face = "bold"))+
    theme(axis.title.x = element_text(face = "bold"))+
    theme(axis.title.y = element_text(face = "bold"))+
    ggspatial::annotation_north_arrow(location = "tr", which_north = "true", pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in")) +
    theme(plot.background = element_rect(color = "black", size = 2))+
    theme(axis.line = element_line(color = "black", size = 1),
          axis.ticks = element_line(color = "black", size = 1), 
          axis.text = element_text(color = "black", size = 11))+
    theme(panel.border = element_rect(colour = "black", fill=NA, size=1))

gg_inset_sh_map <- cowplot::ggdraw()+
    cowplot::draw_plot(slum_sh) +
    cowplot::draw_plot(slum_sh_inset, x=0.02, y=0.12, width=.40, height=0.40)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Loading required namespace: raster
## Zoom: 14
#show(gg_inset_sh_map)

ggsave(
    "selected_slum_shyampur.png",
    plot = last_plot(),
    device = "png",
    path = "F:/Slum Mapping/ggsave",
    #scale = 1,
    width = 40,
    height = 20,
    units = "cm",
    dpi = 300,
    limitsize = TRUE,
    bg = NULL,
)

# Kallayanpur slum

kl_slum_map <- dplyr::filter(dcc_map, dcc_map$slum %in% c("Kallayanpur slum"))

kl_slum <- readr::read_csv("kallayanpur_slum.csv")
## Rows: 349 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): facilitiy, details
## dbl (2): lng, lat
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
kl_slum$facilitiy <- as.factor(kl_slum$facilitiy)

slum_kl_inset <- ggplot()+
    geom_sf(data=dcc_map, fill="white", size=0.2)+
    geom_sf(data=kl_slum_map, fill=NA, color="blue", size=1.2)+ 
    theme_void()

slum_kl <- ggplot(kl_slum_map)+
    ggspatial::annotation_map_tile(zoom = 14)+
    geom_sf(fill="white", alpha=0.9)+
    labs(title="Health related facilities in Kallayanpur slum in Dhaka City Corporation (DCC) North",x="Longitude",y="Lattitude") +
    geom_point(data=kl_slum, aes(x=lng, y=lat, color=facilitiy), size=4) +
    annotate(geom = "text", x=90.355,y=23.801, label="Administrative location", size = 5, color="red")+
    annotate(geom = "text", x=90.355,y=23.798, label="Thana: Darus Salam \nWard: W-10, W-11 (Part)", size = 5, color="black")+
    geom_sf_text(data=kl_slum_map, aes(label=UNI_WAR_NA), check_overlap = FALSE, size=5, color="black")+
    scale_color_manual(values = c("Bazar" = "#000000",
                                  "Community meeting place" = "#E69F00",
                                  "Drinking water collection point" = "#56B4E9",
                                  "Fire prone area" = "#F0E442", 
                                  "Flood prone area" = "#0072B2",
                                  "Green spaces for sports" = "#D55E00", 
                                  "Mosque or temple" = "#CC79A7", 
                                  "Pharmacy with(out) doctor" = "#661100", 
                                  "Political or social club" = "#44AA99", 
                                  "Public office" = "#999933", 
                                  "School or learning centre" = "#882255", 
                                  "Toilet and washing point" = "#FF0000", 
                                  "Unsafe zone (narrow lane)" = "#6699CC", 
                                  "Waste disposal point" = "#888888"))+
    guides(color=guide_legend(title="Legends"))+
    theme(legend.title = element_text(face = "bold"))+
    theme(axis.title.x = element_text(face = "bold"))+
    theme(axis.title.y = element_text(face = "bold"))+
    ggspatial::annotation_north_arrow(location = "bl", which_north = "true", pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in")) + 
    theme(plot.background = element_rect(color = "black", size = 2)) +
    theme(axis.line = element_line(color = "black", size = 1),
          axis.ticks = element_line(color = "black", size = 1), 
          axis.text = element_text(color = "black", size = 11))+
    theme(panel.border = element_rect(colour = "black", fill=NA, size=1))

gg_inset_kl_map <- cowplot::ggdraw()+
    cowplot::draw_plot(slum_kl) +
    cowplot::draw_plot(slum_kl_inset, x=0.40, y=0.62, width=.35, height=0.35)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Zoom: 14
#show(gg_inset_kl_map)

ggsave(
    "selected_slum_kallayanpur.png",
    plot = last_plot(),
    device = "png",
    path = "F:/Slum Mapping/ggsave",
    #scale = 1,
    width = 38,
    height = 20,
    units = "cm",
    dpi = 300,
    limitsize = TRUE,
    bg = NULL,
)

# Dholpur slum

dl_slum_map <- dplyr::filter(dcc_map, dcc_map$slum %in% c("Dholpur slum"))

dl_slum_map <- dl_slum_map[-c(2),]

dl_slum <- readr::read_csv("dholpur_slum.csv")
## Rows: 137 Columns: 4
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): facilitiy, details
## dbl (2): lng, lat
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
dl_slum$facilitiy <- as.factor(dl_slum$facilitiy)

slum_dl_inset <- ggplot()+
    geom_sf(data=dcc_map, fill="white", size=0.2)+
    geom_sf(data=dl_slum_map, fill=NA, color="blue", size=1.2)+ 
    theme_void()

slum_dl <- ggplot(dl_slum_map)+
    ggspatial::annotation_map_tile(zoom = 16)+
    geom_sf(fill="white",alpha=.9)+
    labs(title="Health related facilities in Dholpur slum in Dhaka City Corporation (DCC) South",x="Longitude",y="Lattitude") +
    geom_point(data=dl_slum, aes(x=lng, y=lat, color=facilitiy), size=4) +
    annotate(geom = "text", x=90.429,y=23.723, label="Administrative location", size = 5, color="red")+
    annotate(geom = "text", x=90.429,y=23.722, label="Thana: Jatrabari \nWard: W-49", size = 5, color="black")+
    geom_sf_text(data=dl_slum_map, aes(label=UNI_WAR_NA), check_overlap = FALSE, size=5, color="black")+
    scale_color_manual(values = c("Bazar" = "#000000",
                                  "Community meeting place" = "#E69F00",
                                  "Drinking water collection point" = "#56B4E9",
                                  "Eviction prone area" = "#009E73", 
                                  "Fire prone area" = "#F0E442", 
                                  "Flood prone area" = "#0072B2",
                                  "Green spaces for sports" = "#D55E00", 
                                  "Mosque or temple" = "#CC79A7", 
                                  "Pharmacy with(out) doctor" = "#661100", 
                                  "Place for recreation or amusement park" = "#44AA99",  
                                  "Political or social club" = "#44AA99", 
                                  "Public office" = "#999933", 
                                  "School or learning centre" = "#882255", 
                                  "Toilet and washing point" = "#FF0000", 
                                  "Unsafe zone (narrow lane)" = "#6699CC", 
                                  "Waste disposal point" = "#888888"))+
    guides(color=guide_legend(title="Legends"))+
    theme(legend.title = element_text(face = "bold"))+
    theme(axis.title.x = element_text(face = "bold"))+
    theme(axis.title.y = element_text(face = "bold"))+
    ggspatial::annotation_north_arrow(location = "bl", which_north = "true", pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in")) + 
    theme(plot.background = element_rect(color = "black", size = 2)) +
    theme(axis.line = element_line(color = "black", size = 1),
          axis.ticks = element_line(color = "black", size = 1), 
          axis.text = element_text(color = "black", size = 11))+
    theme(panel.border = element_rect(colour = "black", fill=NA, size=1))

gg_inset_dl_map <- cowplot::ggdraw()+
    cowplot::draw_plot(slum_dl) +
    cowplot::draw_plot(slum_dl_inset, x=0.52, y=0.15, width=.40, height=0.40)
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Zoom: 16
#show(gg_inset_dl_map)

ggsave(
    "selected_slum_dholpur.png",
    plot = last_plot(),
    device = "png",
    path = "F:/Slum Mapping/ggsave",
    #scale = 1,
    width = 35,
    height = 20,
    units = "cm",
    dpi = 300,
    limitsize = TRUE,
    bg = NULL,
)


# grid.arrange(map0,map1,map2,nrow=1)