Python Google Earth Engine – Transforming ee.ImageCollection to xr.Dataset in Colab

google-earth-enginegoogle-earth-engine-python-apilandsatxarray

I am trying to transform an ee.ImageCollection (Landsat) for a Region of Interest into an xr.Dataset so I can further process the data and ultimately export as a .csv. I have tried using the library wxee. However, values in the resulting xarray only show for certain coordinates, I think because there seems to be a scaling_factor of 1 instead of the true scale of each spectral band (that may be nonsense, but I can't see an alternative). Can anybody help?

Here is the code I have written and an example of the output:

Packages

import geopandas as gpd
import json
import wxee
import ee

Authorise access needed by Earth Engine

ee.Authenticate()
ee.Initialize()

Read in shapefile defining Region of Interest

shapefile = gpd.read_file('/Path/To/Shape')
js = json.loads(shapefile.to_json())
roi = ee.Geometry(ee.FeatureCollection(js).geometry())

Import Landsat 8 and 9 collections

# Filter by Region of Interest 

l8 = (ee.ImageCollection('LANDSAT/LC08/C02/T1_L2').filterDate('2013-03-01', '2023-09-30') 
                                                   .filterBounds(roi) 
                                                   .select(['SR_B.']))
l9 = (ee.ImageCollection('LANDSAT/LC09/C02/T1_L2').filterDate('2021-10-31', '2023-09-30') 
                                                   .filterBounds(roi) 
                                                   .select(['SR_B.']))

# Merge the collections
merged_collection = l8.merge(l9)

Transform to xarray using wx.to_xarray()

ds = merged_collection.wx.to_xarray()

Output
enter image description here

Best Answer

Assuming you wanted to download the images over your roi at a 30m scale, you should use:

ds = merged_collection.wx.to_xarray(
    region=roi,
    scale=30,
)

When those parameters aren't provided, wxee tries to use the scale and geometry of the collection, which doesn't always work. In this case, it downloaded each image with only 6x5 pixels, which suggests that the scale was very large.

Depending on the size of roi, the code above may cause an error due to the maximum pixel limitations of Earth Engine, in which case you would need to use a larger scale or a smaller roi. If that doesn't work, you could also check out the Xee package, which is more flexible with allowing you to process data without downloading the entire collection.

Related Question