From 63c94ba075d736a72ce689472e6e728d918d09a4 Mon Sep 17 00:00:00 2001 From: Limber Mamani Date: Wed, 11 Dec 2024 15:16:19 -0400 Subject: [PATCH] [TM-1425] add helper tto restoration by ecoregion indicator --- app/Helpers/RestorationByEcoregionHelper.php | 76 +++++++++++++++++++ ...GetPolygonsIndicatorAnalysisController.php | 9 ++- .../IndicatorEntitySlugExportController.php | 7 +- app/Services/RunIndicatorAnalysisService.php | 7 +- 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100755 app/Helpers/RestorationByEcoregionHelper.php diff --git a/app/Helpers/RestorationByEcoregionHelper.php b/app/Helpers/RestorationByEcoregionHelper.php new file mode 100755 index 00000000..df19dc71 --- /dev/null +++ b/app/Helpers/RestorationByEcoregionHelper.php @@ -0,0 +1,76 @@ + [ + '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]; + } + } +} diff --git a/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php b/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php index 75a216ae..1fa19c41 100644 --- a/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php +++ b/app/Http/Controllers/V2/MonitoredData/GetPolygonsIndicatorAnalysisController.php @@ -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; @@ -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)); } diff --git a/app/Http/Controllers/V2/MonitoredData/IndicatorEntitySlugExportController.php b/app/Http/Controllers/V2/MonitoredData/IndicatorEntitySlugExportController.php index 7e501fa0..88a1301d 100644 --- a/app/Http/Controllers/V2/MonitoredData/IndicatorEntitySlugExportController.php +++ b/app/Http/Controllers/V2/MonitoredData/IndicatorEntitySlugExportController.php @@ -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; @@ -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)); } diff --git a/app/Services/RunIndicatorAnalysisService.php b/app/Services/RunIndicatorAnalysisService.php index bf397f0c..67843e8f 100644 --- a/app/Services/RunIndicatorAnalysisService.php +++ b/app/Services/RunIndicatorAnalysisService.php @@ -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);