-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathREADME.Rmd
161 lines (109 loc) · 5.87 KB
/
README.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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# arcgislayers <img src="man/figures/logo.svg" align="right" height="139" alt="" />
<!-- badges: start -->
[](https://opensource.org/license/apache-2-0)
[](https://github.com/R-ArcGIS/arcgislayers/actions/workflows/R-CMD-check.yaml)
[](https://lifecycle.r-lib.org/articles/stages.html#stable)
[](https://cran.r-project.org/package=arcgislayers)
<!-- badges: end -->
`{arcgislayers}` simplifies accessing and managing data the ArcGIS Ecosystem. With it you can:
- Read data from ArcGIS Online, Enterprise, Survey123, Location Platform, Hub, and more
- Read Imagery as `SpatRaster` from `{terra}`
- Read Feature Services as `sf` objects
- Publish {sf} objects and data.frame's as Feature Services
- Download attachments from Survey123
## Installation
It is recommend you install and use the metapackage `{arcgis}`. You can install the development version of arcgis like so:
``` r
install.packages(
"arcgis",
repos = c("https://r-arcgis.r-universe.dev", "https://cloud.r-project.org")
)
```
## Usage
### Read data from a Feature Service
```{r}
library(arcgis)
```
`arc_open()` takes a URL to create a reference to a remote ArcGIS layer, server, or table. The function can return any of the following classes (corresponding to different ArcGIS service types):
- `FeatureLayer`
- `Table`
- `FeatureServer`
- `ImageServer`
- `MapServer`
- `GroupLayer`
For example, you can create a `FeatureLayer` object based on a Feature Server URL:
```{r}
furl <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0"
county_fl <- arc_open(furl)
county_fl
```
You can then use `arc_select()` to query the feature layer object and return an `sf` object.
If no arguments are provided to `arc_select()` the entire feature layer is returned in memory as an `sf` object.
```{r}
arc_select(county_fl)
```
### Filtering using `where` or `filter_geom` arguments
You can also use the `fields` argument to select columns or the `where` argument to subset rows.
For example, using a character vector of column names for `fields` and a simple SQL where clause for `where` you can select counties with population greater than 1,000,000:
```{r}
arc_select(
county_fl,
fields = c("state_abbr", "population"),
where = "population > 1000000"
)
```
For `FeatureLayer` and `Table` objects, and sometimes `ImageServer`s, the `list_fields()` function can be helpful to check available attributes and build a `where` query:
```{r}
list_fields(county_fl)
```
You can also provide a `bbox`, `sfc`, or `sfg` object to the `filter_geom` argument to perform a spatial filter. If the `sfc` object contains more than one geometry, the object is combined with `sf::st_union()`. See documentation for more (`?arc_select`).
```{r}
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
arc_select(
county_fl,
filter_geom = sf::st_bbox(nc[1,])
)
```
### Creating a `SpatRaster` from an ArcGIS ImageServer
A `SpatRaster` object from the `{terra}` package can be extracted from an `ImageServer` using `arc_raster()`.
`arc_raster()` will extract the area defined by `xmin`, `ymin`, `xmax`, and `ymax`. You can optionally specify the `width` and `height` of the resultant image. Use `format` to define what type of image is returned.
```{r}
img_url <- "https://landsat2.arcgis.com/arcgis/rest/services/Landsat/MS/ImageServer"
landsat <- arc_open(img_url)
res <- arc_raster(
landsat,
xmin = -71, ymin = 43,
xmax = -67, ymax = 47.5,
bbox_crs = 4326,
width = 500, height = 500
)
terra::plotRGB(res, 4, 3, 2, scale = max(landsat[["maxValues"]]))
```
## Authorization and publication
Authorization is not required for reading any public data sources.
Workflows that require authorization include:
- interacting with [non-public](https://doc.arcgis.com/en/arcgis-online/share-maps/share-items.htm) services,
- publishing a new service (the authorized user must also have [publishing privileges](https://doc.arcgis.com/en/arcgis-online/administer/roles.htm)), and
- modifying or deleting any existing service (the authorized user must also have [edit access](https://doc.arcgis.com/en/arcgis-online/manage-data/manage-editing-hfl.htm) to the service).
### Accessing non-public data
The same functions for reading public ArcGIS Online and Enterprise services (such as `arc_open()`,`arc_read()`,`arc_select()`,`arc_raster()`, etc.) can be used to read data from non-public services by using the `token` argument.
For more information on tokens and authorization functions, see the [authorization article](https://developers.arcgis.com/r-bridge/authentication/connecting-to-a-portal/).
### Publishing and modifying services from R
The package includes functions to publish data to an ArcGIS Portal:
- `add_item()`: Creates a new FeatureCollection from a `sf` or `data.frame` object
- `publish_item()`: Publishes an existing FeatureLayer
- `publish_layer()`: is a higher level wrapper around both `add_item()` and `publish_item()`
There are also functions to add or modify data including `add_features()`, `update_features()`, and `delete_features()`. For a more detailed guide to adding, updating, and deleting features, view the tutorial on the [R-ArcGIS Bridge website](https://developers.arcgis.com/r-bridge).
These functions all require authorization since data cannot be published or modified anonymously in ArcGIS Online and ArcGIS Enterprise.