-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathspatial-access.R
72 lines (61 loc) · 1.93 KB
/
spatial-access.R
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
library(aws.s3)
library(dplyr)
library(purrr)
library(sf)
source("utils.R")
# This script retrieves the spatial/location data for
# industrial corridors in the City of Chicago
AWS_S3_RAW_BUCKET <- Sys.getenv("AWS_S3_RAW_BUCKET")
output_bucket <- file.path(AWS_S3_RAW_BUCKET, "spatial", "access")
# List APIs from city site
sources_list <- bind_rows(list(
# INDUSTRIAL CORRIDORS
"ind_2013" = c(
"source" = "https://data.cityofchicago.org/api/geospatial/",
"api_url" = "e6xh-nr8w?method=export&format=GeoJSON",
"boundary" = "industrial_corridor",
"year" = "2013"
),
# PARKS
"prk_2021" = c(
"source" = "https://opendata.arcgis.com/datasets/",
"api_url" = "74d19d6bd7f646ecb34c252ae17cd2f7_7.geojson",
"boundary" = "park",
"year" = "2021"
)
))
# Function to call referenced API, pull requested data, and write it to S3
pwalk(sources_list, function(...) {
df <- tibble::tibble(...)
open_data_to_s3(
s3_bucket_uri = output_bucket,
base_url = df$source,
data_url = df$api_url,
dir_name = df$boundary,
file_year = df$year,
file_ext = ".geojson"
)
})
##### CMAP WALKABILITY #####
# 2017 Data is no longer available online
raw_walk <- data.frame(
"url" = "https://services5.arcgis.com/LcMXE3TFhi1BSaCY/arcgis/rest/services/Walkability/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson",
"year" = "2018")
get_walkability <- function(url, year) {
s3_uri <- file.path(output_bucket, "walkability", paste0(year, ".geojson"))
if (!aws.s3::object_exists(s3_uri)) {
tmp_file <- tempfile(fileext = ".geojson")
tmp_dir <- file.path(tempdir(), "walkability")
# Grab file from CTA, recompress without .htm file
st_read(url) %>% st_write(tmp_file)
save_local_to_s3(s3_uri, tmp_file, overwrite = TRUE)
unlink(gsub("/walkability", "", tmp_dir))
}
}
pwalk(raw_walk, function(...) {
df <- tibble::tibble(...)
get_walkability(
url = df$url,
year = df$year
)
})