|
6 | 6 | "source": [
|
7 | 7 | "# Quickstart\n",
|
8 | 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." |
| 9 | + "This notebook shows how to use PySTAC to read through the public Sentinel catalog and write a local version." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "### Reading STAC" |
10 | 17 | ]
|
11 | 18 | },
|
12 | 19 | {
|
|
243 | 250 | "source": [
|
244 | 251 | "asset.get_bands()[0].to_dict()"
|
245 | 252 | ]
|
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "markdown", |
| 256 | + "metadata": {}, |
| 257 | + "source": [ |
| 258 | + "### Writing a STAC\n", |
| 259 | + "\n", |
| 260 | + "Let's walk the catalog again, but this time create local clones of the STAC object, so we can end up with a copy that we can save off to the local file system." |
| 261 | + ] |
| 262 | + }, |
| 263 | + { |
| 264 | + "cell_type": "code", |
| 265 | + "execution_count": 52, |
| 266 | + "metadata": {}, |
| 267 | + "outputs": [ |
| 268 | + { |
| 269 | + "name": "stdout", |
| 270 | + "output_type": "stream", |
| 271 | + "text": [ |
| 272 | + "Crawling through <Catalog id=sentinel-stac>\n", |
| 273 | + "Crawling through <Collection id=sentinel-2-l1c>\n", |
| 274 | + "Crawling through <Catalog id=9>\n", |
| 275 | + "Crawling through <Catalog id=V>\n" |
| 276 | + ] |
| 277 | + } |
| 278 | + ], |
| 279 | + "source": [ |
| 280 | + "import itertools\n", |
| 281 | + "\n", |
| 282 | + "cat = Catalog.from_file('https://sentinel-stac.s3.amazonaws.com/catalog.json')\n", |
| 283 | + "\n", |
| 284 | + "# Setup the root of our local STAC\n", |
| 285 | + "local_root = cat.clone()\n", |
| 286 | + "local_root.clear_children()\n", |
| 287 | + "\n", |
| 288 | + "# Loop over catalogs and clone\n", |
| 289 | + "curr_local_cat = local_root\n", |
| 290 | + "while len(cat.get_item_links()) == 0:\n", |
| 291 | + " print('Crawling through {}'.format(cat))\n", |
| 292 | + " cat = next(cat.get_children())\n", |
| 293 | + " local_cat = cat.clone()\n", |
| 294 | + " local_cat.clear_children()\n", |
| 295 | + " curr_local_cat.add_child(local_cat)\n", |
| 296 | + " curr_local_cat = local_cat\n", |
| 297 | + " \n", |
| 298 | + "# Clear the items from the last local catalog\n", |
| 299 | + "curr_local_cat.clear_children()\n", |
| 300 | + "curr_local_cat.clear_items()\n", |
| 301 | + "\n", |
| 302 | + "# Take the first 5 items\n", |
| 303 | + "items = itertools.islice(cat.get_items(), 5)\n", |
| 304 | + "\n", |
| 305 | + "# Clone and add them to our local catalog\n", |
| 306 | + "curr_local_cat.add_items([i.clone() for i in items])" |
| 307 | + ] |
| 308 | + }, |
| 309 | + { |
| 310 | + "cell_type": "markdown", |
| 311 | + "metadata": {}, |
| 312 | + "source": [ |
| 313 | + "Now that we have a smaller STAC, let's map over the items to reduce it even further by only including the thumbnail assets in our items:" |
| 314 | + ] |
| 315 | + }, |
| 316 | + { |
| 317 | + "cell_type": "code", |
| 318 | + "execution_count": 53, |
| 319 | + "metadata": {}, |
| 320 | + "outputs": [], |
| 321 | + "source": [ |
| 322 | + "def item_mapper(item):\n", |
| 323 | + " thumbnail_asset = item.assets['thumbnail']\n", |
| 324 | + " \n", |
| 325 | + " #\n", |
| 326 | + " new_assets = { 'thumbnail': item.assets['thumbnail'] }\n", |
| 327 | + " item.assets = new_assets\n", |
| 328 | + " return item\n", |
| 329 | + "\n", |
| 330 | + "local_root_2 = local_root.map_items(item_mapper)\n", |
| 331 | + " " |
| 332 | + ] |
| 333 | + }, |
| 334 | + { |
| 335 | + "cell_type": "markdown", |
| 336 | + "metadata": {}, |
| 337 | + "source": [ |
| 338 | + "We can now normalize our catalog and save it somewhere local:" |
| 339 | + ] |
| 340 | + }, |
| 341 | + { |
| 342 | + "cell_type": "code", |
| 343 | + "execution_count": 61, |
| 344 | + "metadata": {}, |
| 345 | + "outputs": [], |
| 346 | + "source": [ |
| 347 | + "!mkdir -p ./quickstart_stac" |
| 348 | + ] |
| 349 | + }, |
| 350 | + { |
| 351 | + "cell_type": "code", |
| 352 | + "execution_count": 55, |
| 353 | + "metadata": {}, |
| 354 | + "outputs": [ |
| 355 | + { |
| 356 | + "data": { |
| 357 | + "text/plain": [ |
| 358 | + "<Catalog id=sentinel-stac>" |
| 359 | + ] |
| 360 | + }, |
| 361 | + "execution_count": 55, |
| 362 | + "metadata": {}, |
| 363 | + "output_type": "execute_result" |
| 364 | + } |
| 365 | + ], |
| 366 | + "source": [ |
| 367 | + "local_root_2.normalize_hrefs('./quickstart_stac')" |
| 368 | + ] |
| 369 | + }, |
| 370 | + { |
| 371 | + "cell_type": "code", |
| 372 | + "execution_count": 56, |
| 373 | + "metadata": {}, |
| 374 | + "outputs": [], |
| 375 | + "source": [ |
| 376 | + "from pystac import CatalogType\n", |
| 377 | + "\n", |
| 378 | + "local_root_2.save(catalog_type=CatalogType.SELF_CONTAINED)" |
| 379 | + ] |
| 380 | + }, |
| 381 | + { |
| 382 | + "cell_type": "code", |
| 383 | + "execution_count": 60, |
| 384 | + "metadata": {}, |
| 385 | + "outputs": [ |
| 386 | + { |
| 387 | + "name": "stdout", |
| 388 | + "output_type": "stream", |
| 389 | + "text": [ |
| 390 | + "* <Catalog id=sentinel-stac>\n", |
| 391 | + " * <Collection id=sentinel-2-l1c>\n", |
| 392 | + " * <Catalog id=9>\n", |
| 393 | + " * <Catalog id=V>\n", |
| 394 | + " * <Catalog id=XK>\n", |
| 395 | + " * <EOItem id=S2B_9VXK_20171013_0>\n", |
| 396 | + " * <EOItem id=S2A_9VXK_20171015_0>\n", |
| 397 | + " * <EOItem id=S2B_9VXK_20171016_0>\n", |
| 398 | + " * <EOItem id=S2B_9VXK_20171017_0>\n", |
| 399 | + " * <EOItem id=S2A_9VXK_20171002_0>\n" |
| 400 | + ] |
| 401 | + } |
| 402 | + ], |
| 403 | + "source": [ |
| 404 | + "local_root_2.describe()" |
| 405 | + ] |
| 406 | + }, |
| 407 | + { |
| 408 | + "cell_type": "code", |
| 409 | + "execution_count": 64, |
| 410 | + "metadata": {}, |
| 411 | + "outputs": [ |
| 412 | + { |
| 413 | + "name": "stdout", |
| 414 | + "output_type": "stream", |
| 415 | + "text": [ |
| 416 | + "Item {}:\n", |
| 417 | + " Assets: {'thumbnail': <Asset href=https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/13/0/preview.jpg>}\n", |
| 418 | + "Item {}:\n", |
| 419 | + " Assets: {'thumbnail': <Asset href=https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/15/0/preview.jpg>}\n", |
| 420 | + "Item {}:\n", |
| 421 | + " Assets: {'thumbnail': <Asset href=https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/16/0/preview.jpg>}\n", |
| 422 | + "Item {}:\n", |
| 423 | + " Assets: {'thumbnail': <Asset href=https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/17/0/preview.jpg>}\n", |
| 424 | + "Item {}:\n", |
| 425 | + " Assets: {'thumbnail': <Asset href=https://roda.sentinel-hub.com/sentinel-s2-l1c/tiles/9/V/XK/2017/10/2/0/preview.jpg>}\n" |
| 426 | + ] |
| 427 | + } |
| 428 | + ], |
| 429 | + "source": [ |
| 430 | + "for item in local_root_2.get_all_items():\n", |
| 431 | + " print('Item {}:')\n", |
| 432 | + " print(' Assets: {}'.format(item.assets))" |
| 433 | + ] |
246 | 434 | }
|
247 | 435 | ],
|
248 | 436 | "metadata": {
|
|
0 commit comments