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.
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()
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]:
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}))
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)
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')