library(terra)
library(geodata)  
library(rnaturalearth)  
library(sf)
library(cowplot)
library(ggspatial)
library(ggplot2)

setwd("C:/Users/HOLBORNM/Documents/Tobique SAS/Maps/")

Canada_adm <- gadm(country = "Canada", level =1, path = "/outputs")
USA_adm <- gadm(country = "United States", level = 1, path = "/outputs")


#Shapefile downloaded from https://maps.canada.ca/czs/index-en.html#EXTRACT_GEOMETRY
#   Draw rectangle shape around area to clip of interest and extract the CanVec data
#   Upload shapefile in QGIS and edit out the extra features (select and delete) such as streams, ponds, lakes, water not in area of interest
#   Export and upload here
new_waterbody_2 <- vect("C:/Users/HOLBORNM/Documents/Tobique SAS/Maps/data/CanVec_NB_Waterbody_2_reducedfeatures3.shp")

TOB_ext <- ext(-68, -65.7, 44.5, 47.5)  # xmin, xmax, ymin, ymax

###Labels
# Saint John River
# Tobique River

###Markers
# Mactaquac Dam
#     45.954222, -66.868278
# Beechwood Dam
#     46.541694, -67.669639
# Tobique Narrows Dam
#     46.775431, -67.690947
# Two Brooks release site
#     47.071741356440036, -67.29410358985119
# Tobique Barrier release site
#     47.24069092787446, -67.1428566724453
#Beaumaster Flats release site
#     46.684596, -67.726343
#Perth-Andover release site
#     46.736672, -67.708080


###Cities?
# Saint John
#     45.273918, -66.067657
# Fredericton
#     45.9454, -66.6656


Coords <- data.frame(location = c("Mactaquac", "Beechwood", "TobNarrows",
                                  "TwoBrooks", "TobBarrier", "PerthAndover", 
                                  "SaintJohn", "Fredericton"),
                     lat = c(45.954222, 46.541694, 46.775431, 
                             47.071741356440036, 47.24069092787446, 46.736672,
                             45.273918, 45.9454), 
                     long = c(-66.868278, -67.669639, -67.690947, 
                              -67.29410358985119, -67.1428566724453, -67.708080,
                              -66.067657, -66.6656), 
                     featuretype = c("Dam", "Dam", "Dam", 
                                     "Release Site", "Release Site",  "Release Site",  
                                     "City", "City"))

str(Coords)


Cities <- data.frame(location = c("SaintJohn", "Fredericton", "Edmundston"),
                     lat = c(45.273918, 45.9454, 47.369013), 
                     long = c(-66.067657, -66.6656, -68.326674))

## Simplify the coastline
# I do not like how this looks - maybe come back to it later
#Canada_simplif <- simplifyGeom(Canada_adm, tol = 0.007)
#USA_simplif <- simplifyGeom(USA_adm, tol = 0.007)

#plot(Canada_simplif, ext = TOB_ext)
#plot(USA_simplif, add = TRUE)
#plot(new_waterbody_2, ext = TOB_ext, col="darkorchid3", add=TRUE)


#terra::plot(Canada_adm, ext = TOB_ext)
#terra::plot(USA_adm, ext = TOB_ext, add = TRUE)
#terra::plot(new_waterbody_2, ext = TOB_ext, col="seagreen4", add=TRUE)


###################################################################################################

###################################################################################################

###################################################################################################
######################### Try with rNaturalEarth

world <- ne_countries(scale = "large", returnclass = "sf")
TOB_cropped <- st_crop(world, xmin = -68, xmax = -65.7,
                       ymin = 44.5, ymax = 47.5) #cropping map


rivers <- st_read("C:/Users/HOLBORNM/Documents/Tobique SAS/Maps/data/CanVec_NB_Waterbody_2_reducedfeatures3.shp")

rivers_cropped <- st_crop(rivers, xmin = -68, xmax = -65.7,
                          ymin = 44.5, ymax = 47.5)

ggplot() + 
  geom_sf(data = TOB_cropped) + 
  geom_sf(data = rivers_cropped, col= "maroon") +
  theme_bw()


ggplot() + 
  geom_sf(data = TOB_cropped) + 
  geom_sf(data = rivers_cropped, fill= "seagreen4", col = "seagreen4") +
  scale_x_continuous(limits = c(-68, -65.7), expand = c(0, 0)) +
  scale_y_continuous(limits = c(44.5, 47.5), expand = c(0, 0)) +
  xlab(" ") +
  ylab(" ") +
  annotate("text", x=-67.7, y=45, label= "Maine\n(USA)", size=3) +
  annotate("text", x=-67.0, y=45.5, label= "New Brunswick\n(Canada)", size=3) +
  annotate("text", x=-66.3, y=44.8, label= "Bay of\nFundy", size=3) +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, linewidth=1))


# Add state/provincial boundaries
state_prov <-ne_states( returnclass = "sf")
inset_cropped <- st_crop(world, xmin = -70, xmax = -60,
                         ymin = 42, ymax = 50) #cropping map
#state_prov_cropped <- st_crop(state_prov, xmin = -70, xmax = -60,
#                              ymin = 42, ymax = 50) #cropping map
#This worked before, but not now - s2 geography errors
#Map plot still seems to work without the cropping though


