From fffac72b990b06be02b39b7c799a112ee4b3a912 Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 2 Dec 2024 11:15:19 +0100 Subject: [PATCH] adding new raster layers: water, population, forest gain --- .../src/geeAssets/forest-gain.ts | 39 +++++++++++++++ .../src/geeAssets/population-count.ts | 50 +++++++++++++++++++ .../src/geeAssets/surface-water.ts | 34 +++++++++++++ .../earth_engine_tiler/src/index.ts | 6 +++ .../src/tile-request.dto.ts | 5 +- 5 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 cloud_functions/earth_engine_tiler/src/geeAssets/forest-gain.ts create mode 100644 cloud_functions/earth_engine_tiler/src/geeAssets/population-count.ts create mode 100644 cloud_functions/earth_engine_tiler/src/geeAssets/surface-water.ts diff --git a/cloud_functions/earth_engine_tiler/src/geeAssets/forest-gain.ts b/cloud_functions/earth_engine_tiler/src/geeAssets/forest-gain.ts new file mode 100644 index 0000000..addb71d --- /dev/null +++ b/cloud_functions/earth_engine_tiler/src/geeAssets/forest-gain.ts @@ -0,0 +1,39 @@ +import { ContinuousDataset } from './earth-engine-dataset'; +import ee from '@google/earthengine'; +import { EarthEngineUtils } from "../earth-engine-utils"; + +export const ForestGain: ContinuousDataset = { + assetPath: { + default: "projects/glad/GLCLU2020/Forest_gain" + }, + + vizParams: { + bands: ['b1'], + min: 0, + max: 1, + palette: ['#008000'] + }, + + areYearsValid(startYear?: number, endYear?: number): boolean { + // This Asset is static, and year selector is irrelevant + return true; + }, + + getEEAsset() { + // Apply mask + const rawImage = ee.Image(this.assetPath.default); + const maskedImage = rawImage + .select('b1') + .updateMask(rawImage); // Mask out no-data pixels + + return maskedImage; + }, + + async getMapUrl(z, x, y, startYear, endYear) { + const image = this.getEEAsset() + + const mapId = await EarthEngineUtils.getMapId(image, this.vizParams); + + return ee.data.getTileUrl( mapId, x, y, z ); + }, +}; diff --git a/cloud_functions/earth_engine_tiler/src/geeAssets/population-count.ts b/cloud_functions/earth_engine_tiler/src/geeAssets/population-count.ts new file mode 100644 index 0000000..ea8ebed --- /dev/null +++ b/cloud_functions/earth_engine_tiler/src/geeAssets/population-count.ts @@ -0,0 +1,50 @@ +import { ContinuousDataset } from './earth-engine-dataset'; +import ee from '@google/earthengine'; +import { EarthEngineUtils } from "../earth-engine-utils"; + +export const PopulationCount: ContinuousDataset = { + assetPath: { + 1975: "JRC/GHSL/P2023A/GHS_POP/1975", + 1990: "JRC/GHSL/P2023A/GHS_POP/1990", + 2020: "JRC/GHSL/P2023A/GHS_POP/2020" + }, + + vizParams: { + bands: ['population_count'], + min: 0.0, + max: 100.0, + palette: ['000004', '320A5A', '781B6C', 'BB3654', 'EC6824', 'FBB41A', 'FCFFA4'] + }, + + areYearsValid(startYear?: number, endYear?: number): boolean { + const validYears = [1975, 1990, 2020]; + if (!startYear || !validYears.includes(startYear)) { + throw new Error(`Start Year '${startYear}' is not valid. Valid years are: ${validYears.join(", ")}`); + } + return true; + }, + + getEEAsset(startYear: string) { // Change the parameter to string as per the type definition + // Load the asset for the specified year + const assetPath = this.assetPath[parseInt(startYear)]; // Convert the string to number if necessary + if (!assetPath) { + throw new Error(`Asset not found for year '${startYear}'`); + } + + // Load the image and apply masking logic + const rawImage = ee.Image(assetPath); + return rawImage.updateMask(rawImage.gt(0)); // Mask out pixels with value 0 + }, + + async getMapUrl(z: number, x: number, y: number, startYear: number, endYear?: number) { + // Validate the year and get the asset + this.areYearsValid(startYear, endYear); + const image = this.getEEAsset(startYear); + + // Generate the mapId with visualization parameters + const mapId = await EarthEngineUtils.getMapId(image, this.vizParams); + + // Return the Tile URL + return ee.data.getTileUrl(mapId, x, y, z); + }, +}; diff --git a/cloud_functions/earth_engine_tiler/src/geeAssets/surface-water.ts b/cloud_functions/earth_engine_tiler/src/geeAssets/surface-water.ts new file mode 100644 index 0000000..300f07f --- /dev/null +++ b/cloud_functions/earth_engine_tiler/src/geeAssets/surface-water.ts @@ -0,0 +1,34 @@ +import { ContinuousDataset } from './earth-engine-dataset'; +import ee from '@google/earthengine'; +import {EarthEngineUtils} from "../earth-engine-utils"; + + +export const SurfaceWater: ContinuousDataset = { + assetPath: { + default: "JRC/GSW1_4/GlobalSurfaceWater" + }, + + vizParams: { + bands: ['occurrence'], + min: 0, + max: 100, + palette: ['ffffff', 'ffbbbb', '0000ff'] + }, + + areYearsValid (startYear?: number, endYear?: number) : boolean { + //This Asset is static, and year selector are irrelevant + return true; + }, + + getEEAsset() { + return ee.Image(this.assetPath.default); + }, + + async getMapUrl(z, x, y, startYear, endYear) { + const image = this.getEEAsset() + + const mapId = await EarthEngineUtils.getMapId(image, this.vizParams); + + 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 e830421..433b07a 100644 --- a/cloud_functions/earth_engine_tiler/src/index.ts +++ b/cloud_functions/earth_engine_tiler/src/index.ts @@ -17,6 +17,9 @@ import {GriddedLivestockGoat} from './geeAssets/gridded-livestock-goat'; import {GriddedLivestockHorse} from './geeAssets/gridded-livestock-horse'; import {GriddedLivestockPig} from './geeAssets/gridded-livestock-pig'; import {GriddedLivestockSheep} from './geeAssets/gridded-livestock-sheep'; +import {SurfaceWater} from './geeAssets/surface-water'; +import {ForestGain} from './geeAssets/forest-gain'; +import { PopulationCount } from './geeAssets/population-count'; import {EarthEngineDataset} from "./geeAssets/earth-engine-dataset"; import {TileRequestDTO, Tilesets} from "./tile-request.dto"; import {default as fetch , Response as FetchResponse} from "node-fetch"; @@ -39,6 +42,9 @@ const assets: Record = { [Tilesets.gridded_livestock_horse]: GriddedLivestockHorse, [Tilesets.gridded_livestock_pig]: GriddedLivestockPig, [Tilesets.gridded_livestock_sheep]: GriddedLivestockSheep, + [Tilesets.surface_water]: SurfaceWater, + [Tilesets.forest_gain]: ForestGain, + [Tilesets.population_count]: PopulationCount } //We're using express to simplify path parameter parsing for the Tiles endpoint 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 f272e3a..966f4de 100644 --- a/cloud_functions/earth_engine_tiler/src/tile-request.dto.ts +++ b/cloud_functions/earth_engine_tiler/src/tile-request.dto.ts @@ -15,7 +15,10 @@ export enum Tilesets { gridded_livestock_goat = "gridded_livestock_goat", gridded_livestock_horse = "gridded_livestock_horse", gridded_livestock_pig = "gridded_livestock_pig", - gridded_livestock_sheep = "gridded_livestock_sheep" + gridded_livestock_sheep = "gridded_livestock_sheep", + surface_water = "surface_water", + forest_gain = "forest_gain", + population_count = "population_count" }