source("nycfun.r")
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'jsonlite'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     flatten
## 
## 
## Rows: 22 Columns: 54
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (38): location_name, street_number, city, state, borough, type, AddressN...
## dbl (15): zip, property_value, out_bbl, out_bbl_block, out_bbl_lot, out_bin,...
## lgl  (1): out_unit
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 858598 Columns: 92
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (28): borough, schooldist, firecomp, sanitdistrict, sanitsub, address, z...
## dbl (58): block, lot, cd, bct2020, bctcb2020, ct2010, cb2010, council, zipco...
## lgl  (6): zonedist3, zonedist4, overlay2, spdist3, dcpedited, notes
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
## Rows: 22 Columns: 145
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (65): location_name, street_number, city, state, borough.x, type, Addres...
## dbl (72): zip, property_value, out_bbl, out_bbl_block, out_bbl_lot, out_bin,...
## lgl  (8): out_unit, zonedist3, zonedist4, overlay2, spdist2, spdist3, dcpedi...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Set up

options(warn = -1) 
options(readr.show_types = FALSE)

suppressMessages({
library(tidyverse)
library(dplyr)
library(stringr)
library(data.table)
library(mapview)
library(sf)
library(gt)
library(leaflet)
})

Gathering and Organizing PLUTO Data

url <- "https://tonyfraser-data.s3.amazonaws.com/nyc-addresses/nyc_pluto_23v3_csv/pluto_23v3.csv"
pluto_path <- "./nogit_cache/_pluto_23v3.csv"
if (!file.exists(pluto_path)) {
  download.file(url, pluto_path, mode = "wb")
}
pluto <- read_csv(pluto_path)
dfbrklyn<-filter(pluto,borocode=='3', pfirm15_flag=='1')
dfqueens<-filter(pluto,borocode=='4', pfirm15_flag=='1')
dfbronx<-filter(pluto,borocode=='2', pfirm15_flag=='1')
dfmanhattan<-filter(pluto,borocode=='1', pfirm15_flag=='1')
dfstaten<-filter(pluto,borocode=='5', pfirm15_flag=='1')

# df_sample_Brooklyn<-dfbrklyn
# df_sample_Queens<-dfqueens
# df_sample_Bronx<-dfbronx
# df_sample_manhattan<-dfmanhattan
# df_sample_staten<-dfstaten
df_sample_Brooklyn<-sample_n(dfbrklyn,2192)
df_sample_Queens<-sample_n(dfqueens,2155)
df_sample_Bronx<-sample_n(dfbronx,1399)
df_sample_manhattan<-sample_n(dfmanhattan,1283)
df_sample_staten<-sample_n(dfstaten,2040)

Gathering and Organizing PLUTO Data for Popup

df_sample_Brooklyn_flood<- df_sample_Brooklyn %>%
  mutate(Brooklyn_Data= paste("Building Area: ", bldgarea,
  "<br/>Lot Area: ", lotarea,
  "<br/>Total Number of Units: ", unitstotal,
  "<br/>Number of Buildings: ", numbldgs,
  "<br/>Number of Floors: ", numfloors,
  "<br/>Assessed Total: ", assesstot,
  "<br/>Year Built: ", yearbuilt,
  "<br/>Location: ",address))%>%mutate(County=paste("Kings County"))

df_sample_Queens_flood<- df_sample_Queens %>%
  mutate(Queens_Data= paste("Building Area: ", bldgarea,
  "<br/>Lot Area: ", lotarea,
  "<br/>Total Number of Units: ", unitstotal,
  "<br/>Number of Buildings: ", numbldgs,
  "<br/>Number of Floors: ", numfloors,
  "<br/>Assessed Total: ", assesstot,
  "<br/>Year Built: ", yearbuilt,
  "<br/>Location: ",address))%>%mutate(County=paste("Queens County"))

