|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Quickstart\n", |
| 8 | + "\n", |
| 9 | + "This notebook shows how to use PySTAC to read through the public Sentinel catalog, and grab information for a single band's file." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "First, we want to hook into PySTAC to allow for reading of HTTP STAC items, as described in [the STAC_IO Concepts docs](concepts.html#using-stac-io). \n", |
| 17 | + "\n", |
| 18 | + "__Note:__ this requires the [requests](https://requests.kennethreitz.org/en/master) library be installed." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "from urllib.parse import urlparse\n", |
| 28 | + "import requests\n", |
| 29 | + "from pystac import STAC_IO\n", |
| 30 | + "\n", |
| 31 | + "def requests_read_method(uri):\n", |
| 32 | + " parsed = urlparse(uri)\n", |
| 33 | + " if parsed.scheme.startswith('http'):\n", |
| 34 | + " return requests.get(uri).text\n", |
| 35 | + " else:\n", |
| 36 | + " return STAC_IO.default_read_text_method(uri)\n", |
| 37 | + "\n", |
| 38 | + "STAC_IO.read_text_method = requests_read_method" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "metadata": {}, |
| 44 | + "source": [ |
| 45 | + "We can then read the STAC catalog located at the publicly available endpoint hosted by AWS:" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": 3, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "from pystac import Catalog\n", |
| 55 | + "\n", |
| 56 | + "cat = Catalog.from_file('https://sentinel-stac.s3.amazonaws.com/catalog.json')" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "markdown", |
| 61 | + "metadata": {}, |
| 62 | + "source": [ |
| 63 | + "There are a lot of items in this catalog; crawling through it all would take a significant amount of time. Here, we lean on the fact that [link resolution is lazy](concepts.html#lazy-resolution-of-stac-objects) and get to a catalog that contains items:" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": 4, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [ |
| 71 | + { |
| 72 | + "name": "stdout", |
| 73 | + "output_type": "stream", |
| 74 | + "text": [ |
| 75 | + "Crawling through <Catalog id=sentinel-stac>\n", |
| 76 | + "Crawling through <Collection id=sentinel-2-l1c>\n", |
| 77 | + "Crawling through <Catalog id=9>\n", |
| 78 | + "Crawling through <Catalog id=V>\n" |
| 79 | + ] |
| 80 | + } |
| 81 | + ], |
| 82 | + "source": [ |
| 83 | + "while len(cat.get_item_links()) == 0:\n", |
| 84 | + " print('Crawling through {}'.format(cat))\n", |
| 85 | + " cat = next(cat.get_children())" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "We can print some information about the catalog, including how many children it has:" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 8, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [ |
| 100 | + { |
| 101 | + "name": "stdout", |
| 102 | + "output_type": "stream", |
| 103 | + "text": [ |
| 104 | + "XK catalog\n", |
| 105 | + "Contains 388 items.\n" |
| 106 | + ] |
| 107 | + } |
| 108 | + ], |
| 109 | + "source": [ |
| 110 | + "print(cat.description)\n", |
| 111 | + "print('Contains {} items.'.format(len(cat.get_item_links())))" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "markdown", |
| 116 | + "metadata": {}, |
| 117 | + "source": [ |
| 118 | + "Let's grab the first item, check out it's cloud cover, and start exploring the assets." |
| 119 | + ] |
| 120 | + }, |
| 121 | + { |
| 122 | + "cell_type": "code", |
| 123 | + "execution_count": 9, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "item = next(cat.get_items())" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": 10, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [ |
| 135 | + { |
| 136 | + "data": { |
| 137 | + "text/plain": [ |
| 138 | + "41.52" |
| 139 | + ] |
| 140 | + }, |
| 141 | + "execution_count": 10, |
| 142 | + "metadata": {}, |
| 143 | + "output_type": "execute_result" |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "item.cloud_cover" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": 11, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [ |
| 155 | + { |
| 156 | + "name": "stdout", |
| 157 | + "output_type": "stream", |
| 158 | + "text": [ |
| 159 | + "thumbnail: https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/13/0/preview.jpg (None)\n", |
| 160 | + "info: https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/13/0/tileInfo.json (None)\n", |
| 161 | + "metadata: https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/13/0/metadata.xml (None)\n", |
| 162 | + "tki: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/TKI.jp2 (image/jp2)\n", |
| 163 | + "B01: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B01.jp2 (image/jp2)\n", |
| 164 | + "B02: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B02.jp2 (image/jp2)\n", |
| 165 | + "B03: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B03.jp2 (image/jp2)\n", |
| 166 | + "B04: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B04.jp2 (image/jp2)\n", |
| 167 | + "B05: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B05.jp2 (image/jp2)\n", |
| 168 | + "B06: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B06.jp2 (image/jp2)\n", |
| 169 | + "B07: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B07.jp2 (image/jp2)\n", |
| 170 | + "B08: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B08.jp2 (image/jp2)\n", |
| 171 | + "B8A: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B08.jp2 (image/jp2)\n", |
| 172 | + "B09: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B09.jp2 (image/jp2)\n", |
| 173 | + "B10: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B10.jp2 (image/jp2)\n", |
| 174 | + "B11: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B11.jp2 (image/jp2)\n", |
| 175 | + "B12: https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B11.jp2 (image/jp2)\n" |
| 176 | + ] |
| 177 | + } |
| 178 | + ], |
| 179 | + "source": [ |
| 180 | + "for asset_key in item.assets:\n", |
| 181 | + " asset = item.assets[asset_key]\n", |
| 182 | + " print('{}: {} ({})'.format(asset_key, asset.href, asset.media_type))" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "markdown", |
| 187 | + "metadata": {}, |
| 188 | + "source": [ |
| 189 | + "We can use the `to_dict()` method to convert an Asset, or any PySTAC object, into a dictionary:" |
| 190 | + ] |
| 191 | + }, |
| 192 | + { |
| 193 | + "cell_type": "code", |
| 194 | + "execution_count": 12, |
| 195 | + "metadata": {}, |
| 196 | + "outputs": [ |
| 197 | + { |
| 198 | + "data": { |
| 199 | + "text/plain": [ |
| 200 | + "{'href': 'https://sentinel-s2-l1c.s3.amazonaws.com/tiles/9/V/XK/2017/10/13/0/B03.jp2',\n", |
| 201 | + " 'type': 'image/jp2',\n", |
| 202 | + " 'title': 'Band 3 (green)',\n", |
| 203 | + " 'eo:bands': [2]}" |
| 204 | + ] |
| 205 | + }, |
| 206 | + "execution_count": 12, |
| 207 | + "metadata": {}, |
| 208 | + "output_type": "execute_result" |
| 209 | + } |
| 210 | + ], |
| 211 | + "source": [ |
| 212 | + "asset = item.assets['B03']\n", |
| 213 | + "asset.to_dict()" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "markdown", |
| 218 | + "metadata": {}, |
| 219 | + "source": [ |
| 220 | + "Here the asset uses the band information associated with it's item:" |
| 221 | + ] |
| 222 | + }, |
| 223 | + { |
| 224 | + "cell_type": "code", |
| 225 | + "execution_count": 13, |
| 226 | + "metadata": {}, |
| 227 | + "outputs": [ |
| 228 | + { |
| 229 | + "data": { |
| 230 | + "text/plain": [ |
| 231 | + "{'name': 'B03',\n", |
| 232 | + " 'common_name': 'green',\n", |
| 233 | + " 'gsd': 10.0,\n", |
| 234 | + " 'center_wavelength': 0.56,\n", |
| 235 | + " 'full_width_half_max': 0.045}" |
| 236 | + ] |
| 237 | + }, |
| 238 | + "execution_count": 13, |
| 239 | + "metadata": {}, |
| 240 | + "output_type": "execute_result" |
| 241 | + } |
| 242 | + ], |
| 243 | + "source": [ |
| 244 | + "asset.get_bands()[0].to_dict()" |
| 245 | + ] |
| 246 | + } |
| 247 | + ], |
| 248 | + "metadata": { |
| 249 | + "kernelspec": { |
| 250 | + "display_name": "Python [default]", |
| 251 | + "language": "python", |
| 252 | + "name": "python3" |
| 253 | + }, |
| 254 | + "language_info": { |
| 255 | + "codemirror_mode": { |
| 256 | + "name": "ipython", |
| 257 | + "version": 3 |
| 258 | + }, |
| 259 | + "file_extension": ".py", |
| 260 | + "mimetype": "text/x-python", |
| 261 | + "name": "python", |
| 262 | + "nbconvert_exporter": "python", |
| 263 | + "pygments_lexer": "ipython3", |
| 264 | + "version": "3.6.5" |
| 265 | + } |
| 266 | + }, |
| 267 | + "nbformat": 4, |
| 268 | + "nbformat_minor": 2 |
| 269 | +} |
0 commit comments