summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKen Kellner <ken@kenkellner.com>2018-09-06 14:13:46 -0400
committerKen Kellner <ken@kenkellner.com>2018-09-06 14:13:46 -0400
commitf84f8fbb049e07640c7150cc72150bdb9a28d5da (patch)
tree14b8655fa24fc0025e0a08e71e16728526865193
parent15741cbb86ddb17a0192e5030d9c5dccc69d217c (diff)
Add function to streamline reading Buffalo open data
-rw-r--r--scripts/read_bufdata.R41
1 files changed, 41 insertions, 0 deletions
diff --git a/scripts/read_bufdata.R b/scripts/read_bufdata.R
new file mode 100644
index 0000000..fc358a3
--- /dev/null
+++ b/scripts/read_bufdata.R
@@ -0,0 +1,41 @@
+#Streamline process of reading in CSV and Shapefile data
+#from Buffalo Open Data Portal (via Socrata API)
+#
+#Requires RSocrata, tidyverse, and sf
+#
+#read_bufdata() with no arguments for a list of possible dataset titles
+
+read_bufdata <- function(title=NULL, format='CSV'){
+
+ if ( ! format %in% c('CSV', 'csv', 'Shapefile', 'shapefile') ){
+ stop('Data format must be "CSV" or "Shapefile"')
+ }
+
+ datasets = RSocrata::ls.socrata('https://data.buffalony.gov')
+
+ if( is.null(title) || ! title %in% datasets$title ){
+ cat('Available datasets:\n')
+ return(datasets$title)
+ }
+
+ ind = which(datasets$title == title)
+ urls = datasets$distribution[[ind]]$downloadURL
+
+ if ( format %in% c('CSV','csv') ){
+ csv_url = urls[grep('rows.csv',urls)]
+ if(length(csv_url)==0) stop('CSV format not available for dataset')
+ return( tibble::as_tibble(RSocrata::read.socrata(csv_url)) )
+ }
+
+ sf_url = urls[grep('Shapefile',urls)]
+ if(length(sf_url)==0) stop('Shapefile format not available for dataset')
+
+ dest_dir = tempdir()
+ dest <- tempfile(tmpdir=dest_dir)
+ download.file(sf_url, dest, quiet=T)
+ unzip(dest, exdir = dest_dir)
+ shape_name = grep('.shp',list.files(dest_dir),value=T)
+ setwd(dest_dir)
+ sf::st_read(shape_name,quiet=TRUE)
+
+}