{ "metadata": { "name": "", "signature": "sha256:4c426556ca5b7a28672370e6b8fd479d75d797345c393d5137a3249cd200194e" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "%matplotlib inline\n", "\n", "import datetime\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "from IPython.display import display" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Initialize Earth Engine" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Load code to setup authorization for an IPython Notebook. Note that this is a temporary step\n", "# that is required until the Earth Engine Python API is updated to include this logic.\n", "%run 'authorize_earth_engine_in_notebook.ipynb'\n", "\n", "# Initialize Earth Engine\n", "# Note that we are calling a function defined in the previously run IPython Notebook, rather than\n", "# the typical call to ee.Initialize()\n", "ee_initialize()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Load the Google Maps Interactive Widget" ] }, { "cell_type": "code", "collapsed": false, "input": [ "%run 'define_google_maps_interactive_widget.ipynb'" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Google Map Widget - Hello World example" ] }, { "cell_type": "code", "collapsed": false, "input": [ "map = GoogleMapsWidget(lat=70, lng=-140.0, zoom=6)\n", "display(map)\n", "\n", "image1 = ee.Image('LC81412332013146LGN00')\n", "vis_params1 = {'bands':'B4,B3,B2','min':5000,'max':10000}\n", "map.addLayer(image=image1, vis_params=vis_params1, name='Night')\n", "\n", "image2 = ee.Image('LC80660112013132LGN01')\n", "vis_params2 = {'bands':'B4,B3,B2','min':0,'max':40000}\n", "map.addLayer(image=image2, vis_params=vis_params2, name='Day', visible=False)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "(map.lat, map.lng, map.zoom, map.bounds)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Image Collection Example" ] }, { "cell_type": "code", "collapsed": false, "input": [ "lng, lat = (-120.6, 40.0)\n", "map2 = GoogleMapsWidget(lat=lat, lng=lng, zoom=6)\n", "display(map2)\n", "\n", "# Add an Image Collection of Landsat 8 data to the map.\n", "collection = (ee.ImageCollection('LC8')\n", " .filterDate('2014-01-01', '2014-06-01')\n", " .filterMetadata('SUN_ELEVATION', 'greater_than', 0)\n", " )\n", "vis_params3 = {'bands': 'B4,B3,B2', 'min':5000, 'max':30000, 'gamma': 1.6}\n", "map2.addLayer(image=collection.mosaic(), vis_params=vis_params3, name='Landsat 8 mosaic')\n", "map2.addLayer(image=collection.median(), vis_params=vis_params3, name='Landsat 8 median', visible=False)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Extract information from the image collection" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Get the coordinates of the center of the map.\n", "(map2.lng, map2.lat)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "point = ee.Geometry.Point(map2.lng, map2.lat);\n", "info = collection.getRegion(point,500).getInfo()" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "xBand = 'time'\n", "yBandList = ['B4','B5']\n", "# extract the header column names\n", "header = info[0]\n", "# create a Numpy array of the data\n", "data = np.array(info[1:])\n", "# extract the time information\n", "iTime = header.index(xBand)\n", "# convert to Python datetime objects\n", "time = [datetime.datetime.fromtimestamp(i/1000) for i in (data[0:,iTime].astype(int))]\n", "# Extract the data columns we want to plot.\n", "iBands = [header.index(b) for b in yBandList]\n", "yData = data[0:,iBands].astype(np.float)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "red = yData[:,0]\n", "nir = yData[:,1]\n", "ndvi = (nir - red) / (nir + red)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fig, ax = plt.subplots(2, sharex=True, figsize=(10,10))\n", "ax[0].plot(time, red, 'o', color=\"red\", label=\"RED band\")\n", "ax[0].plot(time, nir, 'o', color=\"blue\", label=\"NIR band\")\n", "ax[0].set_ylabel('Band Values')\n", "ax[0].legend(loc='best')\n", "ax[1].plot(time, ndvi, 'o', color=\"black\", label=\"NDVI\")\n", "ax[1].set_ylabel('NDVI')\n", "ax[1].legend(loc='best')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }