In this tutorial, we show how to display RGB (true-color) and false-color image tiles contained in a RasterFrames data structure.
RasterFrames brings the characteristics and capabilities of DataFrames to raster imagery data. Tiles are subsets of scenes and scenes are discrete instances of Earth observation data with specified spatial extent, date-time, and coordinate reference system.
RGB images are also called true- or natural-color images as these contain the wavelengths that human vision processes.
RGB channels can also be assigned to other spectral bands, such as the near-infrared and short-wave infrared bands. In this instance, the composite RGB images are now called false-color images. Below we give examples of how to produce false-color image tiles in addition to true color image tiles.
We will start by importing the Python libraries used in this example.
from earthai.init import * from pyrasterframes.rasterfunctions import *
Query the Earth OnDemand Catalog
Next, we query the Earth OnDemand catalog to obtain imagery with the
earth_ondemand.read_catalog function. There are a number of ways to query the catalog. This example is querying the MODIS Terra and Aqua surface reflectivity catalog centered on a specific point with longitude-latitude coordinates. This geospatial point corresponds to a location near Palmas, Brazil. We have also arbitrarily selected images collected during the months of September and October of 2019.
catalog = earth_ondemand.read_catalog( geo="POINT(-50.8 -10.5)", start_datetime='2019-09-01', end_datetime='2019-10-31', max_cloud=5, collections='mcd43a4', )
Create and Display True-color Tiles
Now we read the results of the Earth OnDemand query into a RasterFrame using the
spark.read.raster function. For this example, we'll select the red, green, and blue MODIS bands: B01, B04, B03, respectively. Simply for clarity we also rename the B01, B04, and B03 columns as red, green, and blue with the
true_color_rf = spark.read.raster(catalog, catalog_col_names=['B01', 'B04', 'B03']) \ .withColumnRenamed('B01', 'red') \ .withColumnRenamed('B04', 'green') \ .withColumnRenamed('B03', 'blue')
Now we will create a new tile column that represents the composite image tiles of the red, green, and blue channels. This is otherwise known as a true-color image. To accomplish this we use the
rf_render_png function from the pyrasterframes.rasterfunctions library. This function takes three tile columns, which in this case are the red, green, and blue bands of the MODIS imagery, and combines them into a single composite. It then converts this composite to a PNG-encoded image. The two lines of code below create a new column png that is made up of true-color composite tiles and displays the first five true color tiles in a notebook cell.
true_color_rf = true_color_rf.withColumn('png', rf_render_png('red', 'green', 'blue')) true_color_rf.select('png')
Create and Display False-color Tiles
You can also assign other spectral bands to the RGB channels producing false-color images. This can highlight Earth image properties such as differentiating between snow, ice, and clouds or showing where flooding has occurred. A very common false-color image utilizes the near-infrared, green, and red bands to show vegetation and vegetated areas. The code below follows the same steps as for generating the true color image tiles, but now we assign the MODIS near-infrared band to the red channel, the red band to the green channel, and the green band to the blue channel. In these image tiles, the red colors depict areas of vegetation. Dark red represents dense vegetation.
false_color_rf = spark.read.raster(catalog, catalog_col_names=['B02', 'B01', 'B04'])\ .withColumnRenamed('B02', 'nir') \ .withColumnRenamed('B04', 'green') \ .withColumnRenamed('B01', 'red')
false_color_rf = false_color_rf.withColumn('png', rf_render_png('nir', 'red', 'green')) false_color_rf.select('png')