Skip to content

Commit

Permalink
Merge pull request #229 from wri/feat/TM-846_site_polygons
Browse files Browse the repository at this point in the history
[TM-846] Start up controller for Site Polygons data
  • Loading branch information
cesarLima1 authored Jun 6, 2024
2 parents 8c2573b + b365352 commit 76309e2
Show file tree
Hide file tree
Showing 28 changed files with 1,464 additions and 44 deletions.
117 changes: 117 additions & 0 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace App\Helpers;

use App\Models\V2\PolygonGeometry;
use App\Models\V2\Projects\Project;
use Illuminate\Support\Facades\Log;

class GeometryHelper
{
public static function getPolygonsBbox($polygonsIds)
{
$envelopes = PolygonGeometry::whereIn('uuid', $polygonsIds)
->selectRaw('ST_ASGEOJSON(ST_Envelope(geom)) as envelope')
->get();

$maxX = $maxY = PHP_INT_MIN;
$minX = $minY = PHP_INT_MAX;

foreach ($envelopes as $envelope) {
$geojson = json_decode($envelope->envelope);
$coordinates = $geojson->coordinates[0];

foreach ($coordinates as $point) {
$x = $point[0];
$y = $point[1];
$maxX = max($maxX, $x);
$minX = min($minX, $x);
$maxY = max($maxY, $y);
$minY = min($minY, $y);
}
}

$bboxCoordinates = [$minX, $minY, $maxX, $maxY];

return $bboxCoordinates;
}

public function updateProjectCentroid(string $projectUuid)
{
try {
$centroid = $this->centroidOfProject($projectUuid);

if ($centroid === null) {
Log::warning("Invalid centroid for projectUuid: $projectUuid");
}

$centroidArray = json_decode($centroid, true);

$latitude = $centroidArray['coordinates'][1];
$longitude = $centroidArray['coordinates'][0];


Project::where('uuid', $projectUuid)
->update([
'lat' => $latitude,
'long' => $longitude,
]);


Log::info("Centroid updated for projectUuid: $projectUuid");

return 'Centroids updated successfully!';
} catch (\Exception $e) {
Log::error("Error updating centroid for projectUuid: $projectUuid");

return response()->json([
'message' => 'Error updating centroid',
'error' => $e->getMessage(),
], 500);
}

}

public function centroidOfProject($projectUuid)
{
$project = Project::where('uuid', $projectUuid)->first();

if (! $project) {
return null;
}

$polyIds = $project->sitePolygons()->pluck('poly_id')->toArray();

if (empty($polyIds)) {
return null;
}

$centroids = PolygonGeometry::selectRaw('ST_AsGeoJSON(ST_Centroid(geom)) AS centroid')
->whereIn('uuid', $polyIds)
->get();

if ($centroids->isEmpty()) {
return null; // Return null if no centroids are found
}

$centroidCount = $centroids->count();
$totalLatitude = 0;
$totalLongitude = 0;

foreach ($centroids as $centroid) {
$centroidData = json_decode($centroid->centroid, true);
$totalLatitude += $centroidData['coordinates'][1];
$totalLongitude += $centroidData['coordinates'][0];
}

$averageLatitude = $totalLatitude / $centroidCount;
$averageLongitude = $totalLongitude / $centroidCount;

$centroidOfCentroids = json_encode([
'type' => 'Point',
'coordinates' => [$averageLongitude, $averageLatitude],
]);

return $centroidOfCentroids;
}
}
44 changes: 44 additions & 0 deletions app/Http/Controllers/V2/Sites/SitePolygonDataController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers\V2\Sites;

use App\Helpers\GeometryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;

class SitePolygonDataController extends Controller
{
public function getSitePolygonData($site): JsonResponse
{
try {
$sitePolygons = SitePolygon::where('site_id', $site)->get();

return response()->json($sitePolygons);
} catch (\Exception $e) {
Log::error($e->getMessage());

return response()->json(['error' => 'An error occurred while fetching site polygons'], 500);
}
}

public function getBboxOfCompleteSite($site): JsonResponse
{
try {
$sitePolygons = SitePolygon::where('site_id', $site)->get();
if ($sitePolygons->isEmpty()) {
return response()->json(['error' => 'No polygons found for the site'], 404);
}

$polygonsIds = $sitePolygons->pluck('poly_id');
$bboxCoordinates = GeometryHelper::getPolygonsBbox($polygonsIds);

return response()->json(['bbox' => $bboxCoordinates]);
} catch (\Exception $e) {
Log::error($e->getMessage());

return response()->json(['error' => 'An error occurred while fetching the bounding box coordinates'], 500);
}
}
}
Loading

0 comments on commit 76309e2

Please sign in to comment.