Skip to content

Commit

Permalink
Add notebook and python scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Nov 5, 2023
1 parent 0d46f81 commit b2d855b
Show file tree
Hide file tree
Showing 762 changed files with 185,370 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import libraries"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import ee\n",
"import geemap"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create an interactive map"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"Map = geemap.Map(center=[40, -100], zoom=4)"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Add Earth Engine Python script"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# Add Earth Engine dataset\n",
"image = ee.Image(\"USGS/SRTMGL1_003\")\n",
"\n",
"# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
"# Chapter: A1.1 Agricultural Environments\n",
"# Checkpoint: A11a\n",
"# Authors: Sherrie Wang, George Azzari\n",
"# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
"\n",
"##############################\n",
"# 1. Pull all Landsat 7 and 8 images over study area\n",
"##############################\n",
"\n",
"# Define study area.\n",
"TIGER = ee.FeatureCollection('TIGER/2018/Counties')\n",
"region = ee.Feature(TIGER \\\n",
" .filter(ee.Filter.eq('STATEFP', '17')) \\\n",
" .filter(ee.Filter.eq('NAME', 'McLean')) \\\n",
" .first())\n",
"geometry = region.geometry()\n",
"Map.centerObject(region)\n",
"Map.addLayer(region, {\n",
" 'color': 'red'\n",
"}, 'McLean County')\n",
"\n",
"# Import Landsat imagery.\n",
"landsat7 = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2')\n",
"landsat8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')\n",
"\n",
"# Functions to rename Landsat 7 and 8 images.\n",
"def renameL7(img):\n",
" return img.rename(['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1',\n",
" 'SWIR2', 'TEMP1', 'ATMOS_OPACITY', 'QA_CLOUD',\n",
" 'ATRAN', 'CDIST',\n",
" 'DRAD', 'EMIS', 'EMSD', 'QA', 'TRAD', 'URAD',\n",
" 'QA_PIXEL',\n",
" 'QA_RADSAT'\n",
" ])\n",
"\n",
"\n",
"def renameL8(img):\n",
" return img.rename(['AEROS', 'BLUE', 'GREEN', 'RED', 'NIR',\n",
" 'SWIR1',\n",
" 'SWIR2', 'TEMP1', 'QA_AEROSOL', 'ATRAN', 'CDIST',\n",
" 'DRAD', 'EMIS',\n",
" 'EMSD', 'QA', 'TRAD', 'URAD', 'QA_PIXEL', 'QA_RADSAT'\n",
" ])\n",
"\n",
"\n",
"# Functions to mask out clouds, shadows, and other unwanted features.\n",
"def addMask(img):\n",
" # Bit 0: Fill\n",
" # Bit 1: Dilated Cloud\n",
" # Bit 2: Cirrus (high confidence) (L8) or unused (L7)\n",
" # Bit 3: Cloud\n",
" # Bit 4: Cloud Shadow\n",
" # Bit 5: Snow\n",
" # Bit 6: Clear\n",
" # 0: Cloud or Dilated Cloud bits are set\n",
" # 1: Cloud and Dilated Cloud bits are not set\n",
" # Bit 7: Water\n",
" clear = img.select('QA_PIXEL').bitwiseAnd(64).neq(0)\n",
" clear = clear.updateMask(clear).rename(['pxqa_clear'])\n",
"\n",
" water = img.select('QA_PIXEL').bitwiseAnd(128).neq(0)\n",
" water = water.updateMask(water).rename(['pxqa_water'])\n",
"\n",
" cloud_shadow = img.select('QA_PIXEL').bitwiseAnd(16).neq(0)\n",
" cloud_shadow = cloud_shadow.updateMask(cloud_shadow).rename([\n",
" 'pxqa_cloudshadow'\n",
" ])\n",
"\n",
" snow = img.select('QA_PIXEL').bitwiseAnd(32).neq(0)\n",
" snow = snow.updateMask(snow).rename(['pxqa_snow'])\n",
"\n",
" masks = ee.Image.cat([\n",
" clear, water, cloud_shadow, snow\n",
" ])\n",
"\n",
" return img.addBands(masks)\n",
"\n",
"\n",
"def maskQAClear(img):\n",
" return img.updateMask(img.select('pxqa_clear'))\n",
"\n",
"\n",
"# Function to add GCVI as a band.\n",
"def addVIs(img):\n",
" gcvi = img.expression('(nir / green) - 1', {\n",
" 'nir': img.select('NIR'),\n",
" 'green': img.select('GREEN')\n",
" }).select([0], ['GCVI'])\n",
"\n",
" return ee.Image.cat([img, gcvi])\n",
"\n",
"\n",
"# Define study time period.\n",
"start_date = '2020-01-01'\n",
"end_date = '2020-12-31'\n",
"\n",
"# Pull Landsat 7 and 8 imagery over the study area between start and end dates.\n",
"landsat7coll = landsat7 \\\n",
" .filterBounds(geometry) \\\n",
" .filterDate(start_date, end_date) \\\n",
" .map(renameL7)\n",
"\n",
"landsat8coll = landsat8 \\\n",
" .filterDate(start_date, end_date) \\\n",
" .filterBounds(geometry) \\\n",
" .map(renameL8)\n",
"\n",
"# Merge Landsat 7 and 8 collections.\n",
"landsat = landsat7coll.merge(landsat8coll) \\\n",
" .sort('system:time_start')\n",
"\n",
"# Mask out non-clear pixels, add VIs and time variables.\n",
"landsat = landsat.map(addMask) \\\n",
" .map(maskQAClear) \\\n",
" .map(addVIs)\n",
"\n",
"# Visualize GCVI time series at one location.\n",
"point = ee.Geometry.Point([-88.81417685576481,\n",
" 40.579804398254005\n",
"])\n",
"landsatChart = ui.Chart.image.series(landsat.select('GCVI'),\n",
" point) \\\n",
" .setChartType('ScatterChart') \\\n",
" .setOptions({\n",
" 'title': 'Landsat GCVI time series',\n",
" 'lineWidth': 1,\n",
" 'pointSize': 3,\n",
" })\n",
"print(landsatChart)\n",
"\n",
"# Get crop type dataset.\n",
"cdl = ee.Image('USDA/NASS/CDL/2020').select(['cropland'])\n",
"Map.addLayer(cdl.clip(geometry), {}, 'CDL 2020')\n",
"\n",
"# -----------------------------------------------------------------------\n",
"# CHECKPOINT\n",
"# -----------------------------------------------------------------------"
],
"outputs": [],
"execution_count": null
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Display the interactive map"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"Map"
],
"outputs": [],
"execution_count": null
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Chapter: A1.1 Agricultural Environments
// Checkpoint: A11a
// Authors: Sherrie Wang, George Azzari
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

