Skip to content

Commit

Permalink
[TM-1634] add endpoint for landscape bbox
Browse files Browse the repository at this point in the history
  • Loading branch information
egrojMonroy committed Jan 27, 2025
1 parent 93b0e55 commit 445b6b4
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 11 deletions.
35 changes: 35 additions & 0 deletions app/Http/Controllers/V2/Dashboard/GetPolygonsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
];
}
};
22 changes: 22 additions & 0 deletions app/Models/LandscapeGeom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class LandscapeGeom extends Model
{
use HasFactory;

public $timestamps = false;

protected $table = 'landscape_geom';

protected $fillable = ['landscape', 'geometry'];

public function scopeForLandscape($query, string $landscape)
{
return $query->where('landscape', $landscape);
}
}
21 changes: 10 additions & 11 deletions database/seeders/LandscapeGeomTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

Expand All @@ -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,
]);
}
}
}
6 changes: 6 additions & 0 deletions openapi-src/V2/definitions/DashboardBBOXLandscape.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: object
properties:
bbox:
type: array
items:
type: number
2 changes: 2 additions & 0 deletions openapi-src/V2/definitions/_index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ DashboardGetPolygonStatusResponse:
$ref: './DashboardGetPolygonStatusResponse.yml'
DashboardBBOXProject:
$ref: './DashboardBBOXProject.yml'
DashboardBBOXLandscape:
$ref: './DashboardBBOXLandscape.yml'
DashboardBBOXCountry:
$ref: './DashboardBBOXCountry.yml'
DashboardPolygonData:
Expand Down
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions openapi-src/V2/paths/_index.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
30 changes: 30 additions & 0 deletions resources/docs/swagger-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43330,6 +43330,13 @@ definitions:
type: array
items:
type: number
DashboardBBOXLandscape:
type: object
properties:
bbox:
type: array
items:
type: number
DashboardBBOXCountry:
type: object
properties:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions routes/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 445b6b4

Please sign in to comment.