Google Earth Engine Integration¶
This module provides functions to query and export arbitrarily large images from Google Earth Engine (GEE). It handles the complexity of tiling large exports and provides convenient query functions for common satellite datasets.
Overview¶
The GEE integration includes:
- Export functions (
georeader.readers.ee_image): Export single images or time-series cubes - Query functions (
georeader.readers.ee_query): Search for Sentinel-1, Sentinel-2, and Landsat imagery
Prerequisites¶
import ee
ee.Authenticate()
ee.Initialize()
Quick Start¶
from georeader.readers import ee_image, ee_query
from shapely.geometry import box
from datetime import datetime
import ee
# Define area of interest
aoi = box(-122.5, 37.5, -122.0, 38.0)
# Query available Sentinel-2 images
images = ee_query.query(aoi, datetime(2023, 1, 1), datetime(2023, 12, 31),
producttype="S2_SR")
# Export a single image
gt = ee_image.export_image(images[0], aoi, scale=10)
# Export a time-series cube
cube = ee_image.export_cube(images[:5], aoi, scale=10)
Key Functions¶
ee_image module¶
| Function | Description |
|---|---|
export_image |
Export a single GEE image to GeoTensor |
export_cube |
Export multiple images as a 4D GeoTensor (time, bands, y, x) |
ee_query module¶
| Function | Description |
|---|---|
query |
Query Sentinel-2 or Landsat-8/9 image collection |
query_s1 |
Query Sentinel-1 SAR imagery |
query_landsat_457 |
Query Landsat 4, 5, 7 imagery |
Google Earth Engine Image Export Module.
This module provides functions for exporting raster data from Google Earth Engine (GEE) to GeoTensor objects. It handles the complexity of GEE's size limits through recursive tile splitting and parallel downloads.
Architecture Overview¶
::
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β GEE EXPORT WORKFLOW β
β β
β User Request β
β ββββββββββββββββββββ β
β β export_image() β β
β β - ee.Image β β
β β - geometry β β
β β - bands β β
β ββββββββββ¬ββββββββββ β
β β β
β βΌ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Try ee.data.computePixels() / ee.data.getPixels() β β
β β (Limited to ~32MB per request) β β
β ββββββββββ¬ββββββββββββββββββββββββββββββββββ¬ββββββββββββββββ β
β β Success β "Total request size" β
β βΌ βΌ error β
β ββββββββββββββββββββ ββββββββββββββββββββββββββββ β
β β Return GeoTensor β β RECURSIVE TILE SPLITTING β β
β ββββββββββββββββββββ β β β
β β ββββββ¬βββββ β β
β β β Q1 β Q2 β Split into β β
β β ββββββΌβββββ€ 4 quadrants β β
β β β Q3 β Q4 β β β
β β ββββββ΄βββββ β β
β β β β β
β β βΌ β β
β β Process each in β β
β β parallel (ThreadPool) β β
β β β β β
β β βΌ β β
β β spatial_mosaic() β β
β β to combine tiles β β
β ββββββββββββββββββββββββββββ β
β β β
β βΌ β
β ββββββββββββββββββββ β
β β Return GeoTensor β β
β ββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Size Limits¶
Google Earth Engine has request size limits (~32MB for computePixels). This module automatically handles large requests by:
- Attempting the full request first
- On "Total request size" error, splitting geometry into quadrants
- Recursively processing each quadrant (may split further if needed)
- Mosaicking results with
spatial_mosaic()
This enables downloading arbitrarily large areas, limited only by time and memory.
Key Functions¶
export_image Main function for exporting a single ee.Image or asset to GeoTensor. Handles recursive splitting automatically.
export_cube Export multiple images (time series) from a query DataFrame. Returns 4D GeoTensor (time, band, y, x).
interpolate_20mbands_s2ee Fix GEE's nearest-neighbor interpolation of Sentinel-2 20m bands when downloaded at 10m resolution.
Usage Examples¶
Basic export from GEE::
import ee
from georeader.readers.ee_image import export_image
from shapely.geometry import box
ee.Initialize()
# Define area of interest (WGS84)
aoi = box(-122.5, 37.7, -122.3, 37.9) # San Francisco Bay
# Get Sentinel-2 image
image = ee.Image('COPERNICUS/S2_SR_HARMONIZED/20230615T184919_20230615T185823_T10SEG')
# Define output grid (UTM Zone 10N, 10m resolution)
from rasterio import Affine
transform = Affine(10, 0, 550000, 0, -10, 4200000)
# Export RGB bands
gt = export_image(
image,
geometry=aoi,
transform=transform,
crs="EPSG:32610",
bands_gee=["B4", "B3", "B2"]
)
print(gt.shape) # (3, H, W)
Export time series::
from georeader.readers.ee_query import query_s2_ee
from georeader.readers.ee_image import export_cube
# Query Sentinel-2 images
query = query_s2_ee(
aoi,
date_start="2023-06-01",
date_end="2023-06-30",
cloud_cover_max=20
)
# Download all images
cube = export_cube(
query,
geometry=aoi,
bands_gee=["B4", "B3", "B2", "B8"],
display_progress=True
)
print(cube.shape) # (N_images, 4, H, W)
Fix 20m band interpolation::
from georeader.readers.ee_image import interpolate_20mbands_s2ee
# GEE uses nearest-neighbor for 20mβ10m, causing blocky artifacts
# This function applies proper bilinear interpolation
gt_fixed = interpolate_20mbands_s2ee(gt, channels_query_original=["B4","B5","B6"])
Notes¶
- Requires
earthengine-apipackage:pip install earthengine-api - Must call
ee.Initialize()before using these functions - Large areas may take significant time due to recursive splitting
- Consider using
ee.batch.Exportfor very large areas (async)
See Also¶
georeader.readers.ee_query : Query image collections georeader.mosaic.spatial_mosaic : Combine tiles into single raster georeader.readers.S2_SAFE_reader : Direct Sentinel-2 SAFE file access
References¶
- GEE Python API: https://developers.google.com/earth-engine/guides/python_install
- GEE Data Catalog: https://developers.google.com/earth-engine/datasets
export_image(image_or_asset_id, geometry, transform, crs, bands_gee, dtype_dst=None, pad_add=(0, 0), crs_polygon='EPSG:4326', resolution_dst=None, timeout=DEFAULT_EE_TIMEOUT)
¶
Exports an image from the GEE as a GeoTensor.
It uses the ee.data.getPixels or ee.data.computePixels method to export the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_or_asset_id
|
Union[str, Image]
|
Name of the asset or ee.Image object. |
required |
geometry
|
Union[Polygon, MultiPolygon]
|
geometry to export |
required |
transform
|
Affine
|
transform of the geometry |
required |
crs
|
str
|
crs of the geometry |
required |
pad_add
|
Tuple[int, int]
|
pad in pixels to add to the resulting |
(0, 0)
|
bands_gee
|
List[str]
|
List of bands to export |
required |
crs_polygon
|
str
|
crs of the geometry. Defaults to "EPSG:4326". |
'EPSG:4326'
|
timeout
|
float
|
Maximum time to wait for calling the export method. Defaults to 120 seconds. |
DEFAULT_EE_TIMEOUT
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
GeoTensor
|
GeoTensor object |
Source code in georeader/readers/ee_image.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
export_cube(query, geometry, transform=None, crs=None, dtype_dst=None, bands_gee=None, crs_polygon='EPSG:4326', display_progress=True)
¶
Download all images in the query that intersects the geometry.
Note: This function is intended for small areas. If the area is too big that there are several images per day that intesesects the geometry, it will not group the images by day.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
GeoDataFrame
|
dataframe from |
required |
geometry
|
Union[Polygon, MultiPolygon]
|
geometry to export |
required |
transform
|
Optional[Affine]
|
transform of the geometry. If None it will use the transform of the first image translated to the geometry. Defaults to None. |
None
|
crs
|
Optional[str]
|
crs of the geometry. If None it will use the crs of the first image. Defaults to None. |
None
|
dtype_dst
|
Optional[str]
|
dtype of the output GeoTensor. Defaults to None. |
None
|
bands_gee
|
Optional[List[str]]
|
List of bands to export. If None it will use the bands_gee column in the query. Defaults to None. |
None
|
crs_polygon
|
_type_
|
crs of the geometry. Defaults to "EPSG:4326". |
'EPSG:4326'
|
display_progress
|
bool
|
Display progress bar. Defaults to False. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
GeoTensor |
Optional[GeoTensor]
|
GeoTensor object with 4 dimensions: (time, band, y, x) |
Source code in georeader/readers/ee_image.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 | |
query(area, date_start, date_end, producttype='S2', filter_duplicates=True, return_collection=False, add_s2cloudless=False, extra_metadata_keys=None, timeout=DEFAULT_EE_TIMEOUT)
¶
Query Landsat and Sentinel-2 products from the Google Earth Engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
area
|
Union[MultiPolygon, Polygon]
|
area to query images in EPSG:4326 |
required |
date_start
|
datetime
|
datetime in a given timezone. If tz not provided UTC will be assumed. |
required |
date_end
|
datetime
|
datetime in UTC. If tz not provided UTC will be assumed. |
required |
producttype
|
str
|
'S2', "Landsat"-> {"L8", "L9"}, "both" -> {"S2", "L8", "L9"}, "S2_SR", "L8", "L9" |
'S2'
|
filter_duplicates
|
bool
|
Filter S2 images that are duplicated |
True
|
return_collection
|
bool
|
returns also the corresponding image collection |
False
|
add_s2cloudless
|
bool
|
Adds a column that indicates if the s2cloudless image is available (from collection COPERNICUS/S2_CLOUD_PROBABILITY collection) |
False
|
extra_metadata_keys
|
Optional[List[str]]
|
list of extra metadata keys to add to the geodataframe. |
None
|
timeout
|
float
|
Maximum time to wait for Earth Engine getInfo() calls (seconds). Defaults to DEFAULT_EE_TIMEOUT. |
DEFAULT_EE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
Union[GeoDataFrame, Tuple[GeoDataFrame, ImageCollection]]
|
geodataframe with available products in the given area and time range |
Union[GeoDataFrame, Tuple[GeoDataFrame, ImageCollection]]
|
if |
Source code in georeader/readers/ee_query.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
query_s1(area, date_start, date_end, filter_duplicates=True, return_collection=False, timeout=DEFAULT_EE_TIMEOUT)
¶
Query Sentinel-1 products from the Google Earth Engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
area
|
MultiPolygon or Polygon
|
Area to query images in EPSG:4326. |
required |
date_start
|
datetime
|
Start date for query (timezone-aware or UTC assumed). |
required |
date_end
|
datetime
|
End date for query (timezone-aware or UTC assumed). |
required |
filter_duplicates
|
bool
|
Filter duplicate images over the same area. Defaults to True. |
True
|
return_collection
|
bool
|
If True, also returns the corresponding ee.ImageCollection. Defaults to False. |
False
|
timeout
|
float
|
Maximum time to wait for Earth Engine getInfo() calls (seconds). Defaults to DEFAULT_EE_TIMEOUT. |
DEFAULT_EE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
Union[GeoDataFrame, Tuple[GeoDataFrame, ImageCollection]]
|
Union[gpd.GeoDataFrame, Tuple[gpd.GeoDataFrame, ee.ImageCollection]]: GeoDataFrame of available products, optionally with the image collection. |
Source code in georeader/readers/ee_query.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | |
query_landsat_457(area, date_start, date_end, producttype='all', filter_duplicates=True, return_collection=False, extra_metadata_keys=None, timeout=DEFAULT_EE_TIMEOUT)
¶
Query Landsat-7, Landsat-5 or Landsat-4 products from the Google Earth Engine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
area
|
Union[MultiPolygon, Polygon]
|
area to query images in EPSG:4326 |
required |
date_start
|
datetime
|
datetime in a given timezone. If tz not provided UTC will be assumed. |
required |
date_end
|
datetime
|
datetime in UTC. If tz not provided UTC will be assumed. |
required |
producttype
|
str
|
'all' -> {"L4", "L5", "L7"}, "L4", "L5" or "L7". Defaults to "all". |
'all'
|
filter_duplicates
|
bool
|
filter duplicate images over the same area. Defaults to True. |
True
|
return_collection
|
bool
|
returns also the corresponding image collection. Defaults to False. |
False
|
extra_metadata_keys
|
Optional[List[str]]
|
extra metadata keys to add to the geodataframe. Defaults to None. |
None
|
timeout
|
float
|
Maximum time to wait for Earth Engine getInfo() calls (seconds). Defaults to DEFAULT_EE_TIMEOUT. |
DEFAULT_EE_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
Union[GeoDataFrame, Tuple[GeoDataFrame, ImageCollection]]
|
Union[gpd.GeoDataFrame, Tuple[gpd.GeoDataFrame, ee.ImageCollection]]: geodataframe with available products in the given area and time range |
Source code in georeader/readers/ee_query.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |