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

Centroids #168

Merged
merged 1 commit 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
36 changes: 36 additions & 0 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace App\Helpers;

use App\Models\V2\PolygonGeometry;
use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class GeometryHelper
Expand Down Expand Up @@ -46,4 +48,38 @@ public function centroidOfProject($projectUuid)

return $centroidOfCentroids;
}
public function updateProjectCentroid(string $projectUuid)
{
try {
$centroid = $this->centroidOfProject($projectUuid);

if ($centroid === null) {
Log::warning("Invalid centroid for projectUuid: $projectUuid");
}

$centroidArray = json_decode($centroid, true);

$latitude = $centroidArray['coordinates'][1];
$longitude = $centroidArray['coordinates'][0];


Project::where('uuid', $projectUuid)
->update([
'lat' => $latitude,
'long' => $longitude,
]);


Log::info("Centroid updated for projectUuid: $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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\V2\Terrafund;

use App\Helpers\GeometryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\Projects\Project;
Expand Down Expand Up @@ -198,6 +199,7 @@ private function validateData(array $properties, array $fields): bool
private function insertSitePolygon(string $polygonUuid, array $properties, float $area)
{
try {
Log::info('Inserting site polygon', ['properties' => $properties, "polygonUuid" => $polygonUuid]);
$fieldsToValidate = ['poly_name', 'plantstart', 'plantend', 'practice', 'target_sys', 'distr', 'num_trees'];
$SCHEMA_CRITERIA_ID = 13;
$validSchema = true;
Expand All @@ -214,13 +216,10 @@ private function insertSitePolygon(string $polygonUuid, array $properties, float

$sitePolygon = new SitePolygon();
$sitePolygon->project_id = $properties['project_id'] ?? null;
$sitePolygon->proj_name = $properties['proj_name'] ?? null;
$sitePolygon->org_name = $properties['org_name'] ?? null;
$sitePolygon->country = $properties['country'] ?? null;
$sitePolygon->poly_id = $polygonUuid ?? null;
$sitePolygon->poly_name = $properties['poly_name'] ?? null;
$sitePolygon->site_id = $properties['site_id'] ?? null;
$sitePolygon->site_name = $properties['site_name'] ?? null;
$sitePolygon->poly_label = $properties['poly_label'] ?? null;
$sitePolygon->plantstart = ! empty($properties['plantstart']) ? $properties['plantstart'] : null;
$sitePolygon->plantend = ! empty($properties['plantend']) ? $properties['plantend'] : null;
Expand All @@ -230,7 +229,10 @@ private function insertSitePolygon(string $polygonUuid, array $properties, float
$sitePolygon->num_trees = $properties['num_trees'] ?? null;
$sitePolygon->est_area = $area ?? null;
$sitePolygon->save();

if ($sitePolygon->project_id) {
$geometryHelper = new GeometryHelper();
$geometryHelper -> updateProjectCentroid($sitePolygon->project_id);
}
return null;
} catch (\Exception $e) {
return $e->getMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace App\Http\Controllers\V2\Terrafund;

use App\Helpers\GeometryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\Projects\Project;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
Expand All @@ -29,24 +31,55 @@ public function getSitePolygonData(string $uuid)

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

// Recalculate the area in hectares
$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;
// 111320 is the length of one degree of latitude in meters at the equator
$unitLatitude = 111320;
$areaSqMeters = $areaSqDegrees * pow($unitLatitude * cos(deg2rad($latitude)), 2);
$areaHectares = $areaSqMeters / 10000;

// Update site_polygon area with recalculated value
if ($sitePolygon) {
$sitePolygon->est_area = $areaHectares;
$sitePolygon->save();
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());
}
Log::info("Updated area for site polygon with UUID: $areaSqMeters");
}


public function updateGeometry(string $uuid, Request $request)
{
Expand All @@ -63,6 +96,7 @@ public function updateGeometry(string $uuid, Request $request)
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ public function updateProjectCentroids()

return "Centroids updated successfully!";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ public function up()
$table->id();
$table->uuid('uuid')->unique();
$table->string('project_id')->nullable();
$table->string('proj_name')->nullable();
$table->string('site_id')->nullable();
$table->string('site_name')->nullable();
$table->string('org_name')->nullable();
$table->string('poly_id')->nullable();
$table->string('poly_name')->nullable();
$table->string('poly_label')->nullable();
Expand All @@ -33,7 +30,6 @@ public function up()
$table->integer('num_trees')->nullable();
$table->float('est_area')->nullable();
$table->date('date_modified')->nullable();
$table->string('country')->nullable();
$table->string('status')->nullable();
$table->string('created_by')->nullable();
$table->string('last_modified_by')->nullable();
Expand Down
Loading