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

adding new raster layers: water, population, forest gain #89

Merged
merged 1 commit into from
Dec 11, 2024
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
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check for this validation here, as it is done previously on the validation block of the main GET endpoint

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 @@ -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";
Expand All @@ -39,6 +42,9 @@ const assets: Record<Tilesets, EarthEngineDataset> = {
[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
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 @@ -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"
}


Expand Down