Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert JavaScript dataset examples to Python #1194

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/AAFC/AAFC_ACI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dataset = ee.ImageCollection('AAFC/ACI')
crop_2016 = dataset.filter(ee.Filter.date('2016-01-01', '2016-12-31')).first()

m = geemap.Map()
m.set_center(-103.8881, 53.0372, 10)
m.add_layer(crop_2016, {}, '2016 Canada AAFC Annual Crop Inventory')
m
8 changes: 8 additions & 0 deletions examples/AHN/AHN_AHN2_05M_INT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dataset = ee.Image('AHN/AHN2_05M_INT')
elevation = dataset.select('elevation')
elevation_vis = {'min': -5.0, 'max': 30.0}

m = geemap.Map()
m.set_center(5.76583, 51.855276, 16)
m.add_layer(elevation, elevation_vis, 'Elevation')
m
11 changes: 11 additions & 0 deletions examples/ASTER/ASTER_AST_L1T_003.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dataset = ee.ImageCollection('ASTER/AST_L1T_003').filter(
ee.Filter.date('2018-01-01', '2018-08-15')
)

false_color = dataset.select(['B3N', 'B02', 'B01'])
false_color_vis = {'min': 0.0, 'max': 255.0}

m = geemap.Map()
m.set_center(-122.0272, 39.6734, 11)
m.add_layer(false_color.median(), false_color_vis, 'False Color')
m
30 changes: 30 additions & 0 deletions examples/BIOPAMA/BIOPAMA_GlobalOilPalm_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Import the dataset a collection of composite granules from 2019.
dataset = ee.ImageCollection('BIOPAMA/GlobalOilPalm/v1')

# Select the classification band.
op_class = dataset.select('classification')

# Mosaic all of the granules into a single image.
mosaic = op_class.mosaic()

# Define visualization parameters.
classification_vis = {
'min': 1,
'max': 3,
'palette': ['ff0000', 'ef00ff', '696969'],
}

# Create a mask to add transparency to non-oil palm plantation class pixels.
mask = mosaic.neq(3)
mask = mask.where(mask.eq(0), 0.6)

# Display the data on the map.
m = geemap.Map()
m.add_layer(
mosaic.updateMask(mask),
classification_vis,
'Oil palm plantation type',
True,
)
m.set_center(-3.0175, 5.2745, 12)
m
7 changes: 7 additions & 0 deletions examples/COPERNICUS/COPERNICUS_CORINE_V20_100m.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dataset = ee.Image('COPERNICUS/CORINE/V20/100m/2012')
land_cover = dataset.select('landcover')

m = geemap.Map()
m.set_center(16.436, 39.825, 6)
m.add_layer(land_cover, {}, 'Land Cover')
m
13 changes: 13 additions & 0 deletions examples/COPERNICUS/COPERNICUS_DEM_GLO30.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
dataset = ee.ImageCollection('COPERNICUS/DEM/GLO30')
elevation = dataset.select('DEM')

elevation_vis = {
'min': 0.0,
'max': 1000.0,
'palette': ['0000ff', '00ffff', 'ffff00', 'ff0000', 'ffffff'],
}

m = geemap.Map()
m.set_center(-6.746, 46.529, 4)
m.add_layer(elevation, elevation_vis, 'DEM')
m
41 changes: 41 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
def mask_s2_clouds(image):
"""Masks clouds using the Sentinel-2 QA band.

Args:
image (ee.Image): Sentinel-2 image.

Returns:
ee.Image: Cloud masked Sentinel-2 image.
"""
qa = image.select('QA60')

# Bits 10 and 11 are clouds and cirrus, respectively.
cloud_bit_mask = 1 << 10
cirrus_bit_mask = 1 << 11

# Both flags should be set to zero, indicating clear conditions.
mask = (
qa.bitwiseAnd(cloud_bit_mask)
.eq(0)
.And(qa.bitwiseAnd(cirrus_bit_mask).eq(0))
)

return image.updateMask(mask).divide(10000)


# Load Sentinel-2 TOA reflectance data.
dataset = (
ee.ImageCollection('COPERNICUS/S2')
.filterDate('2018-01-01', '2018-01-31')
# Pre-filter to get less cloudy granules.
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.map(mask_s2_clouds)
)

rgb_vis = {'min': 0.0, 'max': 0.3, 'bands': ['B4', 'B3', 'B2']}

# Create and display the map.
m = geemap.Map()
m.set_center(-9.1695, 38.6917, 12)
m.add_layer(dataset.median(), rgb_vis, 'RGB')
m
58 changes: 58 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S2_CLOUD_PROBABILITY.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
s2_sr = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
s2_clouds = ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY')

start_date = ee.Date('2019-01-01')
end_date = ee.Date('2019-03-01')
max_cloud_probability = 65
region = ee.Geometry.Rectangle(
coords=[-76.5, 2.0, -74, 4.0], geodesic=False, proj='EPSG:4326'
)