df_sample_Bronx_flood<- df_sample_Bronx %>%
  mutate(Bronx_Data= paste("Building Area: ", bldgarea,
  "<br/>Lot Area: ", lotarea,
  "<br/>Total Number of Units: ", unitstotal,
  "<br/>Number of Buildings: ", numbldgs,
  "<br/>Number of Floors: ", numfloors,
  "<br/>Assessed Total: ", assesstot,
  "<br/>Year Built: ", yearbuilt,
  "<br/>Location: ",address))%>%mutate(County=paste("Bronx County"))

df_sample_manhattan_flood<- df_sample_manhattan %>%
  mutate(Manhattan_Data= paste("Building Area: ", bldgarea,
  "<br/>Lot Area: ", lotarea,
  "<br/>Total Number of Units: ", unitstotal,
  "<br/>Number of Buildings: ", numbldgs,
  "<br/>Number of Floors: ", numfloors,
  "<br/>Assessed Total: ", assesstot,
  "<br/>Year Built: ", yearbuilt,
  "<br/>Location: ",address))%>%mutate(County=paste("New York County"))

df_sample_staten_flood<- df_sample_staten %>%
  mutate(Staten_Island_Data= paste("Building Area: ", bldgarea,
  "<br/>Lot Area: ", lotarea,
  "<br/>Total Number of Units: ", unitstotal,
  "<br/>Number of Buildings: ", numbldgs,
  "<br/>Number of Floors: ", numfloors,
  "<br/>Assessed Total: ", assesstot,
  "<br/>Year Built: ", yearbuilt,
  "<br/>Location: ",address))%>%mutate(County=paste("Richmond County"))

Creating Map Data

m_df_sample_staten_flood<-mapview(df_sample_staten_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Staten_Island_Data", grid = FALSE, layer.name = "Richmond County", col.regions = "red" )

m_df_sample_manhattan_flood<-mapview(df_sample_manhattan_flood,crs = 4269,  xcol = "longitude", ycol = "latitude", zcol="County",popup="Manhattan_Data",legend = TRUE,  grid = FALSE,layer.name = "New York County", col.regions = "green" )

m_df_sample_Bronx_flood<-mapview(df_sample_Bronx_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Bronx_Data",grid = FALSE,layer.name = "Bronx County", col.regions = "blue" )

m_df_sample_Queens_flood<-mapview(df_sample_Queens_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Queens_Data",grid = FALSE , layer.name = "Queens County", col.regions = "orange" )

m_df_sample_Brooklyn_flood<-mapview(df_sample_Brooklyn_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Brooklyn_Data", grid = FALSE,layer.name = "Kings County", col.regions = "black")

Statistical Sample of Map of Flood Zone Addresses

m_df_sample_Brooklyn_flood+m_df_sample_Queens_flood+m_df_sample_Bronx_flood+m_df_sample_manhattan_flood+m_df_sample_staten_flood
pluto <- read_csv(pluto_path)
## Rows: 858598 Columns: 92
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (28): borough, schooldist, firecomp, sanitdistrict, sanitsub, address, z...
## dbl (58): block, lot, cd, bct2020, bctcb2020, ct2010, cb2010, council, zipco...
## lgl  (6): zonedist3, zonedist4, overlay2, spdist3, dcpedited, notes
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
dfbrklyn<-filter(pluto,borocode=='3', pfirm15_flag=='1')
dfqueens<-filter(pluto,borocode=='4', pfirm15_flag=='1')
dfbronx<-filter(pluto,borocode=='2', pfirm15_flag=='1')
dfmanhattan<-filter(pluto,borocode=='1', pfirm15_flag=='1')
dfstaten<-filter(pluto,borocode=='5', pfirm15_flag=='1')

df_sample_Brooklyn<-sample_n(dfbrklyn,2192)
df_sample_Queens<-sample_n(dfqueens,2155)
df_sample_Bronx<-sample_n(dfbronx,1399)
df_sample_manhattan<-sample_n(dfmanhattan,1283)
df_sample_staten<-sample_n(dfstaten,2040)

df_sample_Brooklyn_flood<- df_sample_Brooklyn %>%
  mutate(Brooklyn_Data= paste("<b>Building Area: <b>", bldgarea, "<br>",
  "<b>Lot Area: <b>", lotarea, "<br>",
  "<b>Total Number of Units: <b>", unitstotal, "<br>",
  "<b>Number of Buildings: <b>", numbldgs, "<br>",
  "<b>Number of Floors: <b>", numfloors, "<br>",
  "<b>Assessed Total: <b>", assesstot, "<br>",
  "<b>Year Built: <b>", yearbuilt, "<br>",
  "<b>Location: <b>",address))%>%mutate(County=paste("Kings County"))

df_sample_Queens_flood<- df_sample_Queens %>%
  mutate(Queens_Data= paste("<b>Building Area: <b>", bldgarea, "<br>",
  "<b>Lot Area: <b>", lotarea, "<br>",
  "<b>Total Number of Units: <b>", unitstotal, "<br>",
  "<b>Number of Buildings: <b>", numbldgs, "<br>",
  "<b>Number of Floors: <b>", numfloors, "<br>",
  "<b>Assessed Total: <b>", assesstot, "<br>",
  "<b>Year Built: <b>", yearbuilt, "<br>",
  "<b>Location: <b>",address))%>%mutate(County=paste("Queens County"))

df_sample_Bronx_flood<- df_sample_Bronx %>%
  mutate(Bronx_Data= paste("<b>Building Area: <b>", bldgarea, "<br>",
  "<b>Lot Area: <b>", lotarea, "<br>",
  "<b>Total Number of Units: <b>", unitstotal, "<br>",
  "<b>Number of Buildings: <b>", numbldgs, "<br>",
  "<b>Number of Floors: <b>", numfloors, "<br>",
  "<b>Assessed Total: <b>", assesstot, "<br>",
  "<b>Year Built: <b>", yearbuilt, "<br>",
  "<b>Location: <b>",address))%>%mutate(County=paste("Bronx County"))

df_sample_manhattan_flood<- df_sample_manhattan %>%
  mutate(Manhattan_Data= paste("<b>Building Area: <b>", bldgarea, "<br>",
  "<b>Lot Area: <b>", lotarea, "<br>",
  "<b>Total Number of Units: <b>", unitstotal, "<br>",
  "<b>Number of Buildings: <b>", numbldgs, "<br>",
  "<b>Number of Floors: <b>", numfloors, "<br>",
  "<b>Assessed Total: <b>", assesstot, "<br>",
  "<b>Year Built: <b>", yearbuilt, "<br>",
  "<b>Location: <b>",address))%>%mutate(County=paste("New York County"))

df_sample_staten_flood<- df_sample_staten %>%
  mutate(Staten_Island_Data= paste("<b>Building Area: <b>", bldgarea, "<br>",
  "<b>Lot Area: <b>", lotarea, "<br>",
  "<b>Total Number of Units: <b>", unitstotal, "<br>",
  "<b>Number of Buildings: <b>", numbldgs, "<br>",
  "<b>Number of Floors: <b>", numfloors, "<br>",
  "<b>Assessed Total: <b>", assesstot, "<br>",
  "<b>Year Built: <b>", yearbuilt, "<br>",
  "<b>Location: <b>",address))%>%mutate(County=paste("Richmond County"))

sample_colleges <- joined_pluto %>%
  mutate(colleges_data = paste("<b>College: <b>", location_name, "<br>",
  "<b>Building Area: <b>", bldgarea, "<br>",
  "<b>Lot Area: <b>", lotarea, "<br>",
  "<b>Total Number of Units: <b>", unitstotal, "<br>",
  "<b>Number of Buildings: <b>", numbldgs, "<br>",
  "<b>Number of Floors: <b>", numfloors, "<br>",
  "<b>Assessed Total: <b>", assesstot, "<br>",
  "<b>Year Built: <b>", yearbuilt, "<br>",
  "<b>Location: <b>", address))

m_df_sample_staten_flood<-mapview(df_sample_staten_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Staten_Island_Data", grid = FALSE, layer.name = "Richmond County", col.regions = "red")

m_df_sample_manhattan_flood<-mapview(df_sample_manhattan_flood,crs = 4269,  xcol = "longitude", ycol = "latitude", zcol="County",popup="Manhattan_Data",legend = TRUE,  grid = FALSE, layer.name = "New York County", col.regions = "green")

m_df_sample_Bronx_flood<-mapview(df_sample_Bronx_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Bronx_Data",grid = FALSE, layer.name = "Bronx County", col.regions = "blue" )

m_df_sample_Queens_flood<-mapview(df_sample_Queens_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Queens_Data",grid = FALSE, layer.name = "Queens County", col.regions = "orange" )

m_df_sample_Brooklyn_flood<-mapview(df_sample_Brooklyn_flood,crs = 4269,  xcol = "longitude", ycol = "latitude",zcol="County", legend = TRUE, popup="Brooklyn_Data", grid = FALSE, layer.name = "Kings County", col.regions = "yellow")

colleges <- mapview(sample_colleges, crs = 4269, xcol = "longitude", ycol= "latitude", zcol = "type", legend = TRUE, popup = "colleges_data", grid = FALSE, layer.name = "Colleges in NYC", col.regions = c("black", "gray", "white"))

# This file comes from this website: https://data.cityofnewyork.us/Environment/Floodplain/g62a-qs52
# To get to the file, go to Export, Download, Download a non-geospatial file type, CSV
# Save to your default directory
library(readr)
polygonFile <- read_csv("polygonData.csv")
## Rows: 5982 Columns: 11
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): the_geom, FLD_ZONE
## dbl (9): ID, GRIDCODE, ORIG_FID, Join_Count, TARGET_FID, STATIC_BFE, ABFE_0_...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
polygonFile <- polygonFile |> filter(Shape_Area >= 1)

geometry1 <- strsplit(polygonFile$the_geom, split = ",")
geometry2 <- str_split(geometry1, ",")
geometry3 <- lapply(geometry2, as.data.frame)

df2 <- c()

for (i in 1:nrow(polygonFile))
{
  df1 <- data.frame(XY = geometry3[[i]], num = rep(i, each = nrow(geometry3[[i]])))
  df2 <- rbind(df1, df2)
}

df2$`X..i..` <- gsub('c\\(\\"MULTIPOLYGON \\(\\(\\(', "", df2$`X..i..`)
df2$X..i.. <- gsub('\\)\\)\\)\\"\\)', '', df2$X..i..)
df2$X..i.. <- gsub('\\"', '', df2$X..i..)
df2$X..i.. <- gsub('\\\n', '', df2$X..i..)
df2$X..i.. <- gsub(' 4', ' 44', df2$X..i..)
df2$X..i.. <- gsub('\\(', '', df2$X..i..)
df2$X..i.. <- gsub('\\)', '', df2$X..i..)




df2[c('long', 'lat')] <- str_split_fixed(df2$X..i.., ' 4', 2)
df2 <- df2[,-1]

projcrs <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
myCoords <- st_as_sf(x = df2,
                     coords = c("long", "lat"),
                     crs = projcrs)

polys <- st_sf(
  aggregate(
    myCoords$geometry,
    list(myCoords$num),
    function(g){
       st_cast(st_combine(g),"POLYGON")
    }
   ))

polys$Group.1 <- polygonFile$TARGET_FID
polys <- rename(polys, Group = Group.1)
polys$Field_Zone <- polygonFile$FLD_ZONE
polys$Polygon_Area <- polygonFile$Shape_Area


polygons1 <- mapview(polys, zcol = "Field_Zone", legend = TRUE, grid = FALSE, layer.name = "Flood Zone Polygons")

m_df_sample_Brooklyn_flood+m_df_sample_Queens_flood+m_df_sample_Bronx_flood+m_df_sample_manhattan_flood+m_df_sample_staten_flood + colleges + polygons1