-
Notifications
You must be signed in to change notification settings - Fork 0
/
r311.Rmd
214 lines (159 loc) · 6.77 KB
/
r311.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
---
title: "r311: An R interface to the open311 standard"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{r311: An R interface to the open311 standard}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
httptest2::start_vignette("../man/figures/r311")
library(r311)
```
# Introduction
open311 is an international open-access standard for civic issue management
and public service communication.
The standard allows administrations to better manage citizen requests,
citizens to more easily participate in administrative work, and researchers
and data scientists to access data regarding public service communication.
As an open standard, open311 is not a centralized API, but a framework
implemented by various cities (e.g. San Francisco, CA, Chicago, IL, Cologne,
DE, Turku, FI, Zurich, CH) and services (e.g. SeeClickFix, FixMyStreet).
It is way past the golden age of open311 APIs and much of development in
civic issue tracking has shifted to less open-access and less standardized
alternatives. Many former prime examples have abandoned or severely limited
their open311 endpoints. Nonetheless, open311 still constitutes a valuable
source for open government data. Many cities and services still maintain an
open311 service.
`r311` allows the seamless management and selection of endpoints and
retrieval of service and request data. It supports (but does not depend on)
many popular R frameworks such as the tidyverse, `sf` and `xml2` for
response formatting. `r311` is designed to be slim, both in content
and dependencies. It imports only two import-less packages used for HTTP
response handling. The functionality is limited to two main features:
- Endpoint management
- Sending requests
This vignette will briefly cover these two features.
# Endpoints
Since open311 is an open and decentralized standard, there is not one but
many endpoints that one can access. An endpoint is commonly implemented by
a city administration, but can also be managed by a service provider such
as FixMyStreet. Each endpoint can define multiple jurisdiction IDs, although,
in practice, most endpoints only define a single jurisdiction.
It can thus be difficult to manage the multitude of endpoints and jurisdictions.
Efforts have been made to list open311 servers, but most of them are
incomplete or outdated. `r311` offers an updated and modifiable endpoint list
that defines a number of open311 implementations that are defined for use in
the package. The list can be read using `o311_endpoints`.
```{r}
o311_endpoints()
```
The list does neither claim comprehensiveness nor up-to-dateness. It arguably
provides some of the most important and easily accessible endpoints as of
`r format(Sys.Date(), "%Y")`. However, `r311` also offers the ability to add
new endpoints to `o311_endpoints` using `o311_add_endpoint`. You need to
provide a name (for lookup) and a root URL (the URL used to send requests).
The following code adds the open311 test server of Rostock, Germany.
```{r}
o311_add_endpoint(
name = "Rostock Test",
root = "https://demo.klarschiff-hro.de/backoffice/citysdk/",
jurisdiction = "rostock.de"
)
```
Retrieving the endpoints list again confirms that you successfully added a
new row to the endpoints dataframe.
```{r}
nrow(o311_endpoints())
```
You can now select the Rostock test API to the session using `o311_api`.
This function matches an API using endpoint name and jurisdiction ID and
attaches it to the active session. Query functions automatically detect the
attached API.
```{r}
o311_api("Rostock Test")
```
After attaching an API, `o311_ok` confirms that the selected endpoint is able
to handle request queries.
```{r}
o311_ok()
```
As the result is \code{TRUE}, you can safely start receiving real request data.
# Making requests
After selecting an API and attaching it to the session, all \code{o311_*}
functions can access it. You can now make requests.
## Services
To get an overview of the available services in a jurisdiction, you can
use `o311_services`, which returns a list of Rostock's administrative
services.
```{r}
o311_services()
```
## Requests
To get data about civic issues in the city area, run `o311_requests`.
```{r}
o311_requests()
```
Using the output of `o311_services`, you can further narrow down the output
of requests. Open311 defines a set of standard parameters which are implemented
by all endpoints. Using the `service_code` parameter with one of the previously
returned service codes, only complaints about businesses illegally using shared
spaces are returned.
```{r}
o311_requests(service_code = "16")
```
Similarly, using a `service_request_id` from the output, you can retrieve a
single service request from the API.
```{r}
o311_request("4")
```
## Bulk queries
Many endpoints define a page limit meaning that responses are divided into
pages. A query without parameters returns the first page. Pagination can be
controlled with the `page` argument. To control pagination, the
`o311_request_all` function can come in handy. It automatically iterates
through pages and heuristically decides when to stop. The following example
retrieves data from the first two pages, resulting in a tibble with 100
service requests.
```{r}
o311_api("Rostock, DE")
o311_request_all(max_pages = 2)
```
## Non-standard parameters
`r311` implicitly supports API extensions introducing custom paths and
parameters. One such API is Klarschiff Rostock which is based on CitySDK.
Klarschiff CitySDK defines a number of non-default paths and parameters which
extend the filtering abilities of open311 requests. Available parameters can
usually be found in the respective documentation (e.g. on GitHub for
[Klarschiff CitySDK](https://github.com/bfpi/klarschiff-citysdk)).
The following query returns the last 50 requests tagged with the "idea" keyword.
```{r}
tickets <- o311_requests(keyword = "idea", max_requests = 50)
```
Some custom parameters can also alter the shape of the output. In the following
example, we query just the count of total requests using the `just_count`
parameter. The result is a 1×1 tibble containing a count value.
```{r}
o311_requests(just_count = TRUE)
```
The CitySDK extensions also offers additional URL paths which can be queried
using the generic `o311_query` function.
```{r}
poly <- o311_query("areas")
plot(poly$grenze)
plot(tickets$geometry, add = TRUE, pch = 16)
```
# Cleanup
Endpoint data is stored persistently between sessions so that you can create
your own database of open311 endpoints. This database is stored in the system's
user directory as returned by `tools::R_user_dir("r311")`. To reset the
database, run
```{r}
o311_reset_endpoints()
```
This will default back to the endpoints defined by `r311` and remove all
endpoints manually added by `o311_add_endpoints`.