66 cartoee legend
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 carto python=3.8
conda activate carto
conda install mamba -c conda-forge
mamba install cartopy scipy -c conda-forge
mamba install geemap -c conda-forge
jupyter notebook
In [1]:
Copied!
# !pip install cartopy scipy
# !pip install geemap
# !pip install cartopy scipy
# !pip install geemap
In [2]:
Copied!
%pylab inline
import ee
import geemap
# import the cartoee functionality from geemap
from geemap import cartoee
%pylab inline
import ee
import geemap
# import the cartoee functionality from geemap
from geemap import cartoee
%pylab is deprecated, use %matplotlib inline and import the required libraries. Populating the interactive namespace from numpy and matplotlib cartopy is not installed. Please see https://scitools.org.uk/cartopy/docs/latest/installing.html#installing for instructions on how to install cartopy. The easiest way to install cartopy is using conda: conda install -c conda-forge cartopy
In [3]:
Copied!
geemap.ee_initialize()
geemap.ee_initialize()
Plot an RGB image¶
In [4]:
Copied!
# 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}
# 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}
In [5]:
Copied!
fig = plt.figure(figsize=(15, 10))
# use cartoee to get a map
ax = cartoee.get_map(image, vis_params=vis)
# 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="cyan")
show()
fig = plt.figure(figsize=(15, 10))
# use cartoee to get a map
ax = cartoee.get_map(image, vis_params=vis)
# 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="cyan")
show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [5], in <cell line: 4>() 1 fig = plt.figure(figsize=(15, 10)) 3 # use cartoee to get a map ----> 4 ax = cartoee.get_map(image, vis_params=vis) 6 # pad the view for some visual appeal 7 cartoee.pad_view(ax) File ~/.local/lib/python3.9/site-packages/geemap/cartoee.py:150, in get_map(ee_object, proj, **kwargs) 147 ee_object = ee_object.mosaic() 149 if proj is None: --> 150 proj = ccrs.PlateCarree() 152 if "style" in kwargs: 153 del kwargs["style"] NameError: name 'ccrs' is not defined
<Figure size 1080x720 with 0 Axes>
By default, if a region is not provided via the region
keyword the whole extent of the image will be plotted as seen in the previous Landsat example. We can also zoom to a specific region of an image by defining the region to plot.
In [6]:
Copied!
fig = plt.figure(figsize=(15, 10))
# here is the bounding box of the map extent we want to use
# formatted a [E,S,W,N]
zoom_region = [-121.8025, 37.3458, -122.6265, 37.9178]
# plot the map over the region of interest
ax = cartoee.get_map(image, vis_params=vis, region=zoom_region)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.15, xtick_rotation=0, linestyle=":")
# add coastline
ax.coastlines(color="cyan")
show()
fig = plt.figure(figsize=(15, 10))
# here is the bounding box of the map extent we want to use
# formatted a [E,S,W,N]
zoom_region = [-121.8025, 37.3458, -122.6265, 37.9178]
# plot the map over the region of interest
ax = cartoee.get_map(image, vis_params=vis, region=zoom_region)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.15, xtick_rotation=0, linestyle=":")
# add coastline
ax.coastlines(color="cyan")
show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [6], in <cell line: 8>() 5 zoom_region = [-121.8025, 37.3458, -122.6265, 37.9178] 7 # plot the map over the region of interest ----> 8 ax = cartoee.get_map(image, vis_params=vis, region=zoom_region) 10 # add the gridlines and specify that the xtick labels be rotated 45 degrees 11 cartoee.add_gridlines(ax, interval=0.15, xtick_rotation=0, linestyle=":") File ~/.local/lib/python3.9/site-packages/geemap/cartoee.py:150, in get_map(ee_object, proj, **kwargs) 147 ee_object = ee_object.mosaic() 149 if proj is None: --> 150 proj = ccrs.PlateCarree() 152 if "style" in kwargs: 153 del kwargs["style"] NameError: name 'ccrs' is not defined
<Figure size 1080x720 with 0 Axes>
Adding north arrow, scale bar, and legend¶
In [7]:
Copied!
fig = plt.figure(figsize=(15, 10))
# here is the bounding box of the map extent we want to use
# formatted a [E,S,W,N]
zoom_region = [-121.8025, 37.3458, -122.6265, 37.9178]
# plot the map over the region of interest
ax = cartoee.get_map(image, vis_params=vis, region=zoom_region)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.15, xtick_rotation=0, linestyle=":")
# add coastline
ax.coastlines(color="cyan")
# add north arrow
cartoee.add_north_arrow(
ax, text="N", xy=(0.05, 0.25), text_color="white", arrow_color="white", fontsize=20
)
# add scale bar
cartoee.add_scale_bar_lite(
ax, length=10, xy=(0.1, 0.05), fontsize=20, color="white", unit="km"
)
ax.set_title(label='Landsat False Color Composite (Band 5/4/3)', fontsize=15)
# add legend
legend_elements = [
Line2D([], [], color='#00ffff', lw=2, label='Coastline'),
Line2D(
[],
[],
marker='o',
color='#A8321D',
label='City',
markerfacecolor='#A8321D',
markersize=10,
ls='',
),
]
cartoee.add_legend(ax, legend_elements, loc='lower right')
show()
fig = plt.figure(figsize=(15, 10))
# here is the bounding box of the map extent we want to use
# formatted a [E,S,W,N]
zoom_region = [-121.8025, 37.3458, -122.6265, 37.9178]
# plot the map over the region of interest
ax = cartoee.get_map(image, vis_params=vis, region=zoom_region)
# add the gridlines and specify that the xtick labels be rotated 45 degrees
cartoee.add_gridlines(ax, interval=0.15, xtick_rotation=0, linestyle=":")
# add coastline
ax.coastlines(color="cyan")
# add north arrow
cartoee.add_north_arrow(
ax, text="N", xy=(0.05, 0.25), text_color="white", arrow_color="white", fontsize=20
)
# add scale bar
cartoee.add_scale_bar_lite(
ax, length=10, xy=(0.1, 0.05), fontsize=20, color="white", unit="km"
)
ax.set_title(label='Landsat False Color Composite (Band 5/4/3)', fontsize=15)
# add legend
legend_elements = [
Line2D([], [], color='#00ffff', lw=2, label='Coastline'),
Line2D(
[],
[],
marker='o',
color='#A8321D',
label='City',
markerfacecolor='#A8321D',
markersize=10,
ls='',
),
]
cartoee.add_legend(ax, legend_elements, loc='lower right')
show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [7], in <cell line: 8>() 5 zoom_region = [-121.8025, 37.3458, -122.6265, 37.9178] 7 # plot the map over the region of interest ----> 8 ax = cartoee.get_map(image, vis_params=vis, region=zoom_region) 10 # add the gridlines and specify that the xtick labels be rotated 45 degrees 11 cartoee.add_gridlines(ax, interval=0.15, xtick_rotation=0, linestyle=":") File ~/.local/lib/python3.9/site-packages/geemap/cartoee.py:150, in get_map(ee_object, proj, **kwargs) 147 ee_object = ee_object.mosaic() 149 if proj is None: --> 150 proj = ccrs.PlateCarree() 152 if "style" in kwargs: 153 del kwargs["style"] NameError: name 'ccrs' is not defined
<Figure size 1080x720 with 0 Axes>
Last update:
2022-03-14