104 clip image
Uncomment the following line to install geemap if needed.
In [ ]:
Copied!
# !pip install geemap rasterio fiona
# !pip install geemap rasterio fiona
In [ ]:
Copied!
import geemap
import geemap
Download a sample raster dataset.
In [ ]:
Copied!
url = 'https://github.com/giswqs/data/raw/main/raster/srtm90.tif'
dem = 'dem.tif'
url = 'https://github.com/giswqs/data/raw/main/raster/srtm90.tif'
dem = 'dem.tif'
In [ ]:
Copied!
geemap.download_file(url, dem)
geemap.download_file(url, dem)
Create an interactive map.
In [ ]:
Copied!
m = geemap.Map()
m.add_raster(dem, palette='terrain', layer_name="DEM")
m
m = geemap.Map()
m.add_raster(dem, palette='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.
In [ ]:
Copied!
# mask = 'https://raw.githubusercontent.com/giswqs/leafmap/master/examples/data/mask.geojson'
# 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.
In [ ]:
Copied!
# mask = m.user_roi
# mask = m.user_roi
Or specify a list of coordinates [lon, lat]
as the mask.
In [ ]:
Copied!
mask = [
[-119.679565, 37.256566],
[-119.679565, 38.061067],
[-118.24585, 38.061067],
[-118.24585, 37.256566],
[-119.679565, 37.256566],
]
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.
In [ ]:
Copied!
output = 'clip.tif'
output = 'clip.tif'
Clip image by mask.
In [ ]:
Copied!
geemap.clip_image(dem, mask, output)
geemap.clip_image(dem, mask, output)
Add the clipped image to the map.
In [ ]:
Copied!
m.add_raster(output, palette='gist_earth', layer_name="Clip Image")
m.add_raster(output, palette='gist_earth', layer_name="Clip Image")