{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Planetary Computer: Sentinel-2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[![colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/cloudsen12/easystac/blob/main/docs/tutorials/pc-sentinel-2.ipynb)\n", "[![Open in SageMaker Studio Lab](https://studiolab.sagemaker.aws/studiolab.svg)](https://studiolab.sagemaker.aws/import/github/cloudsen12/easystac/blob/main/docs/tutorials/pc-sentinel-2.ipynb)\n", "[![Open in Planetary Computer](https://img.shields.io/badge/Open-Planetary%20Computer-black?style=flat&logo=microsoft)](https://pccompute.westeurope.cloudapp.azure.com/compute/hub/user-redirect/git-pull?repo=https://github.com/cloudsen12/easystac&urlpath=lab/tree/easystac/docs/tutorials/pc-sentinel-2.ipynb&branch=main)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "_Tutorial created by **David Montero Loaiza**_: [GitHub](https://github.com/davemlz) | [Twitter](https://twitter.com/dmlmont)\n", "\n", "- GitHub Repo: [https://github.com/cloudsen12/easystac](https://github.com/cloudsen12/easystac)\n", "- PyPI link: [https://pypi.org/project/easystac/](https://pypi.org/project/easystac/)\n", "- Conda-forge: [https://anaconda.org/conda-forge/easystac](https://anaconda.org/conda-forge/easystac)\n", "- Documentation: [https://easystac.readthedocs.io/](https://easystac.readthedocs.io/)\n", "- More tutorials: [https://github.com/cloudsen12/easystac/tree/main/docs/tutorials](https://github.com/cloudsen12/easystac/tree/main/docs/tutorials)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Install `easystac`:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install -U easystac" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's start!\n", "\n", "First, import the `planetary` module from `easystac`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import easystac.planetary as pc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It is not required to authenticate for having access to Planetary Computer, however, if you have a key, it is better to use it and avoid limits!" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "To authorize access needed by \u001b[1mPlanetary Computer\u001b[0m, open the following URL: \u001b[1mhttp://planetarycomputer.microsoft.com/compute/hub/token\u001b[0m in your web browser. An API key is required to have a more favorable rate limiting in Planetary Computer , so please copy and paste it below.\n", "\n", "Successfully saved authorization token.\n" ] } ], "source": [ "pc.Authenticate()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, in order to use the token, you have to intialize the module!" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "pc.Initialize()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once intialized, you can start working!\n", "\n", "Planetary Computer needs every item to be signed, but you don't have to worry about that anymore! With `easystac`, querying through their STAC is easy and simple!\n", "\n", "The only thing you need is the collection ID `\"sentinel-2-l2a\"` and that's it:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "S2 = pc.ImageCollection(\"sentinel-2-l2a\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `ImageCollection` object will save all important stuff that you want to query, and then you just have to get the info from it. Let's filter this collection by date!" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "S2 = S2.filterDate(\"2020-01-01\",\"2021-01-01\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's use a geometry to filter the collection by bounds!" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "from geojson import Point\n", "\n", "poi = Point([-76.1,3.4])\n", "\n", "S2 = S2.filterBounds(poi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Great! Now we just have to retrieve our request:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/dmontero/.local/lib/python3.9/site-packages/stackstac/accumulate_metadata.py:168: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n", " np.array(\n", "/home/dmontero/.local/lib/python3.9/site-packages/stackstac/accumulate_metadata.py:168: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n", " np.array(\n", "/home/dmontero/.local/lib/python3.9/site-packages/stackstac/accumulate_metadata.py:168: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n", " np.array(\n" ] } ], "source": [ "S2 = S2.getInfo(resolution = 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, your data is in a `xarray.DataArray`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.DataArray 'stackstac-b590d517c3980ac84eb9dad15e868557' (time: 70,\n",
       "                                                                band: 17,\n",
       "                                                                y: 11000,\n",
       "                                                                x: 10989)>\n",
       "dask.array<fetch_raster_window, shape=(70, 17, 11000, 10989), dtype=float64, chunksize=(1, 1, 1024, 1024), chunktype=numpy.ndarray>\n",
       "Coordinates: (12/46)\n",
       "  * time                                     (time) datetime64[ns] 2020-01-04...\n",
       "    id                                       (time) <U54 'S2B_MSIL2A_20200104...\n",
       "  * band                                     (band) <U7 'AOT' ... 'preview'\n",
       "  * x                                        (x) float64 3e+05 ... 4.099e+05\n",
       "  * y                                        (y) float64 4.001e+05 ... 2.901e+05\n",
       "    s2:granule_id                            (time) <U62 'S2B_OPER_MSI_L2A_TL...\n",
       "    ...                                       ...\n",
       "    proj:bbox                                (band) object [300000.0, 290220....\n",
       "    gsd                                      (band) object 10.0 60 ... 10.0 None\n",
       "    common_name                              (band) object None ... None\n",
       "    center_wavelength                        (band) object None 0.443 ... None\n",
       "    full_width_half_max                      (band) object None 0.027 ... None\n",
       "    epsg                                     int64 32618\n",
       "Attributes:\n",
       "    spec:        RasterSpec(epsg=32618, bounds=(300000, 290130, 409890, 40013...\n",
       "    crs:         epsg:32618\n",
       "    transform:   | 10.00, 0.00, 300000.00|\\n| 0.00,-10.00, 400130.00|\\n| 0.00...\n",
       "    resolution:  10
" ], "text/plain": [ "\n", "dask.array\n", "Coordinates: (12/46)\n", " * time (time) datetime64[ns] 2020-01-04...\n", " id (time)