{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "This example demonstrates how to create a new tile column from conditional statements using RasterFrames. It runs through the steps of 1) acquiring imagery scenes from the EarthAI Catalog, 2) using RasterFrames to read imagery, and 3) using a conditional statement to create a new tile column.\n", "\n", "_Note: if you would like to run through this example in EarthAI Notebook, you can download the companion notebook and vector data source from the attachments provided at the end of this article._\n", "\n", "# Import Libraries\n", "\n", "We will start by importing all of the Python libraries used in this example." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from earthai.init import *\n", "import pyspark.sql.functions as F\n", "import geopandas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Query and Read Imagery\n", "\n", "In the query below, we request [Landsat 8](https://www.usgs.gov/media/files/landsat-collection-1-level-1-product-definition) imagery for September 2020 with a maximum of 10% cloud cover over Washington, D.C. We use `spark.read.raster`, which reads entire scenes of imagery broken up into a gridded set of tiles, to read in the red Landsat 8 band. \n", "\n", "In Landsat 8, zero-valued cells correspond to fill areas. We convert the cell type of the red band to `uint16`, whose NoData value is 0. This will cause any zero-valued cells in the measurement band to be considered NoData. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cat = earth_ondemand.read_catalog(\n", " geo='POINT (-77.009090 38.889766)',\n", " start_datetime='2020-09-01', \n", " end_datetime='2020-09-30',\n", " max_cloud_cover=10,\n", " collections='landsat8_l1tp'\n", ")\n", "\n", "rf = spark.read.raster(cat, ['B4']) \\\n", " .select(rf_convert_cell_type('B4', 'uint16').alias('red')) \\\n", " .filter(rf_data_cells('red') == 65536)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create a Tile Column Using a Conditional Statement\n", "\n", "This very simple example shows how to create a new tile column using a conditional statement. \n", "\n", "The `rf_local_greater` function takes a tile column and a value to compare against as input. The value to compare against can be either a raster, integer, or decimal value. In this example, we are passing the __red__ band and the mean of __red__ tile as the value to compare against. If a pixel in __red__ is greater than the mean, then the tile returned will have a 1 at that location. Otherwise, the value will be 0. \n", "\n", "The new tile column, __red_condition__, is shown below." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rf = rf.withColumn('red_condition', rf_local_greater('red', rf_tile_mean('red')))\n", "\n", "rf.select('red', 'red_condition')" ] } ], "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": 360051382292, "section_id": 360009044691, "title": "Create a New Tile Column from Conditional Statements Using RasterFrames" } }, "nbformat": 4, "nbformat_minor": 4 }