###Ayesha Khanam

# 1. Load Required Libraries
library(bangladesh)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library(tmap) # Essential for creating thematic maps

# 2. Load District data
district_data <- get_map("district")

# 3. Load Upazila data
upazila_data <- get_map("upazila")
# 4. Filter for Khulna District
khulna_district <- district_data %>%
  filter(District == "Khulna")

# 5. Create the map: Khulna District
khulna_map <- tm_shape(khulna_district) +
  tm_polygons(
    col = "District", 
    palette = "Pastel1", 
    title = "District Boundary"
  ) +
  tm_borders(col = "black", lwd = 1.5) + # Boundary
  tm_text("District", size = 1.0, fontface = "bold") + # Labels
  tm_layout(
    title = "Map of Khulna District", # Cartographic Element: Title
    title.size = 1.5,
    legend.show = FALSE
  ) +
  tm_compass(
    position = c("right", "top"), 
    type = "4star", 
    size = 2 # Cartographic Element: Compass
  ) +
  tm_scalebar(
    position = c("left", "bottom"), 
    text.size = 0.8 # Cartographic Element: Scale Bar
  ) 
## 
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_polygons()`: migrate the argument(s) related to the scale of
## the visual variable `fill` namely 'palette' (rename to 'values') to fill.scale
## = tm_scale(<HERE>).
## [v3->v4] `tm_polygons()`: use 'fill' for the fill color of polygons/symbols
## (instead of 'col'), and 'col' for the outlines (instead of 'border.col').
## [v3->v4] `tm_polygons()`: migrate the argument(s) related to the legend of the
## visual variable `fill` namely 'title' to 'fill.legend = tm_legend(<HERE>)'
## [v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`
# 6. Display the District map
print(khulna_map)
## The visual variable `fill` of the layer "polygons" contains a unique value.
## Therefore a categorical scale is applied (tm_scale_categorical).
## [cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Pastel1" is named
## "brewer.pastel1"
## Multiple palettes called "pastel1" found: "brewer.pastel1", "hcl.pastel1". The first one, "brewer.pastel1", is returned.
## Warning: Number of levels of the variable assigned to the aesthetic "fill" of
## the layer "polygons" is 64, which is larger than n.max (which is 30), so levels
## are combined.

# 7. Filter for Bagerhat Sadar Upazila (part of Khulna Division)
bagerhat_sadar_upazila <- upazila_data %>%
  filter(Upazila == "Bagerhat Sadar") 

# 8. Create the map: Bagerhat Sadar Upazila
upazila_map <- tm_shape(bagerhat_sadar_upazila) +
  tm_polygons(
    col = "District", 
    palette = "Reds", 
    lwd = 1.5
  ) +
  tm_text("Upazila", size = 0.9, fontface = "italic") + # Labels
  tm_layout(
    title = "Map of Bagerhat Sadar Upazila", # Cartographic Element: Title
    title.size = 1.3
  ) +
  tm_grid(
    lines = TRUE, 
    labels.size = 0.7, 
    col = "gray80" # Cartographic Element: Graticule/Grid
  ) +
  tm_compass(
    position = c("left", "top"), 
    type = "arrow", 
    size = 3 # Cartographic Element: Compass
  ) +
  tm_scalebar(
    position = c("right", "bottom"), 
    text.size = 0.7 # Cartographic Element: Scale Bar
  ) +
  tm_legend(show = FALSE)
## 
## ── tmap v3 code detected ───────────────────────────────────────────────────────
## [v3->v4] `tm_tm_polygons()`: migrate the argument(s) related to the scale of
## the visual variable `fill` namely 'palette' (rename to 'values') to fill.scale
## = tm_scale(<HERE>).
## [v3->v4] `tm_layout()`: use `tm_title()` instead of `tm_layout(title = )`
# 9. Display the Upazila map
print(upazila_map)
## [v3->v4] `tm_legend()`: use 'tm_legend()' inside a layer function, e.g.
## 'tm_polygons(..., fill.legend = tm_legend())'
## The visual variable `fill` of the layer "polygons" contains a unique value.
## Therefore a categorical scale is applied (tm_scale_categorical).
## [cols4all] color palettes: use palettes from the R package cols4all. Run
## `cols4all::c4a_gui()` to explore them. The old palette name "Reds" is named
## "brewer.reds"
## Multiple palettes called "reds" found: "brewer.reds", "matplotlib.reds". The first one, "brewer.reds", is returned.
## 
## This message is displayed once every 8 hours.
## Warning: Number of levels of the variable assigned to the aesthetic "fill" of
## the layer "polygons" is 64, which is larger than n.max (which is 30), so levels
## are combined.