###Problem 17

library(tidygeocoder)
## Warning: package 'tidygeocoder' was built under R version 4.1.3
data <- read.csv("D:/charan/Documents/FIFA 18/STAT651/Violations_loc (1).csv")

head(data)

###1

Here the latitude and longitude of Emily Dickinson Museum in Amherst, Massachusetts be 42.37615 and -72.5144

library(pacman)
p_load(tidygeocoder,tidyverse,mdsr,sf,ggspatial, leaflet,macleish, dplyr)

Emily <- tibble(
  address = "Emily Dickinson Museum"
) %>%
  tidygeocoder::geocode(address, method = "osm")
## Passing 1 address to the Nominatim single address geocoder
## Query completed in: 1 seconds
Emily
emily_map <- leaflet() %>% 
  addTiles() %>% 
  addMarkers(data= Emily)
## Assuming "long" and "lat" are longitude and latitude, respectively
emily_map

###18.1(a)

The Spatial co-ordinates for these restaurants be.

head(Violations)
viol2 <- Violations %>% mutate(state = "NY")

comb_locations <- viol2 %>% 
  filter(building != 0) %>% 
  select(street, zipcode, dba, boro, state) %>% 
  unite(loc, street, boro, state, zipcode, sep=",")
unique_locations <- comb_locations %>% 
  unique() %>% 
  drop_na()

count_location <- comb_locations %>% group_by(loc) %>% 
  summarise(
    count= n()
  )

remove_na <- unique_locations[1:100,] %>% 
  drop_na()

#restaurants <- tibble(address = remove_na$loc, business = remove_na$dba) %>% 
#tidygeocoder::geocode(address, method = "osm")


restaurants <- readRDS("restaurants.rds")
restaurants
#saveRDS(restaurants, "restaurants.rds")

###18.2(b)

rest2 <- restaurants %>% drop_na()

rest_locations <- rest2 %>% 
  st_as_sf(coords=c("long","lat")) %>% 
  st_set_crs(4326)
rest_locations
Viol1 <- Violations %>% mutate(state = "NYC")

combvoil1 <- Viol1 %>% 
  filter(building != 0) %>% 
  select(street, zipcode, dba, boro, state) %>% 
  unite(loc, street, boro, state, zipcode, sep=",")
unique_locations <- combvoil1%>% 
  unique() %>% 
  drop_na()

count_loc <- combvoil1 %>% group_by(loc) %>% 
  summarise(
    count= n()
  )
count_loc
st_bbox(rest_locations)
##      xmin      ymin      xmax      ymax 
## -77.59250  40.51693 -73.69327  43.15002
rest_merged <- rest_locations %>% 
  left_join(count_location, by = c("address" = "loc"))

drop_X <- rest_merged[-c(63,64),]

ggplot(drop_X) + 
  geom_sf(aes(size = count), alpha = 0.5)

###18.1(c)

rest_merged2 <- restaurants %>% 
  left_join(count_loc, by = c("address" = "loc"))

drop_x1 <- rest_merged2[-c(63,64),]

leaflet() %>% 
  addTiles() %>% 
  addMarkers(
    lng = drop_x1$long, 
    lat = drop_x1$lat, 
    popup = drop_x1$business
             )
## Warning in validateCoords(lng, lat, funcName): Data contains 9 rows with either
## missing or invalid lat/lon values and will be ignored

###18.2

  1. The Interactive static map of the MacLeish Field Station property
boundary <- macleish_layers %>%
  pluck("boundary")

stream <- macleish_layers %>%
  pluck("streams")

building <- macleish_layers %>%
  pluck("buildings")

trail <- macleish_layers %>%
  pluck("trails")

landmark <- macleish_layers %>%
  pluck("landmarks")

boundary_plot <- ggplot(boundary) + 
  geom_sf() + 
  scale_x_continuous(breaks = c(-72.677, -72.683))

boundary_plot +
  geom_sf(data = stream, color = "blue", size = 1.5) +
  geom_sf(data = building, color = "brown") +
  geom_sf(data = trail, color = "Green") +
  geom_sf(data = landmark, color = "orange") 

(b) An informative interactive map of the MacLeish Field Station property.

library(leaflet)

leaflet() %>%
  addTiles() %>%
  addPolygons(
    data = macleish_layers[["boundary"]],
    weight = 1
  ) %>%
  addPolygons(
    data = macleish_layers[["buildings"]],
    weight = 1
  ) %>%
  addMarkers(
    data = filter(macleish_layers[["landmarks"]], grepl("Met", Label)),
    popup = ~Label
  ) %>% 
  addPolylines(
    data = macleish_layers[["trails"]],
    weight = 1
  ) %>% 
  addPolylines(
    data = macleish_layers[["streams"]],
    weight = 1
  )