From 445b6b4de63e10890137f5ab2d274fe3354e84ad Mon Sep 17 00:00:00 2001 From: JORGE Date: Sun, 26 Jan 2025 23:34:23 -0400 Subject: [PATCH] [TM-1634] add endpoint for landscape bbox --- .../V2/Dashboard/GetPolygonsController.php | 35 +++++++++++++++++++ app/Models/LandscapeGeom.php | 22 ++++++++++++ database/seeders/LandscapeGeomTableSeeder.php | 21 ++++++----- .../V2/definitions/DashboardBBOXLandscape.yml | 6 ++++ openapi-src/V2/definitions/_index.yml | 2 ++ .../get-v2-dashboard-get-bbox-landscape.yml | 16 +++++++++ openapi-src/V2/paths/_index.yml | 3 ++ resources/docs/swagger-v2.yml | 30 ++++++++++++++++ routes/api_v2.php | 1 + 9 files changed, 125 insertions(+), 11 deletions(-) create mode 100644 app/Models/LandscapeGeom.php create mode 100644 openapi-src/V2/definitions/DashboardBBOXLandscape.yml create mode 100644 openapi-src/V2/paths/Dashboard/get-v2-dashboard-get-bbox-landscape.yml diff --git a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php index e6e79e1cf..c8f84b4ec 100644 --- a/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php +++ b/app/Http/Controllers/V2/Dashboard/GetPolygonsController.php @@ -6,6 +6,7 @@ use App\Helpers\TerrafundDashboardQueryHelper; use App\Http\Controllers\Controller; use App\Http\Resources\V2\Dashboard\GetPolygonsResource; +use App\Models\LandscapeGeom; use App\Models\V2\PolygonGeometry; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; @@ -58,4 +59,38 @@ public function getProjectBbox(Request $request) return response()->json(['error' => 'An error occurred while fetching the bounding box coordinates'], 404); } } + public function getLandscapeBbox(Request $request) + { + $landscape = $request->input('landscape'); + + $envelopes = LandscapeGeom::where('landscape', $landscape) + ->selectRaw('ST_AsGeoJSON(ST_Envelope(geometry)) as envelope') + ->get(); + + if ($envelopes->isEmpty()) { + return null; + } + + $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); + } + } + + return [ + 'bbox' => [$minX, $minY, $maxX, $maxY], + 'landscape' => $landscape + ]; + } }; diff --git a/app/Models/LandscapeGeom.php b/app/Models/LandscapeGeom.php new file mode 100644 index 000000000..203fa6c2f --- /dev/null +++ b/app/Models/LandscapeGeom.php @@ -0,0 +1,22 @@ +where('landscape', $landscape); + } +} diff --git a/database/seeders/LandscapeGeomTableSeeder.php b/database/seeders/LandscapeGeomTableSeeder.php index f211f9ae9..0eee6578d 100644 --- a/database/seeders/LandscapeGeomTableSeeder.php +++ b/database/seeders/LandscapeGeomTableSeeder.php @@ -2,7 +2,6 @@ namespace Database\Seeders; -use Illuminate\Database\Console\Seeds\WithoutModelEvents; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; @@ -13,18 +12,18 @@ class LandscapeGeomTableSeeder extends Seeder */ public function run(): void { - DB::table('landscape_geom')->delete(); + DB::table('landscape_geom')->delete(); - $geojson = json_decode(file_get_contents(database_path('seeders/Landscapes_polygons.geojson')), true); + $geojson = json_decode(file_get_contents(database_path('seeders/Landscapes_polygons.geojson')), true); - foreach ($geojson['features'] as $feature) { - $geometry = json_encode($feature['geometry']); - $landscape = $feature['properties']['landscape']; + foreach ($geojson['features'] as $feature) { + $geometry = json_encode($feature['geometry']); + $landscape = $feature['properties']['landscape']; - DB::table('landscape_geom')->insert([ - 'geometry' => DB::raw("ST_GeomFromGeoJSON('$geometry')"), - 'landscape' => $landscape, - ]); - } + DB::table('landscape_geom')->insert([ + 'geometry' => DB::raw("ST_GeomFromGeoJSON('$geometry')"), + 'landscape' => $landscape, + ]); + } } } diff --git a/openapi-src/V2/definitions/DashboardBBOXLandscape.yml b/openapi-src/V2/definitions/DashboardBBOXLandscape.yml new file mode 100644 index 000000000..da26f3ec8 --- /dev/null +++ b/openapi-src/V2/definitions/DashboardBBOXLandscape.yml @@ -0,0 +1,6 @@ +type: object +properties: + bbox: + type: array + items: + type: number \ No newline at end of file diff --git a/openapi-src/V2/definitions/_index.yml b/openapi-src/V2/definitions/_index.yml index c3092643f..ce1d1eaa9 100644 --- a/openapi-src/V2/definitions/_index.yml +++ b/openapi-src/V2/definitions/_index.yml @@ -320,6 +320,8 @@ DashboardGetPolygonStatusResponse: $ref: './DashboardGetPolygonStatusResponse.yml' DashboardBBOXProject: $ref: './DashboardBBOXProject.yml' +DashboardBBOXLandscape: + $ref: './DashboardBBOXLandscape.yml' DashboardBBOXCountry: $ref: './DashboardBBOXCountry.yml' DashboardPolygonData: diff --git a/openapi-src/V2/paths/Dashboard/get-v2-dashboard-get-bbox-landscape.yml b/openapi-src/V2/paths/Dashboard/get-v2-dashboard-get-bbox-landscape.yml new file mode 100644 index 000000000..9e16925aa --- /dev/null +++ b/openapi-src/V2/paths/Dashboard/get-v2-dashboard-get-bbox-landscape.yml @@ -0,0 +1,16 @@ +summary: Get Bbox of all polygons of project +tags: + - Projects +parameters: + - name: landscape + type: string + in: query + description: 'The landscape identifier (e.g., "Ghana Cocoa Belt")' + required: true +responses: + '200': + description: Successful response + schema: + $ref: ../../definitions/_index.yml#/DashboardBBOXLandscape + '404': + description: Project not found diff --git a/openapi-src/V2/paths/_index.yml b/openapi-src/V2/paths/_index.yml index 1cfaab18d..47fcd7d68 100644 --- a/openapi-src/V2/paths/_index.yml +++ b/openapi-src/V2/paths/_index.yml @@ -2709,6 +2709,9 @@ /v2/dashboard/bbox/project: get: $ref: './Dashboard/get-v2-dashboard-get-bbox-project.yml' +/v2/dashboard/bbox/landscape: + get: + $ref: './Dashboard/get-v2-dashboard-get-bbox-landscape.yml' /v2/dashboard/country/{country}: get: $ref: './Dashboard/get-v2-dashboard-country.yml' diff --git a/resources/docs/swagger-v2.yml b/resources/docs/swagger-v2.yml index 13baa0300..a54b4568e 100644 --- a/resources/docs/swagger-v2.yml +++ b/resources/docs/swagger-v2.yml @@ -43330,6 +43330,13 @@ definitions: type: array items: type: number + DashboardBBOXLandscape: + type: object + properties: + bbox: + type: array + items: + type: number DashboardBBOXCountry: type: object properties: @@ -97542,6 +97549,29 @@ paths: type: number '404': description: Project not found + /v2/dashboard/bbox/landscape: + get: + summary: Get Bbox of all polygons of project + tags: + - Projects + parameters: + - name: landscape + type: string + in: query + description: 'The landscape identifier (e.g., "Ghana Cocoa Belt")' + required: true + responses: + '200': + description: Successful response + schema: + type: object + properties: + bbox: + type: array + items: + type: number + '404': + description: Project not found '/v2/dashboard/country/{country}': get: summary: Get the bounding box of a country diff --git a/routes/api_v2.php b/routes/api_v2.php index 44e7b99a7..ab7211eb9 100644 --- a/routes/api_v2.php +++ b/routes/api_v2.php @@ -749,6 +749,7 @@ function () { Route::get('/get-bbox-project', [GetPolygonsController::class, 'getBboxOfCompleteProject']); Route::get('/bbox/project', [GetPolygonsController::class, 'getProjectBbox']); Route::get('/country/{country}', [CountryDataController::class, 'getCountryBbox']); + Route::get('/bbox/landscape', [GetPolygonsController::class, 'getLandscapeBbox']); Route::get('/polygon-data/{uuid}', [CountryDataController::class, 'getPolygonData']); Route::get('/project-data/{uuid}', [CountryDataController::class, 'getProjectData']); Route::get('/active-projects', ActiveProjectsTableController::class);