Search code examples
pythonpython-2.7sentinelgoogle-earth-engine

METADATA of images from earth-engine by Python 2.7


I'm trying to get information about the picture, the state of the clouds, the angle of the sun, and any other information I can get.

I'm trying to get a METADATA on the pictures..

To illustrate, I use CLOUD_COVER for the percentage of clouds but I do not get any numerical value.

My Code:

import ee
import ee.mapclient

ee.Initialize()
# Get a download URL for an image.
image1 = ee.Image('COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB');

#Get information about the bands as a list.
bandNames = image1.bandNames();
print ("Band names: " + str(bandNames)) #ee.List of band names

#Get projection information from band 1
b1proj = image1.select('B1').projection()
print('Band 1 projection: ' + str(b1proj))#ee.Projection object

#Get scale (in meters) information from band 1.
b1scale = image1.select('B1').projection().nominalScale()
print('Band 1 scale: ' + str(b1scale))#ee.Number

#Note that different bands can have different projections and scale.
b8scale = image1.select('B8').projection().nominalScale()
print('Band 8 scale: ' + str(b8scale))#ee.Number

#Get a list of all metadata properties.
properties = image1.propertyNames()
print('Metadata properties: ' + str(properties))#ee.List of metadata properties

#Get a specific metadata property.
cloudiness = image1.get('CLOUD_COVER')
print('CLOUD_COVER: ' + str(cloudiness))#ee.Number

Here's the output:

Band names: ee.List({
  "type": "Invocation", 
  "arguments": {
    "image": {
      "type": "Invocation", 
      "arguments": {
        "id": "COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB"
      }, 
      "functionName": "Image.load"
    }
  }, 
  "functionName": "Image.bandNames"
})
Band 1 projection: ee.Projection({
  "type": "Invocation", 
  "arguments": {
    "crs": {
      "type": "Invocation", 
      "arguments": {
        "image": {
          "type": "Invocation", 
          "arguments": {
            "bandSelectors": [
              "B1"
            ], 
            "input": {
              "type": "Invocation", 
              "arguments": {
                "id": "COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB"
              }, 
              "functionName": "Image.load"
            }
          }, 
          "functionName": "Image.select"
        }
      }, 
      "functionName": "Image.projection"
    }
  }, 
  "functionName": "Projection"
})
Band 1 scale: ee.Number({
  "type": "Invocation", 
  "arguments": {
    "proj": {
      "type": "Invocation", 
      "arguments": {
        "crs": {
          "type": "Invocation", 
          "arguments": {
            "image": {
              "type": "Invocation", 
              "arguments": {
                "bandSelectors": [
                  "B1"
                ], 
                "input": {
                  "type": "Invocation", 
                  "arguments": {
                    "id": "COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB"
                  }, 
                  "functionName": "Image.load"
                }
              }, 
              "functionName": "Image.select"
            }
          }, 
          "functionName": "Image.projection"
        }
      }, 
      "functionName": "Projection"
    }
  }, 
  "functionName": "Projection.nominalScale"
})
Band 8 scale: ee.Number({
  "type": "Invocation", 
  "arguments": {
    "proj": {
      "type": "Invocation", 
      "arguments": {
        "crs": {
          "type": "Invocation", 
          "arguments": {
            "image": {
              "type": "Invocation", 
              "arguments": {
                "bandSelectors": [
                  "B8"
                ], 
                "input": {
                  "type": "Invocation", 
                  "arguments": {
                    "id": "COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB"
                  }, 
                  "functionName": "Image.load"
                }
              }, 
              "functionName": "Image.select"
            }
          }, 
          "functionName": "Image.projection"
        }
      }, 
      "functionName": "Projection"
    }
  }, 
  "functionName": "Projection.nominalScale"
})
Metadata properties: ee.List({
  "type": "Invocation", 
  "arguments": {
    "element": {
      "type": "Invocation", 
      "arguments": {
        "id": "COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB"
      }, 
      "functionName": "Image.load"
    }
  }, 
  "functionName": "Element.propertyNames"
})
CLOUD_COVER: ee.ComputedObject({
  "type": "Invocation", 
  "arguments": {
    "property": "CLOUD_COVER", 
    "object": {
      "type": "Invocation", 
      "arguments": {
        "id": "COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB"
      }, 
      "functionName": "Image.load"
    }
  }, 
  "functionName": "Element.get"
})

The problem is that the output has no information. Can anyone explain why ?


Solution

  • This is due to how GEE works. Processing steps are constructed locally as objects and then only evaluated by the server once another function requires it. So when you use python's print function, it will just print the json obect which will be sent to the GEE server for evaluation.

    You can force evaluation with .getInfo() ... this however should be used with caution, because everything is pulled to the client side, which can be problematic with big objects.

    So this works:

    import ee
    ee.Initialize()
    
    image1 = ee.Image('COPERNICUS/S2_SR/20190205T082129_20190205T082130_T36SYB')
    
    bandNames = image1.bandNames()
    
    print(bandNames.getInfo())
    

    [u'B1', u'B2', u'B3', u'B4', u'B5', u'B6', u'B7', u'B8', u'B8A', u'B9', u'B11', u'B12', u'AOT', u'WVP', u'SCL', u'TCI_R', u'TCI_G', u'TCI_B', u'MSK_CLDPRB', u'MSK_SNWPRB', u'QA10', u'QA20', u'QA60']

    This section of the documentation explains the background.

    EDIT:

    Of course, this can be extended to all metadata properties if you use get and the respective property name.

    For example, this is how you get the percentage of cloudy pixels:

    cloudiness = image1.get("CLOUDY_PIXEL_PERCENTAGE").getInfo()
    print(cloudiness)
    

    0.664195