From f84f8fbb049e07640c7150cc72150bdb9a28d5da Mon Sep 17 00:00:00 2001 From: Ken Kellner Date: Thu, 6 Sep 2018 14:13:46 -0400 Subject: Add function to streamline reading Buffalo open data --- scripts/read_bufdata.R | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scripts/read_bufdata.R 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) + +} -- cgit v1.2.3