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 [ ]:
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
In [ ]:
Copied!
# geemap.update_package()
# geemap.update_package()
Create an interactive map¶
In [ ]:
Copied!
Map = geemap.Map()
Map
Map = geemap.Map()
Map
Create an ImageCollection¶
In [ ]:
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 [ ]:
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
Get a sample output image¶
In [ ]:
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()
Create timelapse animations¶
In [ ]:
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,
)