Cloud to Street Earth Engine Widget Demo

The purpose of this code is to demonstrate the potential of Earth Engine for
building maps driven by python code structures and visualized inline in an iPython notebook.

Setting up your work environment

Initialize the Earth Engine object using the authentication credentials. These two lines of code below require a special environment and authentication procedure to work. To read more about how to set up the authentication process, please visit: https://github.com/catherinekuhn/ee-python/ee-python/ and select the "Python API Instructions" notebook for detailed instructions. Once you have followed the set-up directions, you should be ready to access the Google Earth Engine Python API.

Before we actually initialize, always make sure you are in the correct environment. You can do this by typing the following command into the terminal.

conda info --envs

If you aren't in the ee-python environments switch to it by using the following (exclude source if using Windows)

source activate ee-python

If you have followed the installation instructions and are in the correct environment, you are now ready to use the following code.


In [ ]:
import ee

ee.Initialize()

Load Some Cool Packages

Import relevant packages and functions


In [27]:
%matplotlib inline

from __future__ import print_function # For py 2.7 compat


import datetime
from IPython.html import widgets
from IPython.display import display
from IPython.utils import traitlets
from IPython.core.display import Javascript

In [28]:
import IPython
IPython.__version__


Out[28]:
'2.0.0'

Display a test image

This will help you quickly double check you are in the right place. In this step, we are calling in a global SRTM elevation dataset from Earth Engine and visualizing it on the fly in our notebook.

Call in the global SRTM elevation data and add to map just to check and make sure you are able to access the EE master Web API.


In [29]:
image = ee.Image('srtm90_v4')
from IPython.display import Image
Image(url=image.getThumbUrl({'min':0, 'max': 3000}))


---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-29-2c85aa34243c> in <module>()
      1 image = ee.Image('srtm90_v4')
      2 from IPython.display import Image
----> 3 Image(url=image.getThumbUrl({'min':0, 'max': 3000}))

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/ee/deprecation.pyc in Wrapper(*args, **kwargs)
     30           filename=func.func_code.co_filename,
     31           lineno=func.func_code.co_firstlineno + 1)
---> 32       return func(*args, **kwargs)
     33     Wrapper.__doc__ += '\nDEPRECATED: ' + message
     34     return Wrapper

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/ee/image.pyc in getThumbURL(self, params)
    192         raise ee_exception.EEException(
    193             'The region parameter must be an array or a GeoJSON object.')
--> 194     return data.makeThumbUrl(data.getThumbId(request))
    195 
    196   # Deprecated spellings to match the JS library.

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/ee/data.pyc in getThumbId(params)
    265   if 'size' in request and isinstance(request['size'], (list, tuple)):
    266     request['size'] = 'x'.join(map(str, request['size']))