def maskClouds(img):
clouds = ee.Image(img.get('cloud_mask')).select('probability')
is_not_cloud = clouds.lt(max_cloud_probability)
return img.updateMask(is_not_cloud)


# The masks for the 10m bands sometimes do not exclude bad data at
# scene edges, so we apply masks from the 20m and 60m bands as well.
# Example asset that needs this operation:
# COPERNICUS/S2_CLOUD_PROBABILITY/20190301T000239_20190301T000238_T55GDP
def maskEdges(s2_img):
return s2_img.updateMask(
s2_img.select('B8A').mask().updateMask(s2_img.select('B9').mask())
)


# Filter input collections by desired data range and region.
criteria = ee.Filter.And(
ee.Filter.bounds(region), ee.Filter.date(start_date, end_date)
)
s2_sr = s2_sr.filter(criteria).map(maskEdges)
s2_clouds = s2_clouds.filter(criteria)

# Join S2 SR with cloud probability dataset to add cloud mask.
s2_sr_with_cloud_mask = ee.Join.saveFirst('cloud_mask').apply(
primary=s2_sr,
secondary=s2_clouds,
condition=ee.Filter.equals(
leftField='system:index', rightField='system:index'
),
)

s_2_cloud_masked = (
ee.ImageCollection(s2_sr_with_cloud_mask).map(maskClouds).median()
)

rgb_vis = {'min': 0, 'max': 3000, 'bands': ['B4', 'B3', 'B2']}

m = geemap.Map()
m.set_center(-75, 3, 12)
m.add_layer(
s_2_cloud_masked,
rgb_vis,
'S2 SR masked at ' + str(max_cloud_probability) + '%',
True,
)
m
17 changes: 17 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S3_OLCI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
dataset = ee.ImageCollection('COPERNICUS/S3/OLCI').filterDate(
'2018-04-01', '2018-04-04'
)

# Select bands for visualization and apply band-specific scale factors.
rgb = (
dataset.select(['Oa08_radiance', 'Oa06_radiance', 'Oa04_radiance'])
.median()
.multiply(ee.Image([0.00876539, 0.0123538, 0.0115198]))
) # Convert to radiance units.

vis_params = {'min': 0, 'max': 6, 'gamma': 1.5}

m = geemap.Map()
m.set_center(46.043, 1.45, 5)
m.add_layer(rgb, vis_params, 'RGB')
m
16 changes: 16 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S5P_NRTI_L3_NO2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
collection = (
ee.ImageCollection('COPERNICUS/S5P/NRTI/L3_NO2')
.select('NO2_column_number_density')
.filterDate('2019-06-01', '2019-06-06')
)

band_viz = {
'min': 0,
'max': 0.0002,
'palette': ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'],
}

m = geemap.Map()
m.add_layer(collection.mean(), band_viz, 'S5P N02')
m.set_center(65.27, 24.11, 4)
m
16 changes: 16 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S5P_OFFL_L3_AER_AI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
collection = (
ee.ImageCollection('COPERNICUS/S5P/OFFL/L3_AER_AI')
.select('absorbing_aerosol_index')
.filterDate('2019-06-01', '2019-06-06')
)

band_viz = {
'min': -1,
'max': 2.0,
'palette': ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'],
}

m = geemap.Map()
m.add_layer(collection.mean(), band_viz, 'S5P Aerosol')
m.set_center(-118.82, 36.1, 5)
m
16 changes: 16 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S5P_OFFL_L3_CH4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
collection = (
ee.ImageCollection('COPERNICUS/S5P/OFFL/L3_CH4')
.select('CH4_column_volume_mixing_ratio_dry_air')
.filterDate('2019-06-01', '2019-07-16')
)

band_viz = {
'min': 1750,
'max': 1900,
'palette': ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'],
}

m = geemap.Map()
m.add_layer(collection.mean(), band_viz, 'S5P CH4')
m.set_center(0.0, 0.0, 2)
m
16 changes: 16 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S5P_OFFL_L3_CO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
collection = (
ee.ImageCollection('COPERNICUS/S5P/OFFL/L3_CO')
.select('CO_column_number_density')
.filterDate('2019-06-01', '2019-06-11')
)

band_viz = {
'min': 0,
'max': 0.05,
'palette': ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'],
}

m = geemap.Map()
m.add_layer(collection.mean(), band_viz, 'S5P CO')
m.set_center(-25.01, -4.28, 4)
m
16 changes: 16 additions & 0 deletions examples/COPERNICUS/COPERNICUS_S5P_OFFL_L3_NO2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
collection = (
ee.ImageCollection('COPERNICUS/S5P/OFFL/L3_NO2')
.select('tropospheric_NO2_column_number_density')
.filterDate('2019-06-01', '2019-06-06')
)

band_viz = {
'min': 0,
'max': 0.0002,
'palette': ['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'],
}

m = geemap.Map()
m.add_layer(collection.mean(), band_viz, 'S5P N02')
m.set_center(65.27, 24.11, 4)
m
19 changes: 19 additions & 0 deletions examples/ECMWF/ECMWF_ERA5_LAND_HOURLY.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dataset = ee.ImageCollection('ECMWF/ERA5_LAND/HOURLY').filter(
ee.Filter.date('2020-07-01', '2020-07-02')
)

visualization = {
'bands': ['temperature_2m'],
'min': 250.0,
'max': 320.0,
'palette': [
'000080', '0000d9', '4000ff', '8000ff', '0080ff', '00ffff',
'00ff80', '80ff00', 'daff00', 'ffff00', 'fff500', 'ffda00',
'ffb000', 'ffa400', 'ff4f00', 'ff2500', 'ff0a00', 'ff00ff',
],
}

m = geemap.Map()
m.set_center(22.2, 21.2, 0)
m.add_layer(dataset, visualization, 'Air temperature [K] at 2m height')
m
19 changes: 19 additions & 0 deletions examples/ECMWF/ECMWF_ERA5_LAND_MONTHLY_AGGR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dataset = ee.ImageCollection('ECMWF/ERA5_LAND/MONTHLY_AGGR').first()

visualization = {
'bands': ['temperature_2m'],
'min': 250,
'max': 320,
'palette': [
'000080', '0000d9', '4000ff', '8000ff', '0080ff', '00ffff',
'00ff80', '80ff00', 'daff00', 'ffff00', 'fff500', 'ffda00',
'ffb000', 'ffa400', 'ff4f00', 'ff2500', 'ff0a00', 'ff00ff',
],
}

m = geemap.Map()
m.set_center(70, 45, 3)
m.add_layer(
dataset, visualization, 'Air temperature [K] at 2m height', True, 0.8
)
m
19 changes: 19 additions & 0 deletions examples/ECMWF/ECMWF_ERA5_MONTHLY.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
dataset = ee.ImageCollection('ECMWF/ERA5/MONTHLY')

visualization = {
'bands': ['mean_2m_air_temperature'],
'min': 250.0,
'max': 320.0,
'palette': [
'000080', '0000d9', '4000ff', '8000ff', '0080ff', '00ffff',
'00ff80', '80ff00', 'daff00', 'ffff00', 'fff500', 'ffda00',
'ffb000', 'ffa400', 'ff4f00', 'ff2500', 'ff0a00', 'ff00ff',
],
}

m = geemap.Map()
m.set_center(22.2, 21.2, 0)
m.add_layer(
dataset, visualization, 'Monthly average air temperature [K] at 2m height'
)
m
11 changes: 11 additions & 0 deletions examples/EO1/EO1_HYPERION.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
dataset = ee.ImageCollection('EO1/HYPERION').filter(
ee.Filter.date('2016-01-01', '2017-03-01')
)

rgb = dataset.select(['B050', 'B023', 'B015'])
rgb_vis = {'min': 1000.0, 'max': 14000.0, 'gamma': 2.5}

m = geemap.Map()
m.set_center(162.0044, -77.3463, 9)
m.add_layer(rgb.median(), rgb_vis, 'RGB')
m
24 changes: 24 additions & 0 deletions examples/ESA/ESA_CCI_FireCCI_5_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Visualize FireCCI51 for one year
dataset = ee.ImageCollection('ESA/CCI/FireCCI/5_1').filterDate(
'2020-01-01', '2020-12-31'
)

burned_area = dataset.select('BurnDate')
max_ba = burned_area.max()

# Use a circular palette to assign colors to date of first detection
ba_vis = {
'min': 1,
'max': 366,
'palette': [
'ff0000', 'fd4100', 'fb8200', 'f9c400', 'f2ff00', 'b6ff05',
'7aff0a', '3eff0f', '02ff15', '00ff55', '00ff99', '00ffdd',
'00ddff', '0098ff', '0052ff', '0210ff', '3a0dfb', '7209f6',
'a905f1', 'e102ed', 'ff00cc', 'ff0089', 'ff0047', 'ff0004'
],
}

m = geemap.Map()
m.set_center(0, 18, 2.1)
m.add_layer(max_ba, ba_vis, 'Burned Area')
m
7 changes: 7 additions & 0 deletions examples/ESA/ESA_GLOBCOVER_L4_200901_200912_V2_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dataset = ee.Image('ESA/GLOBCOVER_L4_200901_200912_V2_3')
landcover = dataset.select('landcover')

m = geemap.Map()
m.set_center(-88.6, 26.4, 3)
m.add_layer(landcover, {}, 'Landcover')
m
8 changes: 8 additions & 0 deletions examples/ESA/ESA_WorldCover_v200.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
dataset = ee.ImageCollection('ESA/WorldCover/v200').first()

visualization = {'bands': ['Map']}

m = geemap.Map()
m.center_object(dataset)
m.add_layer(dataset, visualization, 'Landcover')
m
Loading
Loading