Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
giswqs
GitHub Repository: giswqs/geemap
Path: blob/master/docs/notebooks/104_clip_image.ipynb
2313 views
Kernel: Python 3

image image

Uncomment the following line to install geemap if needed.

# !pip install geemap rasterio fiona
import geemap

Download a sample raster dataset.

url = "https://github.com/giswqs/data/raw/main/raster/srtm90.tif" dem = "dem.tif"
geemap.download_file(url, dem)

Create an interactive map.

m = geemap.Map() m.add_raster(dem, colormap="terrain", layer_name="DEM") m

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).

For example, the mask can be a filepath to a vector dataset.

# mask = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/mask.geojson'

Or you can draw a polygon on the map, then use m.user_roi as the mask.

# mask = m.user_roi

Or specify a list of coordinates [lon, lat] as the mask.

mask = [ [-119.679565, 37.256566], [-119.679565, 38.061067], [-118.24585, 38.061067], [-118.24585, 37.256566], [-119.679565, 37.256566], ]

Specify the output filename.

output = "clip.tif"

Clip image by mask.

geemap.clip_image(dem, mask, output)

Add the clipped image to the map.

m.add_raster(output, colormap="gist_earth", layer_name="Clip Image")