Skip to content

Instantly share code, notes, and snippets.

@nmpeterson
Last active July 2, 2020 18:15
Show Gist options
  • Save nmpeterson/ca3dd388ae36196864fcd516dfa8e7e4 to your computer and use it in GitHub Desktop.
Save nmpeterson/ca3dd388ae36196864fcd516dfa8e7e4 to your computer and use it in GitHub Desktop.
Chicago 311 flooding data
.Rhistory
*.dbf
*.prj
*.shp*
*.shx
*.geojson
#install.packages("tidyverse","RSocrata","sf")
library(tidyverse)
library(RSocrata)
library(sf)
library(tmap)
setwd('~/Documents/GitHub/chi311-flooding-gist') # Cloned from: <https://gist.github.com/nmpeterson/ca3dd388ae36196864fcd516dfa8e7e4>
tmap_mode("plot")
# Specify which complaint types to fetch ----------------------------------
#View(read_csv('sr_types.csv'))
fetch_codes = c('AAF', 'AAE') # "Water in basement" and "water on street" complaints
#fetch_codes = c('WBJ') # "No water" complaints
# Get 311 data --------------------------------------------------------------------------------
# Get complaints from Chicago data portal using Socrata API.
# Source/metadata: <https://data.cityofchicago.org/Service-Requests/311-Service-Requests/v6vf-nfxy>
API_URL <- "https://data.cityofchicago.org/resource/v6vf-nfxy.json"
API_FILTER <- paste0(
"sr_short_code in('", paste0(fetch_codes, collapse="','"), "')",
" AND duplicate=false AND location IS NOT NULL"
)
chi311_raw <- read.socrata(paste0(API_URL, "?$where=", API_FILTER))
# Drop unnecessary fields ---------------------------------------------------------------------
chi311 <- as_tibble(chi311_raw) %>%
select(
starts_with('sr_'), created_date, street_address, zip_code, community_area, ward,
latitude, longitude
)
# Apply any filters ---------------------------------------------------------------------------
#chi311 <- chi311 %>%
# filter(created_date >= "2020-05-01 00:00:00") # Dataset begins on 7/1/2018
# Create points from coordinates --------------------------------------------------------------
chi311_geo <- st_as_sf(chi311, coords=c("longitude", "latitude"), crs=4326)
tm_shape(chi311_geo) + tm_dots(col="sr_type", alpha=0.35, palette="Dark2")
# Export to GeoJSON ---------------------------------------------------------------------------
out_name = paste0("chi311_", paste0(sort(fetch_codes), collapse="_"))
st_write(chi311_geo, paste0(out_name, ".geojson"), driver="geojson", append=FALSE)
# Export to shapefile -----------------------------------------------------
chi311_shp <- chi311_geo %>%
rename(
sr_code = sr_short_code,
address = street_address,
cca = community_area,
sr_date = created_date
) %>%
st_transform(st_crs(26971)) # IL State Plane East NAD83
st_write(chi311_shp, paste0(out_name, ".shp"), append=FALSE)
sr_type sr_short_code
311 INFORMATION ONLY CALL 311IOC
Abandoned Vehicle Complaint SKA
Aircraft Noise Complaint AVN
Alley Light Out Complaint SFA
Alley Pothole Complaint PHB
Alley Sewer Inspection Request AAI
Animal In Trap Complaint EBD
Bee/Wasp Removal SGG
Bicycle Request/Complaint PCL
Building Violation BBA
Bungalow Rehab/Purchase Information Request BUNGALOW
Cab Feedback CSC
Cable TV Complaint OCC
City Vehicle Sticker Violation QAC
Clean and Green Program Request NAA
Clean Vacant Lot Request SCT
Consumer Fraud Complaint CSF
Consumer Retail Store Complaint CST
Coyote Interaction Complaint CIAC
Dead Animal Pick-Up Request SGQ
Dead Bird SGV
E-Scooter PCL3
Extreme Weather Notification JNS
Fire Safety Inspection Request FAC
Fly Dumping Complaint SDR
Garbage Cart Maintenance SIE
Graffiti Removal Request GRAF
Home Buyer Program Info Request HOP
Ice and Snow Removal Request SDO
Inaccurate Fuel Pump Complaint FPC
Inaccurate Retail Scales Complaint INR
Lead Inspection Request HDF
Licensed Pharmaceutical Representative Complaint LPRC
Liquor Establishment Complaint LIQUORCO
Low Water Pressure Complaint WBK
Minimum Wage Complaint MWC
Missed Garbage Pick-Up Complaint SCC
No Building Permit and Construction Violation BBD
No Solicitation Complaint NOSOLCPP
No Water Complaint WBJ
Nuisance Animal Complaint EAB
Open Fire Hydrant Complaint WBT
Operating Without Business License Complaint RBL
Outdated Merchandise Complaint ODM
Paid Sick Leave Violation PSL
Pavement Cave-In Inspection Request PBE
Pet Wellness Check Request PET
Petcoke Dust Complaint PETCO
Porch Inspection Request BPI
Pothole in Street Complaint PHF
Protected Bike Lane - Debris Removal PBLDR
Public Vehicle/Valet Complaint CSP
Pushcart Food Vendor Complaint CORNVEND
Recycling Inspection Request SCX
Renters and Foreclosure Complaint RFC
Report an Injured Animal EAQ
Restaurant Complaint HFB
Ridesharing Complaint TNP
Rodent Baiting/Rat Complaint SGA
Sanitation Code Violation SCB
Sewer Cave-In Inspection Request AAD
Sewer Cleaning Inspection Request CHECKFOR
Shared Housing/Vacation Rental Complaint SHVR
Sidewalk Cafe Complaint CAFE
Sidewalk Inspection Request PBS
Sign Repair Request - All Other Signs PCE
Sign Repair Request - Do Not Enter Sign PCD
Sign Repair Request - One Way Sign PCC
Sign Repair Request - Stop Sign PCB
Smokeless Tobacco at Sports Event Complaint HFF
Snow - Object/Dibs Removal Request SDW
Snow – Uncleared Sidewalk Complaint SWSNOREM
Snow Removal - Protected Bike Lane or Bridge Sidewalk SNPBLBS
Stray Animal Complaint EAE
Street Cleaning Request SDP
Street Light On During Day Complaint SFN
Street Light Out Complaint SFD
Street Light Pole Damage Complaint SFK
Street Light Pole Door Missing Complaint SFQ
Tobacco - General Complaint BAG
Tobacco - Sale to Minors Complaint BAM
Traffic Signal Out Complaint SFB
Tree Debris Clean-Up Request SEL
Tree Planting Request SED
Tree Removal Request SEE
Tree Trim Request SEF
Vacant/Abandoned Building Complaint BBK
Vehicle Parked in Bike Lane Complaint VBL
Viaduct Light Out Complaint SFC
Vicious Animal Complaint EAF
Water in Basement Complaint AAF
Water On Street Complaint AAE
Water Quality Concern WCA
Weed Removal Request SCP
Wire Basket Request SCS
Yard Waste Pick-Up Request SCQ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment