2

I'm using Earth Engine with Python, and while I finally managed to find an answer here that helped me troubleshoot my problem, I am not sure why it worked, and that makes me concerned that I may be misunderstanding some core element of the EE object that will come to bite me in the ass further down the line.

So here is my homegrown, non functional code:

pt = coords[1]
point = ee.Geometry.Point([pt[0], pt[1]])
start = ee.Date(pt[2])
finish = ee.Date(pt[3])

band_arrs = ee.ImageCollection('LANDSAT/LC08/C01/T1') \
    .filterBounds(point) \
    .filterDate(start,  finish).toBands()
band_arr_b4 = band_arrs.get('B4')
band_arr_b5 = band_arrs.get('B5')
band_arr_b6 = band_arrs.get('B6')
np_arr_b4 = np.array(band_arr_b4.getInfo())
np_arr_b5 = np.array(band_arr_b5.getInfo())
np_arr_b6 = np.array(band_arr_b6.getInfo())
feature = ee.Feature(ee.Geometry.Point([pt[0], pt[1]]));

band_arr_b6.getInfo()

this code returns a correct image object, but attempting to get any of the bands returns a None object. Casting to a feature did not help either, though I imagine that was to be expeced.

and here is the code I got and modified from this link that does work:

region = ee.Geometry.Point([37.3634, 8.8481])
image = ee.ImageCollection("LANDSAT/LC08/C01/T1").filterBounds(region).filterDate('2019-01-01', '2019-02-28').first()


imagereduce = image.reduceRegion(**{
    'reducer': ee.Reducer.toList(),
    'geometry': region,
    'scale': 30
 })


print(imagereduce.get('B1').getInfo())

Does this mean that the image object doesn't carry any band information and only carries metadata? Is the reduction a necessary element, or is it that I'm trying to achieve an outcome with EE that is counter-intuitive? My ultimate goal with this is to extract band information for a series of points of interests. Is there a better way to achieve that than looping through every point via this method?

1 Answer 1

1

Regarding your own code, you are getting None returned for the bands because there are no bands called 'B4', 'B5' etc. When you convert your image collection to an image with toBands(), the bands are named with the original image names concatenated with the band names.

enter image description here

To extract band information for several points, you can use a FeatureCollection:

pointFC = ee.FeatureCollection([ee.Geometry.Point([37.3534, 8.8381]),
                                ee.Geometry.Point([37.3634, 8.8481])])

col = (ee.ImageCollection("LANDSAT/LC08/C01/T1")
        .filterBounds(pointFC)
        .filterDate('2019-01-01', '2019-02-28'))

image = col.first()

sampledPoints = image.reduceRegions(**{
    'reducer': ee.Reducer.first(),
    'collection': pointFC,
    'scale': 30
 })
 
print (sampledPoints.getInfo()['features'])
2
  • Thank you so much, this makes a lot of sense! I have one more question: is it needed to call the filterbounds in the Imagecollection object, if I do give the region to the imagereducer?
    – Soheil
    Commented Jan 12, 2022 at 17:55
  • It is not needed per se, but I think filtering by bounds would probably reduce your memory footprint for subsequent processing. I am not 100% sure, hopefully someone else can offer a more definitive comment!
    – Matt
    Commented Jan 12, 2022 at 18:02

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.