Skip to content

R-ArcGIS/arcgislayers

Repository files navigation

arcgislayers

License R-CMD-check Lifecycle: stable

{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:

install.packages(
  "arcgis",
  repos = c("https://r-arcgis.r-universe.dev", "https://cloud.r-project.org")
)

Usage

Read data from a Feature Service

library(arcgis)
#> Attaching core arcgis packages:
#> → arcgisutils v0.3.1.9000
#> → arcgislayers v0.3.1.9000
#> → arcgisgeocode v0.2.2
#> → arcgisplaces v0.1.1

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:

furl <- "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_Counties_Generalized_Boundaries/FeatureServer/0"

county_fl <- arc_open(furl)
county_fl
#> <FeatureLayer>
#> Name: USA Counties - Generalized
#> Geometry Type: esriGeometryPolygon
#> CRS: 4326
#> Capabilities: Query,Extract

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.

arc_select(county_fl)
#> Registered S3 method overwritten by 'jsonify':
#>   method     from    
#>   print.json jsonlite
#> Iterating ■■■■■■■■■■■■■■■■ 50% | ETA: 1s
#> Simple feature collection with 3144 features and 12 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -178.2176 ymin: 18.92179 xmax: -66.96927 ymax: 71.40624
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    OBJECTID               NAME   STATE_NAME STATE_FIPS  FIPS SQMI
#> 1         1 Grand Forks County North Dakota         38 38035 51.3
#> 2         2       Grant County North Dakota         38 38037  1.4
#> 3         3      Griggs County North Dakota         38 38039  3.1
#> 4         4   Hettinger County North Dakota         38 38041  2.1
#> 5         5      Kidder County North Dakota         38 38043  1.6
#> 6         6     LaMoure County North Dakota         38 38045  3.5
#> 7         7       Logan County North Dakota         38 38047  1.8
#> 8         8     McHenry County North Dakota         38 38049  2.8
#> 9         9    McIntosh County North Dakota         38 38051  2.5
#> 10       10    McKenzie County North Dakota         38 38053  5.3
#>    POPULATION POP_SQMI STATE_ABBR COUNTY_FIPS Shape__Area Shape__Length
#> 1       73170     50.8         ND         035   0.4503702      2.961625
#> 2        2301      1.4         ND         037   0.5040677      3.413506
#> 3        2306      3.2         ND         039   0.2230339      1.949037
#> 4        2489      2.2         ND         041   0.3427475      2.691898
#> 5        2394      1.7         ND         043   0.4378065      2.719487
#> 6        4093      3.6         ND         045   0.3502662      2.702124
#> 7        1876      1.9         ND         047   0.3090110      2.454735
#> 8        5345      2.8         ND         049   0.5877751      3.261400
#> 9        2530      2.5         ND         051   0.2971127      2.421863
#> 10      14704      5.1         ND         053   0.8862776      4.625264
#>                          geometry
#> 1  POLYGON ((-96.88943 47.6739...
#> 2  POLYGON ((-102.0034 46.0528...
#> 3  POLYGON ((-97.96167 47.2449...
#> 4  POLYGON ((-102.0034 46.2058...
#> 5  POLYGON ((-100.0885 46.6357...
#> 6  POLYGON ((-99.00921 46.2830...
#> 7  POLYGON ((-99.04408 46.2833...
#> 8  POLYGON ((-100.9647 47.8540...
#> 9  POLYGON ((-99.87578 45.9435...
#> 10 POLYGON ((-103.6744 47.3320...

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:

arc_select(
  county_fl, 
  fields = c("state_abbr", "population"), 
  where = "population > 1000000"
)
#> Simple feature collection with 49 features and 2 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -158.2674 ymin: 21.24986 xmax: -71.02671 ymax: 47.77552
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    STATE_ABBR POPULATION                       geometry
#> 1          OH    1264817 POLYGON ((-81.37707 41.3463...
#> 2          OH    1323807 POLYGON ((-83.24282 39.8044...
#> 3          PA    1250578 POLYGON ((-79.86399 40.2007...
#> 4          PA    1603797 POLYGON ((-75.1429 39.8816,...
#> 5          HI    1016508 POLYGON ((-157.6733 21.2980...
#> 6          IL    5275541 POLYGON ((-88.26711 41.9887...
#> 7          AZ    4420568 POLYGON ((-111.0425 33.4759...
#> 8          AZ    1043433 POLYGON ((-110.4522 31.7360...
#> 9          CA    1682353 POLYGON ((-121.4721 37.4777...
#> 10         CA    1165927 POLYGON ((-122.3076 37.8917...

For FeatureLayer and Table objects, and sometimes ImageServers, the list_fields() function can be helpful to check available attributes and build a where query:

list_fields(county_fl)
#> # A data frame: 12 × 10
#>    name   type  alias sqlType nullable editable domain defaultValue length
#>  * <chr>  <chr> <chr> <chr>   <lgl>    <lgl>    <lgl>  <lgl>         <int>
#>  1 OBJEC… esri… OBJE… sqlTyp… FALSE    FALSE    NA     NA               NA
#>  2 NAME   esri… Name  sqlTyp… TRUE     TRUE     NA     NA               50
#>  3 STATE… esri… Stat… sqlTyp… TRUE     TRUE     NA     NA               20
#>  4 STATE… esri… Stat… sqlTyp… TRUE     TRUE     NA     NA                2
#>  5 FIPS   esri… FIPS  sqlTyp… TRUE     TRUE     NA     NA                5
#>  6 SQMI   esri… Area… sqlTyp… TRUE     TRUE     NA     NA               NA
#>  7 POPUL… esri… 2020… sqlTyp… TRUE     TRUE     NA     NA               NA
#>  8 POP_S… esri… Peop… sqlTyp… TRUE     TRUE     NA     NA               NA
#>  9 STATE… esri… Stat… sqlTyp… TRUE     TRUE     NA     NA                2
#> 10 COUNT… esri… Coun… sqlTyp… TRUE     TRUE     NA     NA                3
#> 11 Shape… esri… Shap… sqlTyp… TRUE     FALSE    NA     NA               NA
#> 12 Shape… esri… Shap… sqlTyp… TRUE     FALSE    NA     NA               NA
#> # ℹ 1 more variable: description <chr>

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).

nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source 
#>   `/Users/josiahparry/Library/R/arm64/4.5/library/sf/shape/nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27

arc_select(
  county_fl,
  filter_geom = sf::st_bbox(nc[1,])
)
#> Simple feature collection with 6 features and 12 fields
#> Geometry type: POLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -82.0477 ymin: 35.98946 xmax: -80.83795 ymax: 36.80746
#> Geodetic CRS:  WGS 84
#>   OBJECTID             NAME     STATE_NAME STATE_FIPS  FIPS  SQMI
#> 1      467   Johnson County      Tennessee         47 47091  58.8
#> 2     1924 Alleghany County North Carolina         37 37005  47.0
#> 3     1926      Ashe County North Carolina         37 37009  60.3
#> 4     2016   Watauga County North Carolina         37 37189 174.4
#> 5     2018    Wilkes County North Carolina         37 37193  84.9
#> 6     2995   Grayson County       Virginia         51 51077  34.1
#>   POPULATION POP_SQMI STATE_ABBR COUNTY_FIPS Shape__Area Shape__Length
#> 1      17948     59.3         TN         091  0.07960385      1.290607
#> 2      10888     46.1         NC         005  0.06140165      1.231232
#> 3      26577     61.9         NC         009  0.11428581      1.442112
#> 4      54086    172.6         NC         189  0.08142272      1.287674
#> 5      65969     87.2         NC         193  0.19911944      1.984232
#> 6      15333     34.4         VA         077  0.11578917      1.945424
#>                         geometry
#> 1 POLYGON ((-81.74091 36.3919...
#> 2 POLYGON ((-81.2397 36.36549...
#> 3 POLYGON ((-81.47258 36.2344...
#> 4 POLYGON ((-81.80605 36.1046...
#> 5 POLYGON ((-81.02037 36.0350...
#> 6 POLYGON ((-81.34512 36.5729...

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.

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 services,
  • publishing a new service (the authorized user must also have publishing privileges), and
  • modifying or deleting any existing service (the authorized user must also have edit access 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.

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.

These functions all require authorization since data cannot be published or modified anonymously in ArcGIS Online and ArcGIS Enterprise.

Releases

No releases published

Packages

No packages published

Languages