|
| 1 | +--- |
| 2 | +title: "2503_aRcgis" |
| 3 | +output: html_document |
| 4 | +--- |
| 5 | + |
| 6 | +# ArcGIS in R |
| 7 | + |
| 8 | +ArcGIS is a set of powerful propriety geospatial tools developed by ESRI. |
| 9 | +It is widely used in the industry and academia. |
| 10 | +In this blog, I will explore the `arcgis` R package by getting data from |
| 11 | +the Australian tree crop map database, hosted on ArcGIS. |
| 12 | + |
| 13 | +### Austlralian tree crop map |
| 14 | + |
| 15 | +The [Australian tree crop map](https://www.une.edu.au/research/research-centres-institutes/applied-agricultural-remote-sensing-centre/collaborative-r-and-d-opportunities/industry-applications-and-maps/australian-tree-crops) |
| 16 | +is a database of tree crops in Australia. |
| 17 | +The map is the work of the [Applied Agricultural Remote Sensing Centre](https://www.une.edu.au/research/research-centres-institutes/applied-agricultural-remote-sensing-centre) |
| 18 | +at the University of New England. |
| 19 | + |
| 20 | +The data is hosted on ArcGIS and can be accessed at the following link: |
| 21 | +https://www.arcgis.com/home/item.html?id=12bdb57eec7548dd90409603645cfd5e |
| 22 | + |
| 23 | + |
| 24 | +### Installation |
| 25 | + |
| 26 | +Full documentation can be found [at the following link on installation and setup.](https://developers.arcgis.com/r-bridge/installation/) |
| 27 | +This is hosted on R-universe. |
| 28 | + |
| 29 | +`arcgis` is a meta-package for `arcgisutils`, `arcgislayers`, `arcgisgeocode` |
| 30 | +and `arcgisplaces` |
| 31 | + |
| 32 | +```{r eval = FALSE} |
| 33 | +install.packages("arcgis", repos = c("https://r-arcgis.r-universe.dev", "https://cloud.r-project.org")) |
| 34 | +``` |
| 35 | + |
| 36 | +Next lets load the library into the R environment. |
| 37 | + |
| 38 | +```{r} |
| 39 | +library(arcgis) |
| 40 | +library(sf) # also needed for working with spatial data |
| 41 | +``` |
| 42 | + |
| 43 | +### Query features |
| 44 | + |
| 45 | +The URL to query point features from the Australian Tree crop is as follows: |
| 46 | +https://services5.arcgis.com/3foZbDxfCo9kcPwP/ArcGIS/rest/services/AARSC_treecrop_point/FeatureServer/0 |
| 47 | + |
| 48 | +```{r} |
| 49 | +atcm_pts <- arc_open("https://services5.arcgis.com/3foZbDxfCo9kcPwP/ArcGIS/rest/services/AARSC_treecrop_point/FeatureServer/0") |
| 50 | +atcm_pts |
| 51 | +``` |
| 52 | + |
| 53 | +Here we see this feature server contains point geometry, with a coordinate reference |
| 54 | +system 7844 and has Query capabilities. |
| 55 | + |
| 56 | +We can obtain more metadata information from the feature server using the `arc_meta` function. |
| 57 | + |
| 58 | +```{r} |
| 59 | +list_fields(atcm_pts) |
| 60 | +``` |
| 61 | + |
| 62 | + |
| 63 | +### Selecting data |
| 64 | + |
| 65 | +The package contains This returns a `sf` simple features object. |
| 66 | + |
| 67 | +We can query the fields in the `sf` object using the `list_fields` function. |
| 68 | + |
| 69 | +The where argument provides a feature to filter the data based on SQL syntax. |
| 70 | + |
| 71 | +```{r} |
| 72 | +arc_select(atcm_pts, where = c("commodity = 'avocado'")) |
| 73 | +``` |
| 74 | + |
| 75 | + |
| 76 | +#### Query data by spatial reference |
| 77 | + |
| 78 | +From a 10km radius around a point. |
| 79 | +I will choose the centre of Alstonville in Northern New South Wales. |
| 80 | + |
| 81 | +```{r} |
| 82 | +# Define a point (e.g., coordinates for a location) |
| 83 | +point_coords <- c(153.4404,-28.8421) # Example coordinates (longitude,latitude) |
| 84 | +
|
| 85 | +# Create a spatial points data frame |
| 86 | +# We will use the crs 7844 specified in the ATCM metadata |
| 87 | +alston <- st_sfc(st_point(c(point_coords)), crs = 7844) |
| 88 | +
|
| 89 | +# Buffer the point to create a polygon (e.g., 10 kilometers) |
| 90 | +buffer_alston <- st_buffer(alston, 10000) |
| 91 | +
|
| 92 | +# transform the data to WGS84 so we can plot it on a leaflet map |
| 93 | +buffer_alston <- st_transform(buffer_alston, 4326) |
| 94 | +
|
| 95 | +
|
| 96 | +library(leaflet) |
| 97 | +
|
| 98 | +leaflet(buffer_alston) |> |
| 99 | + addProviderTiles(provider = "Esri.WorldImagery")|> |
| 100 | + addMarkers(lng = point_coords[1], lat = point_coords[2], popup = "Alstonville")|> |
| 101 | + addPolygons(color = "green", weight = 2, fillOpacity = 0.2) |
| 102 | +``` |
| 103 | + |
| 104 | + |
| 105 | +Now lets query and download only the data from inside the buffer polygon. |
| 106 | + |
| 107 | +```{r} |
| 108 | +alston_farms <- |
| 109 | + arc_select(atcm_pts, |
| 110 | + filter_geom = buffer_alston, |
| 111 | + predicate = "intersects") |
| 112 | +
|
| 113 | +# transform the data to WGS84 so we can plot it on a leaflet map |
| 114 | +alston_farms <- st_transform(alston_farms, 4326) |
| 115 | +
|
| 116 | +leaflet(alston_farms) |> |
| 117 | + addProviderTiles(provider = "Esri.WorldImagery")|> |
| 118 | + addCircleMarkers(color = "orange", |
| 119 | + weight = 1, |
| 120 | + fillOpacity = 0.8, |
| 121 | + radius = 4, |
| 122 | + popup = alston_farms$commodity)|> |
| 123 | + addMarkers(lng = point_coords[1], lat = point_coords[2], |
| 124 | + popup = "Alstonville") |
| 125 | + |
| 126 | +``` |
| 127 | + |
| 128 | +Lets do this again, however instead of point data, lets get the polygon data from |
| 129 | +the following link: |
| 130 | +https://services5.arcgis.com/3foZbDxfCo9kcPwP/ArcGIS/rest/services/AARSC_treecrop_polygon/FeatureServer/0 |
| 131 | + |
| 132 | + |
| 133 | +```{r} |
| 134 | +atcm_poly <- arc_open("https://services5.arcgis.com/3foZbDxfCo9kcPwP/ArcGIS/rest/services/AARSC_treecrop_polygon/FeatureServer/0") |
| 135 | +list_fields(atcm_poly) |
| 136 | +``` |
| 137 | + |
| 138 | + |
| 139 | +Now lets query and download only the data from inside the buffer polygon. |
| 140 | + |
| 141 | +```{r} |
| 142 | +# lets get the data from inside a bounding box of the buffer polygon |
| 143 | +buffer_box <- |
| 144 | + st_as_sfc(st_bbox(buffer_alston)) |
| 145 | +
|
| 146 | +# request only the data in the box |
| 147 | +alston_farms <- |
| 148 | + arc_select(atcm_poly, |
| 149 | + filter_geom = buffer_box, |
| 150 | + predicate = "intersects") |
| 151 | +
|
| 152 | +# transform the data to WGS84 so we can plot it on a leaflet map |
| 153 | +alston_farms <- st_transform(alston_farms, 4326) |
| 154 | +
|
| 155 | +leaflet(alston_farms) |> |
| 156 | + addProviderTiles(provider = "Esri.WorldImagery")|> |
| 157 | + addPolygons(color = "orange", |
| 158 | + weight = 1, |
| 159 | + fillOpacity = 0.8, |
| 160 | + popup = alston_farms$commodity)|> |
| 161 | + addPolygons(data = buffer_box, color = "green", weight = 2, fillOpacity = 0.2) |
| 162 | + |
| 163 | +``` |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
0 commit comments