Last active
October 23, 2020 02:42
-
-
Save giswqs/55ffe0fc32a86e4265bb2605d66cb1c6 to your computer and use it in GitHub Desktop.
point_based_extraction_gee_102020_v3.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "!pip install whitebox", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Requirement already satisfied: whitebox in /home/qiusheng/.conda/envs/geemap/lib/python3.8/site-packages (1.4.0)\nRequirement already satisfied: Click>=6.0 in /home/qiusheng/.local/lib/python3.8/site-packages (from whitebox) (7.1.2)\n\u001b[33mWARNING: You are using pip version 20.2.2; however, version 20.2.4 is available.\nYou should consider upgrading via the '/home/qiusheng/.conda/envs/geemap/bin/python -m pip install --upgrade pip' command.\u001b[0m\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import os\nimport ee\nimport geemap\nimport whitebox", | |
"execution_count": 2, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "work_dir = os.getcwd()\nwork_dir", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 3, | |
"data": { | |
"text/plain": "'/home/qiusheng/Documents/GEE'" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "wbt = whitebox.WhiteboxTools()", | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "in_csv = os.path.join(work_dir, 'pts.csv')\nout_shp = os.path.join(work_dir, 'pts.shp')", | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "wbt.csv_points_to_vector(in_csv, out_shp, xfield=2, yfield=3, epsg=4326)", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "./whitebox_tools --run=\"CsvPointsToVector\" --input='/home/qiusheng/Documents/GEE/pts.csv' --output='/home/qiusheng/Documents/GEE/pts.shp' --xfield=2 --yfield=3 --epsg='4326' -v\n\n********************************\n* Welcome to CsvPointsToVector *\n********************************\nReading data...\nProgress: 33%\nProgress: 44%\nProgress: 55%\nProgress: 66%\nProgress: 77%\nProgress: 88%\nProgress: 100%\nProgress: 111%\nProgress: 122%\nSaving data...\nOutput file written\nElapsed Time: 0.1s\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": "0" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map = geemap.Map()\nMap", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "display_data", | |
"data": { | |
"text/plain": "Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…", | |
"application/vnd.jupyter.widget-view+json": { | |
"version_major": 2, | |
"version_minor": 0, | |
"model_id": "d903560b6199403aaf24c5893733f646" | |
} | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# points = []\n# #with open('D:/pts.csv') as f:\n# with open('pts.csv') as f:\n# lines = f.readlines()\n# for line in lines[1:]:\n# lon = float(line.split(',')[2])\n# lat = float(line.split(',')[3])\n# points.append([lon, lat])", | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# ee_list = ee.List(points)", | |
"execution_count": 9, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# ee_points = ee_list.map(lambda xy: ee.Feature(ee.Geometry.Point(xy)))", | |
"execution_count": 10, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# roi = ee.FeatureCollection(ee_points)", | |
"execution_count": 11, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "roi = geemap.shp_to_ee(out_shp)", | |
"execution_count": 12, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# roi.getInfo()", | |
"execution_count": 13, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(roi, {}, 'points')\nMap.centerObject(roi, 15)", | |
"execution_count": 14, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "NEDelevation = ee.Image('USGS/NED')\nMap.addLayer(NEDelevation, {'min':0, 'max':4000},'NED')", | |
"execution_count": 15, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "slope = ee.Terrain.slope(NEDelevation)\nMap.addLayer(slope,{'min':0,'max':45},'Slope')", | |
"execution_count": 16, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "aspect = ee.Terrain.aspect(NEDelevation)\nMap.addLayer(aspect,{'min':0,'max':360},'Aspect')", | |
"execution_count": 17, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def maskS2clouds(image):\n qa = image.select('QA60')\n cloudBitMask = 1 << 10\n cirrusBitMask = 1 << 11\n mask = qa.bitwiseAnd(cloudBitMask).eq(0) \\\n .And(qa.bitwiseAnd(cirrusBitMask).eq(0))\n return image.updateMask(mask).divide(10000)", | |
"execution_count": 18, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "start_year = 2016\nend_year = 2020\nstart_month = 6\nend_month = 10\n\ndate_list = []\nfor year in range(start_year, end_year):\n for month in range(start_month, end_month):\n item = str(year) + '-' + str(month).zfill(2) + '-01'\n date_list.append(item)\nprint(date_list)", | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "['2016-06-01', '2016-07-01', '2016-08-01', '2016-09-01', '2017-06-01', '2017-07-01', '2017-08-01', '2017-09-01', '2018-06-01', '2018-07-01', '2018-08-01', '2018-09-01', '2019-06-01', '2019-07-01', '2019-08-01', '2019-09-01']\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "dates = ee.List(date_list)", | |
"execution_count": 20, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "dates.getInfo()", | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 21, | |
"data": { | |
"text/plain": "['2016-06-01',\n '2016-07-01',\n '2016-08-01',\n '2016-09-01',\n '2017-06-01',\n '2017-07-01',\n '2017-08-01',\n '2017-09-01',\n '2018-06-01',\n '2018-07-01',\n '2018-08-01',\n '2018-09-01',\n '2019-06-01',\n '2019-07-01',\n '2019-08-01',\n '2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_ndvi(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \n \n monthly_image = image.expression(\n \" (VNIR - RED) / sqrt((VNIR + RED))\",\n {\n 'RED': image.select(\"B4\"), \n 'VNIR': image.select(\"B8\"), \n } \n )\n \n return monthly_image.rename(ee.String(\"ndvi_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd'))", | |
"execution_count": 22, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "ndvi_images = ee.ImageCollection(dates.map(get_ndvi))\nndvi_bands = ndvi_images.toBands() # convert imageCollection to an image", | |
"execution_count": 23, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "ndvi_bands.bandNames().getInfo()", | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 24, | |
"data": { | |
"text/plain": "['0_ndvi_2016-06-01',\n '1_ndvi_2016-07-01',\n '2_ndvi_2016-08-01',\n '3_ndvi_2016-09-01',\n '4_ndvi_2017-06-01',\n '5_ndvi_2017-07-01',\n '6_ndvi_2017-08-01',\n '7_ndvi_2017-09-01',\n '8_ndvi_2018-06-01',\n '9_ndvi_2018-07-01',\n '10_ndvi_2018-08-01',\n '11_ndvi_2018-09-01',\n '12_ndvi_2019-06-01',\n '13_ndvi_2019-07-01',\n '14_ndvi_2019-08-01',\n '15_ndvi_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(ndvi_bands, {}, 'ndvi')", | |
"execution_count": 25, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_gndvi(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \n \n \n monthly_image = image.expression(\n \" (VNIR - GREEN) / sqrt((VNIR + GREEN))\",\n {\n 'GREEN': image.select(\"B3\"), \n 'VNIR': image.select(\"B8\"), \n } \n )\n \n return monthly_image.rename(ee.String(\"gndvi_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd'))", | |
"execution_count": 26, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "gndvi_images = ee.ImageCollection(dates.map(get_gndvi))\ngndvi_bands = gndvi_images.toBands() # convert imageCollection to an image", | |
"execution_count": 27, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "gndvi_bands.bandNames().getInfo()", | |
"execution_count": 28, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 28, | |
"data": { | |
"text/plain": "['0_gndvi_2016-06-01',\n '1_gndvi_2016-07-01',\n '2_gndvi_2016-08-01',\n '3_gndvi_2016-09-01',\n '4_gndvi_2017-06-01',\n '5_gndvi_2017-07-01',\n '6_gndvi_2017-08-01',\n '7_gndvi_2017-09-01',\n '8_gndvi_2018-06-01',\n '9_gndvi_2018-07-01',\n '10_gndvi_2018-08-01',\n '11_gndvi_2018-09-01',\n '12_gndvi_2019-06-01',\n '13_gndvi_2019-07-01',\n '14_gndvi_2019-08-01',\n '15_gndvi_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(gndvi_bands, {}, 'gndvi')", | |
"execution_count": 29, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# Compute Optimized Soil Adjusted Vegetation Index (OSAVI)", | |
"execution_count": 30, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_osavi(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterBounds(roi) \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \\\n .clip(roi)\n \n monthly_image = image.expression(\n \" (1.0 + 0.16)*(VNIR - RED)/(VNIR + RED + 0.16)\",\n {\n 'RED': image.select(\"B4\"), \n 'VNIR': image.select(\"B8\"), \n } \n )\n \n return monthly_image.rename(ee.String(\"osavi_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd')) ", | |
"execution_count": 31, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "osavi_images = ee.ImageCollection(dates.map(get_osavi))\nosavi_bands = osavi_images.toBands() # convert imageCollection to an image", | |
"execution_count": 32, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "osavi_bands.bandNames().getInfo()", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 33, | |
"data": { | |
"text/plain": "['0_osavi_2016-06-01',\n '1_osavi_2016-07-01',\n '2_osavi_2016-08-01',\n '3_osavi_2016-09-01',\n '4_osavi_2017-06-01',\n '5_osavi_2017-07-01',\n '6_osavi_2017-08-01',\n '7_osavi_2017-09-01',\n '8_osavi_2018-06-01',\n '9_osavi_2018-07-01',\n '10_osavi_2018-08-01',\n '11_osavi_2018-09-01',\n '12_osavi_2019-06-01',\n '13_osavi_2019-07-01',\n '14_osavi_2019-08-01',\n '15_osavi_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(ndvi_bands, {}, 'osavi')", | |
"execution_count": 34, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# Compute Renormalized Difference Vegetation Index (RDVI) ", | |
"execution_count": 35, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_rdvi(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterBounds(roi) \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \\\n .clip(roi)\n \n monthly_image = image.expression(\n \" (VNIR - RED)/sqrt(VNIR + RED)\",\n {\n 'RED': image.select(\"B4\"), \n 'VNIR': image.select(\"B8\"), \n } \n )\n \n return monthly_image.rename(ee.String(\"rdvi_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd')) ", | |
"execution_count": 36, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "rdvi_images = ee.ImageCollection(dates.map(get_rdvi))\nrdvi_bands = rdvi_images.toBands() # convert imageCollection to an image", | |
"execution_count": 37, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "rdvi_bands.bandNames().getInfo()", | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 38, | |
"data": { | |
"text/plain": "['0_rdvi_2016-06-01',\n '1_rdvi_2016-07-01',\n '2_rdvi_2016-08-01',\n '3_rdvi_2016-09-01',\n '4_rdvi_2017-06-01',\n '5_rdvi_2017-07-01',\n '6_rdvi_2017-08-01',\n '7_rdvi_2017-09-01',\n '8_rdvi_2018-06-01',\n '9_rdvi_2018-07-01',\n '10_rdvi_2018-08-01',\n '11_rdvi_2018-09-01',\n '12_rdvi_2019-06-01',\n '13_rdvi_2019-07-01',\n '14_rdvi_2019-08-01',\n '15_rdvi_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(ndvi_bands, {}, 'rdvi')", | |
"execution_count": 39, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# Compute RGB432B\n# Compute SAVI \n# Compute SCL", | |
"execution_count": 40, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_tcari(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterBounds(roi) \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \\\n .clip(roi)\n \n monthly_image = image.expression(\n \" 3.0*(VNIR - RED) - 0.2*(VNIR - GREEN)*(VNIR / RED)\",\n {\n 'GREEN': image.select(\"B3\"),\n 'RED': image.select(\"B4\"), \n 'VNIR': image.select(\"B8\"), \n } \n )\n \n return monthly_image.rename(ee.String(\"tcari_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd')) ", | |
"execution_count": 41, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "tcari_images = ee.ImageCollection(dates.map(get_tcari))\ntcari_bands = tcari_images.toBands() # convert imageCollection to an image", | |
"execution_count": 42, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "tcari_bands.bandNames().getInfo()", | |
"execution_count": 43, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 43, | |
"data": { | |
"text/plain": "['0_tcari_2016-06-01',\n '1_tcari_2016-07-01',\n '2_tcari_2016-08-01',\n '3_tcari_2016-09-01',\n '4_tcari_2017-06-01',\n '5_tcari_2017-07-01',\n '6_tcari_2017-08-01',\n '7_tcari_2017-09-01',\n '8_tcari_2018-06-01',\n '9_tcari_2018-07-01',\n '10_tcari_2018-08-01',\n '11_tcari_2018-09-01',\n '12_tcari_2019-06-01',\n '13_tcari_2019-07-01',\n '14_tcari_2019-08-01',\n '15_tcari_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(ndvi_bands, {}, 'tcari')", | |
"execution_count": 44, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# Compute Triangular chlorophyll index (TCI)", | |
"execution_count": 45, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_tci(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterBounds(roi) \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \\\n .clip(roi)\n \n monthly_image = image.expression(\n \" 1.2*(VNIR - GREEN) - 1.5*(RED - GREEN)*sqrt(VNIR / RED)\",\n {\n 'GREEN': image.select(\"B3\"),\n 'RED': image.select(\"B4\"), \n 'VNIR': image.select(\"B8\"), \n } \n )\n \n return monthly_image.rename(ee.String(\"tci_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd')) ", | |
"execution_count": 46, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "tci_images = ee.ImageCollection(dates.map(get_tci))\ntci_bands = tci_images.toBands() # convert imageCollection to an image", | |
"execution_count": 47, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "tci_bands.bandNames().getInfo()", | |
"execution_count": 48, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 48, | |
"data": { | |
"text/plain": "['0_tci_2016-06-01',\n '1_tci_2016-07-01',\n '2_tci_2016-08-01',\n '3_tci_2016-09-01',\n '4_tci_2017-06-01',\n '5_tci_2017-07-01',\n '6_tci_2017-08-01',\n '7_tci_2017-09-01',\n '8_tci_2018-06-01',\n '9_tci_2018-07-01',\n '10_tci_2018-08-01',\n '11_tci_2018-09-01',\n '12_tci_2019-06-01',\n '13_tci_2019-07-01',\n '14_tci_2019-08-01',\n '15_tci_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(ndvi_bands, {}, 'tci')", | |
"execution_count": 49, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# Normalized Ratio between NIR band and a SWIR (NDWI)\n# NDWI is correlated with vegetaion water content ", | |
"execution_count": 50, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "def get_ndwi(start_date):\n start_date = ee.Date(start_date)\n end_date = start_date.advance(1, 'month')\n image = ee.ImageCollection('COPERNICUS/S2') \\\n .filterBounds(roi) \\\n .filterDate(start_date, end_date) \\\n .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 100)) \\\n .map(maskS2clouds) \\\n .mean() \\\n .clip(roi)\n \n monthly_image = image.expression(\n \" (0.842*SWIR*2.190*SWIR)/(0.842*SWIR + 2.19*SWIR)\",\n {\n 'SWIR': image.select(\"B12\"),\n } \n )\n \n return monthly_image.rename(ee.String(\"ndwi_\").cat(start_date.format('YYYY-MM-dd')))\n \n# return monthly_image.normalizedDifference(['B8', 'B4']).rename(start_date.format('YYYY-MM-dd')) ", | |
"execution_count": 51, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "ndwi_images = ee.ImageCollection(dates.map(get_ndwi))\nndwi_bands = ndwi_images.toBands() # convert imageCollection to an image", | |
"execution_count": 52, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"scrolled": false, | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "ndwi_bands.bandNames().getInfo()", | |
"execution_count": 53, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 53, | |
"data": { | |
"text/plain": "['0_ndwi_2016-06-01',\n '1_ndwi_2016-07-01',\n '2_ndwi_2016-08-01',\n '3_ndwi_2016-09-01',\n '4_ndwi_2017-06-01',\n '5_ndwi_2017-07-01',\n '6_ndwi_2017-08-01',\n '7_ndwi_2017-09-01',\n '8_ndwi_2018-06-01',\n '9_ndwi_2018-07-01',\n '10_ndwi_2018-08-01',\n '11_ndwi_2018-09-01',\n '12_ndwi_2019-06-01',\n '13_ndwi_2019-07-01',\n '14_ndwi_2019-08-01',\n '15_ndwi_2019-09-01']" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Map.addLayer(ndvi_bands, {}, 'ndwi')", | |
"execution_count": 54, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# Normalized Ratio between SWIR band NIR (NDBI) ", | |
"execution_count": 55, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#ndbi_images = ee.ImageCollection(dates.map(get_ndbi))\n#ndbi_bands = ndbi_images.toBands() # convert imageCollection to an image", | |
"execution_count": 56, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#ndbi_bands.bandNames().getInfo()", | |
"execution_count": 57, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "image = NEDelevation.addBands(slope).addBands(aspect).addBands(ndvi_bands).addBands(gndvi_bands).addBands(osavi_bands).addBands(rdvi_bands).addBands(tcari_bands).addBands(tci_bands).addBands(ndwi_bands)\n#image = NEDelevation.addBands(slope).addBands(aspect).addBands(ndvi_bands).addBands(gndvi_bands)", | |
"execution_count": 58, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "values = image.sampleRegions(**{\n 'collection': roi,\n 'scale': 10\n})", | |
"execution_count": 59, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "out_csv = os.path.join(work_dir, 'values.csv')", | |
"execution_count": 60, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "geemap.ee_export_vector(values, out_csv)", | |
"execution_count": 61, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Generating URL ...\nDownloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/bcd745b6f43de0d33195dd0e0863f312-20d7bb5bcdfbd69e6d5b0e1989bbff5f:getFeatures\nPlease wait ...\nData downloaded to /home/qiusheng/Documents/GEE/values.csv\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# values.first().getInfo()", | |
"execution_count": 62, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# result_info = values.first().getInfo()", | |
"execution_count": 63, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# result_info", | |
"execution_count": 64, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "# import pandas as pd\n# df = pd.DataFrame.from_dict(result_info)", | |
"execution_count": 65, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3", | |
"language": "python" | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.8.2", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"hide_input": false, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": true, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Table of Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": true | |
}, | |
"varInspector": { | |
"window_display": false, | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"library": "var_list.py", | |
"delete_cmd_prefix": "del ", | |
"delete_cmd_postfix": "", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"library": "var_list.r", | |
"delete_cmd_prefix": "rm(", | |
"delete_cmd_postfix": ") ", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
] | |
}, | |
"gist": { | |
"id": "55ffe0fc32a86e4265bb2605d66cb1c6", | |
"data": { | |
"description": "point_based_extraction_gee_102020_v3.ipynb", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/55ffe0fc32a86e4265bb2605d66cb1c6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment