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

Feat manage asset #320

Merged
merged 6 commits into from
Feb 6, 2025
Merged
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
12 changes: 11 additions & 1 deletion django_project/analysis/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
InterventionArea,
Landscape,
LandscapeCommunity,
UserAnalysisResults
UserAnalysisResults,
GEEAsset
)


Expand Down Expand Up @@ -112,3 +113,12 @@ def view_analysis_results(self, obj):


admin.site.register(UserAnalysisResults, UserAnalysisResultsAdmin)


@admin.register(GEEAsset)
class GEEAssetAdmin(admin.ModelAdmin):
"""Admin for GEEAsset model."""

list_display = ('key', 'type', 'source',)
search_fields = ('key', 'source',)
list_filter = ('type',)
70 changes: 40 additions & 30 deletions django_project/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import ee
import os

from analysis.models import GEEAsset

SERVICE_ACCOUNT_KEY = os.environ.get('SERVICE_ACCOUNT_KEY', '')
SERVICE_ACCOUNT = os.environ.get('SERVICE_ACCOUNT', '')

TRAINING_DATA_ASSET_PATH = os.environ.get(
'TRAINING_DATA_ASSET_PATH',
''
)

# Sentinel-2 bands and names
S2_BANDS = ['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B11', 'B12']
S2_NAMES = [
Expand Down Expand Up @@ -45,7 +42,7 @@ def get_baseline_table(self):
Get baseline feature collection for GEE analysis
"""
baseline_table = ee.FeatureCollection(
'projects/ee-yekelaso1818/assets/CSA/Baseline_pre_export_20241007'
GEEAsset.fetch_asset_source('baseline_table')
)
return baseline_table

Expand All @@ -55,7 +52,7 @@ def get_temporal_table(self):
"""
# Get the pre-exported time series statistics for project areas
temporal_table = ee.FeatureCollection(
'projects/ee-yekelaso1818/assets/Temporal_pre_export_20241202'
GEEAsset.fetch_asset_source('temporal_table')
)

# Format the table correctly
Expand Down Expand Up @@ -137,7 +134,7 @@ def get_communities(self):
Get community feature collection for GEE analysis.
"""
communities = ee.FeatureCollection(
'projects/ee-yekelaso1818/assets/CSA/CSA_master_20241007'
GEEAsset.fetch_asset_source('communities')
)
communities = communities.map(
lambda ft: ft.set(
Expand All @@ -156,15 +153,18 @@ def get_countries(self):
'NAMIBIA', 'ZIMBABWE', 'BOTSWANA',
'MOZAMBIQUE', 'ZAMBIA'
]
countries = (ee.FeatureCollection('USDOS/LSIB/2013').
filter(ee.Filter.inList('name', names)))
countries = (ee.FeatureCollection(
GEEAsset.fetch_asset_source('countries')).
filter(ee.Filter.inList('name', names)))
return countries

def get_cropland_urban_mask(self):
"""
Get Cropland and urban mask
"""
glc_coll = ee.ImageCollection('users/cgmorton/GlobeLand30')
glc_coll = ee.ImageCollection(
GEEAsset.fetch_asset_source('globe_land30')
)
glc_img = glc_coll.mosaic()

masked = (glc_img.neq(10)
Expand Down Expand Up @@ -194,12 +194,12 @@ def get_soil_carbon(self):
Get image for soil carbon mean.
"""
# Coast fragment fraction 0-1
cfvo = (ee.Image('users/zandersamuel/SA_misc/Soilgrids_CFVO')
cfvo = (ee.Image(GEEAsset.fetch_asset_source('soc_grids_cfvo'))
.selfMask()
.rename('soil_cfvo')
.divide(1000))

cfvo2 = (ee.Image("ISDASOIL/Africa/v1/stone_content")
cfvo2 = (ee.Image(GEEAsset.fetch_asset_source('soc_stone_content'))
.select('mean_0_20')
.rename('soil_cfvo')
.divide(100))
Expand All @@ -211,17 +211,16 @@ def get_soil_carbon(self):
# Fill in gaps with SoilGrids

isda = ee.Image(
"users/zandersamuel/Africa_misc/"
"iSDA_SOC_m_30m_0_20cm_2001_2017_v0_13_wgs84"
GEEAsset.fetch_asset_source('soc_isda')
)
isda = ee.Image(isda.divide(10)).exp().subtract(1)
# Soil bulk density (fine earth) g/m³
bd = (ee.Image("users/zandersamuel/SA_misc/SoilGrids_BD")
bd = (ee.Image(GEEAsset.fetch_asset_source('soc_grids_bd'))
.rename('soil_bd')
.selfMask()
.divide(100))

bd2 = (ee.Image("ISDASOIL/Africa/v1/bulk_density")
bd2 = (ee.Image(GEEAsset.fetch_asset_source('soc_bulk_density'))
.select('mean_0_20')
.rename('soil_bd')
.divide(100))
Expand Down Expand Up @@ -252,7 +251,7 @@ def get_grazing_capacity(self):

# Import pre-exported grazing capacity map
grazing_capacity = ee.Image(
'users/zandersamuel/Consult_CSA/grazingCapacity_srnAfrica_LSU_ha'
GEEAsset.fetch_asset_source('grazing_capacity')
)
grazing_capacity = grazing_capacity.rename('grazingCap')
grazing_capacity = (grazing_capacity
Expand All @@ -268,7 +267,7 @@ def get_soc_col(self):
# Import soil organic carbon data from Venter et al. 2021
# https://www.sciencedirect.com/science/article/pii/S0048969721004526
soc_col = ee.ImageCollection(
"users/grazingresearch/Collaboration/Soil_C/predictions2"
GEEAsset.fetch_asset_source('soil_carbon')
)

def process_image(i):
Expand Down Expand Up @@ -299,10 +298,14 @@ def get_spatial_layer_dict(self):
Get spatial layer dictionary.
"""
# Get MODIS vegetation data
modis_veg = (ee.ImageCollection("MODIS/006/MOD13Q1")
.filterDate('2016-01-01', '2020-01-01')
.select(['NDVI', 'EVI'])
.map(lambda i: i.divide(10000)))
modis_veg = (
ee.ImageCollection(
GEEAsset.fetch_asset_source('modis_vegetation')
)
.filterDate('2016-01-01', '2020-01-01')
.select(['NDVI', 'EVI'])
.map(lambda i: i.divide(10000))
)

evi_baseline = (modis_veg.select('EVI').
median().clipToCollection(self.countries))
Expand All @@ -312,7 +315,7 @@ def get_spatial_layer_dict(self):
# Get fractional ground cover from CGLS
cgls_col = (
ee.ImageCollection(
"COPERNICUS/Landcover/100m/Proba-V-C3/Global"
GEEAsset.fetch_asset_source('cgls_ground_cover')
).select(
[
'bare-coverfraction', 'crops-coverfraction',
Expand Down Expand Up @@ -654,12 +657,16 @@ def get_s2_cloud_masked(aoi, start_date, end_date):
>>> # Print the number of images retrieved
>>> print('Number of images:', s2_collection.size().getInfo())
"""
s2_sr = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED') \
s2_sr = ee.ImageCollection(
GEEAsset.fetch_asset_source('sentinel2_harmonized')
) \
.filterBounds(aoi) \
.filterDate(start_date, end_date) \
.filter(ee.Filter.lte('CLOUDY_PIXEL_PERCENTAGE', 20))

s2_clouds = ee.ImageCollection('COPERNICUS/S2_CLOUD_PROBABILITY') \
s2_clouds = ee.ImageCollection(
GEEAsset.fetch_asset_source('sentinel2_clouds')
) \
.filterBounds(aoi) \
.filterDate(start_date, end_date)

Expand Down Expand Up @@ -863,7 +870,7 @@ def get_sent_quarterly(aoi):
return sent_quarterly


def train_bgt(aoi, training_path=TRAINING_DATA_ASSET_PATH):
def train_bgt(aoi, training_path):
"""
Trains a Random Forest classifier to estimate
bare ground, tree, and grass cover fractions.
Expand All @@ -873,7 +880,7 @@ def train_bgt(aoi, training_path=TRAINING_DATA_ASSET_PATH):
aoi : ee.Geometry
The area of interest over which to filter the training data.
training_path : str
The training data asset path. Default to TRAINING_DATA_ASSET_PATH.
The training data asset path.

Returns
-------
Expand All @@ -889,10 +896,11 @@ def train_bgt(aoi, training_path=TRAINING_DATA_ASSET_PATH):

Example
-------
>>> training_path = ''
>>> # Define an area of interest
>>> aoi = ee.Geometry.Rectangle([30.0, -1.0, 30.1, -0.9])
>>> # Train the classifier
>>> classifier = train_bgt(aoi)
>>> classifier = train_bgt(aoi, training_path)
"""
training_testing_master = ee.FeatureCollection(training_path)
training_testing = training_testing_master.filterBounds(aoi)
Expand Down Expand Up @@ -974,7 +982,9 @@ def get_latest_stats(geo, communities_select):
>>> print(stats.first().getInfo())
"""
col = get_sent_quarterly(communities_select)
classifier = train_bgt(geo)
classifier = train_bgt(
geo, GEEAsset.fetch_asset_source('random_forest_training')
)

def process_image(i):
bg = classify_bgt(i, classifier).select('bare')
Expand Down
164 changes: 164 additions & 0 deletions django_project/analysis/fixtures/2.gee_asset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
[
{
"model": "analysis.geeasset",
"pk": 1,
"fields": {
"key": "baseline_table",
"source": "projects/ee-yekelaso1818/assets/CSA/Baseline_pre_export_20241007",
"type": "table"
}
},
{
"model": "analysis.geeasset",
"pk": 2,
"fields": {
"key": "temporal_table",
"source": "projects/ee-yekelaso1818/assets/Temporal_pre_export_20241202",
"type": "table"
}
},
{
"model": "analysis.geeasset",
"pk": 3,
"fields": {
"key": "communities",
"source": "projects/ee-yekelaso1818/assets/CSA/CSA_master_20241007",
"type": "table"
}
},
{
"model": "analysis.geeasset",
"pk": 4,
"fields": {
"key": "countries",
"source": "USDOS/LSIB/2013",
"type": "table"
}
},
{
"model": "analysis.geeasset",
"pk": 5,
"fields": {
"key": "globe_land30",
"source": "users/cgmorton/GlobeLand30",
"type": "image_collection"
}
},
{
"model": "analysis.geeasset",
"pk": 6,
"fields": {
"key": "soil_carbon",
"source": "users/grazingresearch/Collaboration/Soil_C/predictions2",
"type": "image_collection"
}
},
{
"model": "analysis.geeasset",
"pk": 7,
"fields": {
"key": "modis_vegetation",
"source": "MODIS/006/MOD13Q1",
"type": "image_collection"
}
},
{
"model": "analysis.geeasset",
"pk": 8,
"fields": {
"key": "cgls_ground_cover",
"source": "COPERNICUS/Landcover/100m/Proba-V-C3/Global",
"type": "image_collection"
}
},
{
"model": "analysis.geeasset",
"pk": 9,
"fields": {
"key": "sentinel2_harmonized",
"source": "COPERNICUS/S2_SR_HARMONIZED",
"type": "image_collection"
}
},
{
"model": "analysis.geeasset",
"pk": 10,
"fields": {
"key": "sentinel2_clouds",
"source": "COPERNICUS/S2_CLOUD_PROBABILITY",
"type": "image_collection"
}
},
{
"model": "analysis.geeasset",
"pk": 11,
"fields": {
"key": "soc_grids_cfvo",
"source": "users/zandersamuel/SA_misc/Soilgrids_CFVO",
"type": "image"
}
},
{
"model": "analysis.geeasset",
"pk": 12,
"fields": {
"key": "soc_stone_content",
"source": "ISDASOIL/Africa/v1/stone_content",
"type": "image"
}
},
{
"model": "analysis.geeasset",
"pk": 13,
"fields": {
"key": "soc_isda",
"source": "users/zandersamuel/Africa_misc/iSDA_SOC_m_30m_0_20cm_2001_2017_v0_13_wgs84",
"type": "image"
}
},
{
"model": "analysis.geeasset",
"pk": 14,
"fields": {
"key": "soc_grids_bd",
"source": "users/zandersamuel/SA_misc/SoilGrids_BD",
"type": "image"
}
},
{
"model": "analysis.geeasset",
"pk": 15,
"fields": {
"key": "soc_bulk_density",
"source": "ISDASOIL/Africa/v1/bulk_density",
"type": "image"
}
},
{
"model": "analysis.geeasset",
"pk": 16,
"fields": {
"key": "grazing_capacity",
"source": "users/zandersamuel/Consult_CSA/grazingCapacity_srnAfrica_LSU_ha",
"type": "image"
}
},
{
"model": "analysis.geeasset",
"pk": 17,
"fields": {
"key": "random_forest_training",
"source": "users/zandersamuel/Consult_CSA/Training_data_TOA_bg_t_g",
"type": "table"
}
},
{
"model": "analysis.geeasset",
"pk": 18,
"fields": {
"key": "fire_freq",
"source": "users/zandersamuel/Consult_CSA/fire_freq_SrnAfrica_200m",
"type": "image"
}
}
]
Loading