Skip to content

Commit

Permalink
Merge pull request #171 from wri/deletegeoms
Browse files Browse the repository at this point in the history
Deletegeoms
  • Loading branch information
egrojMonroy authored Apr 29, 2024
2 parents 14e1f5a + 8362174 commit 1bb466b
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 138 deletions.
299 changes: 162 additions & 137 deletions app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,45 @@

class TerrafundEditGeometryController extends Controller
{
public function getSitePolygonData(string $uuid)
{
try {
$sitePolygon = SitePolygon::where('poly_id', $uuid)->first();

if (! $sitePolygon) {
return response()->json(['message' => 'No site polygons found for the given UUID.'], 404);
}

return response()->json(['site_polygon' => $sitePolygon]);
} catch (\Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}
public function getSitePolygonData(string $uuid)
{
try {
$sitePolygon = SitePolygon::where('poly_id', $uuid)->first();

if (!$sitePolygon) {
return response()->json(['message' => 'No site polygons found for the given UUID.'], 404);
}

return response()->json(['site_polygon' => $sitePolygon]);
} catch (\Exception $e) {
return response()->json(['message' => $e->getMessage()], 500);
}

public function updateEstAreainSitePolygon($polygonGeometry, $geometry)
{
try {
$sitePolygon = SitePolygon::where('poly_id', $polygonGeometry->uuid)->first();

if ($sitePolygon) {
$geojson = json_encode($geometry);
$areaSqDegrees = DB::selectOne("SELECT ST_Area(ST_GeomFromGeoJSON('$geojson')) AS area")->area;
$latitude = DB::selectOne("SELECT ST_Y(ST_Centroid(ST_GeomFromGeoJSON('$geojson'))) AS latitude")->latitude;
$unitLatitude = 111320;
$areaSqMeters = $areaSqDegrees * pow($unitLatitude * cos(deg2rad($latitude)), 2);
$areaHectares = $areaSqMeters / 10000;

$sitePolygon->est_area = $areaHectares;
$sitePolygon->save();

Log::info("Updated area for site polygon with UUID: $sitePolygon->uuid");
} else {
Log::warning("Site polygon with UUID $polygonGeometry->uuid not found.");
}
} catch (\Exception $e) {
Log::error('Error updating area in site polygon: ' . $e->getMessage());
}
}

public function updateEstAreainSitePolygon($polygonGeometry, $geometry)
{
try {
$sitePolygon = SitePolygon::where('poly_id', $polygonGeometry->uuid)->first();

if ($sitePolygon) {
$geojson = json_encode($geometry);
$areaSqDegrees = DB::selectOne("SELECT ST_Area(ST_GeomFromGeoJSON('$geojson')) AS area")->area;
$latitude = DB::selectOne("SELECT ST_Y(ST_Centroid(ST_GeomFromGeoJSON('$geojson'))) AS latitude")->latitude;
$unitLatitude = 111320;
$areaSqMeters = $areaSqDegrees * pow($unitLatitude * cos(deg2rad($latitude)), 2);
$areaHectares = $areaSqMeters / 10000;

$sitePolygon->est_area = $areaHectares;
$sitePolygon->save();

Log::info("Updated area for site polygon with UUID: $sitePolygon->uuid");
} else {
Log::warning("Updating Area: Site polygon with UUID $polygonGeometry->uuid not found.");
}
} catch (\Exception $e) {
Log::error("Error updating area in site polygon: " . $e->getMessage());
}

}
public function updateProjectCentroid($polygonGeometry)
{
try {
Expand All @@ -80,110 +79,136 @@ public function updateProjectCentroid($polygonGeometry)
}
}

public function updateGeometry(string $uuid, Request $request)
{
try {
Log::info("Updating geometry for polygon with UUID: $uuid");

$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$geometry = json_decode($request->input('geometry'));
$geom = DB::raw("ST_GeomFromGeoJSON('" . json_encode($geometry) . "')");
$polygonGeometry->geom = $geom;
$polygonGeometry->save();
Log::info('ABOUT TO CREATE');
$this -> updateEstAreainSitePolygon($polygonGeometry, $geometry);
$this -> updateProjectCentroid($polygonGeometry);

return response()->json(['message' => 'Geometry updated successfully.', 'geometry' => $geometry, 'uuid' => $uuid]);
} catch (\Exception $e) {
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}
public function deletePolygonAndSitePolygon(string $uuid)
{
try {
$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (!$polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$sitePolygon = SitePolygon::where('poly_id', $uuid)->first();
$projectUuid = $sitePolygon->project_id;
if ($sitePolygon) {
Log::info("Deleting associated site polygon for UUID: $uuid");
$sitePolygon->delete();
}
$geometryHelper = new GeometryHelper();
$geometryHelper->updateProjectCentroid($projectUuid);
$polygonGeometry->delete();
Log::info("Polygon geometry and associated site polygon deleted successfully for UUID: $uuid");
return response()->json(['message' => 'Polygon geometry and associated site polygon deleted successfully.', 'uuid' => $uuid]);
} catch (\Exception $e) {
Log::error("An error occurred: " . $e->getMessage());
// Return error response if an exception occurs
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}
}


public function updateGeometry(string $uuid, Request $request)
{
try {
Log::info("Updating geometry for polygon with UUID: $uuid");

$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (!$polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$geometry = json_decode($request->input('geometry'));
$geom = DB::raw("ST_GeomFromGeoJSON('" . json_encode($geometry) . "')");
$polygonGeometry->geom = $geom;
$polygonGeometry->save();
Log::info('ABOUT TO CREATE');
$this->updateEstAreainSitePolygon($polygonGeometry, $geometry);
$this->updateProjectCentroid($polygonGeometry);

return response()->json(['message' => 'Geometry updated successfully.', 'geometry' => $geometry, 'uuid' => $uuid]);
} catch (\Exception $e) {
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}
}

public function getPolygonGeojson(string $uuid)
{
$geometryQuery = PolygonGeometry::isUuid($uuid);
if (! $geometryQuery->exists()) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$geojsonData = json_decode($geometryQuery->select(DB::raw('ST_AsGeoJSON(geom) as geojson'))->first()->geojson, true);

return response()->json([
'geojson' => $geojsonData,
]);
public function getPolygonGeojson(string $uuid)
{
$geometryQuery = PolygonGeometry::isUuid($uuid);
if (!$geometryQuery->exists()) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}

public function updateSitePolygon(string $uuid, Request $request)
{
try {
$sitePolygon = SitePolygon::where('uuid', $uuid)->first();
if (! $sitePolygon) {
return response()->json(['message' => 'No site polygons found for the given UUID.'], 404);
}
$validatedData = $request->validate([
'poly_name' => 'nullable|string',
'plantstart' => 'nullable|date',
'plantend' => 'nullable|date',
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'est_area' => 'nullable|numeric',
'target_sys' => 'nullable|string',
]);

$sitePolygon->update($validatedData);

return response()->json(['message' => 'Site polygon updated successfully'], 200);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}
$geojsonData = json_decode($geometryQuery->select(DB::raw('ST_AsGeoJSON(geom) as geojson'))->first()->geojson, true);

return response()->json([
'geojson' => $geojsonData,
]);
}

public function updateSitePolygon(string $uuid, Request $request)
{
try {
$sitePolygon = SitePolygon::where('uuid', $uuid)->first();
if (!$sitePolygon) {
return response()->json(['message' => 'No site polygons found for the given UUID.'], 404);
}
$validatedData = $request->validate([
'poly_name' => 'nullable|string',
'plantstart' => 'nullable|date',
'plantend' => 'nullable|date',
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'est_area' => 'nullable|numeric',
'target_sys' => 'nullable|string',
]);

$sitePolygon->update($validatedData);

return response()->json(['message' => 'Site polygon updated successfully'], 200);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}

public function createSitePolygon(string $uuid, Request $request)
{
try {
$validatedData = $request->validate([
'poly_name' => 'nullable|string',
'plantstart' => 'nullable|date',
'plantend' => 'nullable|date',
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'target_sys' => 'nullable|string',
]);

$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$areaSqDegrees = DB::selectOne('SELECT ST_Area(geom) AS area FROM polygon_geometry WHERE uuid = :uuid', ['uuid' => $uuid])->area;
$latitude = DB::selectOne('SELECT ST_Y(ST_Centroid(geom)) AS latitude FROM polygon_geometry WHERE uuid = :uuid', ['uuid' => $uuid])->latitude;
$areaSqMeters = $areaSqDegrees * pow(111320 * cos(deg2rad($latitude)), 2);
$areaHectares = $areaSqMeters / 10000;
$sitePolygon = new SitePolygon([
'poly_name' => $validatedData['poly_name'],
'plantstart' => $validatedData['plantstart'],
'plantend' => $validatedData['plantend'],
'practice' => $validatedData['practice'],
'distr' => $validatedData['distr'],
'num_trees' => $validatedData['num_trees'],
'est_area' => $areaHectares, // Assign the calculated area
'target_sys' => $validatedData['target_sys'],
]);
$sitePolygon->poly_id = $uuid;
$sitePolygon->uuid = Str::uuid();
$sitePolygon->save();

return response()->json(['message' => 'Site polygon created successfully', 'uuid' => $sitePolygon, 'area' => $areaHectares], 201);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}
}

public function createSitePolygon(string $uuid, Request $request)
{
try {
$validatedData = $request->validate([
'poly_name' => 'nullable|string',
'plantstart' => 'nullable|date',
'plantend' => 'nullable|date',
'practice' => 'nullable|string',
'distr' => 'nullable|string',
'num_trees' => 'nullable|integer',
'target_sys' => 'nullable|string',
]);

$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (!$polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}
$areaSqDegrees = DB::selectOne('SELECT ST_Area(geom) AS area FROM polygon_geometry WHERE uuid = :uuid', ['uuid' => $uuid])->area;
$latitude = DB::selectOne('SELECT ST_Y(ST_Centroid(geom)) AS latitude FROM polygon_geometry WHERE uuid = :uuid', ['uuid' => $uuid])->latitude;
$areaSqMeters = $areaSqDegrees * pow(111320 * cos(deg2rad($latitude)), 2);
$areaHectares = $areaSqMeters / 10000;
$sitePolygon = new SitePolygon([
'poly_name' => $validatedData['poly_name'],
'plantstart' => $validatedData['plantstart'],
'plantend' => $validatedData['plantend'],
'practice' => $validatedData['practice'],
'distr' => $validatedData['distr'],
'num_trees' => $validatedData['num_trees'],
'est_area' => $areaHectares, // Assign the calculated area
'target_sys' => $validatedData['target_sys'],
]);
$sitePolygon->poly_id = $uuid;
$sitePolygon->uuid = Str::uuid();
$sitePolygon->save();

return response()->json(['message' => 'Site polygon created successfully', 'uuid' => $sitePolygon, 'area' => $areaHectares], 201);
} catch (\Exception $e) {
// Handle other exceptions
return response()->json(['error' => 'An error occurred: ' . $e->getMessage()], 500);
}
}

public function getPolygonBbox(string $uuid)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function up()
$table->string('target_sys')->nullable();
$table->string('distr')->nullable();
$table->integer('num_trees')->nullable();
$table->float('est_area')->nullable();
$table->decimal('est_area', 15, 2)->nullable();
$table->date('date_modified')->nullable();
$table->string('status')->nullable();
$table->string('created_by')->nullable();
Expand Down
2 changes: 2 additions & 0 deletions routes/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@
Route::get('/polygon/{uuid}', [TerrafundEditGeometryController::class, 'getSitePolygonData']);
Route::get('/polygon/geojson/{uuid}', [TerrafundEditGeometryController::class, 'getPolygonGeojson']);
Route::put('/polygon/{uuid}', [TerrafundEditGeometryController::class, 'updateGeometry']);
Route::delete('/polygon/{uuid}', [TerrafundEditGeometryController::class, 'deletePolygonAndSitePolygon']);

Route::get('/polygon/bbox/{uuid}', [TerrafundEditGeometryController::class, 'getPolygonBbox']);

Route::put('/site-polygon/{uuid}', [TerrafundEditGeometryController::class, 'updateSitePolygon']);
Expand Down

0 comments on commit 1bb466b

Please sign in to comment.