{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open\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 }