Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Trend analysis

Packages

Set up map window

In this exercise, we are going to map trends. First, we define a region, a coordinate reference system crs, and a scale(pixel size) or affine transformation crsTransform.

Loading...
Loading...
Loading...

Visualization parameters

Loading...

Sentinel-2 data

The code block below filters the Sentinel-2 collection, masks clouds and calculats NDVI. The cloud masking approach uses the AI-based Cloud Score+ available in the Cloud Score+ Harmonized-S2 dataset. The Sentinel-2 harmonized dataset fixes different scaling factors in the original Sentinel-2 dataset.

Loading...
Loading...
Loading...

Harmonic function

In this exercise, I fit a first-order harmonic model of the form:

y^(t)=β0+β1t+β2cos(2πtT)+β3sin(2πtT)\hat{y}(t) = \beta_0 + \beta_1 t + \beta_2 cos(2\pi \frac{t}{T}) + \beta_3 sin(2\pi \frac{t}{T})

The harmonic model is fitted to every pixel, i.e., we will estimate the parameters β0\beta_0, β1\beta_1, β2\beta_2, and β3\beta_3 for every pixel. The β\beta´s are parameters for tt and different transformations of tt, where tt is time in days or (fractional) years and TT is the period. The period TT describes how many tt it takes to complete a full harmonic cylce. For example, TT is 1 if tt is in years and 365 if tt is in days. The model estimates y^\hat{y}, which is the NDVI in our case.

The first step is to assemble the response (NDVI) and predictor variables. The function addPredictorVariables() adds four bands to an NDVI image. The four bands are the intercept and the predictors, i.e., tt and its harmonic tranformations. The band constant has the pixel value 1 and is required to fit an intercept. The band time, associated with parameter β1\beta_1, is time tt expressed in years since January 1st 1970. It is a fractional value with decimal places. Now, the predictor variables do not vary for a specific NDVI image, i.e., tt is constant for a specific image. However, recall that we fit time series to individual pixels. You also see that we can create an image with a scalar value simply by calling ee.Image(scalar). Earth engine will expand the scalar value in space as needed, i.e., defined by the output region.

Loading...

Fit model

Now, we can estimate the β\beta´s using linear regression. The reducer is not aware of variable names and their order in the image. It assumes the last variable to be the response variable. So, we make sure that NDVI (responseName) is selected last.

The output of the regression reduction (trend) contains two bands: coefficients and residuals. However, trend is an array image and not a standard image. An array image can store an entire vector/array in each pixel not just a scalar value. For example, an array image can store a six-band image in a single band, where each pixel would contain a vector of length six. However, pixels in an array image do not have to store vectors of the same length. In our case, the coefficients band stores the coefficients in a 4x1 array in each pixel. To turn this into a four band image, we need to apply arrayProject (removes the second axis of length 1) and arrayFlatten (puts every array element into its own band).

Loading...

Visualizing the coefficients reveals different patterns in seasonality and trend (time) for different land cover types. The harmonic model compresse the NDVI time series into a few parameters that still represent the spectral-temporal dynamics. NOTE: the visualization of the map can take a while. The pixelwise trend fitting is processing intenstive. Keep the zoom level steady. The process restarts every time you zoom in or out.

Loading...

Fitted values

We can obtain the fitted NDVI values to better understand how our models fits the data. To do this, we need to write a funciton that takes a training image (containg NDVI, tt and the harmonic terms) and applies the estimated coefficients. We then map that function to the training collection to obtain a collection of fitted NDVI images.

Loading...

Pixel observations

Let’s evaluate the model fit for individual pixel observations. To visualize these pixels, we first extract the NDVI time series for a single pixel. Second, we extract the coefficients. And third, we extract the fitted values.

Extract NDVI time series

Loading...
Loading...
[['id', 'longitude', 'latitude', 'time', 'ndvi'], ['20180721T102021_20180721T102024_T32UQD', 13.184850310654017, 53.06195679914768, 1532168424461, 0.8832853025936599], ['20180721T102021_20180721T102024_T33UUU', 13.184850310654017, 53.06195679914768, 1532168424461, 0.8791130185979972]]
Loading...
Loading...

The dataset may contain duplicate values due to the overlapping Sentinel-2 tiles. That is, there should only be one observation for a given day. So, let’s drop duplicate values.

Loading...
Loading...

Now we can plot column ndvi against the dataframe index, which is date.

Loading...
<Figure size 500x300 with 1 Axes>

Extract coefficients

We can extract the coefficients of the harmonic model for a single pixel using a reducer. Coefficients is an ee.Image, which has the sampleRegions reducer. You may get an exception “User memory limit exceeded”. To avoid this error, you can limit the processed area size or save intermediate results to your assets.

Loading...
Loading...

Extract fitted values

Let’s extract the fitted NDVI values for a single pixel. Note, fitted is an ee.ImageCollection, which we can reduce with getRegion(). The output is a list that we can turn into a Pandas DataFrame for further analysis.

Loading...
Loading...

Visualize extracted values

Loading...
<Figure size 600x400 with 1 Axes>

Make predictions

Loading...
Loading...
Loading...
<Figure size 600x400 with 1 Axes>

Further reading