From 2419473cdfb3782c62b9b4359f74a8148dc45865 Mon Sep 17 00:00:00 2001 From: sofia Date: Wed, 20 Nov 2024 12:29:49 +0100 Subject: [PATCH] add code for creating the total sum layer for livestock --- .../src/geeAssets/common-styles-utils.ts | 15 ++++++ .../src/geeAssets/gridded-livestock-total.ts | 54 +++++++++++++++++++ .../earth_engine_tiler/src/index.ts | 2 + .../src/tile-request.dto.ts | 1 + 4 files changed, 72 insertions(+) create mode 100644 cloud_functions/earth_engine_tiler/src/geeAssets/gridded-livestock-total.ts diff --git a/cloud_functions/earth_engine_tiler/src/geeAssets/common-styles-utils.ts b/cloud_functions/earth_engine_tiler/src/geeAssets/common-styles-utils.ts index befd537..2ab2d0d 100644 --- a/cloud_functions/earth_engine_tiler/src/geeAssets/common-styles-utils.ts +++ b/cloud_functions/earth_engine_tiler/src/geeAssets/common-styles-utils.ts @@ -11,4 +11,19 @@ export const GriddedLivestockCommonStyles = '' + '' + '' + +'' + +export const GriddedLivestockTotalStyle = + +'' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + '' \ No newline at end of file diff --git a/cloud_functions/earth_engine_tiler/src/geeAssets/gridded-livestock-total.ts b/cloud_functions/earth_engine_tiler/src/geeAssets/gridded-livestock-total.ts new file mode 100644 index 0000000..22c4eb5 --- /dev/null +++ b/cloud_functions/earth_engine_tiler/src/geeAssets/gridded-livestock-total.ts @@ -0,0 +1,54 @@ +import { CategoricalDataset } from './earth-engine-dataset'; +import ee from '@google/earthengine'; +import { EarthEngineUtils } from "../earth-engine-utils"; +import { GriddedLivestockBuffalo } from './gridded-livestock-buffalo'; +import { GriddedLivestockCattle } from './gridded-livestock-cattle'; +import { GriddedLivestockChicken } from './gridded-livestock-chicken'; +import { GriddedLivestockDuck } from './gridded-livestock-duck'; +import { GriddedLivestockGoat } from './gridded-livestock-goat'; +import { GriddedLivestockHorse } from './gridded-livestock-horse'; +import { GriddedLivestockPig } from './gridded-livestock-pig'; +import { GriddedLivestockSheep } from './gridded-livestock-sheep'; +import { GriddedLivestockTotalStyle } from "./common-styles-utils"; + +export const GriddedLivestockTotal: CategoricalDataset = { + assetPath: { + }, + bandName: 'b1', + sldStyles: GriddedLivestockTotalStyle, + + areYearsValid(startYear?: number, endYear?: number): boolean { + // This Asset is static, and year selector is irrelevant + return true; + }, + + // Unmask(0) gives 0 to no data pixels so that they can be included in the sum + getEEAsset() { + const buffaloImage = ee.Image(GriddedLivestockBuffalo.assetPath.default).select(GriddedLivestockBuffalo.bandName).unmask(0); + const cattleImage = ee.Image(GriddedLivestockCattle.assetPath.default).select(GriddedLivestockCattle.bandName).unmask(0); + const chickenImage = ee.Image(GriddedLivestockChicken.assetPath.default).select(GriddedLivestockChicken.bandName).unmask(0); + const duckImage = ee.Image(GriddedLivestockDuck.assetPath.default).select(GriddedLivestockDuck.bandName).unmask(0); + const goatImage = ee.Image(GriddedLivestockGoat.assetPath.default).select(GriddedLivestockGoat.bandName).unmask(0); + const horseImage = ee.Image(GriddedLivestockHorse.assetPath.default).select(GriddedLivestockHorse.bandName).unmask(0); + const pigImage = ee.Image(GriddedLivestockPig.assetPath.default).select(GriddedLivestockPig.bandName).unmask(0); + const sheepImage = ee.Image(GriddedLivestockSheep.assetPath.default).select(GriddedLivestockSheep.bandName).unmask(0); + + // Sum all the images + const totalImage = buffaloImage + .add(cattleImage) + .add(chickenImage) + .add(duckImage) + .add(goatImage) + .add(horseImage) + .add(pigImage) + .add(sheepImage); + + return totalImage; + }, + + async getMapUrl(z, x, y) { + const image = this.getEEAsset().sldStyle(this.sldStyles); + const mapId = await EarthEngineUtils.getMapId(image); + return ee.data.getTileUrl(mapId, x, y, z); + }, +}; \ No newline at end of file diff --git a/cloud_functions/earth_engine_tiler/src/index.ts b/cloud_functions/earth_engine_tiler/src/index.ts index 0138ce2..e830421 100644 --- a/cloud_functions/earth_engine_tiler/src/index.ts +++ b/cloud_functions/earth_engine_tiler/src/index.ts @@ -8,6 +8,7 @@ import {ModisNetPrimaryProductionChange} from './geeAssets/modis-net-primary-pro import {AnthropogenicBiomes} from './geeAssets/anthropogenic-biomes'; import {LivestockProductionSystems} from './geeAssets/livestock-production-systems'; import {ForestLoss} from './geeAssets/forest-loss'; +import {GriddedLivestockTotal} from './geeAssets/gridded-livestock-total'; import {GriddedLivestockBuffalo} from './geeAssets/gridded-livestock-buffalo'; import {GriddedLivestockCattle} from './geeAssets/gridded-livestock-cattle'; import {GriddedLivestockChicken} from './geeAssets/gridded-livestock-chicken'; @@ -29,6 +30,7 @@ const assets: Record = { [Tilesets.anthropogenic_biomes]: AnthropogenicBiomes, [Tilesets.livestock_production_systems]: LivestockProductionSystems, [Tilesets.forest_loss]: ForestLoss, + [Tilesets.gridded_livestock_total]: GriddedLivestockTotal, [Tilesets.gridded_livestock_buffalo]: GriddedLivestockBuffalo, [Tilesets.gridded_livestock_cattle]: GriddedLivestockCattle, [Tilesets.gridded_livestock_chicken]: GriddedLivestockChicken, diff --git a/cloud_functions/earth_engine_tiler/src/tile-request.dto.ts b/cloud_functions/earth_engine_tiler/src/tile-request.dto.ts index 54057c8..f272e3a 100644 --- a/cloud_functions/earth_engine_tiler/src/tile-request.dto.ts +++ b/cloud_functions/earth_engine_tiler/src/tile-request.dto.ts @@ -7,6 +7,7 @@ export enum Tilesets { anthropogenic_biomes = "anthropogenic_biomes", livestock_production_systems = "livestock_production_systems", forest_loss = "forest_loss", + gridded_livestock_total = "gridded_livestock_total", gridded_livestock_buffalo = "gridded_livestock_buffalo", gridded_livestock_cattle = "gridded_livestock_cattle", gridded_livestock_chicken = "gridded_livestock_chicken",