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-1634] add endpoint for countrylandscape bbox #674

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Changes from all commits
Commits
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
76 changes: 76 additions & 0 deletions app/Http/Controllers/V2/Dashboard/GetPolygonsController.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use App\Http\Resources\V2\Dashboard\GetPolygonsResource;
use App\Models\LandscapeGeom;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\WorldCountryGeneralized;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

@@ -101,4 +102,79 @@ public function getLandscapeBbox(Request $request)
'landscapes' => $landscapes,
];
}

public function getCountryLandscapeBbox(Request $request)
{
$landscapes = $request->input('landscapes');
$iso = $request->input('country');

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

if ($landscapes !== null) {
if (is_string($landscapes)) {
$landscapes = explode(',', $landscapes);
}

$envelopes = LandscapeGeom::whereIn('landscape', $landscapes)
->selectRaw('ST_AsGeoJSON(ST_Envelope(geometry)) as envelope, landscape')
->get();

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);
}
}
}

if ($iso !== null) {
$countryData = WorldCountryGeneralized::where('iso', $iso)
->selectRaw('ST_AsGeoJSON(ST_Envelope(geometry)) AS bbox, country')
->first();

if (! $countryData) {
return response()->json(['error' => 'Country not found'], 404);
}

$geoJson = json_decode($countryData->bbox);
$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);
}

$countryName = $countryData->country;
}

if ($maxX === PHP_INT_MIN || $minX === PHP_INT_MAX) {
return response()->json(['error' => 'No valid bounding box found'], 404);
}

$response = [
'bbox' => [$minX, $minY, $maxX, $maxY],
];

if (isset($landscapes)) {
$response['landscapes'] = $landscapes;
}

if (isset($countryName)) {
$response['country'] = $countryName;
}

return response()->json($response);
}
};
2 changes: 1 addition & 1 deletion openapi-src/V2/definitions/DashboardBBOXLandscape.yml
Original file line number Diff line number Diff line change
@@ -3,4 +3,4 @@ properties:
bbox:
type: array
items:
type: number
type: number
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
summary: Get Bbox of landscape geometries
tags:
- Landscapes
parameters:
- name: landscapes
type: string
in: query
description: 'Multiple landscapes can be sent either as comma-separated values (?landscapes=Ghana Cocoa Belt,etc)'
required: true
- name: country
type: string
in: query
description: 'Country iso name'
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
@@ -2712,6 +2712,9 @@
/v2/dashboard/bbox/landscape:
get:
$ref: './Dashboard/get-v2-dashboard-get-bbox-landscape.yml'
/v2/dashboard/bbox/country-landscape:
get:
$ref: './Dashboard/get-v2-dashboard-get-bbox-country-landscape.yml'
/v2/dashboard/country/{country}:
get:
$ref: './Dashboard/get-v2-dashboard-country.yml'
27 changes: 27 additions & 0 deletions resources/docs/swagger-v2.yml
Original file line number Diff line number Diff line change
@@ -97572,6 +97572,33 @@ paths:
type: number
'404':
description: Project not found
/v2/dashboard/bbox/country-landscape:
get:
summary: Get Bbox of landscape geometries
tags:
- Landscapes
parameters:
- name: landscapes
type: string
in: query
description: 'Multiple landscapes can be sent either as comma-separated values (?landscapes=Ghana Cocoa Belt,etc)'
required: true
- name: country
type: string
in: query
description: Country iso name
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
1 change: 1 addition & 0 deletions routes/api_v2.php
Original file line number Diff line number Diff line change
@@ -749,6 +749,7 @@ function () {
Route::get('/bbox/project', [GetPolygonsController::class, 'getProjectBbox']);
Route::get('/country/{country}', [CountryDataController::class, 'getCountryBbox']);
Route::get('/bbox/landscape', [GetPolygonsController::class, 'getLandscapeBbox']);
Route::get('/bbox/country-landscape', [GetPolygonsController::class, 'getCountryLandscapeBbox']);
Route::get('/polygon-data/{uuid}', [CountryDataController::class, 'getPolygonData']);
Route::get('/project-data/{uuid}', [CountryDataController::class, 'getProjectData']);
Route::get('/active-projects', ActiveProjectsTableController::class);