3

I am trying to extract values from an ImageCollection for a set of coordinates in Google Earth Engine using the Python API.

First, I set up my EE environment:

import ee
import pprint
ee.Authenticate()
ee.Initialize()

Then I load the image collection and points. Pts is intended to be read into a table with latitude, longitude, and siteID as columns.

img = ee.ImageCollection('UMT/NTSG/v2/MODIS/NPP')
pts = ee.FeatureCollection('users/name/sites_unique.csv');

The image collection contains many images, each representing a year. I would like to iterate through each year and extract the associated NPP (band: ‘annual_npp’) for each point in pts. I anticipate this looking like (pseudocode):

For year in [2010, 2011, 2012, 2013]:
    img_selection = img.select(year = yr)
    output = img_selection.sample(pts, 1000).get('annual_npp').getInfo()

Does anyone know a generalizable way to extract this information from an ImageCollection?

Edit I get something close to the intended outcome in Earth Engine using the following:

var dataset = ee.ImageCollection('UMT/NTSG/v2/MODIS/NPP')
                  .filter(ee.Filter.date('2010-01-01', '2013-01-01'));
var npp = dataset.select('annualNPP').mean();
var out = npp.select(['annualNPP']).sampleRegions({
  collection: pts,
  properties: ['SiteID'],
  geometries: true,
  scale: 1000
})

1 Answer 1

1

Maybe something like this?

def sample_point(point, image):
    return image \
        .select(['annualNPP']) \
        .sample(
            region=point.geometry(),
            scale=1000, 
            numPixels=1
        ) \
        .first() \
        .set('year', image.date().get('year')) \
        .set('point', point.geometry().coordinates())


def sample_image(image):
    return pts.map(lambda point: sample_point(point, image))


img = ee.ImageCollection('UMT/NTSG/v2/MODIS/NPP')
pts = ee.FeatureCollection([
    ee.Feature(ee.Geometry.Point([-100, 35])),
    ee.Feature(ee.Geometry.Point([-100, 40]))
])

dataset = ee.ImageCollection('UMT/NTSG/v2/MODIS/NPP') \
    .filter(ee.Filter.date('2010-01-01', '2013-01-01'))

nppFeatures = dataset \
    .map(sample_image) \
    .flatten()

npp = [feature.get('properties') for feature in nppFeatures.getInfo().get('features')]

pp = pprint.PrettyPrinter()
pp.pprint(npp)
1
  • Perfect, thank you Daniel!
    – Ben
    Nov 29, 2021 at 23:33

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.