Skip to content

Commit

Permalink
adding new raster layers: water, population, forest gain
Browse files Browse the repository at this point in the history
  • Loading branch information
SofiaAldabet committed Dec 2, 2024
1 parent 2c67140 commit 71d31a1
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
39 changes: 39 additions & 0 deletions cloud_functions/earth_engine_tiler/src/geeAssets/forest-gain.ts
Original file line number Diff line number Diff line change
@@ -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 );
},
};
Original file line number Diff line number Diff line change
@@ -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);
},
};
34 changes: 34 additions & 0 deletions cloud_functions/earth_engine_tiler/src/geeAssets/surface-water.ts
Original file line number Diff line number Diff line change
@@ -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 );
},
};
6 changes: 6 additions & 0 deletions cloud_functions/earth_engine_tiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {GriddedLivestockGoat} from './geeAssets/gridded-livestock-goat';
import {GriddedLivestockHorse} from './geeAssets/gridded-livestock-horse';
import {GriddedLivestockBuffalo} from './geeAssets/gridded-livestock-buffalo';
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";
Expand All @@ -31,6 +34,9 @@ const assets: Record<Tilesets, EarthEngineDataset> = {
[Tilesets.gridded_livestock_horse]: GriddedLivestockHorse,
[Tilesets.gridded_livestock_buffalo]: GriddedLivestockBuffalo,
[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
Expand Down
5 changes: 4 additions & 1 deletion cloud_functions/earth_engine_tiler/src/tile-request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export enum Tilesets {
gridded_livestock_goat = "gridded_livestock_goat",
gridded_livestock_horse = "gridded_livestock_horse",
gridded_livestock_buffalo = "gridded_livestock_buffalo",
gridded_livestock_sheep = "gridded_livestock_sheep"
gridded_livestock_sheep = "gridded_livestock_sheep",
surface_water = "surface_water",
forest_gain = "forest_gain",
population_count = "population_count"
}


Expand Down

0 comments on commit 71d31a1

Please sign in to comment.