25 load rasters
Uncomment the following line to install geemap if needed.
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
To follow this tutorial, you need to install the [geemap] and xarray_leaflet Python packages. Use the following conda commands to create a conda env and install packages:
conda create -n gee python
conda activate gee
conda install mamba -c conda-forge
mamba install geemap xarray_leaflet -c conda-forge
Import libraries
In [2]:
Copied!
import os
import geemap
import os
import geemap
Specify input raster datasets
In [3]:
Copied!
out_dir = os.path.expanduser('~/Downloads')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
landsat = os.path.join(out_dir, 'landsat.tif')
dem = os.path.join(out_dir, 'dem.tif')
out_dir = os.path.expanduser('~/Downloads')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
landsat = os.path.join(out_dir, 'landsat.tif')
dem = os.path.join(out_dir, 'dem.tif')
Download samples raster datasets
More datasets can be downloaded from https://viewer.nationalmap.gov/basic/
In [4]:
Copied!
if not os.path.exists(landsat):
landsat_url = 'https://drive.google.com/file/d/1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw/view?usp=sharing'
geemap.download_from_gdrive(landsat_url, 'landsat.tif', out_dir, unzip=False)
if not os.path.exists(landsat):
landsat_url = 'https://drive.google.com/file/d/1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw/view?usp=sharing'
geemap.download_from_gdrive(landsat_url, 'landsat.tif', out_dir, unzip=False)
Google Drive file id: 1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw Downloading 1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw into /home/runner/Downloads/landsat.tif... Done.
In [5]:
Copied!
if not os.path.exists(dem):
dem_url = 'https://drive.google.com/file/d/1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8/view?usp=sharing'
geemap.download_from_gdrive(dem_url, 'dem.tif', out_dir, unzip=False)
if not os.path.exists(dem):
dem_url = 'https://drive.google.com/file/d/1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8/view?usp=sharing'
geemap.download_from_gdrive(dem_url, 'dem.tif', out_dir, unzip=False)
Google Drive file id: 1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8 Downloading 1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8 into /home/runner/Downloads/dem.tif... Done.
Create an interactive map
In [6]:
Copied!
Map = geemap.Map()
Map = geemap.Map()
Add local raster datasets to the map
More colormap can be found at https://matplotlib.org/stable/tutorials/colors/colormaps.html
In [7]:
Copied!
Map.add_raster(dem, colormap='terrain', layer_name='DEM')
Map.add_raster(dem, colormap='terrain', layer_name='DEM')
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [7], in <cell line: 1>() ----> 1 Map.add_raster(dem, colormap='terrain', layer_name='DEM') AttributeError: 'Map' object has no attribute 'add_raster'
In [8]:
Copied!
Map.add_raster(landsat, bands=[5, 4, 3], layer_name='Landsat')
Map.add_raster(landsat, bands=[5, 4, 3], layer_name='Landsat')
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [8], in <cell line: 1>() ----> 1 Map.add_raster(landsat, bands=[5, 4, 3], layer_name='Landsat') AttributeError: 'Map' object has no attribute 'add_raster'
Display the map
In [9]:
Copied!
Map
Map
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Last update:
2022-03-14