Skip to content

Commit

Permalink
[TM-1467] Adjust GFW data areas with correction factor using ESRI:54009
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarLima1 committed Dec 20, 2024
1 parent a2df0d6 commit 3c1fe63
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions app/Services/RunIndicatorAnalysisService.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,32 @@ public function sendApiRequestIndicator($secret_key, $query_url, $query_sql, $ge
'sql' => $query_sql,
'geometry' => $geometry,
]);

if ($response->successful()) {
$gfwDataFile = tempnam(sys_get_temp_dir(), 'gfw_') . '.json';
$geometryFile = tempnam(sys_get_temp_dir(), 'geom_') . '.json';
$outputFile = tempnam(sys_get_temp_dir(), 'output_') . '.json';

try {
file_put_contents($gfwDataFile, json_encode($response->json()));
file_put_contents($geometryFile, json_encode($geometry));

$process = new Process([
'python3',
base_path() . '/resources/python/gfw-area-adjustment/app.py',
$gfwDataFile,
$geometryFile,
$outputFile,
]);

$process->run();
if (!$process->isSuccessful()) {

if (! $process->isSuccessful()) {
Log::error('Area adjustment failed: ' . $process->getErrorOutput());

return $response;
}

$adjustedData = json_decode(file_get_contents($outputFile), true);

return new \Illuminate\Http\Client\Response(
Expand All @@ -175,17 +176,18 @@ public function sendApiRequestIndicator($secret_key, $query_url, $query_sql, $ge
json_encode($adjustedData)
)
);

} catch (\Exception $e) {
Log::error('Error adjusting areas: ' . $e->getMessage());

return $response;
} finally {
@unlink($gfwDataFile);
@unlink($geometryFile);
@unlink($outputFile);
}
}

return $response;
}

Expand Down

0 comments on commit 3c1fe63

Please sign in to comment.