Planetary Computer: Sentinel-2

colab Open in SageMaker Studio Lab Open in Planetary Computer

Tutorial created by **David Montero Loaiza**: GitHub | Twitter

Install easystac:

[ ]:
!pip install -U easystac

Now, let’s start!

First, import the planetary module from easystac:

[1]:
import easystac.planetary as pc

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!

[2]:
pc.Authenticate()
To authorize access needed by Planetary Computer, open the following URL: http://planetarycomputer.microsoft.com/compute/hub/token 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.

Successfully saved authorization token.

Now, in order to use the token, you have to intialize the module!

[3]:
pc.Initialize()

Once intialized, you can start working!

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!

The only thing you need is the collection ID "sentinel-2-l2a" and that’s it:

[4]:
S2 = pc.ImageCollection("sentinel-2-l2a")

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!

[5]:
S2 = S2.filterDate("2020-01-01","2021-01-01")

Now, let’s use a geometry to filter the collection by bounds!

[8]:
from geojson import Point

poi = Point([-76.1,3.4])

S2 = S2.filterBounds(poi)

Great! Now we just have to retrieve our request:

[9]:
S2 = S2.getInfo(resolution = 10)
/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.
  np.array(
/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.
  np.array(
/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.
  np.array(

Now, your data is in a xarray.DataArray:

[10]:
S2
[10]:
<xarray.DataArray 'stackstac-b590d517c3980ac84eb9dad15e868557' (time: 70,
                                                                band: 17,
                                                                y: 11000,
                                                                x: 10989)>
dask.array<fetch_raster_window, shape=(70, 17, 11000, 10989), dtype=float64, chunksize=(1, 1, 1024, 1024), chunktype=numpy.ndarray>
Coordinates: (12/46)
  * time                                     (time) datetime64[ns] 2020-01-04...
    id                                       (time) <U54 'S2B_MSIL2A_20200104...
  * band                                     (band) <U7 'AOT' ... 'preview'
  * x                                        (x) float64 3e+05 ... 4.099e+05
  * y                                        (y) float64 4.001e+05 ... 2.901e+05
    s2:granule_id                            (time) <U62 'S2B_OPER_MSI_L2A_TL...
    ...                                       ...
    proj:bbox                                (band) object [300000.0, 290220....
    gsd                                      (band) object 10.0 60 ... 10.0 None
    common_name                              (band) object None ... None
    center_wavelength                        (band) object None 0.443 ... None
    full_width_half_max                      (band) object None 0.027 ... None
    epsg                                     int64 32618
Attributes:
    spec:        RasterSpec(epsg=32618, bounds=(300000, 290130, 409890, 40013...
    crs:         epsg:32618
    transform:   | 10.00, 0.00, 300000.00|\n| 0.00,-10.00, 400130.00|\n| 0.00...
    resolution:  10

That was all! Now you can play with the DataArray as you wish!