{ "cells": [ { "cell_type": "markdown", "id": "91f57380-43bd-4093-aa69-9146532d0ac7", "metadata": {}, "source": [ "STAC objects -> data containers\n", "\n", "Hi all,\n", "\n", "I'm tenataively making a pitch to add convenience methods for converting pystac objects (Asset, Item, ItemCollection, ...) to commonly used data containers (`xarray.Dataset`, `geopandas.GeoDataFrame`, `pandas.DataFrame`, etc.).\n", "\n", "I'm opening this in `pystac` since **this is primarily for convenience, so that users can method-chain their way from STAC Catalog to data container**, and `pystac` owns the namespaces I care about. You can already do everything I'm showing today without any changes to `pystac` but it *feels less nice*. I really think that `pd.read_csv` is part of why Python is where it is today for data analytics; I want using STAC from Python to be as easy to use as `pd.read_csv`.\n", "Secondarily, it *can* elevate the best-practice way to go from STAC to data containers, by providing a top-level method similar to `to_dict()`.\n", "\n", "As a couple hypothetical examples, to give an idea:\n", "\n", "```python\n", "ds = (\n", " catalog\n", " .get_collection(\"sentinel-2-l2a\")\n", " .get_item(\"S2B_MSIL2A_20220612T182919_R027_T24XWR_20220613T123251\")\n", " .assets[\"B03\"]\n", " .to_xarray()\n", ")\n", "ds\n", "```\n", "\n", "Or building a datacube from a pystac-client search (which subclasses pystac).\n", "\n", "```python\n", "ds = (\n", " catalog\n", " .search(collections=\"sentinel-2-l2a\", bbox=bbox)\n", " .get_all_items() # ItemCollection\n", " .to_xarray()\n", ")\n", "ds\n", "```\n", "\n", "## Implementation details\n", "\n", "This would be optional. `pystac` would not add required dependencies on `pandas`, `xarray`, etc. It would merely provide the methods `Item.to_xarray`, `Asset.to_xarray`, ... Internally those methods would try to import the implementation and raise an `ImportError` if the optional dependencies aren't met at runtime.\n", "\n", "Speaking of the implementations, there's a few things to figure out. Some relatively complicated conversions (like ItemCollection -> xarray) are implemented multiple times (https://stackstac.readthedocs.io/, https://odc-stac.readthedocs.io/en/latest/examples.html). `pystac` certainly wouldn't want to re-implement that conversion and would dispatch to one or either of those libraries (perhaps letting users decide with an `engine` argument).\n", "\n", "Others conversions, like Asset -> Zarr, are so straightforward they haven't really been codified in a library yet (though I have a prototype at https://github.com/TomAugspurger/staccontainers/blob/086c2a7d46520ca5213d70716726b28ba6f36ba5/staccontainers/_xarray.py#L61-L63).\n", "*Maybe* those could live in pystac; I'd be happy to maintain them.\n", "\n", "## Problems\n", "\n", "A non-exhaustive list of reasons not to do this:\n", "\n", "- It's not strictly necessary: You *can* do all this today, with some effort.\n", "- It's a can of worms: Why `to_xarray` and not `to_numpy()`, `to_PIL`, ...? Why `to_pandas()` and not `to_spark()`, `to_modin`, ...?\n", "\n", "## Alternatives\n", "\n", "Alternatively, we could recommend using [intake](https://intake.readthedocs.io/), along with [intake-stac](https://intake-stac.readthedocs.io/en/latest/), which would wrap `pystac-client` and `pystac`. That would be the primary \"user-facing\" catalog people actually interface with. It already has a rich ecosystem of drivers that convert from files to data containers. I've hit some issues with trying to use intake-stac, but those could presumably be fixed with some effort.\n", "\n", "## Examples\n", "\n", "A whole bunch of examples, to give some ideas of the various conversions. You'll notice a pattern.\n", "\n", "### catalog -> collection -> item -> asset -> xarray (raster)\n", "\n", "\n", "```python\n", "ds = (\n", " catalog\n", " .get_collection(\"sentinel-2-l2a\")\n", " .get_item(\"S2B_MSIL2A_20220612T182919_R027_T24XWR_20220613T123251\")\n", " .assets[\"B03\"]\n", " .to_xarray()\n", ")\n", "```\n", "\n", "### catalog -> collection -> item -> asset -> xarray (zarr)\n", "\n", "```python\n", "ds = (\n", " catalog\n", " .get_collection(\"cil-gdpcir-cc0\")\n", " .get_item(\"cil-gdpcir-INM-INM-CM5-0-ssp585-r1i1p1f1-day\")\n", " .assets[\"pr\"]\n", " .to_xarray()\n", ")\n", "```\n", "\n", "### catlaog -> collection -> item -> asset -> xarray (references)\n", "\n", "```python\n", "ds = (\n", " catalog\n", " .get_collection(\"deltares-floods\")\n", " .get_item(\"NASADEM-90m-2050-0010\")\n", " .assets[\"index\"]\n", " .to_xarray()\n", ")\n", "```\n", "### catalog -> collection -> item -> asset -> geodataframe\n", "\n", "```python\n", "df = (\n", " catalog\n", " .get_collection(\"us-census\")\n", " .get_item(\"2020-cb_2020_us_tbg_500k\")\n", " .assets[\"data\"]\n", " .to_geopandas()\n", ")\n", "df.head()\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "id": "5f21f977-919d-4d0b-b7da-affa392a0b00", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Found existing installation: pystac-client 0.4.0\n", "Uninstalling pystac-client-0.4.0:\n", " Successfully uninstalled pystac-client-0.4.0\n", "Found existing installation: staccontainers 0.1.0\n", "Uninstalling staccontainers-0.1.0:\n", " Successfully uninstalled staccontainers-0.1.0\n", "Found existing installation: planetary-computer 0.4.7\n", "Uninstalling planetary-computer-0.4.7:\n", " Successfully uninstalled planetary-computer-0.4.7\n" ] } ], "source": [ "!pip uninstall -y pystac-client staccontainers planetary-computer\n", "!pip install -q git+https://github.com/TomAugspurger/pystac-client@feature/sign\n", "!pip install -q git+https://github.com/microsoft/planetary-computer-sdk-for-python\n", "!pip install -q git+https://github.com/TomAugspurger/staccontainers" ] }, { "cell_type": "code", "execution_count": 1, "id": "9529365d-123f-4bfe-9e61-08fdf2baab11", "metadata": {}, "outputs": [], "source": [ "import pystac_client\n", "import planetary_computer\n", "\n", "from staccontainers import *\n", "\n", "bbox = [9.4, 0, 9.5, 1]\n", "catalog = pystac_client.Client.open(\n", " \"https://planetarycomputer.microsoft.com/api/stac/v1\",\n", " sign_function=planetary_computer.sign\n", ")" ] }, { "cell_type": "markdown", "id": "3f9fb595-b818-4c5f-b284-72d28971b7e6", "metadata": {}, "source": [ "## Asset -> xarray (raster)" ] }, { "cell_type": "code", "execution_count": 2, "id": "06eb61a4-b851-4cdf-b72a-e8d95d01e941", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:      (band: 1, x: 10980, y: 10980)\n",
       "Coordinates:\n",
       "  * band         (band) int64 1\n",
       "  * x            (x) float64 5e+05 5e+05 5e+05 ... 6.098e+05 6.098e+05 6.098e+05\n",
       "  * y            (y) float64 9.1e+06 9.1e+06 9.1e+06 ... 8.99e+06 8.99e+06\n",
       "    spatial_ref  int64 0\n",
       "Data variables:\n",
       "    band_data    (band, y, x) float32 ...
" ], "text/plain": [ "\n", "Dimensions: (band: 1, x: 10980, y: 10980)\n", "Coordinates:\n", " * band (band) int64 1\n", " * x (x) float64 5e+05 5e+05 5e+05 ... 6.098e+05 6.098e+05 6.098e+05\n", " * y (y) float64 9.1e+06 9.1e+06 9.1e+06 ... 8.99e+06 8.99e+06\n", " spatial_ref int64 ...\n", "Data variables:\n", " band_data (band, y, x) float32 ..." ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# catalog -> item -> asset -> xarray (zarr)\n", "ds = (\n", " catalog\n", " .get_collection(\"sentinel-2-l2a\")\n", " .get_item(\"S2B_MSIL2A_20220612T182919_R027_T24XWR_20220613T123251\")\n", " .assets[\"B03\"]\n", " .to_xarray()\n", ")\n", "ds" ] }, { "cell_type": "markdown", "id": "c6df3bda-bcaa-4b63-9dbf-683ff97b1af8", "metadata": {}, "source": [ "## Asset -> xarray (zarr)" ] }, { "cell_type": "code", "execution_count": 3, "id": "dfe64e9c-9d20-4a95-80b0-28d4020f1a1f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:  (lat: 720, lon: 1440, time: 31390)\n",
       "Coordinates:\n",
       "  * lat      (lat) float64 -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88\n",
       "  * lon      (lon) float64 -179.9 -179.6 -179.4 -179.1 ... 179.4 179.6 179.9\n",
       "  * time     (time) object 2015-01-01 12:00:00 ... 2100-12-31 12:00:00\n",
       "Data variables:\n",
       "    pr       (time, lat, lon) float64 dask.array<chunksize=(365, 360, 360), meta=np.ndarray>\n",
       "Attributes: (12/47)\n",
       "    Conventions:                  CF-1.7 CMIP-6.2\n",
       "    activity_id:                  ScenarioMIP\n",
       "    contact:                      climatesci@rhg.com\n",
       "    creation_date:                2019-07-23T13:02:14Z\n",
       "    data_specs_version:           01.00.29\n",
       "    dc6_bias_correction_method:   Quantile Delta Method (QDM)\n",
       "    ...                           ...\n",
       "    sub_experiment_id:            none\n",
       "    table_id:                     day\n",
       "    tracking_id:                  hdl:21.14100/ba34d30b-fca8-4737-887f-344ec5...\n",
       "    variable_id:                  pr\n",
       "    variant_label:                r1i1p1f1\n",
       "    version_id:                   v20190724
" ], "text/plain": [ "\n", "Dimensions: (lat: 720, lon: 1440, time: 31390)\n", "Coordinates:\n", " * lat (lat) float64 -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88\n", " * lon (lon) float64 -179.9 -179.6 -179.4 -179.1 ... 179.4 179.6 179.9\n", " * time (time) object 2015-01-01 12:00:00 ... 2100-12-31 12:00:00\n", "Data variables:\n", " pr (time, lat, lon) float64 dask.array\n", "Attributes: (12/47)\n", " Conventions: CF-1.7 CMIP-6.2\n", " activity_id: ScenarioMIP\n", " contact: climatesci@rhg.com\n", " creation_date: 2019-07-23T13:02:14Z\n", " data_specs_version: 01.00.29\n", " dc6_bias_correction_method: Quantile Delta Method (QDM)\n", " ... ...\n", " sub_experiment_id: none\n", " table_id: day\n", " tracking_id: hdl:21.14100/ba34d30b-fca8-4737-887f-344ec5...\n", " variable_id: pr\n", " variant_label: r1i1p1f1\n", " version_id: v20190724" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# catalog -> item -> asset -> xarray (zarr)\n", "ds = (\n", " catalog\n", " .get_collection(\"cil-gdpcir-cc0\")\n", " .get_item(\"cil-gdpcir-INM-INM-CM5-0-ssp585-r1i1p1f1-day\")\n", " .assets[\"pr\"]\n", " .to_xarray()\n", ")\n", "ds" ] }, { "cell_type": "markdown", "id": "23c555d9-284d-4705-af28-25f59bad2684", "metadata": {}, "source": [ "## Asset -> xarray (references)" ] }, { "cell_type": "code", "execution_count": 4, "id": "7fdaa08b-01b5-47dd-9911-10ef3bd93b7a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:     (time: 1, lat: 216000, lon: 432000)\n",
       "Coordinates:\n",
       "  * lat         (lat) float64 -90.0 -90.0 -90.0 -90.0 ... 90.0 90.0 90.0 90.0\n",
       "  * lon         (lon) float64 -180.0 -180.0 -180.0 -180.0 ... 180.0 180.0 180.0\n",
       "  * time        (time) datetime64[ns] 2010-01-01\n",
       "Data variables:\n",
       "    inun        (time, lat, lon) float32 dask.array<chunksize=(1, 600, 600), meta=np.ndarray>\n",
       "    projection  object ...\n",
       "Attributes:\n",
       "    Conventions:  CF-1.6\n",
       "    config_file:  /mnt/globalRuns/watermask_post_NASA90m_rest/run_rp0010_slr2...\n",
       "    institution:  Deltares\n",
       "    project:      Microsoft Planetary Computer - Global Flood Maps\n",
       "    references:   https://www.deltares.nl/en/\n",
       "    source:       Global Tide and Surge Model v3.0 - ERA5\n",
       "    title:        GFM - NASA DEM 90m - 2050 slr - 0010-year return level
" ], "text/plain": [ "\n", "Dimensions: (time: 1, lat: 216000, lon: 432000)\n", "Coordinates:\n", " * lat (lat) float64 -90.0 -90.0 -90.0 -90.0 ... 90.0 90.0 90.0 90.0\n", " * lon (lon) float64 -180.0 -180.0 -180.0 -180.0 ... 180.0 180.0 180.0\n", " * time (time) datetime64[ns] 2010-01-01\n", "Data variables:\n", " inun (time, lat, lon) float32 dask.array\n", " projection object ...\n", "Attributes:\n", " Conventions: CF-1.6\n", " config_file: /mnt/globalRuns/watermask_post_NASA90m_rest/run_rp0010_slr2...\n", " institution: Deltares\n", " project: Microsoft Planetary Computer - Global Flood Maps\n", " references: https://www.deltares.nl/en/\n", " source: Global Tide and Surge Model v3.0 - ERA5\n", " title: GFM - NASA DEM 90m - 2050 slr - 0010-year return level" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# catlaog -> item -> asset -> xarray (references)\n", "ds = (\n", " catalog\n", " .get_collection(\"deltares-floods\")\n", " .get_item(\"NASADEM-90m-2050-0010\")\n", " .assets[\"index\"]\n", " .to_xarray()\n", ")\n", "ds" ] }, { "cell_type": "markdown", "id": "70478a65-1bf7-49a4-8574-8cdf5c807cb9", "metadata": {}, "source": [ "## Asset -> geopandas" ] }, { "cell_type": "code", "execution_count": 5, "id": "a7855a40-c770-463e-9470-63ad6e3f12af", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
AIANNHCETTRACTCETBLKGPCEAFFGEOIDGEOIDNAMELSADLSADALANDAWATERgeometry
02430T03700C2580000US2430T03700C2430T03700CTribal Block Group CIB39451950POLYGON ((-111.26008 36.10715, -111.25910 36.1...
120T00400B2580000US0020T00400B0020T00400BTribal Block Group BIB1200584100165POLYGON ((-116.47052 33.78691, -116.46940 33.7...
21150T00100C2580000US1150T00100C1150T00100CTribal Block Group CIB6543546132911122MULTIPOLYGON (((-108.90981 47.91399, -108.8883...
32555T01000A2580000US2555T01000A2555T01000ATribal Block Group AIB396343904216784POLYGON ((-75.91155 43.00678, -75.90228 43.006...
4275T00100A2580000US0275T00100A0275T00100ATribal Block Group AIB4826510POLYGON ((-122.88954 39.02367, -122.88639 39.0...
\n", "
" ], "text/plain": [ " AIANNHCE TTRACTCE TBLKGPCE AFFGEOID GEOID \\\n", "0 2430 T03700 C 2580000US2430T03700C 2430T03700C \n", "1 20 T00400 B 2580000US0020T00400B 0020T00400B \n", "2 1150 T00100 C 2580000US1150T00100C 1150T00100C \n", "3 2555 T01000 A 2580000US2555T01000A 2555T01000A \n", "4 275 T00100 A 2580000US0275T00100A 0275T00100A \n", "\n", " NAMELSAD LSAD ALAND AWATER \\\n", "0 Tribal Block Group C IB 3945195 0 \n", "1 Tribal Block Group B IB 1200584 100165 \n", "2 Tribal Block Group C IB 654354613 2911122 \n", "3 Tribal Block Group A IB 39634390 4216784 \n", "4 Tribal Block Group A IB 482651 0 \n", "\n", " geometry \n", "0 POLYGON ((-111.26008 36.10715, -111.25910 36.1... \n", "1 POLYGON ((-116.47052 33.78691, -116.46940 33.7... \n", "2 MULTIPOLYGON (((-108.90981 47.91399, -108.8883... \n", "3 POLYGON ((-75.91155 43.00678, -75.90228 43.006... \n", "4 POLYGON ((-122.88954 39.02367, -122.88639 39.0... " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = (\n", " catalog\n", " .get_collection(\"us-census\")\n", " .get_item(\"2020-cb_2020_us_tbg_500k\")\n", " .assets[\"data\"]\n", " .to_geopandas()\n", ")\n", "df.head()" ] }, { "cell_type": "markdown", "id": "bac888ba-1889-4d36-9a33-254b51f9286f", "metadata": {}, "source": [ "## Asset -> dask_geopandas" ] }, { "cell_type": "code", "execution_count": 6, "id": "d8016766-19b4-40b6-acb8-4da75aaea0b9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Dask-GeoPandas GeoDataFrame Structure:
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
geometryRegionName
npartitions=13
geometrycategory[known]
......
.........
......
......
\n", "
\n", "
Dask Name: read-parquet, 13 tasks
" ], "text/plain": [ "Dask GeoDataFrame Structure:\n", " geometry RegionName\n", "npartitions=13 \n", " geometry category[known]\n", " ... ...\n", "... ... ...\n", " ... ...\n", " ... ...\n", "Dask Name: read-parquet, 13 tasks" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ddf = (\n", " catalog\n", " .get_collection(\"ms-buildings\")\n", " .get_item(\"Germany_2022-06-14\")\n", " .assets[\"data\"]\n", " .to_dask_geopandas()\n", ")\n", "ddf" ] }, { "cell_type": "markdown", "id": "8eaf9cd8-3085-4ad4-9b6a-8ec202043ffd", "metadata": {}, "source": [ "## ItemCollection -> xarray" ] }, { "cell_type": "code", "execution_count": 7, "id": "e726aad6-1e90-44a2-86b7-1ebb48db7f26", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:                                  (time: 100, y: 30984, x: 10981,\n",
       "                                              band: 2)\n",
       "Coordinates: (12/43)\n",
       "  * time                                     (time) datetime64[ns] 2022-01-26...\n",
       "    id                                       (time) <U54 'S2A_MSIL2A_20220126...\n",
       "  * x                                        (x) float64 5e+05 ... 6.098e+05\n",
       "  * y                                        (y) float64 1.02e+07 ... 9.89e+06\n",
       "    s2:unclassified_percentage               (time) float64 4.19 4.19 ... 0.3259\n",
       "    s2:not_vegetated_percentage              (time) float64 2.283 ... 0.321\n",
       "    ...                                       ...\n",
       "    gsd                                      float64 10.0\n",
       "    proj:shape                               object {10980}\n",
       "    common_name                              (band) <U5 'blue' 'green'\n",
       "    center_wavelength                        (band) float64 0.49 0.56\n",
       "    full_width_half_max                      (band) float64 0.098 0.045\n",
       "    epsg                                     int64 32732\n",
       "Dimensions without coordinates: band\n",
       "Data variables:\n",
       "    B02                                      (time, y, x) float64 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>\n",
       "    B03                                      (time, y, x) float64 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>\n",
       "Attributes:\n",
       "    spec:           RasterSpec(epsg=32732, bounds=(499979.99999708973, 989020...\n",
       "    crs:            epsg:32732\n",
       "    transform:      | 10.00, 0.00, 499980.00|\\n| 0.00,-10.00, 10200040.00|\\n|...\n",
       "    resolution_xy:  (9.999999999941792, 10.0)
" ], "text/plain": [ "\n", "Dimensions: (time: 100, y: 30984, x: 10981,\n", " band: 2)\n", "Coordinates: (12/43)\n", " * time (time) datetime64[ns] 2022-01-26...\n", " id (time) \n", " B03 (time, y, x) float64 dask.array\n", "Attributes:\n", " spec: RasterSpec(epsg=32732, bounds=(499979.99999708973, 989020...\n", " crs: epsg:32732\n", " transform: | 10.00, 0.00, 499980.00|\\n| 0.00,-10.00, 10200040.00|\\n|...\n", " resolution_xy: (9.999999999941792, 10.0)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ic = (\n", " catalog.search(\n", " collections=[\"sentinel-2-l2a\"],\n", " bbox=[9.4, 0, 9.5, 1]\n", " )\n", ")\n", "\n", "ds = ic.to_xarray(assets=[\"B02\", \"B03\"], epsg=32732)\n", "ds" ] }, { "cell_type": "markdown", "id": "7f88780c-5fe0-4874-a1ce-83e9ea951e65", "metadata": {}, "source": [ "## Search -> xarray" ] }, { "cell_type": "code", "execution_count": 8, "id": "ac96a76b-a4d9-4b01-a378-6b26fd94d256", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:                                  (time: 100, y: 30984, x: 10981,\n",
       "                                              band: 2)\n",
       "Coordinates: (12/43)\n",
       "  * time                                     (time) datetime64[ns] 2022-01-26...\n",
       "    id                                       (time) <U54 'S2A_MSIL2A_20220126...\n",
       "  * x                                        (x) float64 5e+05 ... 6.098e+05\n",
       "  * y                                        (y) float64 1.02e+07 ... 9.89e+06\n",
       "    s2:unclassified_percentage               (time) float64 4.19 4.19 ... 0.3259\n",
       "    s2:not_vegetated_percentage              (time) float64 2.283 ... 0.321\n",
       "    ...                                       ...\n",
       "    gsd                                      float64 10.0\n",
       "    proj:shape                               object {10980}\n",
       "    common_name                              (band) <U5 'blue' 'green'\n",
       "    center_wavelength                        (band) float64 0.49 0.56\n",
       "    full_width_half_max                      (band) float64 0.098 0.045\n",
       "    epsg                                     int64 32732\n",
       "Dimensions without coordinates: band\n",
       "Data variables:\n",
       "    B02                                      (time, y, x) float64 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>\n",
       "    B03                                      (time, y, x) float64 dask.array<chunksize=(1, 1024, 1024), meta=np.ndarray>\n",
       "Attributes:\n",
       "    spec:           RasterSpec(epsg=32732, bounds=(499979.99999708973, 989020...\n",
       "    crs:            epsg:32732\n",
       "    transform:      | 10.00, 0.00, 499980.00|\\n| 0.00,-10.00, 10200040.00|\\n|...\n",
       "    resolution_xy:  (9.999999999941792, 10.0)
" ], "text/plain": [ "\n", "Dimensions: (time: 100, y: 30984, x: 10981,\n", " band: 2)\n", "Coordinates: (12/43)\n", " * time (time) datetime64[ns] 2022-01-26...\n", " id (time) \n", " B03 (time, y, x) float64 dask.array\n", "Attributes:\n", " spec: RasterSpec(epsg=32732, bounds=(499979.99999708973, 989020...\n", " crs: epsg:32732\n", " transform: | 10.00, 0.00, 499980.00|\\n| 0.00,-10.00, 10200040.00|\\n|...\n", " resolution_xy: (9.999999999941792, 10.0)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds = (\n", " catalog.search(\n", " collections=[\"sentinel-2-l2a\"],\n", " bbox=[9.4, 0, 9.5, 1]\n", " ).to_xarray(assets=[\"B02\", \"B03\"], epsg=32732)\n", ")\n", "ds" ] }, { "cell_type": "markdown", "id": "0e673fc4-61cb-4c7f-b750-3ee0dddfdf9b", "metadata": {}, "source": [ "## Search / ItemCollection -> geopandas" ] }, { "cell_type": "code", "execution_count": 9, "id": "97dba696-24bf-410d-b7f8-d7d8d05742b6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
typestac_versionidgeometrylinksbboxstac_extensionscollectiondatetimeplatform...assets.datastrip-metadata.rolesassets.tilejson.hrefassets.tilejson.typeassets.tilejson.titleassets.tilejson.rolesassets.rendered_preview.hrefassets.rendered_preview.typeassets.rendered_preview.titleassets.rendered_preview.relassets.rendered_preview.roles
0Feature1.0.0S2B_MSIL2A_20220710T093039_R136_T32NNG_2022071...POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982018, 0.81630719, 9.98700503, 1.80981859][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-07-10T09:30:39.024000ZSentinel-2B...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
1Feature1.0.0S2B_MSIL2A_20220710T093039_R136_T32NNF_2022071...POLYGON ((8.99982 0.90491, 9.98664 0.90478, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982024, -0.08848273, 9.98663827, 0.90491156][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-07-10T09:30:39.024000ZSentinel-2B...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
2Feature1.0.0S2B_MSIL2A_20220710T093039_R136_T32MNE_2022071...POLYGON ((8.99982 0.00000, 9.98652 0.00000, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982024, -0.99339404, 9.98666334, 0.0][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-07-10T09:30:39.024000ZSentinel-2B...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
3Feature1.0.0S2A_MSIL2A_20220705T093051_R136_T32NNG_2022070...POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982018, 0.81630719, 9.98700503, 1.80981859][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-07-05T09:30:51.025000ZSentinel-2A...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
4Feature1.0.0S2A_MSIL2A_20220705T093051_R136_T32NNF_2022070...POLYGON ((8.99982 0.90491, 9.98664 0.90478, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982024, -0.08848273, 9.98663827, 0.90491156][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-07-05T09:30:51.025000ZSentinel-2A...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
..................................................................
95Feature1.0.0S2B_MSIL2A_20220131T093119_R136_T32NNG_2022021...POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982018, 0.81630719, 9.98700503, 1.80981859][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-01-31T09:31:19.024000ZSentinel-2B...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
96Feature1.0.0S2B_MSIL2A_20220131T093119_R136_T32NNF_2022021...POLYGON ((8.99982 0.90491, 9.98664 0.90478, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982024, -0.08848273, 9.98663827, 0.90491156][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-01-31T09:31:19.024000ZSentinel-2B...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
97Feature1.0.0S2B_MSIL2A_20220131T093119_R136_T32MNE_2022021...POLYGON ((8.99982 0.00000, 9.98652 0.00000, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982024, -0.99339404, 9.98666334, 0.0][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-01-31T09:31:19.024000ZSentinel-2B...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
98Feature1.0.0S2A_MSIL2A_20220126T093251_R136_T32NNG_2022022...POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9....[{'rel': 'collection', 'href': 'https://planet...[8.99982018, 0.81630719, 9.98700503, 1.80981859][https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-01-26T09:32:51.024000ZSentinel-2A...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
99Feature1.0.0S2A_MSIL2A_20220126T093251_R136_T32NNG_2022021...POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9....[{'rel': 'collection', 'href': 'https://planet...NaN[https://stac-extensions.github.io/eo/v1.0.0/s...sentinel-2-l2a2022-01-26T09:32:51.024000ZSentinel-2A...[metadata]https://planetarycomputer.microsoft.com/api/da...application/jsonTileJSON with default rendering[tiles]https://planetarycomputer.microsoft.com/api/da...image/pngRendered previewpreview[overview]
\n", "

100 rows × 215 columns

\n", "
" ], "text/plain": [ " type stac_version id \\\n", "0 Feature 1.0.0 S2B_MSIL2A_20220710T093039_R136_T32NNG_2022071... \n", "1 Feature 1.0.0 S2B_MSIL2A_20220710T093039_R136_T32NNF_2022071... \n", "2 Feature 1.0.0 S2B_MSIL2A_20220710T093039_R136_T32MNE_2022071... \n", "3 Feature 1.0.0 S2A_MSIL2A_20220705T093051_R136_T32NNG_2022070... \n", "4 Feature 1.0.0 S2A_MSIL2A_20220705T093051_R136_T32NNF_2022070... \n", ".. ... ... ... \n", "95 Feature 1.0.0 S2B_MSIL2A_20220131T093119_R136_T32NNG_2022021... \n", "96 Feature 1.0.0 S2B_MSIL2A_20220131T093119_R136_T32NNF_2022021... \n", "97 Feature 1.0.0 S2B_MSIL2A_20220131T093119_R136_T32MNE_2022021... \n", "98 Feature 1.0.0 S2A_MSIL2A_20220126T093251_R136_T32NNG_2022022... \n", "99 Feature 1.0.0 S2A_MSIL2A_20220126T093251_R136_T32NNG_2022021... \n", "\n", " geometry \\\n", "0 POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9.... \n", "1 POLYGON ((8.99982 0.90491, 9.98664 0.90478, 9.... \n", "2 POLYGON ((8.99982 0.00000, 9.98652 0.00000, 9.... \n", "3 POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9.... \n", "4 POLYGON ((8.99982 0.90491, 9.98664 0.90478, 9.... \n", ".. ... \n", "95 POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9.... \n", "96 POLYGON ((8.99982 0.90491, 9.98664 0.90478, 9.... \n", "97 POLYGON ((8.99982 0.00000, 9.98652 0.00000, 9.... \n", "98 POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9.... \n", "99 POLYGON ((8.99982 1.80982, 9.98700 1.80955, 9.... \n", "\n", " links \\\n", "0 [{'rel': 'collection', 'href': 'https://planet... \n", "1 [{'rel': 'collection', 'href': 'https://planet... \n", "2 [{'rel': 'collection', 'href': 'https://planet... \n", "3 [{'rel': 'collection', 'href': 'https://planet... \n", "4 [{'rel': 'collection', 'href': 'https://planet... \n", ".. ... \n", "95 [{'rel': 'collection', 'href': 'https://planet... \n", "96 [{'rel': 'collection', 'href': 'https://planet... \n", "97 [{'rel': 'collection', 'href': 'https://planet... \n", "98 [{'rel': 'collection', 'href': 'https://planet... \n", "99 [{'rel': 'collection', 'href': 'https://planet... \n", "\n", " bbox \\\n", "0 [8.99982018, 0.81630719, 9.98700503, 1.80981859] \n", "1 [8.99982024, -0.08848273, 9.98663827, 0.90491156] \n", "2 [8.99982024, -0.99339404, 9.98666334, 0.0] \n", "3 [8.99982018, 0.81630719, 9.98700503, 1.80981859] \n", "4 [8.99982024, -0.08848273, 9.98663827, 0.90491156] \n", ".. ... \n", "95 [8.99982018, 0.81630719, 9.98700503, 1.80981859] \n", "96 [8.99982024, -0.08848273, 9.98663827, 0.90491156] \n", "97 [8.99982024, -0.99339404, 9.98666334, 0.0] \n", "98 [8.99982018, 0.81630719, 9.98700503, 1.80981859] \n", "99 NaN \n", "\n", " stac_extensions collection \\\n", "0 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "1 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "2 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "3 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "4 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", ".. ... ... \n", "95 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "96 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "97 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "98 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "99 [https://stac-extensions.github.io/eo/v1.0.0/s... sentinel-2-l2a \n", "\n", " datetime platform ... \\\n", "0 2022-07-10T09:30:39.024000Z Sentinel-2B ... \n", "1 2022-07-10T09:30:39.024000Z Sentinel-2B ... \n", "2 2022-07-10T09:30:39.024000Z Sentinel-2B ... \n", "3 2022-07-05T09:30:51.025000Z Sentinel-2A ... \n", "4 2022-07-05T09:30:51.025000Z Sentinel-2A ... \n", ".. ... ... ... \n", "95 2022-01-31T09:31:19.024000Z Sentinel-2B ... \n", "96 2022-01-31T09:31:19.024000Z Sentinel-2B ... \n", "97 2022-01-31T09:31:19.024000Z Sentinel-2B ... \n", "98 2022-01-26T09:32:51.024000Z Sentinel-2A ... \n", "99 2022-01-26T09:32:51.024000Z Sentinel-2A ... \n", "\n", " assets.datastrip-metadata.roles \\\n", "0 [metadata] \n", "1 [metadata] \n", "2 [metadata] \n", "3 [metadata] \n", "4 [metadata] \n", ".. ... \n", "95 [metadata] \n", "96 [metadata] \n", "97 [metadata] \n", "98 [metadata] \n", "99 [metadata] \n", "\n", " assets.tilejson.href assets.tilejson.type \\\n", "0 https://planetarycomputer.microsoft.com/api/da... application/json \n", "1 https://planetarycomputer.microsoft.com/api/da... application/json \n", "2 https://planetarycomputer.microsoft.com/api/da... application/json \n", "3 https://planetarycomputer.microsoft.com/api/da... application/json \n", "4 https://planetarycomputer.microsoft.com/api/da... application/json \n", ".. ... ... \n", "95 https://planetarycomputer.microsoft.com/api/da... application/json \n", "96 https://planetarycomputer.microsoft.com/api/da... application/json \n", "97 https://planetarycomputer.microsoft.com/api/da... application/json \n", "98 https://planetarycomputer.microsoft.com/api/da... application/json \n", "99 https://planetarycomputer.microsoft.com/api/da... application/json \n", "\n", " assets.tilejson.title assets.tilejson.roles \\\n", "0 TileJSON with default rendering [tiles] \n", "1 TileJSON with default rendering [tiles] \n", "2 TileJSON with default rendering [tiles] \n", "3 TileJSON with default rendering [tiles] \n", "4 TileJSON with default rendering [tiles] \n", ".. ... ... \n", "95 TileJSON with default rendering [tiles] \n", "96 TileJSON with default rendering [tiles] \n", "97 TileJSON with default rendering [tiles] \n", "98 TileJSON with default rendering [tiles] \n", "99 TileJSON with default rendering [tiles] \n", "\n", " assets.rendered_preview.href \\\n", "0 https://planetarycomputer.microsoft.com/api/da... \n", "1 https://planetarycomputer.microsoft.com/api/da... \n", "2 https://planetarycomputer.microsoft.com/api/da... \n", "3 https://planetarycomputer.microsoft.com/api/da... \n", "4 https://planetarycomputer.microsoft.com/api/da... \n", ".. ... \n", "95 https://planetarycomputer.microsoft.com/api/da... \n", "96 https://planetarycomputer.microsoft.com/api/da... \n", "97 https://planetarycomputer.microsoft.com/api/da... \n", "98 https://planetarycomputer.microsoft.com/api/da... \n", "99 https://planetarycomputer.microsoft.com/api/da... \n", "\n", " assets.rendered_preview.type assets.rendered_preview.title \\\n", "0 image/png Rendered preview \n", "1 image/png Rendered preview \n", "2 image/png Rendered preview \n", "3 image/png Rendered preview \n", "4 image/png Rendered preview \n", ".. ... ... \n", "95 image/png Rendered preview \n", "96 image/png Rendered preview \n", "97 image/png Rendered preview \n", "98 image/png Rendered preview \n", "99 image/png Rendered preview \n", "\n", " assets.rendered_preview.rel assets.rendered_preview.roles \n", "0 preview [overview] \n", "1 preview [overview] \n", "2 preview [overview] \n", "3 preview [overview] \n", "4 preview [overview] \n", ".. ... ... \n", "95 preview [overview] \n", "96 preview [overview] \n", "97 preview [overview] \n", "98 preview [overview] \n", "99 preview [overview] \n", "\n", "[100 rows x 215 columns]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = catalog.search(collections=[\"sentinel-2-l2a\"], bbox=[9.4, 0, 9.5, 1]).to_geopandas()\n", "df" ] }, { "cell_type": "code", "execution_count": null, "id": "b4a5aa76-df04-4d0c-9034-f0d84e89bffb", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.13" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "03f55cfe30964add91b58bb624b69aa1": { "model_module": "@jupyter-widgets/controls", "model_module_version": "1.5.0", "model_name": "VBoxModel", "state": { "layout": "IPY_MODEL_f7f4e6f6c4c9493c93223388eeabf41c" } }, "f7f4e6f6c4c9493c93223388eeabf41c": { "model_module": "@jupyter-widgets/base", "model_module_version": "1.2.0", "model_name": "LayoutModel", "state": {} } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }