Skip to content

Commit

Permalink
Merge pull request #181 from wri/downloadFeatureCollection
Browse files Browse the repository at this point in the history
feat: add download endpoint for sites as FeatureCollection
  • Loading branch information
cesarLima1 authored May 3, 2024
2 parents 62fd7f1 + a384477 commit 87f3382
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,52 @@ public function getPolygonAsGeoJSON(Request $request)
}
}

public function getAllPolygonsAsGeoJSON(Request $request)
{
try {
$siteUuid = $request->query('uuid');
$polygonsUuids = SitePolygon::where('site_id', $siteUuid)->pluck('poly_id');
$features = [];
foreach ($polygonsUuids as $polygonUuid) {
$feature = [];
$polygonGeometry = PolygonGeometry::where('uuid', $polygonUuid)
->select(DB::raw('ST_AsGeoJSON(geom) AS geojson'))
->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}

$sitePolygon = SitePolygon::where('poly_id', $polygonUuid)->first();
if (! $sitePolygon) {
return response()->json(['message' => 'No site polygon found for the given UUID.'], 404);
}

$properties = [];
$fieldsToValidate = ['poly_name', 'plantstart', 'plantend', 'practice', 'target_sys', 'distr', 'num_trees'];
foreach ($fieldsToValidate as $field) {
$properties[$field] = $sitePolygon->$field;
}

$propertiesJson = json_encode($properties);

$feature = [
'type' => 'Feature',
'geometry' => json_decode($polygonGeometry->geojson),
'properties' => json_decode($propertiesJson),
];
$features[] = $feature;
}
$featureCollection = [
'type' => 'FeatureCollection',
'features' => $features,
];

return response()->json($featureCollection);
} catch (\Exception $e) {
return response()->json(['message' => 'Failed to generate GeoJSON.', 'error' => $e->getMessage()], 500);
}
}

public function getAllCountryNames()
{
$countries = WorldCountryGeneralized::select('country')
Expand Down
66 changes: 66 additions & 0 deletions resources/docs/swagger-v2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44309,6 +44309,52 @@ definitions:
ModifiedDate:
type: string
format: date
FeatureCollection:
type: object
properties:
type:
type: string
Feature:
type: object
properties:
type:
type: string
geometry:
$ref: '#/definitions/Geometry'
properties:
$ref: '#/definitions/FeatureProperties'
Geometry:
type: object
properties:
type:
type: string
coordinates:
type: array
items:
type: array
items:
type: array
items:
type: number
FeatureProperties:
type: object
properties:
poly_name:
type: string
plantstart:
type: string
format: date
plantend:
type: string
format: date
practice:
type: string
target_sys:
type: string
distr:
type: string
num_trees:
type: integer
paths:
'/v2/tree-species/{entity}/{UUID}':
get:
Expand Down Expand Up @@ -93962,6 +94008,26 @@ paths:
description: Bad request
'500':
description: Internal server error
'/v2/terrafund/geojson/site':
get:
summary: Get collection Feature as GeoJSON for a site
parameters:
- in: query
name: uuid
type: string
required: true
description: UUID of the aite.
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/definitions/FeatureCollection'
'400':
description: Bad request
'500':
description: Internal server error
'/v2/terrafund/polygon/bbox/{uuid}':
get:
operationId: get-v2-polygon-bbox
Expand Down
1 change: 1 addition & 0 deletions routes/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@
Route::post('/upload-kml', [TerrafundCreateGeometryController::class, 'uploadKMLFile']);
Route::post('/polygon/{uuid}', [TerrafundCreateGeometryController::class, 'processGeometry']);
Route::get('/geojson/complete', [TerrafundCreateGeometryController::class, 'getPolygonAsGeoJSON']);
Route::get('/geojson/site', [TerrafundCreateGeometryController::class, 'getAllPolygonsAsGeoJSON']);

Route::get('/validation/self-intersection', [TerrafundCreateGeometryController::class, 'checkSelfIntersection']);
Route::get('/validation/size-limit', [TerrafundCreateGeometryController::class, 'validatePolygonSize']);
Expand Down

0 comments on commit 87f3382

Please sign in to comment.