Initialize dataset

Read File

file.loc = "C:/Users/rnussba1/OneDrive/ARK/ARK - Science/Dakatcha Nature Reserve/4. Biodiversity studies/BiodiversitySurveys/Birds/data/Birdlasser_Event_Dakatcha_Reserve/BL_EVENT_dakatchareserve.csv"

t <- read_csv(file.loc, col_types = cols(
  englishName = col_character(),
  sabap2 = col_double(),
  date = col_date(format = ""),
  time = col_time(format = ""),
  locationLat = col_double(),
  locationLon = col_double(),
  lead = col_character()
))

Clean observer

Add acronym of each observer. The default email adress is long and not nice to read.

observers <- data.frame(
  email=c("colin.jackson@arocha.org", "albert_baya@yahoo.com", "rafnuss@gmail.com", "lennoxk9@gmail.com", "sammykatisho@gmail.com", "kzlughanje@live.com", "ciaran.smyth5@gmail.com"),
  acro = c("CJ","AB","RN","LK","SK","ZK","CS")
)

t <- t %>% left_join(observers, by=c("lead" = "email")) %>% select(-lead)

Add grid reference

dl = 1/120;
origin = c(4772,-345)*dl;
label.lng = c('1','2','3','4','5','6','7','8','9','10')
label.lat = c('A','B','C','D','E','F','G','H','I','J','K','L','M','N')
  
GPStoGrid <- function(lat, lng) {
  paste(label.lat[ceiling( -(lat-origin[2])/dl )],label.lng[ceiling( (lng-origin[1])/dl )], sep='')
}

t <- t %>% mutate(grid = GPStoGrid(locationLat, locationLon))

Clean Species

species.excluded = c('Black Boubou',"Abdim's Stork","Aberdare Cisticola","Abyssinian Ground Hornbill")

t <- t %>% filter( !englishName %in% species.excluded )

List of Specie

tspe = t %>% group_by(englishName,sabap2)  %>% summarise(NbObs = n(), NbGrid = n_distinct(grid), Obs = paste(unique(acro), collapse = ', ')) %>% arrange(sabap2)
## `summarise()` regrouping output by 'englishName' (override with `.groups` argument)
datatable(tspe, filter = "top", extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = c('csv')), rownames = FALSE, class = "compact")

Grid base

grids = t %>% group_by(grid) %>% 
  summarise(NbObs = n(), NbSpe = n_distinct(englishName), Obs = paste(unique(acro), collapse = ', '))
## `summarise()` ungrouping output (override with `.groups` argument)
kml <- readr::read_file("C:/Users/rnussba1/OneDrive/ARK/ARK - Science/Dakatcha Nature Reserve/4. Biodiversity studies/BiodiversitySurveys/Birds/analysis/Target_Reserve.kml")


m = leaflet(width = "100%") %>%
  addProviderTiles("MapBox", options = providerTileOptions(
    id = "mapbox.satellite",
    accessToken = 'pk.eyJ1IjoicmFmbnVzcyIsImEiOiIzMVE1dnc0In0.3FNMKIlQ_afYktqki-6m0g')) %>% 
  addFullscreenControl() %>% 
  addMarkers(t$locationLon, t$locationLat,
              clusterOptions = markerClusterOptions()
  ) %>% 
  addKML(kml, color='#000000',fillOpacity = 0,opacity =1)

bins <- c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90) 
pal <- colorBin("YlOrRd", domain = grids$NbSpe, bins = bins)


for (dlat in 0:length(label.lat)){
  for (dlng in 0:length(label.lng)){
    lng = origin[1] + dl*c(dlng,dlng+1,dlng+1,dlng, dlng)
    lat = origin[2] - dl*c(dlat,dlat,dlat+1,dlat+1,dlat)
    g = paste0(label.lat[dlat+1],label.lng[dlng+1])

    if (any(grids$grid == g)){
      NbObs = grids$NbObs[grids$grid == g]
      NbSpe = grids$NbSpe[grids$grid == g]
      Obs = grids$Obs[grids$grid == g]
    } else {
      NbObs = 0
      NbSpe = 0
      Obs = 0
    }

    labels <- sprintf(
  "<strong>%s</strong><br/>%g Species | %g Observations<br>Observers: %s",
  g, NbSpe, NbObs, Obs
) %>% lapply(htmltools::HTML)
    
    # Create the polygon
    m = m %>% 
      addPolygons(lng, lat, 
                  fillColor = pal(NbSpe),
                  weight = 2,
                  opacity = 1,
                  color = "white",
                  dashArray = "3",
                  fillOpacity = 0.5,
                  highlight = highlightOptions(
                    weight = 5,
                    color = "#666",
                    dashArray = "",
                    fillOpacity = 0.7,
                    bringToFront = TRUE),
                  label = labels,
                  labelOptions = labelOptions(
                    style = list("font-weight" = "normal", padding = "3px 8px"),
                    textsize = "15px",
                    direction = "auto"
                    )
                  )
  }
}


m %>% 
  addLegend(pal = pal, values = grids$NbSpe, opacity = 0.5, title = 'he', position = "bottomright")