/
ZA.Rmd
72 lines (50 loc) · 1.91 KB
/
ZA.Rmd
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
---
title: "Zone Analysis"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(keyring)
library(jsonlite)
library(RCurl)
library(httr)
```
# Background
This notebook runs through an [zone activity analysis](https://support.streetlightdata.com/hc/en-us/articles/360018833951-Zone-Activity-Analysis) in the StreetLight platform. See the *StreetLight API Set-up.Rmd* notebook for set-up info.
## Input credentials
```{r}
# Credentials
key <- keyring::key_get("StL API", "Met Council - API")
# Paths
project_req <- paste0("https://insight.streetlightdata.com:/api/v1/projects?key=", key)
```
## Run Zone Analysis Project
```{r}
create_req <- httr::POST(project_req,
config = add_headers('Content-Type' = 'application/json', 'x-stl-key' = key),
body = upload_file("za_project.json", type = 'json'),
encode = 'json'
)
request <- jsonlite::prettify(create_req)
```
## Look at retrievable metrics of request/get uuid
```{r}
# Look at retrievable metrics of request
jsonlite::parse_json(request)
metrics <-httr::POST(url ="https://insight.streetlightdata.com:/api/v1/projects/status",
config = add_headers('Content-Type' = 'application/json', 'x-stl-key' = key),
body = upload_file("za_status_query.json", type = "json"))
# Which "metrics" (csv sheets) are available?
content(metrics, "parsed")$projects[[1]]$metrics
# Find project UUID to use for data export
project_uuid <- content(metrics, "parsed")$projects[[1]]$uuid
```
## Export data
Note that each metric needs to be called separately.
```{r}
# OD Trip-Person Metric
za_pers <- httr::GET(url = paste0("https://insight.streetlightdata.com:/api/v1/projects/download/uuid/", project_uuid, "/za_pers", "?key=", key),
config = add_headers('x-stl-key' = key))
za_pers_df <- as_tibble(content(za_pers, as = "parsed"))
```