Export data
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
In [2]:
Copied!
import os
import ee
import geemap
import os
import ee
import geemap
In [3]:
Copied!
from geemap.datasets import DATA
from geemap.datasets import DATA
In [4]:
Copied!
Map = geemap.Map()
Map = geemap.Map()
Set an output directory¶
In [5]:
Copied!
out_dir = os.path.expanduser("~/Downloads")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
out_dir = os.path.expanduser("~/Downloads")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
Export an ee.FeatureCollection¶
In [6]:
Copied!
fc = ee.FeatureCollection('users/giswqs/public/countries')
fc = ee.FeatureCollection('users/giswqs/public/countries')
In [7]:
Copied!
Map.addLayer(fc, {}, "Countries")
Map.centerObject(fc)
Map
Map.addLayer(fc, {}, "Countries")
Map.centerObject(fc)
Map
Out[7]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Export data to a local computer
In [8]:
Copied!
out_shp = os.path.join(out_dir, "countries.shp")
geemap.ee_export_vector(fc, out_shp, verbose=True)
out_shp = os.path.join(out_dir, "countries.shp")
geemap.ee_export_vector(fc, out_shp, verbose=True)
Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/tables/af2cd31629bab2fcc5007bd374c36321-5d0bf8b3b18d8530e2fb24de043eb545:getFeatures Please wait ... Data downloaded to /home/runner/Downloads/countries.shp
Export data to Google Drive
In [9]:
Copied!
geemap.ee_export_vector_to_drive(fc, description="countries", folder="export")
geemap.ee_export_vector_to_drive(fc, description="countries", folder="export")
Exporting countries...
Export an ee.Image¶
In [10]:
Copied!
image = ee.Image('LE7_TOA_5YEAR/1999_2003')
landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.7}
Map.addLayer(image, landsat_vis, "LE7_TOA_5YEAR/1999_2003", True, 1)
Map
image = ee.Image('LE7_TOA_5YEAR/1999_2003')
landsat_vis = {'bands': ['B4', 'B3', 'B2'], 'gamma': 1.7}
Map.addLayer(image, landsat_vis, "LE7_TOA_5YEAR/1999_2003", True, 1)
Map
Out[10]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [11]:
Copied!
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
filename = os.path.join(out_dir, 'landsat.tif')
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
filename = os.path.join(out_dir, 'landsat.tif')
Exporting all bands as one single image¶
In [12]:
Copied!
# set a default ROI
roi = ee.Geometry.Polygon(
[
[
[-482.643127, 37.547027],
[-482.643127, 37.996433],
[-481.908417, 37.996433],
[-481.908417, 37.547027],
[-482.643127, 37.547027],
]
]
)
# set a default ROI
roi = ee.Geometry.Polygon(
[
[
[-482.643127, 37.547027],
[-482.643127, 37.996433],
[-481.908417, 37.996433],
[-481.908417, 37.547027],
[-482.643127, 37.547027],
]
]
)
In [13]:
Copied!
# Draw any shapes on the map using the Drawing tools before executing this code block
if Map.user_roi is not None:
roi = Map.user_roi
# Draw any shapes on the map using the Drawing tools before executing this code block
if Map.user_roi is not None:
roi = Map.user_roi
In [14]:
Copied!
roi.getInfo()
roi.getInfo()
Out[14]:
{'type': 'Polygon', 'coordinates': [[[-482.643127, 37.547027], [-481.908417, 37.547027], [-481.908417, 37.996433], [-482.643127, 37.996433], [-482.643127, 37.547027]]]}
In [15]:
Copied!
image_clip = image.clip(roi)
image_clip = image.clip(roi)
In [16]:
Copied!
Map.addLayer(image_clip, landsat_vis, "image")
Map.addLayer(image_clip, landsat_vis, "image")
In [17]:
Copied!
geemap.ee_export_image(
image_clip, filename=filename, scale=90, region=roi, file_per_band=False
)
geemap.ee_export_image(
image_clip, filename=filename, scale=90, region=roi, file_per_band=False
)
Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/83a4ec0d21102c6b0b0fc3fcd9ab8d86-0dd8f1c19c81c94c9c8dc12c93ea1ed5:getPixels Please wait ... Data downloaded to /home/runner/Downloads/landsat.tif
Exporting each band as one image¶
In [18]:
Copied!
geemap.ee_export_image(
image, filename=filename, scale=90, region=roi, file_per_band=True
)
geemap.ee_export_image(
image, filename=filename, scale=90, region=roi, file_per_band=True
)
Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/a39da6f090dceafbb1b3469419e5eee0-65c0234332f338368a7fdeda1d7fba41:getPixels Please wait ... Data downloaded to /home/runner/Downloads
Export an image to Google Drive¶
In [19]:
Copied!
geemap.ee_export_image_to_drive(
image, description='landsat', folder='export', region=roi, scale=30
)
geemap.ee_export_image_to_drive(
image, description='landsat', folder='export', region=roi, scale=30
)
Exporting landsat ...
Download an ee.ImageCollection¶
In [20]:
Copied!
loc = ee.Geometry.Point(-99.2222, 46.7816)
collection = (
ee.ImageCollection('USDA/NAIP/DOQQ')
.filterBounds(loc)
.filterDate('2008-01-01', '2020-01-01')
.filter(ee.Filter.listContains("system:band_names", "N"))
)
loc = ee.Geometry.Point(-99.2222, 46.7816)
collection = (
ee.ImageCollection('USDA/NAIP/DOQQ')
.filterBounds(loc)
.filterDate('2008-01-01', '2020-01-01')
.filter(ee.Filter.listContains("system:band_names", "N"))
)
In [21]:
Copied!
out_dir = os.path.expanduser('~/Downloads')
out_dir = os.path.expanduser('~/Downloads')
In [22]:
Copied!
collection.aggregate_array('system:index').getInfo()
collection.aggregate_array('system:index').getInfo()
Out[22]:
['m_4609915_sw_14_060_20180902_20181213', 'm_4609915_sw_14_060_20190626', 'm_4609915_sw_14_1_20090818', 'm_4609915_sw_14_1_20100629', 'm_4609915_sw_14_1_20120714', 'm_4609915_sw_14_1_20140901', 'm_4609915_sw_14_1_20150926', 'm_4609915_sw_14_h_20160704', 'm_4609915_sw_14_h_20170703']
In [23]:
Copied!
geemap.ee_export_image_collection(collection, out_dir=out_dir)
geemap.ee_export_image_collection(collection, out_dir=out_dir)
Total number of images: 9 Exporting 1/9: m_4609915_sw_14_060_20180902_20181213.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/12dba27394c3260fc731c0d6229e43a5-b7aecc663d9d6fc76ee1d863a7656ff2:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_060_20180902_20181213.tif Exporting 2/9: m_4609915_sw_14_060_20190626.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/a05dd6d80c920403e8f1f943da8c4174-cdc561f26fd67f3de0dd0d22ef1b6679:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_060_20190626.tif Exporting 3/9: m_4609915_sw_14_1_20090818.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/e0218e3dbca864608a201086538782b5-42176983ae44cc6f2b7ac5a9a2f0d43e:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_1_20090818.tif Exporting 4/9: m_4609915_sw_14_1_20100629.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/e8b801d6cba914811034b6a836fa64e8-a64734817cb9f6bfed51409ae9620a6a:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_1_20100629.tif Exporting 5/9: m_4609915_sw_14_1_20120714.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/a2835ac668b547edc38f2c74320168d8-d90a4c6d198fb2699afe894288993380:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_1_20120714.tif Exporting 6/9: m_4609915_sw_14_1_20140901.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/dfc26d0ec2aa00709a488938a348c954-a48c1ac45fff1d6a0e3e8a3403623ab0:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_1_20140901.tif Exporting 7/9: m_4609915_sw_14_1_20150926.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/a3f09592597bb46998d6ca0905a1b1e2-0812b3171b22ef953320887c8625cb4b:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_1_20150926.tif Exporting 8/9: m_4609915_sw_14_h_20160704.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/0df29ebae58261ebc7edbe5743f785f7-7c01961fb6c682fae07b7055ac6b7875:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_h_20160704.tif Exporting 9/9: m_4609915_sw_14_h_20170703.tif Generating URL ... Downloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/b8e7221f77039b3b23cd347962c1a654-cef83aa3c63a1af1e11e4c47de28c28f:getPixels Please wait ... Data downloaded to /home/runner/Downloads/m_4609915_sw_14_h_20170703.tif
In [24]:
Copied!
geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)
geemap.ee_export_image_collection_to_drive(collection, folder='export', scale=10)
Total number of images: 9
Extract pixels as a Numpy array¶
In [25]:
Copied!
import ee
import geemap
import numpy as np
import matplotlib.pyplot as plt
img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810').select(['B4', 'B5', 'B6'])
aoi = ee.Geometry.Polygon(
[[[-110.8, 44.7], [-110.8, 44.6], [-110.6, 44.6], [-110.6, 44.7]]], None, False
)
rgb_img = geemap.ee_to_numpy(img, region=aoi)
print(rgb_img.shape)
import ee
import geemap
import numpy as np
import matplotlib.pyplot as plt
img = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_038029_20180810').select(['B4', 'B5', 'B6'])
aoi = ee.Geometry.Polygon(
[[[-110.8, 44.7], [-110.8, 44.6], [-110.6, 44.6], [-110.6, 44.7]]], None, False
)
rgb_img = geemap.ee_to_numpy(img, region=aoi)
print(rgb_img.shape)
(373, 531, 3)
In [26]:
Copied!
# Scale the data to [0, 255] to show as an RGB image.
# Adapted from https://bit.ly/2XlmQY8. Credits to Justin Braaten
rgb_img_test = (255 * ((rgb_img[:, :, 0:3] - 100) / 3500)).astype('uint8')
plt.imshow(rgb_img_test)
plt.show()
# Scale the data to [0, 255] to show as an RGB image.
# Adapted from https://bit.ly/2XlmQY8. Credits to Justin Braaten
rgb_img_test = (255 * ((rgb_img[:, :, 0:3] - 100) / 3500)).astype('uint8')
plt.imshow(rgb_img_test)
plt.show()
Last update:
2022-03-25