---
title: "Assessing the Impact of Affordable Housing Development"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    #social: menu
    source: embed
---


```{r setup, include=FALSE} 
library(readr)
library(readxl)
library(tidyverse)
library(flexdashboard)
library(stringr)
library(sf)
library(maps)
```



```{r}
setwd("../affordable_housing-dasher/Data")
filtered_sales <- read_csv("filtered_sales.csv") 
assessments <- read_csv("assessments.csv")
LIHTC <- read_csv("LIHTC.csv")
LIHTC_filtered <- subset(LIHTC, YR_PIS >= 2000 & !YR_PIS %in% c(8888,9999))

property_details <- read_csv("property_details.csv")

barnes <- read_csv("barnes.csv")

barnes$HUD_ID <- c('TNA081023A00400CO',
	        'TNA09211010400',
            'TNA081023A90000CO',
            'TNA08600035500',
            'TNA10613000800',
            'TNA05108017800',
            'TNA09311022700',
            'TNA06912006600',
            'TNA10601016800')
barnes = barnes %>% rename(YS_PIS = `Barnes Year`, 
                           PROJ_ADD = `Street Address`, 
                           PROJ_CTY = City, 
                           PROJ_ZIP = `Zip Code`,
                           LATITUDE = lat,
                           LONGITUDE = lng) %>%
                    select(YS_PIS,PROJ_ADD,PROJ_CTY,PROJ_ZIP, HUD_ID, LATITUDE, LONGITUDE)

LIHTC_filtered <- bind_rows(LIHTC_filtered, barnes)


property_details[c('Longitude', 'Latitude')] <- str_split_fixed(property_details$centroid, ',', 2)
property_details$Longitude <- str_sub(property_details$Longitude, 2, -1)
property_details$Latitude <- str_sub(property_details$Latitude, 1, -2)


#Generate 'geometry' column in LIHTC and property_details
LIHTC_filtered <- st_as_sf(LIHTC_filtered, coords = c("LONGITUDE", "LATITUDE"), 
                 crs = 4326, agr = "constant")

property_details <- st_as_sf(property_details, coords = c("Longitude", "Latitude"), 
                 crs = 4326, agr = "constant")

#Add HUD_ID column to property_details
property_details$HUD_ID <- LIHTC_filtered$HUD_ID[st_nearest_feature(property_details, LIHTC_filtered)]

#Rohit code from Slack
nearest_development <- LIHTC_filtered[st_nearest_feature(property_details, LIHTC_filtered), ]
property_details$NNDist <- as.numeric(st_distance(property_details, nearest_development, by_element = T))


#subset of lat long around Davidson County from map_data 
county.lines <- subset(map_data('county'),region=='tennessee' & subregion=='davidson') 


```


```{r}
#Create an interactive map using leaflet

library(leaflet)

#Read in shapefile for Davidson County Boarder
boarder_shapefile <- st_read("Data/Davidson County Border (GIS)/geo_export_bb5b211c-8776-4c69-b6b8-a10ee043fd18.shp", as_tibble = T, quiet = T) %>%
  st_transform('+proj=longlat +datum=WGS84')

map <- leaflet(options = leafletOptions(minZoom = 10)) %>%
  addProviderTiles(provider = "CartoDB.Voyager") %>%
  setView(lng = -86.7816, lat = 36.1627, zoom = 12) %>%
  setMaxBounds(lng1 = -86.7816 + 1, 
               lat1 = 36.1627 + 1, 
               lng2 = -86.7816 - 1, 
               lat2 = 36.1627 - 1) %>%
  addCircleMarkers(data = LIHTC_filtered,
                   radius = 5,
                   color = "white",
                   weight = 1,
                   fillColor = "red",
                   fillOpacity = 0.75) %>%
  addMarkers(data = property_details,
                   # radius = 5,
                   # color = "white",
                   # weight = 1,
                   # fillColor = "blue",
                   # fillOpacity = 0.75,
                   clusterOptions = markerClusterOptions()) %>%
  addPolylines(data = boarder_shapefile)

map

```