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.
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.
We will start by importing all of the Python libraries used in this example.
from earthai.init import * import pyspark.sql.functions as F import geopandas
Query and Read Imagery
In the query below, we request Landsat 8 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.
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.
cat = earth_ondemand.read_catalog( geo='POINT (-77.009090 38.889766)', start_datetime='2020-09-01', end_datetime='2020-09-30', max_cloud_cover=10, collections='landsat8_l1tp' ) rf = spark.read.raster(cat, ['B4']) \ .select(rf_convert_cell_type('B4', 'uint16').alias('red')) \ .filter(rf_data_cells('red') == 65536)
Create a Tile Column Using a Conditional Statement
This very simple example shows how to create a new tile column using a conditional statement.
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.
The new tile column, red_condition, is shown below.
rf = rf.withColumn('red_condition', rf_local_greater('red', rf_tile_mean('red'))) rf.select('red', 'red_condition')