Created
July 29, 2023 18:42
-
-
Save calebrob6/416beef8c638ea2ed77518587db42676 to your computer and use it in GitHub Desktop.
Make a GIF of Sentinel 2 scenes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"id": "f2a25d4c-7882-4256-998a-02ad02cd8b20", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pystac_client\n", | |
"from tqdm import tqdm\n", | |
"import numpy as np\n", | |
"import rasterio\n", | |
"import rasterio.mask\n", | |
"import fiona.transform\n", | |
"import imageio.v2 as imageio\n", | |
"import shapely.geometry\n", | |
"import planetary_computer\n", | |
"import skimage.transform" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "975cc213-175d-4416-b476-1713dc17dc2a", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"catalog = pystac_client.Client.open(\n", | |
" \"https://planetarycomputer.microsoft.com/api/stac/v1\",\n", | |
" modifier=planetary_computer.sign_inplace,\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "c7970cd3-719b-4aa1-bdfd-9524720f3b98", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"aoi_geom = {\"coordinates\":[[[-149.70533617499035,60.0157150834068],[-149.70533617499035,59.905899414174],[-149.47542900015293,59.905899414174],[-149.47542900015293,60.0157150834068],[-149.70533617499035,60.0157150834068]]],\"type\":\"Polygon\"}\n", | |
"aoi_shape = shapely.geometry.shape(aoi_geom)\n", | |
"date_range = \"2015-07-01/2023-07-01\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "8b247616-3782-45c2-9c73-60cf477303f9", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Returned 248 Items\n" | |
] | |
} | |
], | |
"source": [ | |
"search = catalog.search(\n", | |
" collections=[\"sentinel-2-l2a\"],\n", | |
" intersects=aoi_geom,\n", | |
" datetime=date_range,\n", | |
" query={\"eo:cloud_cover\": {\"lt\": 10}},\n", | |
")\n", | |
"\n", | |
"# Check how many items were returned\n", | |
"items = search.item_collection()\n", | |
"print(f\"Returned {len(items)} Items\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"id": "d4e0dd21-b844-4a52-9d43-7479e6923e76", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100%|██████████| 248/248 [01:01<00:00, 4.05it/s]\n" | |
] | |
} | |
], | |
"source": [ | |
"images = []\n", | |
"for item in tqdm(items):\n", | |
" shape = shapely.geometry.shape(item.geometry)\n", | |
" url = item.assets[\"visual\"].href\n", | |
" date = item.properties[\"datetime\"][:10]\n", | |
" crs = f\"epsg:{item.properties['proj:epsg']}\"\n", | |
" \n", | |
" geom = fiona.transform.transform_geom(\"epsg:4326\", crs, aoi_geom)\n", | |
" geom = shapely.geometry.mapping(shapely.geometry.shape(geom).envelope)\n", | |
" \n", | |
" if not shape.contains(aoi_shape) or crs != \"epsg:32605\":\n", | |
" continue\n", | |
" \n", | |
" with rasterio.open(url) as f:\n", | |
" image, _ = rasterio.mask.mask(f, [geom], crop=True)\n", | |
" image = image.transpose(1,2,0)\n", | |
" image = skimage.transform.resize(image, (600, 600), mode='reflect')\n", | |
" images.append(image)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"id": "480465e2-69d8-4ddc-9cb1-60d1c0ea908e", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'epsg:32605'" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"crs" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"id": "02282d6b-c573-4157-a57f-eab1920d3115", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"images = np.array(images)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"id": "6afaa75b-3d11-488c-9a4f-846095f23695", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(127, 600, 600, 3)" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"images.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"id": "39d32f8c-c413-4ccd-935c-168f1a97289a", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [], | |
"source": [ | |
"images = (images * 255).astype(np.uint8)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"id": "18eea478-3e0a-4ee3-87c6-7e05360240a2", | |
"metadata": { | |
"tags": [] | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16, resizing from (600, 600) to (608, 608) to ensure video compatibility with most codecs and players. To prevent resizing, make your input image divisible by the macro_block_size or set the macro_block_size to 1 (risking incompatibility).\n", | |
"[swscaler @ 0x5f55300] Warning: data is not aligned! This can lead to a speed loss\n" | |
] | |
} | |
], | |
"source": [ | |
"imageio.mimwrite(\n", | |
" \"bear_glacier.mp4\",\n", | |
" images,\n", | |
" fps=5,\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1efa5754-413f-4c5c-a0fc-5103433a2916", | |
"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.11.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment