4

I'm trying to adapt this example for my needs: https://mygeoblog.com/2017/10/06/from-gee-to-numpy-to-geotiff/

The difference is that I'm using the MODIS NDVI image collection.

# define the image
img = ee.ImageCollection('MODIS/MCD43A4_006_NDVI').filterDate(date_start, date_end)

median_data = img.median()

# get the lat lon and add the ndvi
latlon = ee.Image.pixelLonLat().addBands(median_data)

# apply reducer to list
latlon = latlon.reduceRegion(
  reducer=ee.Reducer.toList(),
  geometry=area,
  scale=10000);


# get data into three different arrays
data = np.array((ee.Array(latlon.get("NDVI")).getInfo()))
lats = np.array((ee.Array(latlon.get("latitude")).getInfo()))
lons = np.array((ee.Array(latlon.get("longitude")).getInfo()))

I thought I understood the example but my data, lats and lons arrays are coming out different sizes which is obviously incorrect.

In [2]: data.shape
Out[2]: (21005,)

In [3]: lats.shape
Out[3]: (21051,)

In [4]: lons.shape
Out[4]: (21051,)

Any ideas what I'm doing wrong?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Rob
  • 275
  • 2
  • 11

1 Answers1

5

This works for me:

import ee 
import numpy as np

ee.Initialize()

date_start = ee.Date('2004-05-01')
end_date = ee.Date('2004-06-12')

area = ee.FeatureCollection(r'users/bcoerver/gadm36_PSE_0')

# define the image
img = ee.ImageCollection('MODIS/MCD43A4_006_NDVI').filterDate(date_start, end_date)

median_data = img.median()

# get the lat lon and add the ndvi
latlon = ee.Image.pixelLonLat().addBands(median_data)

# apply reducer to list
latlon = latlon.reduceRegion(
  reducer=ee.Reducer.toList(),
  geometry=area,
  scale=100);

# get data into three different arrays
data = np.array((ee.Array(latlon.get("NDVI")).getInfo()))
lats = np.array((ee.Array(latlon.get("latitude")).getInfo()))
lons = np.array((ee.Array(latlon.get("longitude")).getInfo()))

output:

lons.size
Out[17]: 734995

lats.size
Out[18]: 734995

data.size
Out[19]: 734995

But maybe you can specify which dates and area you are using?

Bert Coerver
  • 1,945
  • 10
  • 22