-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.qmd
More file actions
271 lines (219 loc) · 7.99 KB
/
Copy pathREADME.qmd
File metadata and controls
271 lines (219 loc) · 7.99 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
---
format: gfm
bibliography: inst/REFERENCES.bib
link-citations: true
default-image-extension: ''
tbl-cap-location: bottom
knitr:
opts_chunk:
collapse: true
comment: "#>"
warning: false
message: false
dev: "ragg_png"
tidy: "styler"
fig.path: "man/figures/README-"
dpi: 120
out.width: "100%"
---
<!-- README.md is generated from README.qmd. Please edit that file -->
# arcgeocoder <a href="https://dieghernan.github.io/arcgeocoder/"><img src="man/figures/logo.png" alt="arcgeocoder website" align="right" height="139"/></a>
<!-- badges: start -->
[](https://CRAN.R-project.org/package=arcgeocoder)
[](https://cran.r-project.org/web/checks/check_results_arcgeocoder.html)
[](https://CRAN.R-project.org/package=arcgeocoder)
[](https://github.com/dieghernan/arcgeocoder/actions/workflows/check-full.yaml)
[](https://app.codecov.io/gh/dieghernan/arcgeocoder)
[](https://coveralls.io/github/dieghernan/arcgeocoder)
[](https://dieghernan.r-universe.dev/arcgeocoder)
[](https://www.codefactor.io/repository/github/dieghernan/arcgeocoder)
[](https://www.repostatus.org/#active)
[](https://doi.org/10.32614/CRAN.package.arcgeocoder)
[](https://CRAN.R-project.org/package=arcgeocoder)
<!-- badges: end -->
**arcgeocoder** provides a lightweight interface to the [ArcGIS REST
API](https://developers.arcgis.com/rest/geocode/api-reference/overview-world-geocoding-service.htm).
It geocodes single-line and structured addresses, reverse geocodes coordinates
and finds places by category.
The full site with examples and vignettes is available at
<https://dieghernan.github.io/arcgeocoder/>.
## Why arcgeocoder?
**arcgeocoder** accesses the ArcGIS REST API without requiring an API key or an
additional HTTP package such as **curl**. It uses base R download functions,
which keeps its dependency footprint small.
The package provides focused interfaces to the `findAddressCandidates` and
`reverseGeocode` endpoints. It supports single-line addresses, structured
address components, category filters and reverse geocoding.
## Recommended packages
The following packages provide related geocoding features:
- [**tidygeocoder**](https://jessecambon.github.io/tidygeocoder/)
[@R-tidygeocoder] provides an interface to ArcGIS, Nominatim (OpenStreetMap),
Google, TomTom, Mapbox and other geocoding services.
- [**nominatimlite**](https://dieghernan.github.io/nominatimlite/)
[@R-nominatimlite] is similar to **arcgeocoder** but uses data from
OpenStreetMap through the [Nominatim
API](https://nominatim.org/release-docs/latest/).
## Installation
::: pkgdown-release
Install **arcgeocoder** from
[**CRAN**](https://CRAN.R-project.org/package=arcgeocoder) with:
```{r}
#| eval: false
install.packages("arcgeocoder")
```
:::
::: pkgdown-devel
Read the documentation for the development version at
<https://dieghernan.github.io/arcgeocoder/dev/>.
You can install the development version of **arcgeocoder** with:
```{r}
#| eval: false
# install.packages("pak")
pak::pak("dieghernan/arcgeocoder")
```
Alternatively, you can install **arcgeocoder** using the
[r-universe](https://dieghernan.r-universe.dev/arcgeocoder):
```{r}
#| eval: false
# Install arcgeocoder in R.
install.packages(
"arcgeocoder",
repos = c(
"https://dieghernan.r-universe.dev",
"https://cloud.r-project.org"
)
)
```
:::
## Usage
### Geocoding and reverse geocoding
*The examples in this section are adapted from the **tidygeocoder** package.*
The `arc_geo()` function converts single-line addresses into geographic
coordinates. It requires no API key or additional setup.
```{r}
#| label: example
library(arcgeocoder)
library(dplyr)
# Create a data frame with addresses.
some_addresses <- tribble(
~name, ~addr,
"White House", "1600 Pennsylvania Ave NW, Washington, DC",
"Transamerica Pyramid", "600 Montgomery St, San Francisco, CA 94111",
"Willis Tower", "233 S Wacker Dr, Chicago, IL 60606"
)
# Geocode the addresses.
lat_longs <- arc_geo(
some_addresses$addr,
lat = "latitude",
long = "longitude",
progressbar = FALSE
)
```
By default, `arc_geo()` returns a small set of fields. Set `full_results = TRUE`
to return all available API fields.
```{r}
#| echo: false
#| output: asis
knitr::kable(lat_longs)
cat("<p class=\"caption\">Table 1: Example: geocoding addresses.</p>")
```
The `arc_reverse_geo()` function converts longitude and latitude values into
addresses. Supply longitude values to `x` and latitude values to `y`. The
following example uses the coordinates returned by the previous query. The
`address` argument sets the name of the address column in the output.
```{r}
reverse <- arc_reverse_geo(
x = lat_longs$longitude,
y = lat_longs$latitude,
address = "address_found",
progressbar = FALSE
)
```
```{r}
#| echo: false
#| output: asis
knitr::kable(reverse)
cat("<p class=\"caption\">Table 2: Example: reverse geocoding addresses.</p>")
```
The `arc_geo_categories()` function finds places by category near a location or
within a bounding box. Available categories are documented in the
`arc_categories` dataset and in the [ArcGIS category filtering
documentation](https://developers.arcgis.com/rest/geocode/api-reference/geocoding-category-filtering.htm).
The following example finds food-related places, such as restaurants, coffee
shops and bakeries, near the Eiffel Tower in France.
```{r}
#| label: eiffel
#| fig-cap: 'Example: Food places near the Eiffel Tower'
library(ggplot2) # For plotting.
# Step 1: Locate the Eiffel Tower using a structured query.
eiffel_tower <- arc_geo_multi(
address = "Tour Eiffel",
city = "Paris",
countrycode = "FR",
langcode = "FR",
custom_query = list(outFields = "LongLabel")
)
# Display results.
eiffel_tower |>
select(lon, lat, LongLabel)
# Use `lon` and `lat` as a reference location for `category = "Food"`.
food_eiffel <- arc_geo_categories(
"Food",
x = eiffel_tower$lon,
y = eiffel_tower$lat,
limit = 50,
full_results = TRUE
)
# Plot by food type.
ggplot(eiffel_tower, aes(x, y)) +
geom_point(shape = 15, color = "blue", size = 4) +
geom_point(data = food_eiffel, aes(x, y, color = Type)) +
labs(
title = "Food near the Eiffel Tower",
subtitle = "Using arcgeocoder",
color = "Type of place",
x = "",
y = "",
caption = "Data from the ArcGIS REST API"
)
```
### Convert results to spatial data
Use the longitude and latitude columns returned by **arcgeocoder** to create an
**sf** object:
```{r}
#| label: eiffel_sf
#| fig-cap: 'Example: Food places near the Eiffel Tower using the sf package.'
library(sf)
eiffel_tower_sf <- eiffel_tower |>
select(lon, lat, LongLabel) |>
st_as_sf(
coords = c("lon", "lat"),
# Set the CRS of the resulting coordinates.
crs = eiffel_tower$wkid
)
food_eiffel_sf <- st_as_sf(food_eiffel,
coords = c("lon", "lat"),
crs = eiffel_tower$wkid
)
ggplot(eiffel_tower_sf) +
geom_sf(shape = 15, color = "blue", size = 4) +
geom_sf(data = food_eiffel_sf, aes(color = Type)) +
coord_sf(crs = 3035)
```
## Citation
```{r}
#| echo: false
#| output: asis
print(citation("arcgeocoder"), style = "html")
```
A BibTeX entry for LaTeX users is shown below.
```{r}
#| echo: false
#| comment: ''
toBibtex(citation("arcgeocoder"))
```
## References