From 731ea52e660f94a6c945d3ab73b26ec25350cb9f Mon Sep 17 00:00:00 2001 From: Justin Braaten Date: Tue, 16 Jan 2024 15:33:18 -0800 Subject: [PATCH] fix auth/init, use geemap for interactive map, use Google asset PiperOrigin-RevId: 598977841 --- tutorials/imad-tutorial-pt1/index.md | 52 ++++------------------- tutorials/imad-tutorial-pt2/index.md | 62 +++++++--------------------- tutorials/imad-tutorial-pt3/index.md | 61 ++++++--------------------- 3 files changed, 35 insertions(+), 140 deletions(-) diff --git a/tutorials/imad-tutorial-pt1/index.md b/tutorials/imad-tutorial-pt1/index.md index 0ee75595d..60582b668 100644 --- a/tutorials/imad-tutorial-pt1/index.md +++ b/tutorials/imad-tutorial-pt1/index.md @@ -73,58 +73,22 @@ The cells below execute the necessary formalities for accessing Earth Engine fro ```python -# Authenticate -COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS = None - import ee -import google -import os - -if COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS is None: - print("Authenticating using Notebook auth...") - if os.path.exists(ee.oauth.get_credentials_path()) is False: - ee.Authenticate() - else: - print('\N{check mark} ' - 'Previously created authentication credentials were found.') - ee.Initialize() -else: - print('Authenticating using Colab auth...') - # Authenticate to populate Application Default Credentials in the Colab VM. - google.colab.auth.authenticate_user() - # Create credentials needed for accessing Earth Engine. - credentials, auth_project_id = google.auth.default() - # Initialize Earth Engine. - ee.Initialize(credentials, project=COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS) -print('\N{check mark} Successfully initialized!') -``` - -```python -# Install the ee_jupyter package -import os -try: - import ee_jupyter -except ModuleNotFoundError: - print('ee_jupyter was not found. Installing now...') - result = os.system('pip -q install earthengine-jupyter') - import ee_jupyter -print(f'ee_jupyter (version {ee_jupyter.__version__}) ' - f'is installed.') +ee.Authenticate(auth_mode='notebook') +ee.Initialize() ``` ```python # Import other packages used in the tutorial %matplotlib inline +import geemap import numpy as np import random, time import matplotlib.pyplot as plt from scipy.stats import norm, chi2 -from ee_jupyter.colab import set_colab_output_cell_height -from ee_jupyter.ipyleaflet import Map -from ee_jupyter.ipyleaflet import Inspector from pprint import pprint # for pretty printing ``` @@ -181,8 +145,7 @@ For example, here is an area of interest (aoi) covering a heavily forested admin ```python -aois = ee.FeatureCollection('projects/ee-mortcanty/assets/dvg1krs_nw').geometry() -aoi = ee.Geometry(aois.geometries().get(26)) +aoi = ee.FeatureCollection('projects/google/imad_tutorial_aoi').geometry() ``` We first collect two Sentinel-2 scenes which bracket some of the recent clear cutting (June, 2021 and June, 2022) and print out their timestamps. For demonstration purposes we choose top of atmosphere reflectance since the MAD transformation does not require absolute surface reflectances. @@ -221,8 +184,8 @@ In order to view the images, we instantiate an interactive map located at the ce ```python # Interactive map -location = aoi.centroid().coordinates().getInfo()[::-1] -M1 = Map(**{'center': location, 'zoom': 11}) +M1 = geemap.Map() +M1.centerObject(aoi, 11) M1 ``` @@ -520,7 +483,8 @@ we display an RGB composite of the first 3 MAD variates, together with the origi ```python -M2 = Map(**{'center': location, 'zoom': 11}) +M2 = geemap.Map() +M2.centerObject(aoi, 11) display_ls(im1.select(visbands), M2, 'Image 1') display_ls(im2.select(visbands), M2, 'Image 2') display_ls(diff, M2, 'Difference') diff --git a/tutorials/imad-tutorial-pt2/index.md b/tutorials/imad-tutorial-pt2/index.md index 2286ccc0e..979e29b86 100644 --- a/tutorials/imad-tutorial-pt2/index.md +++ b/tutorials/imad-tutorial-pt2/index.md @@ -47,56 +47,21 @@ EXPORT_PATH = 'projects/YOUR_GEE_PROJECT_NAME/assets/imad/' ``` ```python -# Authenticate -COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS = None - import ee -import google -import os - -if COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS is None: - print("Authenticating using Notebook auth...") - if os.path.exists(ee.oauth.get_credentials_path()) is False: - ee.Authenticate() - else: - print('\N{check mark} ' - 'Previously created authentication credentials were found.') - ee.Initialize() -else: - print('Authenticating using Colab auth...') - # Authenticate to populate Application Default Credentials in the Colab VM. - google.colab.auth.authenticate_user() - # Create credentials needed for accessing Earth Engine. - credentials, auth_project_id = google.auth.default() - # Initialize Earth Engine. - ee.Initialize(credentials, project=COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS) -print('\N{check mark} Successfully initialized!') -``` -```python -# Install the ee_jupyter package. -import os -try: - import ee_jupyter -except ModuleNotFoundError: - print('ee_jupyter was not found. Installing now...') - result = os.system('pip -q install earthengine-jupyter') - import ee_jupyter -print(f'ee_jupyter (version {ee_jupyter.__version__}) ' - f'is installed.') +ee.Authenticate(auth_mode='notebook') +ee.Initialize() ``` ```python # Import other packages used in the tutorial. %matplotlib inline +import geemap import numpy as np import random, time import matplotlib.pyplot as plt from scipy.stats import norm, chi2 -from ee_jupyter.colab import set_colab_output_cell_height -from ee_jupyter.ipyleaflet import Map -from ee_jupyter.ipyleaflet import Inspector from pprint import pprint # for pretty printing ``` @@ -322,9 +287,7 @@ the MAD variates that we have determined so far for the Landkreis Olpe scene. ```python # Landkreis Olpe. -# (`aois` FeatureCollection is owned and managed by tutorial author) -aois = ee.FeatureCollection('projects/ee-mortcanty/assets/dvg1krs_nw').geometry() -aoi = ee.Geometry(aois.geometries().get(26)) +aoi = ee.FeatureCollection('projects/google/imad_tutorial_aoi').geometry() visirbands = ['B2', 'B3', 'B4', 'B8', 'B11', 'B12'] visbands = ['B2', 'B3', 'B4'] @@ -549,8 +512,8 @@ Agreement is not perfect, but the plot is certainly closer to the ideal chi-squa after iteration than after the single MAD transformation. So let's display the *iMAD* image: ```python -location = aoi.centroid().coordinates().getInfo()[::-1] -M1 = Map(**{'center': location, 'zoom': 11}) +M1 = geemap.Map() +M1.centerObject(aoi, 11) display_ls(im1.select(visbands), M1, 'im1') display_ls(im2.select(visbands), M1, 'im2') display_ls(im_imad.select('iMAD1', 'iMAD2', 'iMAD3'), M1, 'iMAD123', True) @@ -603,7 +566,8 @@ result = im_imadstd.cluster(clusterer) Here we display the four clusters overlaid onto the two Sentinel 2 images: ```python -M2 = Map(**{'center': location, 'zoom': 13}) +M2 = geemap.Map() +M2.centerObject(aoi, 13) display_ls(im1.select(visbands), M2, 'im1') display_ls(im2.select(visbands), M2, 'im2') cluster0 = result.updateMask(result.eq(0)) @@ -655,7 +619,8 @@ dyn = ee.ImageCollection('GOOGLE/DYNAMICWORLD/V1') \ # 'trees' class = class 1 dw = dyn.clip(aoi).updateMask(dyn.eq(1)) -M3 = Map(**{'center': location, 'zoom': 13}) +M3 = geemap.Map() +M3.centerObject(aoi, 13) display_ls(im1.select(visbands), M3, 'im1') display_ls(im2.select(visbands), M3, 'im2') M3.addLayer(dw, {'min': 0, 'max': 1, 'palette': ['black', 'green']}, 'dynamic world') @@ -672,7 +637,8 @@ and is also only weakly correlated with the other 5 bands. However, close inspec that there are many more false positives, especially in agricultural fields, as well as in the reservoir. ```python -M4 = Map(**{'center': location, 'zoom': 13}) +M4 = geemap.Map() +M4.centerObject(aoi, 13) diff = im1.subtract(im2).select(visirbands) training = diff.sample(region=aoi, scale=20, numPixels=50000) clusterer = ee.Clusterer.wekaKMeans(4).train(training) @@ -778,8 +744,8 @@ aois = filtered.geometry() # There are many Montgomery Counties in USA, we want the 12th in the list. aoi = ee.Geometry(aois.geometries().get(12)) im1, im2 = collect(aoi, '2021-07-01', '2021-07-30', '2022-06-01', '2022-06-30') -location = aoi.centroid().coordinates().getInfo()[::-1] -M5 = Map(**{'center':location, 'zoom': 10}) +M5 = geemap.Map() +M5.centerObject(aoi, 10) display_ls(im1.select(visbands), M5, 'Image 1') display_ls(im2.select(visbands), M5, 'Image 2') diff --git a/tutorials/imad-tutorial-pt3/index.md b/tutorials/imad-tutorial-pt3/index.md index e43711226..9e8fd22f2 100644 --- a/tutorials/imad-tutorial-pt3/index.md +++ b/tutorials/imad-tutorial-pt3/index.md @@ -47,57 +47,21 @@ EXPORT_PATH = 'projects/YOUR_GEE_PROJECT_NAME/assets/imad/' ``` ```python -# Authenticate. -COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS = None - import ee -import google -import os - -if COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS is None: - print("Authenticating using Notebook auth...") - if os.path.exists(ee.oauth.get_credentials_path()) is False: - ee.Authenticate() - else: - print('\N{check mark} ' - 'Previously created authentication credentials were found.') - #ee.Authenticate() - ee.Initialize() -else: - print('Authenticating using Colab auth...') - # Authenticate to populate Application Default Credentials in the Colab VM. - google.colab.auth.authenticate_user() - # Create credentials needed for accessing Earth Engine. - credentials, auth_project_id = google.auth.default() - # Initialize Earth Engine. - ee.Initialize(credentials, project=COLAB_AUTH_FLOW_CLOUD_PROJECT_FOR_API_CALLS) -print('\N{check mark} Successfully initialized!') -``` -```python -# Install the ee_jupyter package. -import os -try: - import ee_jupyter -except ModuleNotFoundError: - print('ee_jupyter was not found. Installing now...') - result = os.system('pip -q install earthengine-jupyter') - import ee_jupyter -print(f'ee_jupyter (version {ee_jupyter.__version__}) ' - f'is installed.') +ee.Authenticate(auth_mode='notebook') +ee.Initialize() ``` ```python -# Import other packages used in the tutorial. +# Import other packages used in the tutorial %matplotlib inline +import geemap import numpy as np import random, time import matplotlib.pyplot as plt from scipy.stats import norm, chi2 -from ee_jupyter.colab import set_colab_output_cell_height -from ee_jupyter.ipyleaflet import Map -from ee_jupyter.ipyleaflet import Inspector from pprint import pprint # for pretty printing ``` @@ -467,10 +431,7 @@ As a first illustration, we collect two minimally cloudy Sentinel-2 images over ```python # Landkreis Olpe area of interest. -# (`aois` FeatureCollection is owned and managed by tutorial author) -aois = ee.FeatureCollection('projects/ee-mortcanty/assets/dvg1krs_nw').geometry() -aoi = ee.Geometry(aois.geometries().get(26)) -location = aoi.centroid().coordinates().getInfo()[::-1] +aoi = ee.FeatureCollection('projects/google/imad_tutorial_aoi').geometry() # Spectral band combinations #Sentinel-2 @@ -560,7 +521,8 @@ plot_orthoregress( coeffs, im_stack ) The next cell compares the normalized and unnormalized TOA images with the reference SR image. The no-change pixels used in the regression are also displayed: ```python -M1 = Map(**{'center': location, 'zoom': 13}) +M1 = geemap.Map() +M1.centerObject(aoi, 13) M1.addLayer(im1_sr.select(visnirbands), visnirparams, 'Im1_sr') M1.addLayer(im2_toa.select(visnirbands), visnirparams, 'Im2_toa') M1.addLayer(im2_toa_norm.select(visnirbands), visnirparams, 'Im2_toa_norm') @@ -579,7 +541,8 @@ def ndvi_s2(im): ``` ```python -M2 = Map(**{'center': location, 'zoom': 13}) +M2 = geemap.Map() +M2.centerObject(aoi, 13) M2.addLayer(ndvi_s2(im1_sr), {'min': 0, 'max': 1.2, 'palette': ['black', 'yellow']}, 'Im1_ndvi') M2.addLayer(ndvi_s2(im2_toa_norm), {'min': 0, 'max': 1.2, 'palette': ['black', 'yellow']}, 'Im2_toa_norm_ndvi') M2.addLayer(ndvi_s2(im2_toa), {'min': 0, 'max': 1.2, 'palette': ['black', 'yellow']}, 'Im2_toa_ndvi') @@ -620,7 +583,8 @@ The above plot for the NIR band B8 is interesting as it seems to indicate that t In any case, the regression plots look reasonable, so here again is the comparison of the normalized and unnormalized TOA images with the reference SR image: ```python -M3 = Map(**{'center': location, 'zoom': 13}) +M3 = geemap.Map() +M3.centerObject(aoi, 13) M3.addLayer(im1_sr.select(visnirbands), visnirparams, 'Im1_sr') M3.addLayer(im2_toa.select(visnirbands), visnirparams, 'Im1_toa') M3.addLayer(im2_toa_norm.select(visnirbands), visnirparams, 'Im2_toa_norm') @@ -664,7 +628,8 @@ plot_orthoregress(coeffs, im_stack, bandNames=visirbands_ls) For most of the bands, the intercepts are small and the slopes fairly close to one. The NIR band 'SR_B5' is the exception. This is apparent in the direct comparison of the original and harmonized Landsat 8 images: ```python -M4 = Map(**{'center': location, 'zoom': 11}) +M4 = geemap.Map() +M4.centerObject(aoi, 11) M4.addLayer(im_ls9.select(visnirbands_ls), visnirparams, 'Im_ls9') M4.addLayer(im_ls8.select(visnirbands_ls), visnirparams, 'Im_ls8') M4.addLayer(im_ls8_norm.select(visnirbands_ls), visnirparams, 'Im_ls8_norm')