summaryrefslogtreecommitdiff
path: root/scripts/read_bufdata.R
blob: fc358a383339c67f9aae3fab93287d71d52553f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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) 
  
}