{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This article describes how to read in image scenes from the EarthAI Catalog into Numpy arrays using [Rasterio](https://rasterio.readthedocs.io/en/latest/).\n", "\n", "In a [previous article](https://docs.astraea.earth/hc/en-us/articles/360043451772-Query-Imagery-Data-using-the-EarthAI-Catalog-API), we discussed how to query imagery data using the EarthAI Catalog API and in [another article](https://astraeahelp.zendesk.com/knowledge/articles/360051440351/en-us?brand_id=360003221551), we discussed how to read that catalog of imagery scenes into a RasterFrame using `spark.read.raster`. Now we will show another way to read image scenes using Rasterio.\n", "\n", "# Import Library\n", "\n", "We import the EarthAI library as a first step." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from earthai.init import *\n", "import rasterio\n", "import rasterio.env\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Query EarthAI Catalog\n", "\n", "The code below queries the EarthAI Catalog for [Landsat 8](https://www.usgs.gov/media/files/landsat-collection-1-level-1-product-definition) imagery with a maximum cloud cover of 10% in the month of August 2018. We pass a single lat-long point of a location within the Yellowstone region. For more information on querying the EarthAI catalog, please refer the [previous article](https://docs.astraea.earth/hc/en-us/articles/360043451772-Query-Imagery-Data-using-the-EarthAI-Catalog-API) where we discuss this in detail. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cat = earth_ondemand.read_catalog(\n", " geo='POINT(-110.0 44.5)',\n", " start_datetime='2018-08-01',\n", " end_datetime='2018-08-31',\n", " max_cloud_cover=10,\n", " collections='landsat8_l1tp',\n", ") " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Read Imagery into Numpy Arrays\n", "\n", "The EarthAI catalog query returns a catalog of Landsat 8 imagery scenes. It contains references to the imagery files, but not actual imagery. \n", "\n", "The first step to read in the imagery is to determine which bands you want to read in. To view a list of available Landsat 8 bands, you can run `earth_ondemand.item_assets('landsat8_l1tp')`. This function provides information on the available bands, spatial resolution, and band details for each collection in the EarthAI catalog." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "earth_ondemand.item_assets('landsat8_l1tp').sort_values('asset_name')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this example, we read in __B4__, __B3__, and __B2__ bands, which correspond to the red, green, and blue bands, respectively. \n", "\n", "The `rasterio.env.Env` function provides Rasterio the AWS credentials it needs to pull the imagery from it's location in Amazon S3. The `rasterio.open` function creates a DatasetReader object that contains important metadata about the image file as shown below. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "with rasterio.env.Env(CURL_CA_BUNDLE='/etc/ssl/certs/ca-certificates.crt'):\n", " with rasterio.open(cat.iloc[0].B4) as src:\n", " display(src.meta)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To read the actual imagery into Numpy arrays, you have to call the `read` function on the DatasetReader object. In the code below, we loop through each scene, read the red, green, and blue bands into Numpy arrays, and store them in a list. Since each image file only contains a single band, we pass 1 as a parameter in the `read` function, which tells Rasterio to read in only the first band." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "red_bands = []\n", "grn_bands = []\n", "blu_bands = []\n", "\n", "with rasterio.env.Env(CURL_CA_BUNDLE='/etc/ssl/certs/ca-certificates.crt'):\n", " for idx, scene in cat.iterrows():\n", " red_bands.append(rasterio.open(scene.B4).read(1))\n", " grn_bands.append(rasterio.open(scene.B3).read(1))\n", " blu_bands.append(rasterio.open(scene.B2).read(1))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To view one of your image scenes, you pass a Numpy array to `plt.imshow`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.imshow(red_bands[0])\n", "plt.show()" ] } ], "metadata": { "kernelspec": { "display_name": "EarthAI Environment", "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.7.9" }, "zendesk": { "draft": true, "id": 360051407432, "section_id": 360008732711, "title": "Read in Scenes from a Raster Catalog Using Rasterio" } }, "nbformat": 4, "nbformat_minor": 4 }