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

polygon validation endpoints #145

Merged
merged 17 commits into from
Apr 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public function __invoke(Request $request, string $entity, string $framework)
private function getSlug(string $framework)
{
$frameworkModel = Framework::where('access_code', $framework)->firstOrFail();

return $frameworkModel->slug;
}

private function getForm(string $modelClass, string $framework)
{
return Form::where('model', $modelClass)
Expand Down

Large diffs are not rendered by default.

124 changes: 124 additions & 0 deletions app/Http/Controllers/V2/Terrafund/TerrafundEditGeometryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace App\Http\Controllers\V2\Terrafund;

use App\Http\Controllers\Controller;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\Sites\SitePolygon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

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 updateGeometry(string $uuid, Request $request)
{
$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();

return response()->json(['message' => 'Geometry updated successfully.', 'geometry' => $geometry, 'uuid' => $uuid]);
}

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);
}
}
}
39 changes: 39 additions & 0 deletions app/Models/V2/PolygonGeometry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Models\V2;

use App\Models\Traits\HasUuid;
use App\Models\V2\Sites\CriteriaSite;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class PolygonGeometry extends Model
{
use HasUuid;
use SoftDeletes;

protected $table = 'polygon_geometry';

protected $fillable = [
'polygon_id', 'geom',
];

public function criteriaSite()
{
return $this->hasMany(CriteriaSite::class, 'polygon_id', 'polygon_id');
}

public function getDbGeometryAttribute()
{
$result = DB::selectOne(
'
SELECT ST_Area(geom) AS area, ST_Y(ST_Centroid(geom)) AS latitude
FROM polygon_geometry
WHERE uuid = :uuid',
['uuid' => $this->uuid]
);

return $result;
}
}
22 changes: 22 additions & 0 deletions app/Models/V2/Sites/CodeCriteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models\V2\Sites;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CodeCriteria extends Model
{
use SoftDeletes;

protected $table = 'code_criteria';

protected $fillable = [
'uuid', 'uuid_primary', 'name', 'description', 'is_active',
];

public function criteriaSites()
{
return $this->hasMany(CriteriaSite::class, 'criteria_id', 'id');
}
}
42 changes: 42 additions & 0 deletions app/Models/V2/Sites/CriteriaSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Models\V2\Sites;

use App\Models\Traits\HasUuid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class CriteriaSite extends Model
{
use HasUuid;
use SoftDeletes;

/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'criteria_site';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'criteria_id',
'polygon_id',
'valid',
'date_created',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'deleted_at',
'date_created',
];
}
46 changes: 46 additions & 0 deletions app/Models/V2/Sites/SitePolygon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Models\V2\Sites;

use App\Models\Traits\HasUuid;
use App\Models\V2\PolygonGeometry;
use App\Models\V2\Projects\Project;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SitePolygon extends Model
{
use HasUuid;
use SoftDeletes;

protected $table = 'site_polygon';

protected $fillable = [
'proj_name',
'org_name',
'poly_id',
'poly_name',
'site_id',
'site_name',
'project_id',
'poly_label',
'plantstart',
'plantend',
'practice',
'target_sys',
'distr',
'num_trees',
'est_area',
'country',
];

public function polygonGeometry()
{
return $this->belongsTo(PolygonGeometry::class, 'poly_id', 'uuid');
}

public function project()
{
return $this->belongsTo(Project::class, 'project_id', 'uuid');
}
}
14 changes: 14 additions & 0 deletions app/Models/V2/WorldCountryGeneralized.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Models\V2;

use Illuminate\Database\Eloquent\Model;

class WorldCountryGeneralized extends Model
{
protected $table = 'world_countries_generalized';

protected $fillable = [
'countryaff', 'country', 'iso', 'country_aff', 'aff_iso', 'geometry', 'OGR_FID',
];
}
32 changes: 32 additions & 0 deletions database/migrations/2024_03_08_161030_create_code_criteria.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('code_criteria', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();
$table->string('uuid_primary');
$table->string('name');
$table->string('description');
$table->integer('is_active');
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('code_criteria');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::dropIfExists('polygon_geometry');
Schema::create('polygon_geometry', function (Blueprint $table) {
$table->id();
$table->uuid('uuid')->unique();
$table->geometry('geom')->nullable();
;
$table->softDeletes();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('polygon_geometry');
}
};
Loading
Loading