Skip to content

Instantly share code, notes, and snippets.

@giswqs
Created May 21, 2020 04:35
Show Gist options
  • Save giswqs/010ce53129b5c6a47cdb12d9142291e9 to your computer and use it in GitHub Desktop.
Save giswqs/010ce53129b5c6a47cdb12d9142291e9 to your computer and use it in GitHub Desktop.
download sentinel-2 image
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import ee\nimport geemap\nimport os",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "geemap.show_youtube('_6JOA-iiEGU')",
"execution_count": 2,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Output(layout=Layout(width='815px'))",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "8116e042c2aa433ead8d788fc8c6dd77"
}
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "Map = geemap.Map()\nMap",
"execution_count": 3,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "Map(center=[40, -100], controls=(WidgetControl(options=['position'], widget=HBox(children=(ToggleButton(value=…",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "0d093b6f82014ed697ca73408866f3ee"
}
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Download an ee.Image"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "roi = ee.Geometry.Point(-81.743792, 39.992225)\nstart = ee.Date.fromYMD(2015, 12, 22)\nend = ee.Date.fromYMD(2016, 12, 22)\n\nimages = ee.ImageCollection('COPERNICUS/S2') \\\n .filterBounds(roi) \\\n .filterDate(start, end) \\\n .sort('CLOUDY_PIXEL_PERCENTAGE')\n\nimage = images.first()",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "rgbVis = {\n 'min': 0,\n 'max': 3000,\n 'bands': ['B4', 'B3', 'B2'],\n}\n\nMap.addLayer(image, rgbVis, 'Sentinel-2')\nMap.setCenter(-81.743792, 39.992225, 10)",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')\nfilename = os.path.join(out_dir, 'sentinel-2.tif')",
"execution_count": 6,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### Exporting all bands as one single image"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "region = roi.buffer(1000)\nresolution = 10\ngeemap.ee_export_image(image, filename=filename, scale=resolution, region=region, file_per_band=False)",
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": "Generating URL ...\nDownloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/01851e1e7516bd2a9dc613d0b706e0a6-64873e07c18843ed60d3c19941e2aad0:getPixels\nPlease wait ...\nData downloaded to /home/qiusheng/Downloads/sentinel-2.tif\n",
"name": "stdout"
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "#### Exporting each band as one image"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "geemap.ee_export_image(image, filename=filename, scale=resolution, region=region, file_per_band=True)",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "Generating URL ...\nDownloading data from https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/thumbnails/19f3f0e0633ebe2304213da334f54788-4f3c21b55a542668aa9ed8b3c3abf678:getPixels\nPlease wait ...\nData downloaded to /home/qiusheng/Downloads\n",
"name": "stdout"
}
]
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "download sentinel-2 image",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}
@moshiour
Copy link

Hi,
I got an error fir exporting all bands as one single image to Geotiff. Here is my error:
Generating URL ...
Downloading data from https://earthengine.googleapis.com/v1/projects/earthengine-legacy/thumbnails/969929a1f74b2444c7f2217757de688b-401c805c3440f0464907ad04638a2e61:getPixels
Please wait ...
An error occurred while downloading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment