Skip to content

Commit

Permalink
[TM-710, 647, 757, 758, 759, 760, 761, 762, 764, 765, 766] polygon va…
Browse files Browse the repository at this point in the history
…lidation endpoints (#145)

* polygon validation endpoints

* move code from migration to seeder

* remove unused code

* move code from migration to seeder

* add missing pieces in docker file for php

* fix lint

* implement some suggestion on the model creation and uuid usage

* change memory limit

* merge single polygon of geo and dashboard

* keep merging

* add country in site_polygon migration and log error of site polygon

* add Eloquent and clear query

* add function to call area and latitude of polygon in model

* add relation with polygon for site polygon

* move to variable for clarity

* fix: lint

---------

Co-authored-by: JORGE <[email protected]>
Co-authored-by: cesarLima1 <[email protected]>
  • Loading branch information
3 people committed Apr 22, 2024
1 parent c1a0e46 commit c5ce68d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ private function insertSinglePolygon(array $geometry, int $srid)
$geom = DB::raw("ST_GeomFromGeoJSON('$geojson')");
$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;
$areaSqMeters = $areaSqDegrees * pow(111320 * cos(deg2rad($latitude)), 2);
// 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;

Expand Down Expand Up @@ -111,7 +113,9 @@ public function insertGeojsonToDB(string $geojsonFilename)
$data = $this->insertSinglePolygon($feature['geometry'], $srid);
$uuids[] = $data['uuid'];
$returnSite = $this->insertSitePolygon($data['uuid'], $feature['properties'], $data['area']);
Log::info($returnSite) ;
if ($returnSite) {
Log::info($returnSite) ;
}
} elseif ($feature['geometry']['type'] === 'MultiPolygon') {
foreach ($feature['geometry']['coordinates'] as $polygon) {
$singlePolygon = ['type' => 'Polygon', 'coordinates' => $polygon];
Expand All @@ -121,10 +125,10 @@ public function insertGeojsonToDB(string $geojsonFilename)
$data = $this->insertSinglePolygon($singlePolygon, $srid);
$uuids[] = $data['uuid'];
$returnSite = $this->insertSitePolygon($data['uuid'], $feature['properties'], $data['area']);
Log::info($returnSite);
if ($returnSite) {
Log::info($returnSite) ;
}
}
} else {
return ['error' => 'Invalid geometry type, geometry should be Polygon or MultiPolygon type.'];
}
}

Expand Down Expand Up @@ -227,7 +231,7 @@ private function insertSitePolygon(string $polygonUuid, array $properties, float
$sitePolygon->est_area = $area ?? null;
$sitePolygon->save();

return 'has saved correctly';
return null;
} catch (\Exception $e) {
return $e->getMessage();
}
Expand Down Expand Up @@ -287,6 +291,8 @@ public function uploadKMLFile(Request $request)

private function findShpFile($directory)
{
Log::info('find shp: ' . $directory);

$shpFile = null;
$files = scandir($directory);
foreach ($files as $file) {
Expand All @@ -302,8 +308,7 @@ private function findShpFile($directory)

public function uploadShapefile(Request $request)
{
ini_set('memory_limit', '-1');
Log::debug('File not found in request', ['request' => $request->all()]);
Log::debug('Upload Shape file data', ['request' => $request->all()]);
if ($request->hasFile('file')) {
$file = $request->file('file');
if ($file->getClientOriginalExtension() !== 'zip') {
Expand Down Expand Up @@ -448,9 +453,10 @@ public function validatePolygonSize(Request $request)
if (! $geometry) {
return response()->json(['error' => 'Geometry not found'], 404);
}
$areaAndLatitude = $geometry->getDbGeometryAttribute();

$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;
$areaSqDegrees = $areaAndLatitude->area;
$latitude = $areaAndLatitude->latitude;
$areaSqMeters = $areaSqDegrees * pow(111320 * cos(deg2rad($latitude)), 2);
$SIZE_CRITERIA_ID = 6;
$valid = $areaSqMeters <= 10000000;
Expand Down Expand Up @@ -492,10 +498,7 @@ public function checkWithinCountry(Request $request)
return response()->json(['error' => 'Site polygon data not found for the specified polygonUuid'], 404);
}

// Find the country ISO using project_id from v2projects
$countryIso = Project::where('uuid', $sitePolygonData->project_id)
->value('country');

$countryIso = $sitePolygonData->project->country;
if (! $countryIso) {
return response()->json(['error' => 'Country ISO not found for the specified project_id'], 404);
}
Expand Down Expand Up @@ -572,14 +575,7 @@ public function getCriteriaData(Request $request)
$criteriaList = [];
foreach ($criteriaData as $criteria) {
$criteriaId = $criteria->criteria_id;

// Check if the criteria is valid
$validCriteriaQuery = 'SELECT valid FROM criteria_site
WHERE polygon_id = ? AND criteria_id = ?';
$validResult = DB::selectOne($validCriteriaQuery, [$uuid, $criteriaId]);

$valid = $validResult ? $validResult->valid : null;

$valid = CriteriaSite::where(['polygon_id' => $uuid, 'criteria_id' => $criteriaId])->select('valid')->first()?->valid;
$criteriaList[] = [
'criteria_id' => $criteriaId,
'latest_created_at' => $criteria->latest_created_at,
Expand Down Expand Up @@ -639,7 +635,7 @@ public function validateOverlapping(Request $request)
$OVERLAPPING_CRITERIA_ID = 3;
$insertionSuccess = $this->insertCriteriaSite($uuid, $OVERLAPPING_CRITERIA_ID, $valid);

return response()->json(['intersects' => $intersects, 'project_id' => $projectId, 'uuid' => $uuid, 'valid' => $valid, 'criteria_success' => $insertionSuccess], 200);
return response()->json(['intersects' => $intersects, 'project_id' => $projectId, 'uuid' => $uuid, 'valid' => $valid, 'creteria_succes' => $insertionSuccess], 200);
}

public function validateEstimatedArea(Request $request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function getSitePolygonData(string $uuid)

public function updateGeometry(string $uuid, Request $request)
{
$geometry = json_decode($request->input('geometry'));
$geom = DB::raw("ST_GeomFromGeoJSON('" . json_encode($geometry) . "')");
$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();

Expand All @@ -42,17 +42,11 @@ public function updateGeometry(string $uuid, Request $request)

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

$geojsonData = json_decode($geojson[0]->geojson, true);
$geojsonData = json_decode($geometryQuery->select(DB::raw('ST_AsGeoJSON(geom) as geojson'))->first()->geojson, true);

return response()->json([
'geojson' => $geojsonData,
Expand Down Expand Up @@ -99,7 +93,6 @@ public function createSitePolygon(string $uuid, Request $request)
'target_sys' => 'nullable|string',
]);

// Get the geometry from the polygon_geometry table using the UUID
$polygonGeometry = PolygonGeometry::where('uuid', $uuid)->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
Expand Down
14 changes: 14 additions & 0 deletions app/Models/V2/PolygonGeometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
{
Expand All @@ -22,4 +23,17 @@ 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;
}
}
9 changes: 8 additions & 1 deletion app/Models/V2/Sites/SitePolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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;

Expand All @@ -21,6 +22,7 @@ class SitePolygon extends Model
'poly_name',
'site_id',
'site_name',
'project_id',
'poly_label',
'plantstart',
'plantend',
Expand All @@ -34,6 +36,11 @@ class SitePolygon extends Model

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

public function project()
{
return $this->belongsTo(Project::class, 'project_id', 'uuid');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function up()
$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();
$table->softDeletes();
Expand Down

0 comments on commit c5ce68d

Please sign in to comment.