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
How to create timelapse animations using cartoee¶
In [1]:
Copied!
import os
import ee
import geemap
from geemap import cartoee
%pylab inline
import os
import ee
import geemap
from geemap import cartoee
%pylab inline
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 %pylab is deprecated, use %matplotlib inline and import the required libraries. Populating the interactive namespace from numpy and matplotlib
In [2]:
Copied!
# geemap.update_package()
# geemap.update_package()
Create an interactive map¶
In [3]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Create an ImageCollection¶
In [4]:
Copied!
lon = -115.1585
lat = 36.1500
start_year = 1984
end_year = 2011
point = ee.Geometry.Point(lon, lat)
years = ee.List.sequence(start_year, end_year)
def get_best_image(year):
start_date = ee.Date.fromYMD(year, 1, 1)
end_date = ee.Date.fromYMD(year, 12, 31)
image = (
ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterBounds(point)
.filterDate(start_date, end_date)
.sort("CLOUD_COVER")
.first()
)
return ee.Image(image)
collection = ee.ImageCollection(years.map(get_best_image))
lon = -115.1585
lat = 36.1500
start_year = 1984
end_year = 2011
point = ee.Geometry.Point(lon, lat)
years = ee.List.sequence(start_year, end_year)
def get_best_image(year):
start_date = ee.Date.fromYMD(year, 1, 1)
end_date = ee.Date.fromYMD(year, 12, 31)
image = (
ee.ImageCollection("LANDSAT/LT05/C01/T1_SR")
.filterBounds(point)
.filterDate(start_date, end_date)
.sort("CLOUD_COVER")
.first()
)
return ee.Image(image)
collection = ee.ImageCollection(years.map(get_best_image))
Display a sample image¶
In [5]:
Copied!
vis_params = {"bands": ['B4', 'B3', 'B2'], "min": 0, "max": 5000}
image = ee.Image(collection.first())
Map.addLayer(image, vis_params, 'First image')
Map.setCenter(lon, lat, 8)
Map
vis_params = {"bands": ['B4', 'B3', 'B2'], "min": 0, "max": 5000}
image = ee.Image(collection.first())
Map.addLayer(image, vis_params, 'First image')
Map.setCenter(lon, lat, 8)
Map
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Get a sample output image¶
In [6]:
Copied!
w = 0.4
h = 0.3
region = [lon + w, lat - h, lon - w, lat + h]
fig = plt.figure(figsize=(10, 8))
# use cartoee to get a map
ax = cartoee.get_map(image, region=region, vis_params=vis_params)
# add gridlines to the map at a specified interval
cartoee.add_gridlines(ax, interval=[0.2, 0.2], linestyle=":")
# add north arrow
north_arrow_dict = {
"text": "N",
"xy": (0.1, 0.3),
"arrow_length": 0.15,
"text_color": "white",
"arrow_color": "white",
"fontsize": 20,
"width": 5,
"headwidth": 15,
"ha": "center",
"va": "center",
}
cartoee.add_north_arrow(ax, **north_arrow_dict)
# add scale bar
scale_bar_dict = {
"length": 10,
"xy": (0.1, 0.05),
"linewidth": 3,
"fontsize": 20,
"color": "white",
"unit": "km",
"ha": "center",
"va": "bottom",
}
cartoee.add_scale_bar_lite(ax, **scale_bar_dict)
ax.set_title(label='Las Vegas, NV', fontsize=15)
show()
w = 0.4
h = 0.3
region = [lon + w, lat - h, lon - w, lat + h]
fig = plt.figure(figsize=(10, 8))
# use cartoee to get a map
ax = cartoee.get_map(image, region=region, vis_params=vis_params)
# add gridlines to the map at a specified interval
cartoee.add_gridlines(ax, interval=[0.2, 0.2], linestyle=":")
# add north arrow
north_arrow_dict = {
"text": "N",
"xy": (0.1, 0.3),
"arrow_length": 0.15,
"text_color": "white",
"arrow_color": "white",
"fontsize": 20,
"width": 5,
"headwidth": 15,
"ha": "center",
"va": "center",
}
cartoee.add_north_arrow(ax, **north_arrow_dict)
# add scale bar
scale_bar_dict = {
"length": 10,
"xy": (0.1, 0.05),
"linewidth": 3,
"fontsize": 20,
"color": "white",
"unit": "km",
"ha": "center",
"va": "bottom",
}
cartoee.add_scale_bar_lite(ax, **scale_bar_dict)
ax.set_title(label='Las Vegas, NV', fontsize=15)
show()
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [6], in <cell line: 9>() 6 fig = plt.figure(figsize=(10, 8)) 8 # use cartoee to get a map ----> 9 ax = cartoee.get_map(image, region=region, vis_params=vis_params) 11 # add gridlines to the map at a specified interval 12 cartoee.add_gridlines(ax, interval=[0.2, 0.2], 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 720x576 with 0 Axes>
Create timelapse animations¶
In [7]:
Copied!
cartoee.get_image_collection_gif(
ee_ic=collection,
out_dir=os.path.expanduser("~/Downloads/timelapse"),
out_gif="animation.gif",
vis_params=vis_params,
region=region,
fps=5,
mp4=True,
grid_interval=(0.2, 0.2),
plot_title="Las Vegas, NV",
date_format='YYYY-MM-dd',
fig_size=(10, 8),
dpi_plot=100,
file_format="png",
north_arrow_dict=north_arrow_dict,
scale_bar_dict=scale_bar_dict,
verbose=True,
)
cartoee.get_image_collection_gif(
ee_ic=collection,
out_dir=os.path.expanduser("~/Downloads/timelapse"),
out_gif="animation.gif",
vis_params=vis_params,
region=region,
fps=5,
mp4=True,
grid_interval=(0.2, 0.2),
plot_title="Las Vegas, NV",
date_format='YYYY-MM-dd',
fig_size=(10, 8),
dpi_plot=100,
file_format="png",
north_arrow_dict=north_arrow_dict,
scale_bar_dict=scale_bar_dict,
verbose=True,
)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Input In [7], in <cell line: 1>() 1 cartoee.get_image_collection_gif( 2 ee_ic=collection, 3 out_dir=os.path.expanduser("~/Downloads/timelapse"), 4 out_gif="animation.gif", 5 vis_params=vis_params, 6 region=region, 7 fps=5, 8 mp4=True, 9 grid_interval=(0.2, 0.2), 10 plot_title="Las Vegas, NV", 11 date_format='YYYY-MM-dd', 12 fig_size=(10, 8), 13 dpi_plot=100, 14 file_format="png", ---> 15 north_arrow_dict=north_arrow_dict, 16 scale_bar_dict=scale_bar_dict, 17 verbose=True, 18 ) NameError: name 'north_arrow_dict' is not defined
Last update:
2022-03-14