I can't replicate the error. However, there are a number of other problems in your script, which could be confounding the issue.
- replace
.aAnd
with .And
- replace
cloudsBitMask
with cloudBitMask
vizParams
needs to be {'bands':['B4','B5','B2'], 'min': 0, 'max': 3000, 'gamma': 1.4}
Map.setCenter()
and Map.addLayer()
do not work with the Python API - use Folium.
-More guidance on Python syntax
-More information on map display with Folium
You do not show how you are installing/importing Earth Engine. It is possible that you are using !pip install earthengine-api
and then !earthengine authenticate
, which is unnecessary now in Colab, and may be causing the error. Earth Engine is now installed in Colab by default, so you only need to add the following to the top of your script:
import ee
ee.Authenticate()
ee.Initialize()
Please try this script in Colab and report whether you get the same error.
import ee
import folium
ee.Authenticate()
ee.Initialize()
# Define a cloud masking function.
def maskL8sr(image):
cloudShadowBitMask = 1<<3
cloudBitMask = 1<<5
qa = image.select('pixel_qa')
mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).And(qa.bitwiseAnd(cloudBitMask).eq(0))
return image.updateMask(mask)
# Get an example Landsat 8 surface reflectance image that has been cloud masked.
# Print the number of bands - a trivial test to see that the code block does
# not throw any errors.
l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') \
.filterBounds(ee.Geometry.Point(-121.74, 37.5)) \
.filterDate('2015-01-01', '2017-12-31').map(maskL8sr)
exampleImg = l8sr.first()
print(exampleImg.bandNames().getInfo())
# Define a method for displaying Earth Engine image tiles to folium map.
def add_ee_layer(self, ee_image_object, vis_params, name):
map_id_dict = ee.Image(ee_image_object).getMapId(vis_params)
folium.raster_layers.TileLayer(
tiles = map_id_dict['tile_fetcher'].url_format,
attr = "Map Data © Google Earth Engine",
name = name,
overlay = True,
control = True
).add_to(self)
# Add EE drawing method to folium.
folium.Map.add_ee_layer = add_ee_layer
# Set visualization parameters.
vis_params = {'bands':['B4','B5','B2'], 'min': 0, 'max': 3000, 'gamma': 1.4}
# Create a folium map object.
my_map = folium.Map(location=[36.8, -121.74], zoom_start=8, height=500)
# Add the exampleImg model to the map object.
my_map.add_ee_layer(exampleImg, vis_params, 'ExampleImg')
# Add a layer control panel to the map.
my_map.add_child(folium.LayerControl())
# Display the map.
display(my_map)