Skip to content

Commit

Permalink
[TM-1425] add helper tto restoration by ecoregion indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
LimberHope committed Dec 11, 2024
1 parent 379d097 commit 63c94ba
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
76 changes: 76 additions & 0 deletions app/Helpers/RestorationByEcoregionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace App\Helpers;

class RestorationByEcoregionHelper
{
public static function getCategoryEcoRegion($value, ?bool $isExport = false) {
$categoriesFromEcoRegion = [
'australasian' => [
'Southeast Australia temperate forests',
'Madeira-Tapajós moist forests',
'Tocantins/Pindare moist forests',
'Tapajós-Xingu moist forests',
'Mato Grosso seasonal forests',
'Mato Grosso seasonal forests, Xingu-Tocantins-Araguaia moist forests',
'Bahia coastal forests',
'Tonle Sap freshwater swamp forests',
],
'afrotropical' => [
'Sinú Valley dry forests',
'Santa Marta montane forests',
'Atlantic mixed forests',
'Petén-Veracruz moist forests',
'Central American Atlantic moist forests',
'Petén-Veracruz moist forests, Central American Atlantic moist forests',
'Central American montane forests',
'Central American Atlantic moist forests, Central American montane forests',
'Northern Acacia-Commiphora bushlands and thickets',
'Southern Rift montane forest-grassland mosaic',
'Sierra Madre de Chiapas moist forests',
'Iberian sclerophyllous and semi-deciduous forests',
'Northwest Iberian montane forests',
'Northwestern Congolian lowland forests',
'Albertine Rift montane forests',
'Sahelian Acacia savanna',
'Northern Congolian forest-savanna mosaic',
'Nigerian lowland forests',
'West Sudanian savanna',
'Northern Congolian forest-savanna mosaic, Northwestern Congolian lowland forests',
'Eastern Guinean forests',
'Victoria Basin forest-savanna mosaic',
'Guinean forest-savanna mosaic',
'East Sudanian savanna',
'Central Zambezian Miombo woodlands',
'Ethiopian montane grasslands and woodlands',
'Central African mangroves',
],
'paleartic' => [
'southern-zanzibar-inhambane-coastal-forest-mosaic',
]
];
$formatedValue = [];
foreach ($categoriesFromEcoRegion as $category => $values) {
$formatedValue[$category] = 0;
foreach ($value as $key => $val) {
if (in_array($key, $values)) {
$formatedValue[$category] = round((float) $val, 3);
break;
}
}
}

$result = array_filter($formatedValue, function($val) {
return $val !== 0;
});

if (empty($result)) {
return $result;
}
if ($isExport) {
return $result;
} else {
return ['data' => $result];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\V2\MonitoredData;

use App\Helpers\RestorationByEcoregionHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\EntityModel;
use App\Models\V2\Projects\Project;
Expand Down Expand Up @@ -94,7 +95,13 @@ public function __invoke(EntityModel $entity, string $slug)
$results['data']['2023'] = round((float) $valueYears['2023'], 3);
$results['data']['2024'] = round((float) $valueYears['2024'], 3);
}
if (str_contains($slug, 'restorationBy')) {

if ($slug == 'restorationByEcoRegion') {
$values = json_decode($indicator->value, true);
$results = array_merge($results, RestorationByEcoregionHelper::getCategoryEcoRegion($values));
}

if ($slug == 'restorationByLandUse' || $slug == 'restorationByStrategy') {
$values = json_decode($indicator->value, true);
$results = array_merge($results, $this->processValuesHectares($values));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\V2\MonitoredData;

use App\Helpers\RestorationByEcoregionHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\EntityModel;
use App\Models\V2\Projects\Project;
Expand Down Expand Up @@ -141,7 +142,11 @@ public function exportCsv($entity, $slug)
$results['2023'] = $valueYears['2023'];
$results['2024'] = $valueYears['2024'];
}
if (str_contains($slug, 'restorationBy')) {
if ($slug == 'restorationByEcoRegion') {
$values = json_decode($indicator->value, true);
$results = array_merge($results, RestorationByEcoregionHelper::getCategoryEcoRegion($values, true));
}
if ($slug == 'restorationByLandUse' || $slug == 'restorationByStrategy') {
$values = json_decode($indicator->value, true);
$results = array_merge($results, $this->processValuesHectares($values));
}
Expand Down
7 changes: 6 additions & 1 deletion app/Services/RunIndicatorAnalysisService.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,16 @@ public function run(array $request, string $slug)
$geojson = GeometryHelper::getPolygonGeojson($uuid);
$indicatorRestorationResponse = App::make(PythonService::class)->IndicatorPolygon($geojson, $slugMappings[$slug]['indicator'], getenv('GFW_SECRET_KEY'));

if ($slug == 'restorationByEcoRegion') {
$value = json_encode($indicatorRestorationResponse['area'][$slugMappings[$slug]['indicator']]);
} else {
$value = $this->formatKeysValues($indicatorRestorationResponse['area'][$slugMappings[$slug]['indicator']]);
}
$data = [
'indicator_slug' => $slug,
'site_polygon_id' => $polygonGeometry['site_polygon_id'],
'year_of_analysis' => Carbon::now()->year,
'value' => $this->formatKeysValues($indicatorRestorationResponse['area'][$slugMappings[$slug]['indicator']]),
'value' => $value,
];
$slugMappings[$slug]['model']::create($data);

Expand Down

0 comments on commit 63c94ba

Please sign in to comment.