Skip to content

Commit

Permalink
Add notebook example
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Dec 25, 2023
1 parent 564df9a commit 4208d91
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 4 deletions.
202 changes: 202 additions & 0 deletions docs/notebooks/141_image_array_viz.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/141_image_array_viz.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
"\n",
"**Visualizing in-memory raster datasets and image arrays**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install -U geemap"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ee\n",
"import geemap"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use a Landsat image covering San Francisco Bay Area."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = geemap.Map()\n",
"image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n",
" ['B5', 'B4', 'B3']\n",
")\n",
"vis_params = {\n",
" 'min': 0.0,\n",
" 'max': 0.4,\n",
"}\n",
"m.add_layer(image, vis_params, 'False Color (543)')\n",
"m.centerObject(image)\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Retrieve the projection information from the image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"projection = image.select(0).projection().getInfo()\n",
"crs = projection['crs']\n",
"crs"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Read the Earth Engine image into an Xarray DataArray."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ds = geemap.ee_to_xarray(\n",
" image, \n",
" crs=\"EPSG:32610\", \n",
" scale=300,\n",
" geometry=image.geometry(),\n",
" ee_mask_value=0\n",
")\n",
"ds"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Save the DataArray as a Cloud Optimized GeoTIFF."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"geemap.xee_to_image(ds, filenames=['landsat.tif'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calculate the NDVI and save it to the DataArray."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndvi = (ds['B5'] - ds['B4']) / (ds['B5'] + ds['B4'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create an in-memory raster dataset from the DataArray."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ndvi_image = geemap.array_to_image(ndvi, source=\"landsat.tif\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Visualize the in-memory raster dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m.add_raster('landsat.tif', band=[1, 2, 3], nodata=-1, layer_name=\"Landsat 7\")\n",
"m.add_raster(ndvi_image, cmap=\"Greens\", layer_name=\"NDVI\")\n",
"m"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Save the in-memory raster dataset to a GeoTIFF file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"geemap.array_to_image(ndvi, output=\"ndvi.tif\", source=\"landsat.tif\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
1 change: 1 addition & 0 deletions docs/tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,4 @@ More video tutorials for geemap and Earth Engine are available on my [YouTube ch
138. Clipping Earth Engine images interactively with the Draw Control ([notebook](https://geemap.org/notebooks/138_draw_control))
139. Converting an Earth Engine to an image ([notebook](https://geemap.org/notebooks/139_layer_to_image))
140. Converting Earth Engine images to an Xarray Dataset ([notebook](https://geemap.org/notebooks/140_ee_to_xarray))
141. Visualizing in-memory raster datasets and image arrays ([notebook](https://geemap.org/notebooks/141_image_array_viz))
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ More video tutorials for geemap and Earth Engine are available on my [YouTube ch
138. Clipping Earth Engine images interactively with the Draw Control ([notebook](https://geemap.org/notebooks/138_draw_control))
139. Converting an Earth Engine to an image ([notebook](https://geemap.org/notebooks/139_layer_to_image))
140. Converting Earth Engine images to an Xarray Dataset ([notebook](https://geemap.org/notebooks/140_ee_to_xarray))
141. Visualizing in-memory raster datasets and image arrays ([notebook](https://geemap.org/notebooks/141_image_array_viz))

### 1. Introducing the geemap Python package for interactive mapping with Google Earth Engine

Expand Down
Loading

0 comments on commit 4208d91

Please sign in to comment.