{ "cells": [ { "cell_type": "markdown", "id": "7407dcdd", "metadata": {}, "source": [ "## Reading from the STAC API with R\n", "\n", "This quickstart introduces STAC for R users. To use the Planetary Computer's STAC API from Python, see [here](https://planetarycomputer.microsoft.com/docs/quickstarts/reading-stac/).\n", "\n", "We'll use the [rstac](https://github.com/brazil-data-cube/rstac) library to make requests to the STAC API." ] }, { "cell_type": "code", "execution_count": 1, "id": "732a9e0f", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "terra 1.5.12\n", "\n", "\n", "Attaching package: ‘terra’\n", "\n", "\n", "The following objects are masked from ‘package:magrittr’:\n", "\n", " extract, inset\n", "\n", "\n" ] } ], "source": [ "library(rstac)\n", "library(magrittr)\n", "library(terra)\n", "\n", "s_obj <- stac(\"https://planetarycomputer.microsoft.com/api/stac/v1/\")" ] }, { "cell_type": "markdown", "id": "afae50de", "metadata": {}, "source": [ "We can search for items using `s_obj` and the `stac_search` function:" ] }, { "cell_type": "code", "execution_count": 2, "id": "856823b0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[1m###STACItemCollection\u001b[22m\n", "- \u001b[1mfeatures\u001b[22m (250 item(s)):\n", " - LC09_L2SP_217072_20220515_02_T1\n", " - LC09_L2SP_217071_20220515_02_T2\n", " - LC09_L2SP_219071_20220513_02_T1\n", " - LC09_L2SP_221071_20220511_02_T1\n", " - LC09_L2SP_221070_20220511_02_T1\n", " - LC09_L2SP_221069_20220511_02_T1\n", " - LC08_L2SP_217072_20220507_02_T2\n", " - LC08_L2SP_217071_20220507_02_T1\n", " - LC09_L2SP_218072_20220506_02_T1\n", " - LC09_L2SP_218071_20220506_02_T1\n", " - ... with 240 more feature(s).\n", "- \u001b[1massets:\u001b[22m \n", "qa, ang, red, blue, drad, emis, emsd, trad, urad, atran, cdist, green, nir08, lwir11, swir16, swir22, coastal, mtl.txt, mtl.xml, mtl.json, qa_pixel, qa_radsat, qa_aerosol, tilejson, rendered_preview\n", "- \u001b[1mother field(s):\u001b[22m type, features, links\n" ] } ], "source": [ "it_obj <- s_obj %>% \n", " stac_search(collections = \"landsat-c2-l2\",\n", " bbox = c(-47.02148, -17.35063, -42.53906, -12.98314)) %>%\n", " get_request()\n", "\n", "print(it_obj)" ] }, { "cell_type": "markdown", "id": "55657d45", "metadata": {}, "source": [ "As detailed in [Using tokens for data access](https://planetarycomputer.microsoft.com/docs/concepts/sas/), accessing *data* from the Planetary Computer typically requires signing the item. `rstac` has built-in support for signing." ] }, { "cell_type": "code", "execution_count": 3, "id": "7bc2c07d", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Warning message:\n", "“Items matched not provided.”\n" ] } ], "source": [ "it_obj <- s_obj %>% \n", " stac_search(collections = \"landsat-c2-l2\",\n", " bbox = c(-47.02148, -17.35063, -42.53906, -12.98314)) %>%\n", " get_request() %>%\n", " items_sign(sign_fn = sign_planetary_computer())" ] }, { "cell_type": "code", "execution_count": 4, "id": "ec2fdba2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "plot without title" ] }, "metadata": { "image/png": { "height": 420, "width": 420 } }, "output_type": "display_data" } ], "source": [ "url <- paste0(\"/vsicurl/\", it_obj$features[[1]]$assets$blue$href)\n", "\n", "data <- rast(url)\n", "plot(data)" ] }, { "cell_type": "markdown", "id": "ddf6735a", "metadata": {}, "source": [ "### Learn more\n", "\n", "For more about the Planetary Computer's STAC API, see [Using tokens for data access](https://planetarycomputer.microsoft.com/docs/concepts/sas/) and the [STAC API reference](https://planetarycomputer.microsoft.com/docs/reference/stac/). For more about rstac, see [its documentation](https://rdrr.io/cran/rstac/)." ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.1.2" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }