7. Exporting Earth Engine Data#

7.1. Introduction#

7.2. Technical requirements#

conda create -n gee python
conda activate gee
conda install -c conda-forge mamba
mamba install -c conda-forge geemap pygis
jupyter lab

Open in Colab

# pip install pygis
import os
import ee
import geemap
geemap.ee_initialize()

7.3. Exporting images#

Map = geemap.Map()

image = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318').select(
    ['B5', 'B4', 'B3']
)

vis_params = {'min': 0, 'max': 0.5, 'gamma': [0.95, 1.1, 1]}

Map.centerObject(image)
Map.addLayer(image, vis_params, 'Landsat')
Map
region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)
fc = ee.FeatureCollection(region)
style = {'color': 'ffff00ff', 'fillColor': '00000000'}
Map.addLayer(fc.style(**style), {}, 'ROI')
Map

7.3.1. To local drive#

geemap.ee_export_image(image, filename="landsat.tif", scale=30, region=region)
projection = image.select(0).projection().getInfo()
projection
crs = projection['crs']
crs_transform = projection['transform']
geemap.ee_export_image(
    image,
    filename="landsat_crs.tif",
    crs=crs,
    crs_transform=crs_transform,
    region=region,
)
geemap.download_ee_image(image, filename='landsat_full.tif', scale=60)
fishnet = geemap.fishnet(image.geometry(), rows=4, cols=4, delta=0.5)
style = {'color': 'ffff00ff', 'fillColor': '00000000'}
Map.addLayer(fishnet.style(**style), {}, 'Fishnet')
Map
out_dir = os.path.expanduser('~/Downloads')
geemap.download_ee_image_tiles(
    image, fishnet, out_dir, prefix="landsat_", crs="EPSG:3857", scale=30
)

7.3.2. To Google Drive#

geemap.ee_export_image_to_drive(
    image, description='landsat', folder='export', region=region, scale=30
)

7.3.3. To Asset#

assetId = 'landsat_sfo'
geemap.ee_export_image_to_asset(
    image, description='landsat', assetId=assetId, region=region, scale=30
)

7.3.4. To Cloud Storage#

bucket = 'your-bucket'
geemap.ee_export_image_to_cloud_storage(
    image, description='landsat', bucket=None, region=region, scale=30
)

7.3.5. To NumPy array#

region = ee.Geometry.BBox(-122.5003, 37.7233, -122.3410, 37.8026)
rgb_img = geemap.ee_to_numpy(image, region=region)
print(rgb_img.shape)
import matplotlib.pyplot as plt

rgb_img_test = (255 * ((rgb_img[:, :, 0:3]) + 0.2)).astype('uint8')
plt.imshow(rgb_img_test)
plt.show()

7.4. Exporting image collections#

point = ee.Geometry.Point(-99.2222, 46.7816)
collection = (
    ee.ImageCollection('USDA/NAIP/DOQQ')
    .filterBounds(point)
    .filterDate('2008-01-01', '2018-01-01')
    .filter(ee.Filter.listContains("system:band_names", "N"))
)
collection.aggregate_array('system:index').getInfo()

7.4.1. To local drive#

out_dir = os.path.expanduser('~/Downloads')
geemap.ee_export_image_collection(collection, out_dir=out_dir, scale=10)

7.4.2. To Google Drive#

geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)

7.4.3. To Assets#

geemap.ee_export_image_collection_to_asset(collection)

7.5. Exporting videos#

# Load a Landsat 5 image collection.
collection = (
    ee.ImageCollection('LANDSAT/LT05/C01/T1_TOA')
    .filter(ee.Filter.eq('WRS_PATH', 44))
    .filter(ee.Filter.eq('WRS_ROW', 34))
    .filter(ee.Filter.lt('CLOUD_COVER', 30))
    .filterDate('1991-01-01', '2011-12-30')
    .select(['B4', 'B3', 'B2'])
    .map(lambda img: img.multiply(512).uint8())
)
# Need to make the data 8-bit.

region = ee.Geometry.Rectangle([-122.7286, 37.6325, -122.0241, 37.9592]);
geemap.ee_export_video_to_dirve(
    collection, folder='export', framesPerSecond=12, dimensions=720, region=region
)

7.6. Exporting image thumbnails#

roi = ee.Geometry.Point([-122.44, 37.75])
collection = (
    ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
    .filterBounds(roi)
    .sort("CLOUD_COVER")
    .limit(10)
)

image = collection.first()
Map = geemap.Map()

vis_params = {
    'bands': ['B5', 'B4', 'B3'],
    'min': 0,
    'max': 0.3,
    'gamma': [0.95, 1.1, 1],
}

Map.addLayer(image, vis_params, "LANDSAT 8")
Map.setCenter(-122.44, 37.75, 8)
Map
out_img = 'landsat.jpg'
region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)
geemap.get_image_thumbnail(image, out_img, vis_params, dimensions=1000, region=region)
geemap.show_image(out_img)
out_dir = os.path.expanduser("~/Downloads")
geemap.get_image_collection_thumbnails(
    collection,
    out_dir,
    vis_params,
    dimensions=1000,
    region=region,
)

7.7. Exporting feature collections#

Map = geemap.Map()
fc = ee.FeatureCollection('USDOS/LSIB_SIMPLE/2017').filter(
    ee.Filter.eq('wld_rgn', 'Europe')
)

Map.addLayer(fc, {}, "Europe")
Map.centerObject(fc, 3)
Map

7.7.1. To local drive#

geemap.ee_to_shp(fc, filename='europe.shp', selectors=None)
geemap.ee_export_vector(fc, filename='europe2.shp')
geemap.ee_to_geojson(fc, filename='europe.geojson')
geemap.ee_to_csv(fc, filename='europe.csv')
gdf = geemap.ee_to_gdf(fc)
gdf
df = geemap.ee_to_df(fc)
df

7.7.2. To Google Drive#

geemap.ee_export_vector_to_drive(
    fc, description="europe", fileFormat='SHP', folder="export"
)

7.7.3. To Asset#

geemap.ee_export_vector_to_asset(fc, description='Exporting Europe', assetId='europe')

7.8. Exporting maps#

Map = geemap.Map()
image = ee.Image('USGS/SRTMGL1_003')
vis_params = {
    'min': 0,
    'max': 4000,
    'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
Map.addLayer(image, vis_params, 'SRTM DEM', True)
Map
Map.to_html(
    filename="mymap.html", title="Earth Engine Map", width='100%', height='800px'
)

7.9. Using the high-volume endpoint#

import ee
import geemap
import logging
import multiprocessing
import os
import requests
import shutil
from retry import retry
ee.Initialize(opt_url='https://earthengine-highvolume.googleapis.com')
region = Map.user_roi

if region is None:
    region = ee.Geometry.Polygon(
        [
            [
                [-122.513695, 37.707998],
                [-122.513695, 37.804359],
                [-122.371902, 37.804359],
                [-122.371902, 37.707998],
                [-122.513695, 37.707998],
            ]
        ],
        None,
        False,
    )
image = (
    ee.ImageCollection('USDA/NAIP/DOQQ')
    .filterBounds(region)
    .filterDate('2020', '2021')
    .mosaic()
    .clip(region)
    .select('N', 'R', 'G')
)
Map = geemap.Map()
Map.addLayer(image, {}, "Image")
Map.addLayer(region, {}, "ROI", False)
Map.centerObject(region, 12)
Map
out_dir = os.path.expanduser('~/Downloads/')
params = {
    'count': 1000,  # How many image chips to export
    'buffer': 127,  # The buffer distance (m) around each point
    'scale': 100,  # The scale to do stratified sampling
    'seed': 1,  # A randomization seed to use for subsampling.
    'dimensions': '256x256',  # The dimension of each image chip
    'format': "png",  # The output image format, can be png, jpg, ZIPPED_GEO_TIFF, GEO_TIFF, NPY
    'prefix': 'tile_',  # The filename prefix
    'processes': 25,  # How many processes to used for parallel processing
    'out_dir': out_dir,  # The output directory. Default to the current working directly
}
def getRequests():
    img = ee.Image(1).rename("Class").addBands(image)
    points = img.stratifiedSample(
        numPoints=params['count'],
        region=region,
        scale=params['scale'],
        seed=params['seed'],
        geometries=True,
    )
    Map.data = points
    return points.aggregate_array('.geo').getInfo()
@retry(tries=10, delay=1, backoff=2)
def getResult(index, point):
    point = ee.Geometry.Point(point['coordinates'])
    region = point.buffer(params['buffer']).bounds()

    if params['format'] in ['png', 'jpg']:
        url = image.getThumbURL(
            {
                'region': region,
                'dimensions': params['dimensions'],
                'format': params['format'],
            }
        )
    else:
        url = image.getDownloadURL(
            {
                'region': region,
                'dimensions': params['dimensions'],
                'format': params['format'],
            }
        )

    if params['format'] == "GEO_TIFF":
        ext = 'tif'
    else:
        ext = params['format']

    r = requests.get(url, stream=True)
    if r.status_code != 200:
        r.raise_for_status()

    out_dir = os.path.abspath(params['out_dir'])
    basename = str(index).zfill(len(str(params['count'])))
    filename = f"{out_dir}/{params['prefix']}{basename}.{ext}"
    with open(filename, 'wb') as out_file:
        shutil.copyfileobj(r.raw, out_file)
    print("Done: ", basename)
%%time
logging.basicConfig()
items = getRequests()

pool = multiprocessing.Pool(params['processes'])
pool.starmap(getResult, enumerate(items))

pool.close()
Map.addLayer(Map.data, {}, "Sample points")
Map
geemap.ee_to_shp(Map.data, filename='points.shp')

7.10. Summary#

7.11. References#