ggplot() + 
  geom_sf(data = inset_cropped) +
  geom_sf(data = state_prov, fill = NA) +
  scale_x_continuous(limits = c(-70, -60), expand = c(0, 0)) +
  scale_y_continuous(limits = c(43, 50), expand = c(0, 0)) +
  xlab(" ") +
  ylab(" ") +
  annotate(geom = "rect", xmin = -68, xmax = -65.7, ymin = 44.5, ymax = 47.5, colour = "red", fill = NA, linewidth=1) +
  annotate("text", x=-68.9, y=45.5, label= "Maine\n(USA)", size=3) +
  annotate("text", x=-66.4, y=46.5, label= "New\nBrunswick", size=3) +
  annotate("text", x=-66.3, y=44.8, label= "Bay of\nFundy", size=3) +
  annotate("text", x=-66.3, y=48.6, label= "Quebec", size=3) +
  annotate("text", x=-62.7, y=45.3, label= "Nova\nScotia", size=3) +
  annotate("text", x=-62.0, y=43.8, label= "Atlantic\nOcean", size=3) +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, linewidth=1))

################################################################################
#   Lets try insetting the larger map

TOB_Plot <- ggplot() + 
  geom_sf(data = TOB_cropped) + 
  geom_sf(data = rivers_cropped, fill= "#849CAF", col = "#849CAF") +
  scale_x_continuous(limits = c(-68, -65.7), expand = c(0, 0)) +
  scale_y_continuous(limits = c(44.5, 47.5), expand = c(0, 0)) +
  xlab("Longitude") +
  ylab("Latitude") +
  annotate("text", x=-67.7, y=45, label= "Maine\n(USA)", size=3) +
  annotate("text", x=-67.0, y=45.5, label= "New Brunswick\n(Canada)", size=3) +
  annotate("text", x=-66.3, y=44.8, label= "Bay of\nFundy", size=3) +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, linewidth=1))

# Create custom shapes, sizes, colors
# Order: City, Dam, Release Site 
pt_colors=c("black", "black", "#BF520F")
shapes = c(19, 19, 15)
shape_size = c(2, 0.5, 2)


p1 <- TOB_Plot +
  geom_point(data = Coords, aes(x=long, y=lat, colour = featuretype, shape = featuretype, size = featuretype)) +
  scale_color_manual(values=pt_colors) + 
  scale_shape_manual(values=shapes) + 
  scale_size_manual(values=shape_size) +
  annotate("text", x=-65.93, y=45.3, label= "Saint John", size=2.25) +
  annotate("text", x=-66.7, y=45.91, label= "Fredericton", size=2.25) +
  annotate("text", x=-67.00, y=47.30, label= "Tobique Barrier", size=3) +
  annotate("text", x=-67.12, y=47.07, label= "Two Brooks", size=3) +
  annotate("text", x=-67.52, y=46.72, label= "Perth-Andover", size=3) +
  annotate("text", x=-67.68, y=46.85, label= "Tobique Narrows\nDam", size=3) +
  annotate("text", x=-67.00, y=45.97, label= "Mactaquac\nDam", size=3) +
  annotate("text", x=-67.46, y=46.55, label= "Beechwood Dam", size=3) +
  labs(colour = " ") +
  labs(shape = " ") +
  labs(size = " ") +
  ggspatial::annotation_north_arrow(location = "tr", which_north = "true", height = unit(0.8, "cm"), width = unit(0.8, "cm"),
                                    pad_x = unit(0.5, "cm"), pad_y = unit(0.5, "cm"),
                                    style = ggspatial::north_arrow_fancy_orienteering(text_size = 7)) +
  ggspatial::annotation_scale(location = "bl", bar_cols = c("grey60", "white")) +
  theme(legend.position = "bottom")
p1



Large_Plot <- ggplot() + 
  geom_sf(data = inset_cropped) +
  geom_sf(data = state_prov, fill = NA) +
  scale_x_continuous(limits = c(-70, -60), expand = c(0, 0)) +
  scale_y_continuous(limits = c(43, 50), expand = c(0, 0)) +
  xlab(" ") +
  ylab(" ") +
  annotate(geom = "rect", xmin = -68, xmax = -65.7, ymin = 44.5, ymax = 47.5, colour = "black", fill = NA, linewidth=0.65) + #linewith was 1 but very thick on pdf 
  annotate("text", x=-68.9, y=45.5, label= "Maine\n(USA)", size=2.4) +
  annotate("text", x=-66.6, y=46.5, label= "New\nBrunswick", size=2.4) +
  annotate("text", x=-66.3, y=44.85, label= "Bay of\nFundy", size=2.2) +
  annotate("text", x=-66.25, y=48.6, label= "Quebec", size=2.4) +
  annotate("text", x=-62.7, y=45.3, label= "Nova\nScotia", size=2.4) +
  annotate("text", x=-62.0, y=43.8, label= "Atlantic\nOcean", size=2.4) +
  theme(panel.background = element_blank(),
        panel.grid.major = element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        panel.border = element_rect(colour = "black", fill=NA, linewidth=1))
Large_Plot



## Placement of the inset seems to "move" based on the plot window size
## Plot window size for current parameters
#         #Width 1348
#         #Height 799
map_with_inset <- ggdraw() +
  draw_plot(p1) +
  draw_plot(Large_Plot, x = 0.655, y = 0.5, width = 0.4, height = 0.4)
map_with_inset
