Skip to content

Commit

Permalink
update local
Browse files Browse the repository at this point in the history
  • Loading branch information
JosiahParry committed Sep 10, 2024
1 parent 5395e6f commit a6522b6
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 391 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
_arcgis/
/.quarto/
.Rproj.user
_site/
^.quarto*$
.quarto
./quarto/*
/.quarto/
/.quarto/
17 changes: 17 additions & 0 deletions _freeze/docs/layers/read-rasters/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"hash": "5ccd2dc4ed13c30630dbb1766df09ddd",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Reading Image Services\"\nfreeze: true\n---\n\n\nArcGIS Online and Enterprise web services can easily be read into R using`{arcgislayers}`. Supported service types include:\n\n - [FeatureServer](https://developers.arcgis.com/rest/services-reference/enterprise/feature-service.htm)\n - [FeatureLayer](https://developers.arcgis.com/rest/services-reference/enterprise/feature-layer.htm)\n - [Table](https://developers.arcgis.com/rest/services-reference/enterprise/feature-layer.htm)\n - [MapServer](https://developers.arcgis.com/rest/services-reference/enterprise/map-service.htm)\n - [GroupLayer](https://developers.arcgis.com/web-map-specification/objects/groupLayer/)\n - [ImageServer](https://developers.arcgis.com/rest/services-reference/enterprise/image-service.htm)\n\n\nMetadata for all of the above service types can be accessed using `arc_open()`. Feature data can be read in using `arc_select()` for FeatureLayer, Table, and ImageServer.\n\nThis tutorial will teach you the basics of reading data from hosted image services into R as [`{terra} SpatRaster`](https://rspatial.github.io/terra/reference/rast.html) objects using`{arcgislayers}`. The source for an image service is published raster or imagery data. To learn more about image services, see the [Image services documentation](https://enterprise.arcgis.com/en/server/latest/publish-services/windows/key-concepts-for-image-services.htm).\n\n## Objective\n\nThe objective of this tutorial is to teach you how to:\n\n- find a image service url from ArcGIS Online\n- read in the data from the image service\n- filter the image service by a bounding box\n- use `terra` for viewing and writing\n\n## Obtaining an image service url\n\nFor this example, you will read in multispectral Landsat imagery of the Ouarkziz Crater from ArcGIS Online. \n\nYou will use the functions `arc_open()` and `arc_raster()` to read image data from ArcGIS Online into R. However, these functions require the url of the hosted image service. To find this, navigate to the [item](https://www.arcgis.com/home/item.html?id=d9b466d6a9e647ce8d1dd5fe12eb434b) in ArcGIS Online. \n\n\n![](../images/read-data/multispectral-landsat.png)\nWhen you scroll down, on the right hand side, you will see a button to view the service itself. \n\n![](../images/read-data/view-url-imagery.png){width=45%}\n\nClicking this will bring you to the Image Service, where you can more closely investigate the metadata and supported operations for this service. Navigate to your browser's search bar and copy the url.\n\n```\nhttps://landsat2.arcgis.com/arcgis/rest/services/Landsat/MS/ImageServer\n```\n\n## Opening an Image Service\n\nFirst, load the `arcgis` R package. If you do not have `arcgis` installed, install it with `pak::pak(\"r-arcgis/arcgis\")` or `install.packages(\"arcgis\")`.\n\n:::{.aside}\n`{pak}` is an R package that makes it faster and easier to install R packages. If you do not have it installed, run `install.packages(\"pak\")` first. \n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(arcgis)\n```\n\n::: {.cell-output .cell-output-stderr}\n\n```\nAttaching core arcgis packages:\n→ arcgisutils v0.3.0\n→ arcgislayers v0.3.0\n→ arcgisgeocode v0.1.3\n→ arcgisplaces v0.1.0\n```\n\n\n:::\n:::\n\n\nUse the below code to store the image service url in an object called `url`.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nurl <- \"https://landsat2.arcgis.com/arcgis/rest/services/Landsat/MS/ImageServer\"\n```\n:::\n\n\nThen pass this variable to `arc_open()` and save it to `imgsrv` (image service).\n\n\n::: {.cell}\n\n```{.r .cell-code}\nimgsrv <- arc_open(url)\nimgsrv\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n<ImageServer <11 bands, 26 fields>>\nName: Landsat/MS\nDescription: Multispectral Landsat image service covering the landmass of the W\nExtent: -20037507.07 20037507.84 -9694091.07 9691188.93 (xmin, xmax, ymin, ymax)\nResolution: 30 x 30\nCRS: 3857\nCapabilities: Catalog,Image,Metadata\n```\n\n\n:::\n:::\n\n\n`arc_open()` will create a `ImageServer` object. Under the hood, this is really just a list containing the image service's metadata. \n\n:::{.callout-note collapse=\"true\" title=\"ImageServer details for the curious\"}\nThe `ImageServer` object is obtained by adding `?f=json` to the image server url and processing the json. All of the metadata is stored in the `ImageServer` object. You can see this by running `unclass(imgsrv)`. Be warned! It gets messy. \n:::\n\nWith this `ImageServer` object, you can read data from the service into R! \n\n## Reading from a Image Service\n\nOnce you have a `ImageServer` object, you can access the image data using the `arc_raster()` function. Pass the coordinates for a bounding box into the function using the `xmin`, `ymin`, `xmax`, and `ymax` arguments. Store the results of `arc_raster()` in the object `crater`. \n\n:::{.callout-warning}\nAvoid reading in more data than you need! When extracting data from an image service, it is best practice to include a bounding box to limit the extraction to just the area that you need. Make sure to provide the bounding box coordinates in the Coordinate Reference System (CRS) of the image service or use the `bbox_crs` argument to specify another CRS for these coordinates.\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\ncrater <- arc_raster(\n imgsrv,\n xmin = \"-846028\",\n ymin = \"3373101\",\n xmax = \"-833783\",\n ymax = \"3380738\"\n)\ncrater\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\nclass : SpatRaster \ndimensions : 400, 400, 11 (nrow, ncol, nlyr)\nresolution : 30.6125, 30.6125 (x, y)\nextent : -846028, -833783, 3370797, 3383042 (xmin, xmax, ymin, ymax)\ncoord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857) \nsource : file13b4a2cd01c61.tiff \nnames : Coast~rosol, Blue, Green, Red, NearInfrared, Short~red_1, ... \n```\n\n\n:::\n:::\n\n\nThe result is a `SpatRaster` object that you can now work with using **`terra`** and any other R packages. \n\n\n### Using `terra`\n\nFrom here, you can pursue your own raster and imagery workflows using `terra`. For some simple examples, consider plotting the image:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nterra::plotRGB(crater, stretch = \"lin\")\n```\n\n::: {.cell-output-display}\n![](read-rasters_files/figure-html/unnamed-chunk-5-1.png){width=672}\n:::\n:::\n\n\nor saving the image locally:\n\n\n::: {.cell}\n\n```{.r .cell-code}\nterra::writeRaster(crater, \"ouarkziz-crater-RGB.tif\", overwrite = TRUE)\n```\n:::\n",
"supporting": [
"read-rasters_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions _quarto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ format:
highlight-style: pygments

execute:
keep-md: true
freeze: true
15 changes: 0 additions & 15 deletions arcgis-site.Rproj

This file was deleted.

27 changes: 26 additions & 1 deletion dev/render-devs.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,36 @@ render_qmd_to_md <- function(in_path, out_path, work_dir = dirname(in_path)) {
file.create(out_path)

# add autolinking and syntax highlighting (we will have to choose colors manually later)
downlit::downlit_md_path(tmp, out_path = out_path)
tryCatch(downlit::downlit_md_path(tmp, out_path = out_path), error = function(e) {
cli::cli_alert_danger("Failed apply downlit to {.file {in_path}}")
file.copy(tmp, out_path, TRUE)
})
}


in_fps <- list.files(pattern = "*.qmd", recursive = TRUE)
out_fps <- paste0(tools::file_path_sans_ext(file.path("_arcgis", in_fps)), ".md")


# create directories
for (dirp in unique(dirname(out_fps))) {
if (!dir.exists(dirp)) {
dir.create(dirp, recursive = TRUE)
}
}

# failed at 11 (docs)"_arcgis/docs/geocode/overview.md"
for (i in 1:length(in_fps)) {
ip <- in_fps[[i]]
op <- out_fps[[i]]
cli::cli_alert_info("Rendering # file {i}: {.file {ip}} to {.file {op}}")
render_qmd_to_md(ip, op)
}

render_qmd_to_md(in_fps[20], out_fps[20])
# Example
# render_qmd_to_md(
# "location-services/publishing.qmd",
# "markdown/publishing.md"
# )

Empty file.
Binary file added docs/layers/ouarkziz-crater-RGB.tif
Binary file not shown.
53 changes: 0 additions & 53 deletions docs/layers/read-layers.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -168,59 +168,6 @@ arc_select(
)
```

## Using `dplyr`

If writing the field names out by hand and coming up with SQL where clauses isn't your thing, that's okay. We also provide `dplyr::select()` and `dplyr::filter()` methods for `FeatureLayer` objects.

The dplyr functionality is modeled off of [`dbplyr`](https://dbplyr.tidyverse.org/). The general concept is that you have a connection object that specifies what you will be querying against. Then you build up queries using dplyr functions. Unlike using dplyr on `data.frame`s, the results aren't fetched eagerly. Instead they are _lazy_. With `dbplyr`, you use the `collect()` function to execute a query and bring it into memory. The same is true with `FeatureLayer` objects.

Let's build up a query and see it in action! First, load dplyr to bring the functions into scope.

```{r, message = FALSE}
library(dplyr)
fl_query <- flayer |>
select(STATE_ABBR, POPULATION, NAME)
fl_query
```

After doing this, your `FeatureLayer` object now prints out a `Query` field with the `outFields` parameter set to the result of your `select()` function.

:::{.callout-note collapse="true" title="A note for advanced useRs"}
You build up and store the query in the `query` attribute of a `FeatureLayer` object. It is a named list that will be passed directly to the API endpoint. The names match endpoint parameters.

```{r}
attr(fl_query, "query")
```

You can also manually specify parameters using the `update_params()` function. Note that there is _no_ parameter validation.

```{r}
update_params(fl_query, key = "value")
```

:::

You can continue to build up your query using `filter()`

:::{.callout-tip}
Only very basic filter statements are supported such as `==`, `<`, `>`, etc.
:::

```{r, message = FALSE}
fl_query |>
filter(POPULATION > 1000000, STATE_ABBR == "CA")
```

The query is stored in the `FeatureLayer` object and will not be executed until you request it with `collect()`.

```{r}
fl_query |>
filter(POPULATION > 1000000, STATE_ABBR == "CA") |>
collect()
```

## Map and Feature Servers

This example has only illustrated how to work with `FeatureLayer` objects. However, often times you may wish to work with a collection of layers in a `FeatureServer`, `MapServer`, or `GroupLayer`. All of these are collections of multiple layers. Like a `FeatureLayer`, these are accessed with `arc_open()`.
Expand Down
8 changes: 6 additions & 2 deletions docs/layers/read-rasters.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ First, load the `arcgis` R package. If you do not have `arcgis` installed, insta
library(arcgis)
```

```{r include=FALSE}
unset_arc_token()
```

Use the below code to store the image service url in an object called `url`.

```{r}
Expand Down Expand Up @@ -104,12 +108,12 @@ The result is a `SpatRaster` object that you can now work with using **`terra`**
From here, you can pursue your own raster and imagery workflows using `terra`. For some simple examples, consider plotting the image:

```{r, message = FALSE}
terra::plotRGB(img, stretch = "lin")
terra::plotRGB(crater, stretch = "lin")
```

or saving the image locally:

```{r, message = FALSE}
terra::writeRaster(crater, "ouarkziz-crater-RGB.tif", overwrite=TRUE)
terra::writeRaster(crater, "ouarkziz-crater-RGB.tif", overwrite = TRUE)
```

Loading

0 comments on commit a6522b6

Please sign in to comment.