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

[TM-846] Start up controller for Site Polygons data #229

Merged
merged 36 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
a87e794
[TM-846] Start up controller for Site Polygons data
cesarLima1 May 23, 2024
11a8dcb
[TM-846] fix lint
cesarLima1 May 23, 2024
2a0fec3
Merge branch 'staging' into feat/TM-846_site_polygons
cesarLima1 May 24, 2024
44207ae
Merge branch 'refactor/TM-851_modify_site_polygon_table' into feat/TM…
cesarLima1 May 24, 2024
75ed499
Merge branch 'refactor/TM-851_modify_site_polygon_table' into feat/TM…
cesarLima1 May 24, 2024
d65f520
[TM-846] add definition - create Site Polygon controller
cesarLima1 May 24, 2024
704675f
[TM-846] get polygon bounding box
cesarLima1 May 27, 2024
a651a27
[TM-853] add polygon and site validation controllers
cesarLima1 May 27, 2024
14785f6
[TM-853] Add download functionality for single and complete geojsons
egrojMonroy May 27, 2024
c411667
[TM-846] fix lint
cesarLima1 May 27, 2024
c6b2b7f
Merge branch 'feat/TM-846_site_polygons' into feat/TM-853_Polygon_check
cesarLima1 May 27, 2024
7596b7e
[TM-853] fix lint
cesarLima1 May 27, 2024
3161a7b
Merge branch 'feat/TM-853_Polygon_check' into feat/TM-853_download_sh…
egrojMonroy May 27, 2024
307fd62
[TM-853] endpoint to delete polygon and site polygon
egrojMonroy May 27, 2024
11d3d92
[TM-853] fix lint
egrojMonroy May 27, 2024
b5695f7
[TM-848] add site id when uploading polygon and recalculate calc_area
cesarLima1 May 27, 2024
9c5c5c9
Merge branch 'refactor/TM-851_modify_site_polygon_table' into feat/TM…
cesarLima1 Jun 4, 2024
5b8611d
Merge branch 'feat/TM-846_site_polygons' into feat/TM-853_Polygon_check
cesarLima1 Jun 4, 2024
f25b40d
Merge branch 'feat/TM-853_Polygon_check' into feat/TM-853_download_sh…
egrojMonroy Jun 4, 2024
4bfc457
[TM-853] add indentation
egrojMonroy Jun 4, 2024
a7b7027
Merge branch 'feat/TM-853_download_shapefiles' into feat/TM-853_delet…
egrojMonroy Jun 4, 2024
6761d2f
[TM-853] remove redundance and nesting
egrojMonroy Jun 4, 2024
81beb57
[TM-848] fix: remove site_id from createGeojsonModels function
cesarLima1 Jun 4, 2024
6c1e077
Merge branch 'feat/TM-853_delete_polygon_and_site' into fix/TM-848_up…
cesarLima1 Jun 4, 2024
69bbf03
[TM-853] remove redundan query
egrojMonroy Jun 4, 2024
f702446
Merge branch 'refactor/TM-851_modify_site_polygon_table' into feat/TM…
cesarLima1 Jun 6, 2024
7da19b7
Merge branch 'feat/TM-846_site_polygons' into feat/TM-853_Polygon_check
cesarLima1 Jun 6, 2024
46760ee
Merge branch 'feat/TM-853_Polygon_check' into feat/TM-853_download_sh…
egrojMonroy Jun 6, 2024
530c3ce
Merge branch 'feat/TM-853_download_shapefiles' into feat/TM-853_delet…
egrojMonroy Jun 6, 2024
b2f321f
Merge branch 'feat/TM-853_delete_polygon_and_site' into fix/TM-848_up…
cesarLima1 Jun 6, 2024
a60d1ac
Merge pull request #234 from wri/fix/TM-848_upload_polygon
cesarLima1 Jun 6, 2024
5e86e36
Merge pull request #233 from wri/feat/TM-853_delete_polygon_and_site
cesarLima1 Jun 6, 2024
4b1e912
Merge pull request #232 from wri/feat/TM-853_download_shapefiles
cesarLima1 Jun 6, 2024
18e6ecb
Merge pull request #231 from wri/feat/TM-853_Polygon_check
cesarLima1 Jun 6, 2024
d01369f
[TM-846] make lint fix
cesarLima1 Jun 6, 2024
b365352
[TM-846] fix issues with definitions in openapi src folder
cesarLima1 Jun 6, 2024
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
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
Loading