////////////////////////////////////////////////////////////
// 1. Pull all Landsat 7 and 8 images over study area
////////////////////////////////////////////////////////////

// Define study area.
var TIGER = ee.FeatureCollection('TIGER/2018/Counties');
var region = ee.Feature(TIGER
.filter(ee.Filter.eq('STATEFP', '17'))
.filter(ee.Filter.eq('NAME', 'McLean'))
.first());
var geometry = region.geometry();
Map.centerObject(region);
Map.addLayer(region, {
'color': 'red'
}, 'McLean County');

// Import Landsat imagery.
var landsat7 = ee.ImageCollection('LANDSAT/LE07/C02/T1_L2');
var landsat8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2');

// Functions to rename Landsat 7 and 8 images.
function renameL7(img) {
return img.rename(['BLUE', 'GREEN', 'RED', 'NIR', 'SWIR1',
'SWIR2', 'TEMP1', 'ATMOS_OPACITY', 'QA_CLOUD',
'ATRAN', 'CDIST',
'DRAD', 'EMIS', 'EMSD', 'QA', 'TRAD', 'URAD',
'QA_PIXEL',
'QA_RADSAT'
]);
}

function renameL8(img) {
return img.rename(['AEROS', 'BLUE', 'GREEN', 'RED', 'NIR',
'SWIR1',
'SWIR2', 'TEMP1', 'QA_AEROSOL', 'ATRAN', 'CDIST',
'DRAD', 'EMIS',
'EMSD', 'QA', 'TRAD', 'URAD', 'QA_PIXEL', 'QA_RADSAT'
]);
}

// Functions to mask out clouds, shadows, and other unwanted features.
function addMask(img) {
// Bit 0: Fill
// Bit 1: Dilated Cloud
// Bit 2: Cirrus (high confidence) (L8) or unused (L7)
// Bit 3: Cloud
// Bit 4: Cloud Shadow
// Bit 5: Snow
// Bit 6: Clear
// 0: Cloud or Dilated Cloud bits are set
// 1: Cloud and Dilated Cloud bits are not set
// Bit 7: Water
var clear = img.select('QA_PIXEL').bitwiseAnd(64).neq(0);
clear = clear.updateMask(clear).rename(['pxqa_clear']);

var water = img.select('QA_PIXEL').bitwiseAnd(128).neq(0);
water = water.updateMask(water).rename(['pxqa_water']);

var cloud_shadow = img.select('QA_PIXEL').bitwiseAnd(16).neq(0);
cloud_shadow = cloud_shadow.updateMask(cloud_shadow).rename([
'pxqa_cloudshadow'
]);

var snow = img.select('QA_PIXEL').bitwiseAnd(32).neq(0);
snow = snow.updateMask(snow).rename(['pxqa_snow']);

var masks = ee.Image.cat([
clear, water, cloud_shadow, snow
]);

return img.addBands(masks);
}

function maskQAClear(img) {
return img.updateMask(img.select('pxqa_clear'));
}

// Function to add GCVI as a band.
function addVIs(img){
var gcvi = img.expression('(nir / green) - 1', {
nir: img.select('NIR'),
green: img.select('GREEN')
}).select([0], ['GCVI']);

return ee.Image.cat([img, gcvi]);
}

// Define study time period.
var start_date = '2020-01-01';
var end_date = '2020-12-31';

// Pull Landsat 7 and 8 imagery over the study area between start and end dates.
var landsat7coll = landsat7
.filterBounds(geometry)
.filterDate(start_date, end_date)
.map(renameL7);

var landsat8coll = landsat8
.filterDate(start_date, end_date)
.filterBounds(geometry)
.map(renameL8);

// Merge Landsat 7 and 8 collections.
var landsat = landsat7coll.merge(landsat8coll)
.sort('system:time_start');

// Mask out non-clear pixels, add VIs and time variables.
landsat = landsat.map(addMask)
.map(maskQAClear)
.map(addVIs);

// Visualize GCVI time series at one location.
var point = ee.Geometry.Point([-88.81417685576481,
40.579804398254005
]);
var landsatChart = ui.Chart.image.series(landsat.select('GCVI'),
point)
.setChartType('ScatterChart')
.setOptions({
title: 'Landsat GCVI time series',
lineWidth: 1,
pointSize: 3,
});
print(landsatChart);

// Get crop type dataset.
var cdl = ee.Image('USDA/NASS/CDL/2020').select(['cropland']);
Map.addLayer(cdl.clip(geometry), {}, 'CDL 2020');

// -----------------------------------------------------------------------
// CHECKPOINT
// -----------------------------------------------------------------------
Loading

0 comments on commit b2d855b

Please sign in to comment.