0

Am trying to use this ee.ImageCollection() method to load Landsat data using Google Colab. See code below:

def maskL8sr(image):
  cloudShadowBitMask = 1<<3
  cloudBitMask = 1<<5
  qa = image.select('pixel_qa')
  mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).aAnd(qa.bitwiseAnd(cloudsBitMask).eq(0))
  return image.updateMask(mask)

l8sr = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR').filterDate('2015-01-01', '2017-12-31').map(maskL8sr)
vizParams = {bands:['B4','B5','B2'], min: 0, max: 3000, gamma: 1.4,}
Map.setCenter(114.0079, -26.0765, 9)
Map.addLayer(l8sr.median(), vizParams)

But I keep getting this error:

imageCollectionerror

Is it a bug?

1
  • Could you add the error message as text to your question by clicking edit beneath the question? Feb 20, 2020 at 14:32

2 Answers 2

1

I can't replicate the error. However, there are a number of other problems in your script, which could be confounding the issue.

  1. replace .aAnd with .And
  2. replace cloudsBitMask with cloudBitMask
  3. vizParams needs to be {'bands':['B4','B5','B2'], 'min': 0, 'max': 3000, 'gamma': 1.4}
  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)
0

I have never seen this error before so I am spit balling here....It looks like you are getting this error because you do not have a cloud project set/created that Earth Engine can use. My understanding is that Earth Engine recently updated the backend to be integrated with Google Cloud Platform which needs a cloud project to run code.

You can fix this error by following these steps to create a new project. And then set the earthengine project in your colab notebook with this line code before you get started:

!earthengine set_project <PROJECT>

Change <PROJECT> to the project id you created in the cloud console (keep in mind the project name and project id can be different).

Cloud projects are free unless you use resources such a VM or storage which you need to explicitly enable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.