From 31b35ba6b2c538c38b2e686074578f114af06bb5 Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 29 Apr 2024 14:04:16 -0400 Subject: [PATCH 1/3] add delete geometry --- app/Helpers/GeometryHelper.php | 9 +- .../TerrafundEditGeometryController.php | 352 ++++++++++-------- routes/api_v2.php | 2 + 3 files changed, 196 insertions(+), 167 deletions(-) diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index 391905024..45691ac70 100644 --- a/app/Helpers/GeometryHelper.php +++ b/app/Helpers/GeometryHelper.php @@ -12,6 +12,7 @@ class GeometryHelper { public function centroidOfProject($projectUuid) { + Log::info("Calculating centroid of projectUuid: $projectUuid"); $sitePolygons = SitePolygon::where('project_id', $projectUuid)->get(); if ($sitePolygons->isEmpty()) { @@ -19,7 +20,7 @@ public function centroidOfProject($projectUuid) } $polyIds = $sitePolygons->pluck('poly_id')->toArray(); - + Log::info("Polygons found for projectUuid: $projectUuid"); $centroids = PolygonGeometry::selectRaw("ST_AsGeoJSON(ST_Centroid(geom)) AS centroid") ->whereIn('uuid', $polyIds) ->get(); @@ -51,6 +52,7 @@ public function centroidOfProject($projectUuid) public function updateProjectCentroid(string $projectUuid) { try { + Log::info("Updating centroid in helper for projectUuid: $projectUuid"); $centroid = $this->centroidOfProject($projectUuid); if ($centroid === null) { @@ -75,10 +77,7 @@ public function updateProjectCentroid(string $projectUuid) return "Centroids updated successfully!"; } catch (\Exception $e) { Log::error("Error updating centroid for projectUuid: $projectUuid"); - return response()->json([ - 'message' => 'Error updating centroid', - 'error' => $e->getMessage() - ], 500); + return $e->getMessage(); } } diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php index 44f598589..8e656db9a 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php @@ -14,175 +14,203 @@ 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 updateProjectCentroid($polygonGeometry) - { - try { - $sitePolygon = SitePolygon::where('poly_id', $polygonGeometry->uuid)->first(); - - if ($sitePolygon) { - $project = Project::where('uuid', $sitePolygon->project_id)->first(); - - if ($project) { - $geometryHelper = new GeometryHelper(); - $centroid = $geometryHelper->centroidOfProject($project->uuid); - - if ($centroid === null) { - Log::warning("Invalid centroid for project UUID: $project->uuid"); - } - } else { - Log::warning("Project with UUID $sitePolygon->project_id not found."); - } - } else { - Log::warning("Site polygon with UUID $polygonGeometry->uuid not found."); - } - } catch (\Exception $e) { - Log::error("Error updating project centroid: " . $e->getMessage()); - } - } - - - 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 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 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); + public function updateProjectCentroid($polygonGeometry) + { + try { + $sitePolygon = SitePolygon::where('poly_id', $polygonGeometry->uuid)->first(); - return response()->json([ - 'geojson' => $geojsonData, - ]); - } + if ($sitePolygon) { + $project = Project::where('uuid', $sitePolygon->project_id)->first(); + + if ($project) { + $geometryHelper = new GeometryHelper(); + $centroid = $geometryHelper->centroidOfProject($project->uuid); - 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); + if ($centroid === null) { + Log::warning("updating Centroid: Invalid centroid for project UUID: $project->uuid"); + } + } else { + Log::warning("updating Centroid: Project with UUID $sitePolygon->project_id not found."); } + } else { + Log::warning("updating Centroid: Site polygon with UUID $polygonGeometry->uuid not found."); + } + } catch (\Exception $e) { + Log::error("Error updating project centroid: " . $e->getMessage()); } + } + public function deletePolygonAndSitePolygon(string $uuid) + { + try { + Log::info("Deleting polygon and site polygon for UUID: $uuid"); + $polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first(); + if (!$polygonGeometry) { + Log::info("No polygon geometry found for UUID: $uuid"); + 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(); + } + Log::info("Updating project centroid for UUID: $uuid"); + $geometryHelper = new GeometryHelper(); + $geometryHelper->updateProjectCentroid($projectUuid); + Log::info("Deleting polygon geometry for UUID: $uuid"); + $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 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 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 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); } + } } diff --git a/routes/api_v2.php b/routes/api_v2.php index e21c12323..dfd55a1f6 100644 --- a/routes/api_v2.php +++ b/routes/api_v2.php @@ -640,6 +640,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::put('/site-polygon/{uuid}', [TerrafundEditGeometryController::class, 'updateSitePolygon']); Route::post('/site-polygon/{uuid}', [TerrafundEditGeometryController::class, 'createSitePolygon']); From 0772ccae116fff1a7d4518d88fee1c71eb33e21b Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 29 Apr 2024 14:05:29 -0400 Subject: [PATCH 2/3] remove logs --- app/Helpers/GeometryHelper.php | 1 - .../V2/Terrafund/TerrafundEditGeometryController.php | 4 ---- 2 files changed, 5 deletions(-) diff --git a/app/Helpers/GeometryHelper.php b/app/Helpers/GeometryHelper.php index 45691ac70..c42349974 100644 --- a/app/Helpers/GeometryHelper.php +++ b/app/Helpers/GeometryHelper.php @@ -52,7 +52,6 @@ public function centroidOfProject($projectUuid) public function updateProjectCentroid(string $projectUuid) { try { - Log::info("Updating centroid in helper for projectUuid: $projectUuid"); $centroid = $this->centroidOfProject($projectUuid); if ($centroid === null) { diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php index 8e656db9a..c517abb6c 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php @@ -82,10 +82,8 @@ public function updateProjectCentroid($polygonGeometry) public function deletePolygonAndSitePolygon(string $uuid) { try { - Log::info("Deleting polygon and site polygon for UUID: $uuid"); $polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first(); if (!$polygonGeometry) { - Log::info("No polygon geometry found for UUID: $uuid"); return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404); } $sitePolygon = SitePolygon::where('poly_id', $uuid)->first(); @@ -94,10 +92,8 @@ public function deletePolygonAndSitePolygon(string $uuid) Log::info("Deleting associated site polygon for UUID: $uuid"); $sitePolygon->delete(); } - Log::info("Updating project centroid for UUID: $uuid"); $geometryHelper = new GeometryHelper(); $geometryHelper->updateProjectCentroid($projectUuid); - Log::info("Deleting polygon geometry for UUID: $uuid"); $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]); From 8362174f978cbad559fc764c25d780aa465137eb Mon Sep 17 00:00:00 2001 From: JORGE Date: Mon, 29 Apr 2024 19:19:03 -0400 Subject: [PATCH 3/3] change est area to decimal --- database/migrations/2024_04_2_121238_create_sites_polygons.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/migrations/2024_04_2_121238_create_sites_polygons.php b/database/migrations/2024_04_2_121238_create_sites_polygons.php index c09863b50..9499a2389 100644 --- a/database/migrations/2024_04_2_121238_create_sites_polygons.php +++ b/database/migrations/2024_04_2_121238_create_sites_polygons.php @@ -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();