0

Due to my shorten explanation, I edit this question at 2020/01/20. Sorry for answerer of original.

I want the TOA reflectance value of a specific latitude / longitude band. I could do this with javascript by below code, but I could not do the same with python. I would be glad for your future help.

var start = '2018-01-01'
var end = '2018-01-16'
var lon =  134.01
var lat = 34.04
var p = ee.Geometry.Point(lon, lat) 

var imageCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
  .filterDate(start, end) 
  .filterBounds(p) 

var im1 = imageCollection
  .first()
var image = ee.Image(im1);

// Extract the data 
var data_b1 = im1.select("B1").reduceRegion(ee.Reducer.mean(),p,10).get("B1")
print('B1 T1_TOA: ', data_b1)

Javascript output is this TOA reflectance:

B1 T1_TOA: 
0.25892749428749084

But at Python, it doesn't work well. Output is ComputedObject, but it does not contain TOA reflectance value.

import ee
ee.Initialize()

start = '2018-01-01'
end = '2018-01-16'
lon = 134.01
lat = 34.04
p = ee.Geometry.Point([lon, lat])

imageCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA').filterDate(start, end).filterBounds(p)
im1 = imageCollection.first()
data_b1 = im1.select("B1").reduceRegion(ee.Reducer.mean(),p,10).get("B1")

print(data_b1)

Output is below json, not including TOA reflectance value "0.25892749428749084":

ee.ComputedObject({
  "type": "Invocation",
  "arguments": {
    "dictionary": {
      "type": "Invocation",
      "arguments": {
        "image": {
          "type": "Invocation",
          "arguments": {
            "input": {
              "type": "Invocation",
              "arguments": {
                "collection": {
                  "type": "Invocation",
                  "arguments": {
                    "collection": {
                      "type": "Invocation",
                      "arguments": {
                        "collection": {
                          "type": "Invocation",
                          "arguments": {
                            "id": "LANDSAT/LC08/C01/T1_TOA"
                          },
                          "functionName": "ImageCollection.load"
                        },
                        "filter": {
                          "type": "Invocation",
                          "arguments": {
                            "rightField": "system:time_start",
                            "leftValue": {
                              "type": "Invocation",
                              "arguments": {
                                "start": "2018-01-01",
                                "end": "2018-01-16"
                              },
                              "functionName": "DateRange"
                            }
                          },
                          "functionName": "Filter.dateRangeContains"
                        }
                      },
                      "functionName": "Collection.filter"
                    },
                    "filter": {
                      "type": "Invocation",
                      "arguments": {
                        "leftField": ".all",
                        "rightValue": {
                          "type": "Invocation",
                          "arguments": {
                            "geometry": {
                              "type": "Point",
                              "coordinates": [
                                134.01,
                                34.04
                              ]
                            }
                          },
                          "functionName": "Feature"
                        }
                      },
                      "functionName": "Filter.intersects"
                    }
                  },
                  "functionName": "Collection.filter"
                }
              },
              "functionName": "Collection.first"
            },
            "bandSelectors": [
              "B1"
            ]
          },
          "functionName": "Image.select"
        },
        "reducer": {
          "type": "Invocation",
          "arguments": {},
          "functionName": "Reducer.mean"
        },
        "geometry": {
          "type": "Point",
          "coordinates": [
            134.01,
            34.04
          ]
        },
        "scale": 10
      },
      "functionName": "Image.reduceRegion"
    },
    "key": "B1"
  },
  "functionName": "Dictionary.get"
})
Kamoriq
  • 3
  • 2
  • What is the output of the program? Which error occured? – mrEvgenX Jan 17 '20 at 07:07
  • 1
    Welcome to Stack Overflow! People here are willing to help answer questions, but you need to provide a bit more information. For instance, what output do you get? What output do you expect? What else have you tried? What happens when you run this program line by line in the Python interactive interpreter? – Jim DeLaHunt Jan 17 '20 at 08:02
  • Just like in GEE's javascript console, with the python client you are just building a JSON object on the _client_ side. Nothing will happen until you submit it to the server for evaluation. In your case, you need to call `.getInfo()` on the final `data_b1` object. More info: [GEE concepts overview](https://developers.google.com/earth-engine/concepts_overview) – Val Jan 21 '20 at 09:00
  • Note: please direct future Earth Engine questions to [GIS Stack Exchange (google-earth-engine tag)](https://gis.stackexchange.com/questions/tagged/google-earth-engine) to help keep questions consolidated for easier discovery and engagement by the EE community. – Justin Braaten Jan 22 '20 at 01:19

2 Answers2

1

You need to convert the server-side object to a client-side object. Try:

print(data_b1.getInfo())

For more information on printing Earth Engine objects with the Python API see this guide.

Justin Braaten
  • 711
  • 7
  • 13
0

I follow your python code is working. First, I sign up to Google Earth Engine at this link https://earthengine.google.com. Then I install the earthengine-api package on Google Colab. When you execute the code you will see a message asking you to authorize access needed by Earth Engine. Please follow the instructions.

Python code:

import ee

# Trigger the authentication flow.
ee.Authenticate()

# Initialize the library.
ee.Initialize()

lat = 35.1000
lon = 135.2000
p = ee.Geometry.Point([lon, lat])
start = '2017-11-21'
end = '2017-12-30'
imageCollection = ee.ImageCollection('LANDSAT/LC08/C01/T1').filterDate(start, end).filterBounds(p)
im1 = imageCollection.sort('CLOUD_COVER', True).first()
data_b1 = im1.select("B1").reduceRegion(ee.Reducer.mean(),p,10).get("B1")

print(data_b1)

The example of a message prompt when executing the code.

enter image description here

The output result.

enter image description here

Neda Peyrone
  • 190
  • 1
  • 5