Kamoriq
Kamoriq

Reputation: 3

Output value of a specific latitude and longitude point at Python

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"
})

Upvotes: 0

Views: 1009

Answers (2)

Justin Braaten
Justin Braaten

Reputation: 761

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.

Upvotes: 1

Neda Peyrone
Neda Peyrone

Reputation: 350

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

Upvotes: 0

Related Questions