Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Last active March 19, 2021 20:54
Show Gist options
  • Save jdbcode/afdf3cd3381f1987bcbeaa718607518c to your computer and use it in GitHub Desktop.
Save jdbcode/afdf3cd3381f1987bcbeaa718607518c to your computer and use it in GitHub Desktop.
Calculate Landsat surface reflectance Tasseled Cap transformation using Earth Engine
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "gee_calculate_tc.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyOy8ZPfcgLrl76IXmNACMAi",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/jdbcode/afdf3cd3381f1987bcbeaa718607518c/gee_calculate_tc.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "sLHNETPr-0-w"
},
"source": [
"import ee\n",
"ee.Authenticate()\n",
"ee.Initialize()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "9POJ8TQYCEiJ"
},
"source": [
"import folium\n",
"import pprint\n",
"\n",
"def add_ee_layer(self, ee_image_object, vis_params, name):\n",
" map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)\n",
" folium.raster_layers.TileLayer(\n",
" tiles=map_id_dict['tile_fetcher'].url_format,\n",
" attr='Map Data &copy; <a href=\"https://earthengine.google.com/\">Google Earth Engine</a>',\n",
" name=name,\n",
" overlay=True,\n",
" control=True\n",
" ).add_to(self)\n",
"\n",
"folium.Map.add_ee_layer = add_ee_layer"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "yLUAdtWw-5BZ"
},
"source": [
"# For Landsat surface reflectance (see: Crist (1985) and Huang et al. (2002)).\n",
"# Note that band selection is for Landsat 8.\n",
"def calc_tc(img):\n",
" b = ee.Image(img).select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7'])\n",
" brt_coeffs = ee.Image.constant([0.2043, 0.4158, 0.5524, 0.5741, 0.3124, 0.2303])\n",
" grn_coeffs = ee.Image.constant([-0.1603, -0.2819, -0.4934, 0.7940, -0.0002, -0.1446])\n",
" wet_coeffs = ee.Image.constant([0.0315, 0.2021, 0.3102, 0.1594, -0.6806, -0.6109])\n",
"\n",
" brightness = b.multiply(brt_coeffs).reduce(ee.Reducer.sum())\n",
" greenness = b.multiply(grn_coeffs).reduce(ee.Reducer.sum())\n",
" wetness = b.multiply(wet_coeffs).reduce(ee.Reducer.sum())\n",
" return ee.Image.cat(brightness, greenness, wetness).rename(['tcb', 'tcg', 'tcw'])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "uXETYXvoAnGW"
},
"source": [
"col = (ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')\n",
" .filterBounds(ee.Geometry.Point([-123.2654, 44.5662]))\n",
" .filterDate('2020-06-01', '2020-08-01'))\n",
"\n",
"tc_col = col.map(calc_tc)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "6ZCqafy6CNMu"
},
"source": [
"tc_img = tc_col.first()\n",
"pprint.pprint(tc_img.getInfo())"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "ii2w-npXFGz_"
},
"source": [
"tc_params = {\n",
" 'bands': ['tcb', 'tcg', 'tcw'],\n",
" 'min': [604, -49, -2245],\n",
" 'max': [5592, 3147, 843],\n",
"}\n",
"map = folium.Map(location=[44.5662, -123.2654], zoom_start=9)\n",
"map.add_ee_layer(col.first(), {'bands': ['B6', 'B5', 'B4'], 'min': 100, 'max': 4000}, 'B6, B5, B4')\n",
"map.add_ee_layer(tc_col.first(), tc_params, 'TC')\n",
"display(map.add_child(folium.LayerControl()))"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment