Uncomment the following line to install geemap and cartopy if needed. Keep in mind that cartopy can be challenging to install. If you are unable to install cartopy on your computer, you can try Google Colab with this the notebook example.
See below the commands to install cartopy and geemap using conda/mamba:
conda create -n gee python=3.9
conda activate gee
conda install mamba -c conda-forge
mamba install cartopy scipy -c conda-forge
mamba install geemap -c conda-forge
%pylab inline
import ee
import geemap
# import the cartoee functionality from geemap
from geemap import cartoee
import cartopy.io.img_tiles as cimgt
geemap.ee_initialize()
# get a landsat image to visualize
image = ee.Image('LANDSAT/LC08/C01/T1_SR/LC08_044034_20140318')
# define the visualization parameters to view
vis = {"bands": ['B5', 'B4', 'B3'], "min": 0, "max": 5000, "gamma": 1.3}
fig = plt.figure(figsize=(15, 10))
# use cartoee to get a map
ax = cartoee.get_map(image, vis_params=vis, basemap='ROADMAP', zoom_level=8)
# pad the view for some visual appeal
cartoee.pad_view(ax)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.5, xtick_rotation=0, linestyle=":")
# add the coastline
ax.coastlines(color="yellow")
plt.show()
basemap = cimgt.Stamen('terrain-background')
fig = plt.figure(figsize=(15, 10))
# use cartoee to get a map
ax = cartoee.get_map(image, vis_params=vis, basemap=basemap, zoom_level=8)
# pad the view for some visual appeal
cartoee.pad_view(ax)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.5, xtick_rotation=0, linestyle=":")
# add the coastline
ax.coastlines(color="yellow")
plt.show()
basemap = cimgt.OSM()
fig = plt.figure(figsize=(15, 10))
# use cartoee to get a map
ax = cartoee.get_map(image, vis_params=vis, basemap=basemap, zoom_level=8)
# pad the view for some visual appeal
cartoee.pad_view(ax)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.5, xtick_rotation=0, linestyle=":")
# add the coastline
ax.coastlines(color="yellow")
plt.show()
For more basemaps, see https://scitools.org.uk/cartopy/docs/v0.19/cartopy/io/img_tiles.html
from matplotlib.transforms import offset_copy
import cartopy.crs as ccrs
fig = plt.figure(figsize=(15, 10))
# use cartoee to get a map
ax = cartoee.get_map(image, vis_params=vis, basemap='SATELLITE', zoom_level=8)
# pad the view for some visual appeal
cartoee.pad_view(ax)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.5, xtick_rotation=0, linestyle=":")
# add the coastline
ax.coastlines(color="yellow")
plt.plot(
-122.4457,
37.7574,
marker='o',
color='blue',
markersize=10,
alpha=0.7,
transform=ccrs.Geodetic(),
)
# Use the cartopy interface to create a matplotlib transform object
# for the Geodetic coordinate system. We will use this along with
# matplotlib's offset_copy function to define a coordinate system which
# translates the text by 25 pixels to the left.
geodetic_transform = ccrs.Geodetic()._as_mpl_transform(ax)
text_transform = offset_copy(geodetic_transform, units='dots', x=-25)
plt.text(
-122.4457,
37.7574,
u'San Francisco',
verticalalignment='center',
horizontalalignment='right',
transform=text_transform,
fontsize='large',
fontweight='bold',
color='white',
bbox=dict(facecolor='sandybrown', alpha=0.5, boxstyle='round'),
)
# Save the plot by calling plt.savefig() BEFORE plt.show()
plt.savefig('sfo.pdf')
plt.savefig('sfo.jpg', dpi=300)
plt.show()
# get an earth engine image of ocean data for Jan-Mar 2018
ocean = (
ee.ImageCollection('NASA/OCEANDATA/MODIS-Terra/L3SMI')
.filter(ee.Filter.date('2018-01-01', '2018-03-01'))
.median()
.select(["sst"], ["SST"])
)
# set parameters for plotting
# will plot the Sea Surface Temp with specific range and colormap
visualization = {'bands': "SST", 'min': -2, 'max': 30}
# specify region to focus on
bbox = [180, -88, -180, 88]
fig = plt.figure(figsize=(15, 10))
# plot the result with cartoee using a PlateCarre projection (default)
ax = cartoee.get_map(
ocean,
cmap='plasma',
vis_params=visualization,
region=bbox,
basemap=cimgt.Stamen('terrain'),
zoom_level=2,
)
cb = cartoee.add_colorbar(ax, vis_params=visualization, loc='right', cmap='plasma')
ax.set_title(label='Sea Surface Temperature', fontsize=15)
ax.coastlines()
# Save the plot by calling plt.savefig() BEFORE plt.show()
plt.savefig('sst.pdf')
plt.savefig('sst.jpg', dpi=300)
plt.show()