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

Deletegeoms #171

Merged
merged 5 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
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
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
Loading