--> 267   return send_('/thumb', request)
    268 
    269 

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/ee/data.pyc in send_(path, params, opt_method, opt_raw)
    650   try:
    651     response, content = http.request(url, method=opt_method, body=payload,
--> 652                                      headers=headers)
    653   except httplib2.HttpLib2Error, e:
    654     raise ee_exception.EEException(

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/oauth2client/client.pyc in new_request(uri, method, body, headers, redirections, connection_type)
    570                 logger.info('Attempting refresh to obtain '
    571                             'initial access_token')
--> 572                 self._refresh(request_orig)
    573 
    574             # Clone and modify the request headers to add the appropriate

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/oauth2client/client.pyc in _refresh(self, http_request)
    840         """
    841         if not self.store:
--> 842             self._do_refresh_request(http_request)
    843         else:
    844             self.store.acquire_lock()

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/oauth2client/client.pyc in _do_refresh_request(self, http_request)
    872         logger.info('Refreshing access_token')
    873         resp, content = http_request(
--> 874             self.token_uri, method='POST', body=body, headers=headers)
    875         content = _from_bytes(content)
    876         if resp.status == 200:

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/httplib2/__init__.pyc in request(self, uri, method, body, headers, redirections, connection_type)
   1607                     content = ""
   1608                 else:
-> 1609                     (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
   1610         except Exception, e:
   1611             if self.force_exception_to_status_code:

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/httplib2/__init__.pyc in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
   1349             auth.request(method, request_uri, headers, body)
   1350 
-> 1351         (response, content) = self._conn_request(conn, request_uri, method, body, headers)
   1352 
   1353         if auth:

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/httplib2/__init__.pyc in _conn_request(self, conn, request_uri, method, body, headers)
   1270             try:
   1271                 if hasattr(conn, 'sock') and conn.sock is None:
-> 1272                     conn.connect()
   1273                 conn.request(method, request_uri, body, headers)
   1274             except socket.timeout:

/Users/catherinekuhn/miniconda2/envs/ee-python/lib/python2.7/site-packages/httplib2/__init__.pyc in connect(self)
   1073             break
   1074         if not self.sock:
-> 1075             raise socket.error, msg
   1076 
   1077 SCHEME_TO_CONNECTION = {

error: [Errno 65] No route to host

Run Tyler's ever-useful interactive widget to enable map interactive zoomable map visualization of our data.


In [5]:
%run 'define_google_maps_interactive_widget.ipynb'


Call in our Dartmouth Flood Observatory training imagery and prepare it for visualization. Use the print function to make sure it is there:)


In [ ]:
Irene= ee.Image("users/kuhniculous/floodwithnoletters")
flood_img = (Irene.select('b1').eq(160))
flood_img2 = flood_img.mask(flood_img)
print(flood_img2.getInfo())

Use the map widget to display your imagery and celebrate!


In [ ]:
map = GoogleMapsWidget(lat=41.14, lng=-73.83, zoom=10) # lat, lng and zoom are optional
display(map)

vis_params1 = {'palette': 'f64104', 'bands':'b1','min':0,'max':1}

map.addLayer(image=flood_img2, vis_params=vis_params1, name='NY DFO Flood Image')

This function from ee_utils will isolate pixels classified as open water by the National Land Cover Dataset


In [18]:
NLCD2011 = ee.Image("NLCD2011");
water_features_ = NLCD2011.select('landcover').eq(11);
water_features = water_features_.mask(water_features_)

Get the percent imperviousness for each pixel from the 2011 National Land Cover Dataset.


In [21]:
impervious = NLCD2011.select('impervious')

Call elevation (10 m) from the USGS National Elevation Dataset.


In [22]:
elevation = ee.Image("USGS/NED");

Filter the NLCD to visualize just cultivated cropland


In [20]:
crops = NLCD2011.select('landcover').eq(82);
crops = crops.mask(crops)

Interactive Mapping

Define palettes to display imagery


In [ ]:
print('UPPER MISSISSIPPI RIVER BASIN')

# Call the map widget and center the map
map = GoogleMapsWidget(lat=41.14, lng=-73.83, zoom=10) # lat, lng and zoom are optional
display(map)


# Define palettes for landscape characteristics
h20pal = {'min': 0, 'max': 1, 'palette': ','.join(["000000","0000FF"])}
imppal = {'min': 0, 'max': 100, 'palette': ','.
          join(["66ffff","00ff00", "ffff00", "ff3300", "ff0000"])}  
elpal = {'min': 0, 'max': 600, 'palette': ','.join(['000000','ffffff'])} 
croppal = {'min': 0, 'max': 1, 'palette': ','.join(["000000","76EE00"])}


# Display landscape characteristics
map.addLayer(image = water_features, vis_params=h20pal, name = 'Permanent Water' )
map.addLayer(image = impervious, vis_params=imppal, name ='Impervious Surface (%)')
map.addLayer(image = elevation, vis_params = elpal, name = 'Elevation (m)')  
map.addLayer( image = crops, vis_params = croppal, name = 'Cultivated Crops')