2018

Column

Cambridge building permits completed or scheduled to complete in 2018

2017

Column

Cambridge building permits completed in 2017

2016

Column

Cambridge building permits completed in 2016

2015

Column

Cambridge building permits completed in 2015

About

About these maps

These maps show building permits for commercial and residential units in Cambridge that were completed or expected to complete in 2015-2017.

Data is from the City of Cambridge, mapped by Kent S Johnson.

Building Permits: 1 and 2 Family as of 2018-12-18.
Building Permits: Commercial and Multi-Family Buildings as of 2018-12-11.

Copyright 2017 Kent S Johnson Creative Commons License 2018-12-22

---
title: "Cambridge Building Permits 2015-2018"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
    theme: spacelab
---

```{r parameters, echo=FALSE,include=FALSE,message=FALSE}
library(htmltools)
library(httr)
library(leaflet)
library(lubridate)
library(RColorBrewer)
library(sf)
library(sp)
library(stringr)
library(tidyverse)

source(here::here('Cambridge Open Data/Development/Maybe.R'))
knitr::opts_chunk$set(echo=FALSE, fig.width=10, fig.height=6,
                      results='asis',
                      comment=NA, warning=FALSE, message=FALSE)
```

2018
============

Column
-----------------------------------------------------------------------

### Cambridge building permits completed or scheduled to complete in 2018

```{r}
city = raster::shapefile(here::here('Cambridge Open Data/Shapefiles/BOUNDARY_CityBoundary.shp/BOUNDARY_CityBoundary.shp')) %>% 
  spTransform(CRS("+init=EPSG:4326"))

parcels = st_read(here::here('Cambridge Open Data/Shapefiles/ASSESSING_ParcelsFY2019.gdb.zip'), quiet=TRUE) %>% 
  as('Spatial')

# Building permits from file
# bp <- read_csv("../Building_Permits__Commercial_and_Multi-Family_Buildings.csv")
# bp2 = read_csv('../Building_Permits__1_and_2_Family.csv')

# Building permits from Socrata
# Commercial and Multi-family
# Use httr to get the data so we can retrieve the Last-Modified date
r = GET("https://data.cambridgema.gov/api/views/24a8-ex6a/rows.csv?accessType=DOWNLOAD")
bp_date = headers(r)$`last-modified` %>% parse_http_date %>%
  with_tz('America/New_York')
bp = content(r)

r = GET('https://data.cambridgema.gov/api/views/52p4-36ct/rows.csv?accessType=DOWNLOAD')
bp2_date = headers(r)$`last-modified` %>% parse_http_date %>% with_tz('America/New_York')
bp2 = content(r)

common_names = c('PLANNUMBER', 'PARCELNUMBER', 'Complete_Date', 'Class', 'ADDRESS', "DetailedDescriptionofWork", 'TotalCostofConstruction')
bp_all = bp[,common_names] %>% bind_rows(bp2[,common_names])

# split out a real address field
bp_all = bp_all %>% 
  mutate(Addr = str_match(ADDRESS, '(.*?)\n')[,2],
         YearComplete = year(mdy_hms(Complete_Date, tz='America/New_York')),
         Cost = TotalCostofConstruction %>% sub('\\$', '', .) %>% 
           as.numeric %>% scales::dollar(.),
         Cost = ifelse(Cost=='$NA', NA, Cost)) %>% 
  filter(YearComplete >= 2015) %>% 
  rename(ML=PARCELNUMBER)

# Many addresses have multiple permits. Reduce them to a single item containing
# - year
# - map-lot
# - address
# - class - use just the first, this will be the label
# - popup - information about all permits

make_popup = function(d) {
  permits = lapply(1:nrow(d),
       function(i) {
         row = d[i,]
         as.character(p(
           tags$b(row$Class), br(),
           'Completed', row$YearComplete,
           maybe_bp(row$PLANNUMBER),
           maybe(row$Cost, 'Cost'),
           br(), 'Description:', row$DetailedDescriptionofWork
         ))
         })
   HTML(as.character(p(
     tags$b(d$Addr[1]),
     maybe_ml(d$ML[1], 'Map-Lot'))), paste(permits, collapse=''))
}

for_map = bp_all %>% group_by(YearComplete, ML) %>% do(
  Addr=.$Addr[1], Class=.$Class[1], popup=make_popup(.)
) %>% unnest

color_fun = colorFactor(brewer.pal(7, 'Set1'), unique(bp_all$Class))

map = leaflet() %>% addProviderTiles('Esri.WorldTopoMap') %>% 
  setView(-71.1129096, 42.3783904, zoom = 14) %>% 
  addPolygons(data=city, fill=FALSE, color='steelblue', weight=3)

for_year = merge(parcels, 
                 for_map[for_map$YearComplete==2018,], by='ML', all.x=FALSE) %>% 
  spTransform(CRS("+init=EPSG:4326"))

map %>% addPolygons(data=for_year, color='black',
            fillColor=~color_fun(Class),
            label=~Class, popup=~popup, group=~Class,
            weight=1, dashArray=1,           # Line weight
            opacity=0.7, fillOpacity=0.6, 
            popupOptions = popupOptions(maxWidth=500, closeOnClick=TRUE),
            highlightOptions = 
                highlightOptions(opacity=1, fillOpacity=1, dashArray='1, 0',
                                 weight=2, bringToFront = TRUE)) %>% 
  addLayersControl(overlayGroups=sort(unique(for_year$Class))) %>% 
  addLegend(position='bottomleft', pal=color_fun, values=for_year$Class)
```

2017
============

Column
-----------------------------------------------------------------------

### Cambridge building permits completed in 2017

```{r}
for_year = merge(parcels, 
                 for_map[for_map$YearComplete==2017,], by='ML', all.x=FALSE) %>% 
  spTransform(CRS("+init=EPSG:4326"))

map %>% addPolygons(data=for_year, color='black',
            fillColor=~color_fun(Class),
            label=~Class, popup=~popup, group=~Class,
            weight=1, dashArray=1,           # Line weight
            opacity=0.7, fillOpacity=0.6, 
            popupOptions = popupOptions(maxWidth=500, closeOnClick=TRUE),
            highlightOptions = 
                highlightOptions(opacity=1, fillOpacity=1, dashArray='1, 0',
                                 weight=2, bringToFront = TRUE)) %>% 
  addLayersControl(overlayGroups=sort(unique(for_year$Class))) %>% 
  addLegend(position='bottomleft', pal=color_fun, values=for_year$Class)
```


2016
============

Column
-----------------------------------------------------------------------

### Cambridge building permits completed in 2016

```{r}
for_year = merge(parcels, 
                 for_map[for_map$YearComplete==2016,], by='ML', all.x=FALSE) %>% 
  spTransform(CRS("+init=EPSG:4326"))

map %>% addPolygons(data=for_year, color='black',
            fillColor=~color_fun(Class),
            label=~Class, popup=~popup, group=~Class,
            weight=1, dashArray=1,           # Line weight
            opacity=0.7, fillOpacity=0.6, 
            popupOptions = popupOptions(maxWidth=500, closeOnClick=TRUE),
            highlightOptions = 
                highlightOptions(opacity=1, fillOpacity=1, dashArray='1, 0',
                                 weight=2, bringToFront = TRUE)) %>% 
  addLayersControl(overlayGroups=sort(unique(for_year$Class))) %>% 
  addLegend(position='bottomleft', pal=color_fun, values=for_year$Class)
```


2015
============

Column
-----------------------------------------------------------------------

### Cambridge building permits completed in 2015

```{r}
for_year = merge(parcels, 
                 for_map[for_map$YearComplete==2015,], by='ML', all.x=FALSE) %>% 
  spTransform(CRS("+init=EPSG:4326"))

map %>% addPolygons(data=for_year, color='black',
            fillColor=~color_fun(Class),
            label=~Class, popup=~popup, group=~Class,
            weight=1, dashArray=1,           # Line weight
            opacity=0.7, fillOpacity=0.6, 
            popupOptions = popupOptions(maxWidth=500, closeOnClick=TRUE),
            highlightOptions = 
                highlightOptions(opacity=1, fillOpacity=1, dashArray='1, 0',
                                 weight=2, bringToFront = TRUE)) %>% 
  addLayersControl(overlayGroups=sort(unique(for_year$Class))) %>% 
  addLegend(position='bottomleft', pal=color_fun, values=for_year$Class)
```


About
================

### About these maps

These maps show building permits for commercial and residential units in Cambridge that were completed or expected to complete in 2015-2017.

Data is from the [City of Cambridge](http://cambridgema.gov), mapped by Kent S Johnson. 

[Building Permits: 1 and 2 Family](https://data.cambridgema.gov/Inspectional-Services/Building-Permits-1-and-2-Family/52p4-36ct) as of `r format(bp2_date, '%Y-%m-%d')`.  
[Building Permits: Commercial and Multi-Family Buildings](https://data.cambridgema.gov/Inspectional-Services/Building-Permits-Commercial-and-Multi-Family-Build/24a8-ex6a) as of `r format(bp_date, '%Y-%m-%d')`.

Copyright 2017 Kent S Johnson

  Creative Commons License
  `r Sys.Date()`