{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://githubtocolab.com/gee-community/geemap/blob/master/examples/notebooks/104_clip_image.ipynb)\n", "[![image](https://mybinder.org/badge_logo.svg)](https://gishub.org/geemap-binder)\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# !pip install geemap rasterio fiona" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import geemap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Download a sample raster dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "url = 'https://github.com/giswqs/data/raw/main/raster/srtm90.tif'\n", "dem = 'dem.tif'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.download_file(url, dem)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create an interactive map." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m = geemap.Map()\n", "m.add_raster(dem, palette='terrain', layer_name=\"DEM\")\n", "m" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a mask to extract the image. The mask can be a string representing a file path to a vector dataset (e.g., geojson, shp), or a list of coordinates (e.g., `[[lon,lat], [lon,lat]]`), or a dictionary representing a feature (e.g., m.user_roi).\n", "\n", "For example, the mask can be a filepath to a vector dataset." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mask = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/mask.geojson'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or you can draw a polygon on the map, then use `m.user_roi` as the mask." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# mask = m.user_roi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or specify a list of coordinates `[lon, lat]` as the mask." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mask = [\n", " [-119.679565, 37.256566],\n", " [-119.679565, 38.061067],\n", " [-118.24585, 38.061067],\n", " [-118.24585, 37.256566],\n", " [-119.679565, 37.256566],\n", "]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Specify the output filename." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output = 'clip.tif'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Clip image by mask." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "geemap.clip_image(dem, mask, output)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Add the clipped image to the map." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "m.add_raster(output, palette='gist_earth', layer_name=\"Clip Image\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }