Skip to content

Instantly share code, notes, and snippets.

@giswqs
Created June 13, 2020 01:06
Show Gist options
  • Save giswqs/c5bfaad50f0838ad8384a3168b4f3a87 to your computer and use it in GitHub Desktop.
Save giswqs/c5bfaad50f0838ad8384a3168b4f3a87 to your computer and use it in GitHub Desktop.
Calcuate NDVI and export images from Earth Engine
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import ee\nimport geemap",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Map = geemap.Map()\nMap",
"execution_count": 2,
"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": "c3c804e7368e4bab8b60a690de52d65a"
}
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "point = ee.Geometry.Point(-122.1899, 37.5010)",
"execution_count": 3,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Map.addLayer(point, {}, 'ROI')\nMap.centerObject(point, 8)",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "images = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA') \\\n .filterBounds(point) \\\n .filterDate('2019-01-01', '2019-12-31') \\\n .sort('CLOUD_COVER') \\\n .limit(5)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "print(images.size().getInfo())",
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"text": "5\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "first_image = images.first()\nvis_params = {\n 'bands': ['B5', 'B4', 'B3'],\n 'min': 0,\n 'max': 0.5,\n 'gamma': [0.95, 1.1, 1]\n}\nMap.addLayer(first_image, vis_params, 'first image')",
"execution_count": 7,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def cal_ndvi(img):\n return img.normalizedDifference(['B5', 'B4'])",
"execution_count": 18,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "ndvi_images = images.map(cal_ndvi)",
"execution_count": 19,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "first_ndvi = ndvi_images.first()\n\nndvi_vis = {\n 'min': 0, \n 'max': 1,\n 'palette': [\n 'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',\n '74A901', '66A000', '529400', '3E8601', '207401', '056201',\n '004C00', '023B01', '012E01', '011D01', '011301']\n}\n\nMap.addLayer(first_ndvi, ndvi_vis, 'first NDVI')",
"execution_count": 21,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "geemap.ee_export_image_collection_to_drive(ndvi_images, scale=100)",
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"text": "Total number of images: 5\n\nExporting LC08_044034_20190924 ...\nExporting LC08_044034_20191010 ...\nExporting LC08_044034_20190316 ...\nExporting LC08_044034_20191026 ...\nExporting LC08_044034_20190706 ...\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"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
},
"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"
},
"gist": {
"id": "",
"data": {
"description": "Calcuate NDVI and export images from Earth Engine",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment