Skip to content

Commit

Permalink
updates for tethys 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeymac committed Aug 17, 2024
1 parent 063b9f8 commit a44e003
Show file tree
Hide file tree
Showing 20 changed files with 10,846 additions and 249 deletions.
14 changes: 8 additions & 6 deletions install.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file should be committed to your app code.
version: 1.0
version: 1.1
# This should be greater or equal to your tethys-platform in your environment
tethys_version: ">=4.0.0"
# This should match the app - package name in your setup.py
Expand All @@ -10,13 +10,15 @@ requirements:
skip: false
conda:
channels:
- conda-forge
- conda-forge
packages:
- earthengine-api
- oauth2client
- geojson
- earthengine-api
- oauth2client
- geojson
- simplejson
- pandas
pip:

npm:

post:
post:
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
dependencies = []

# -- Get Resource File -- #
resource_files = find_all_resource_files(app_package, TethysAppBase.package_namespace)

resource_files = find_all_resource_files(
app_package, TethysAppBase.package_namespace
)

setup(
name=release_package,
Expand All @@ -28,4 +29,4 @@
include_package_data=True,
zip_safe=False,
install_requires=dependencies,
)
)
7 changes: 3 additions & 4 deletions tethysapp/earth_engine/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from tethys_sdk.base import TethysAppBase


class EarthEngine(TethysAppBase):
class App(TethysAppBase):
"""
Tethys app class for Earth Engine.
"""

name = 'Google Earth Engine Tutorial'
name = 'Earth Engine'
description = ''
package = 'earth_engine' # WARNING: Do not change this value
index = 'home'
Expand All @@ -15,4 +14,4 @@ class EarthEngine(TethysAppBase):
color = '#524745'
tags = ''
enable_feedback = False
feedback_emails = []
feedback_emails = []
27 changes: 13 additions & 14 deletions tethysapp/earth_engine/controllers.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import datetime as dt
import geojson
import logging
from simplejson.errors import JSONDecodeError
from django.http import JsonResponse, HttpResponseNotAllowed
from django.shortcuts import render
import geojson
from simplejson.errors import JSONDecodeError
from tethys_sdk.routing import controller
from tethys_sdk.gizmos import SelectInput, DatePicker, Button, MapView, MVView, PlotlyView, MVDraw
from .gee.methods import get_image_collection_asset, get_time_series_from_image_collection
from tethys_sdk.gizmos import SelectInput, DatePicker, Button, MapView, MVDraw, MVView, PlotlyView
from .gee.products import EE_PRODUCTS
from .gee.methods import get_image_collection_asset, get_time_series_from_image_collection
from .app import App
from .helpers import generate_figure

log = logging.getLogger(f'tethys.apps.{__name__}')

log = logging.getLogger(f'tethys.apps.{__name__}')

@controller
def home(request):
"""
Controller for the app home page.
"""
context = {}
return render(request, 'earth_engine/home.html', context)


return App.render(request, 'home.html', context)

@controller
def viewer(request):
"""
Expand Down Expand Up @@ -132,15 +131,14 @@ def viewer(request):
style='outline-secondary',
attributes={'id': 'load_map'}
)

map_view = MapView(
height='100%',
width='100%',
controls=[
'ZoomSlider', 'Rotate', 'FullScreen',
{'ZoomToExtent': {
'projection': 'EPSG:4326',
'extent': [29.25, -4.75, 46.25, 5.2]
'extent': [29.25, -4.75, 46.25, 5.2] #: Kenya
}}
],
basemap=[
Expand All @@ -157,7 +155,7 @@ def viewer(request):
maxZoom=18,
minZoom=2
),
draw=MVDraw(
draw = MVDraw(
controls=['Pan', 'Modify', 'Delete', 'Move', 'Point', 'Polygon', 'Box'],
initial='Pan',
output_format='GeoJSON'
Expand Down Expand Up @@ -193,8 +191,8 @@ def viewer(request):
'map_view': map_view
}

return render(request, 'earth_engine/viewer.html', context)

return App.render(request, 'viewer.html', context)

@controller(url='viewer/get-image-collection')
def get_image_collection(request):
Expand Down Expand Up @@ -306,4 +304,5 @@ def get_time_series_plot(request):
context['error'] = f'An unexpected error has occurred. Please try again.'
log.exception('An unexpected error occurred.')

return render(request, 'earth_engine/plot.html', context)
print(context)
return App.render(request, 'plot.html', context)
6 changes: 3 additions & 3 deletions tethysapp/earth_engine/gee/cloud_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def mask_l8_sr(image):
cloudsBitMask = (1 << 5)

# Get the pixel QA band.
qa = image.select('pixel_qa')
qa = image.select('QA_PIXEL')

# Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).And(qa.bitwiseAnd(cloudsBitMask).eq(0))
Expand All @@ -21,7 +21,7 @@ def cloud_mask_l457(image):
"""
Cloud Mask for Landsat 7 surface reflectance. Derived From: https://developers.google.com/earth-engine/datasets/catalog/LANDSAT_LE07_C01_T1_SR
"""
qa = image.select('pixel_qa')
qa = image.select('QA_PIXEL')

# If the cloud bit (5) is set and the cloud confidence (7) is high
# or the cloud shadow bit is set (3), then it's a bad pixel.
Expand All @@ -46,4 +46,4 @@ def mask_s2_clouds(image):
# Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudBitMask).eq(0).And(qa.bitwiseAnd(cirrusBitMask).eq(0))

return image.updateMask(mask).divide(10000)
return image.updateMask(mask).divide(10000)
22 changes: 7 additions & 15 deletions tethysapp/earth_engine/gee/methods.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
import logging
import ee
from ee.ee_exception import EEException
import geojson
import pandas as pd
from .products import EE_PRODUCTS
from . import params as gee_account
from .products import EE_PRODUCTS
from . import cloud_mask as cm
import geojson
import pandas as pd

log = logging.getLogger(f'tethys.apps.{__name__}')

if gee_account.service_account:
try:
credentials = ee.ServiceAccountCredentials(gee_account.service_account, gee_account.private_key)
ee.Initialize(credentials)
log.info('Successfully initialized GEE using service account.')
except EEException as e:
print(str(e))
log.warning('Unable to initialize GEE using service account. If installing ignore this warning.')
else:
try:
ee.Initialize()
except EEException as e:
from oauth2client.service_account import ServiceAccountCredentials
credentials = ServiceAccountCredentials.from_p12_keyfile(
service_account_email='',
filename='',
private_key_password='notasecret',
scopes=ee.oauth.SCOPE + ' https://www.googleapis.com/auth/drive '
)
ee.Initialize(credentials)
log.warning('Unable to initialize GEE with local credentials. If installing ignore this warning.')


def image_to_map_id(image_name, vis_params={}):
Expand All @@ -42,7 +36,6 @@ def image_to_map_id(image_name, vis_params={}):
except EEException:
log.exception('An error occurred while attempting to retrieve the map id.')


def get_image_collection_asset(platform, sensor, product, date_from=None, date_to=None, reducer='median'):
"""
Get tile url for image collection asset.
Expand Down Expand Up @@ -83,7 +76,6 @@ def get_image_collection_asset(platform, sensor, product, date_from=None, date_t
except EEException:
log.exception('An error occurred while attempting to retrieve the image collection asset.')


def get_time_series_from_image_collection(platform, sensor, product, index_name, scale=30, geometry=None,
date_from=None, date_to=None, reducer='median'):
"""
Expand Down Expand Up @@ -143,4 +135,4 @@ def get_index(image):
log.exception('An error occurred while attempting to retrieve the time series.')

log.debug(f'Time Series: {time_series}')
return time_series
return time_series
2 changes: 2 additions & 0 deletions tethysapp/earth_engine/gee/params.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from pathlib import Path

service_account = '' # your google service account
private_key = '' # path to the json private key for the service account
88 changes: 30 additions & 58 deletions tethysapp/earth_engine/gee/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
'terra': {
'snow': {
'display': 'Snow Cover Daily Global 500m',
'collection': 'MODIS/006/MOD10A1',
'collection': 'MODIS/061/MOD10A1',
'index': 'NDSI_Snow_Cover',
'vis_params': {
'min': 0.0,
Expand All @@ -15,7 +15,7 @@
},
'temperature': {
'display': 'Land Surface Temperature and Emissivity Daily Global 1km',
'collection': 'MODIS/006/MOD11A1',
'collection': 'MODIS/061/MOD11A1',
'index': 'LST_Day_1km',
'vis_params': {
'min': 13000.0,
Expand Down Expand Up @@ -98,91 +98,63 @@
}
},
'landsat': {
'7': {
'8': {
'surface': {
'display': 'Surface Reflectance',
'collection': 'LANDSAT/LE07/C01/T1_SR',
'collection': 'LANDSAT/LC08/C02/T1_L2',
'index': None,
'vis_params': {
'bands': ['B3', 'B2', 'B1'],
'bands': ['SR_B4', 'SR_B3', 'SR_B2'],
'min': 0,
'max': 3000,
'gamma': 1.4,
},
'cloud_mask': 'cloud_mask_l457',
'start_date': '1999-01-01',
'end_date': None # to present
},
'evi': {
'display': '8-day Enhanced Vegetation Index (EVI)',
'collection': 'LANDSAT/LE07/C01/T1_8DAY_EVI',
'index': 'EVI',
'vis_params': {
'min': 0.0,
'max': 1.0,
'palette': [
'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301'
],
},
'start_date': '1999-01-01',
'cloud_mask': 'mask_l8_sr',
'start_date': '2013-04-01',
'end_date': None # to present
},
'ndwi': {
'display': '8-day Normalized Difference Water Index (NDWI)',
'collection': 'LANDSAT/LE07/C01/T1_8DAY_NDWI',
'index': 'NDWI',
'toa': {
'display': 'Top-of-Atmosphere(TOA) Reflectance',
'collection': 'LANDSAT/LC08/C02/T1_TOA',
'index': None,
'vis_params': {
'min': 0.0,
'max': 1.0,
'palette': ['0000ff', '00ffff', 'ffff00', 'ff0000', 'ffffff'],
'bands': ['B4', 'B3', 'B2'],
'min': 0,
'max': 3000,
'gamma': 1.4,
},
'start_date': '1999-01-01',
'start_date': '2013-04-01',
'end_date': None # to present
},
},
'8': {
'9': {
'surface': {
'display': 'Surface Reflectance',
'collection': 'LANDSAT/LC08/C01/T1_SR',
'collection': 'LANDSAT/LC09/C02/T1_L2',
'index': None,
'vis_params': {
'bands': ['B4', 'B3', 'B2'],
'bands': ['SR_B4', 'SR_B3', 'SR_B2'],
'min': 0,
'max': 3000,
'gamma': 1.4,
},
'cloud_mask': 'mask_l8_sr',
'start_date': '2013-04-01',
'start_date': '2021-10-31',
'end_date': None # to present
},
'ndvi': {
'display': '8-day Normalized Difference Vegetation (NDVI)',
'collection': 'LANDSAT/LC08/C01/T1_8DAY_NDVI',
'index': 'NDVI',
'vis_params': {
'min': 0.0,
'max': 1.0,
'palette': [
'FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301'
],
},
'start_date': '2013-04-01',
'end_date': None # to present
},
'ndsi': {
'display': '8-day Normalized Difference Snow Index (NDSI)',
'collection': 'LANDSAT/LC08/C01/T1_8DAY_NDSI',
'index': 'NDSI',
'toa': {
'display': 'Top-of-Atmosphere(TOA) Reflectance',
'collection': 'LANDSAT/LC09/C02/T1_TOA',
'index': None,
'vis_params': {
'palette': ['000088', '0000FF', '8888FF', 'FFFFFF'],
'bands': ['B4', 'B3', 'B2'],
'min': 0,
'max': 3000,
'gamma': 1.4,
},
'start_date': '2013-04-01',
'start_date': '2021-10-31',
'end_date': None # to present
},
}
}
}
}
2 changes: 1 addition & 1 deletion tethysapp/earth_engine/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ def generate_figure(figure_title, time_series):
}
}

return figure
return figure
Loading

0 comments on commit a44e003

Please sign in to comment.