From 995c22c3f6516c45976f7440bb56cb1c19b8470c Mon Sep 17 00:00:00 2001 From: JORGE Date: Tue, 16 Apr 2024 16:17:50 -0400 Subject: [PATCH] add country in site_polygon migration and log error of site polygon --- .../TerrafundCreateGeometryController.php | 32 +++++++++++++++++-- app/Models/V2/Sites/SitePolygon.php | 1 + ...2024_04_2_121238_create_sites_polygons.php | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php index c0604319d..28f8bdcaf 100644 --- a/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php +++ b/app/Http/Controllers/V2/Terrafund/TerrafundCreateGeometryController.php @@ -65,6 +65,32 @@ private function validatePolygonBounds(array $geometry): bool { return true; } } + +private function insertSinglePolygon(array $geometry, int $srid) +{ + try { + // Convert geometry to GeoJSON string with specified SRID + $geojson = json_encode(['type' => 'Feature', 'geometry' => $geometry, 'crs' => ['type' => 'name', 'properties' => ['name' => "EPSG:$srid"]]]); + + // Insert GeoJSON data into the database + $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); + + $areaHectares = $areaSqMeters / 10000; + + $polygonGeometry = PolygonGeometry::create([ + 'geom' => $geom, + ]); + + return ['uuid' => $polygonGeometry->uuid, 'id' => $polygonGeometry->id, 'area' => $areaHectares]; + } catch (\Exception $e) { + echo $e; + + return $e->getMessage(); + } +} public function insertGeojsonToDB(string $geojsonFilename) { $srid = 4326; @@ -82,6 +108,7 @@ 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) ; } elseif ($feature['geometry']['type'] === 'MultiPolygon') { foreach ($feature['geometry']['coordinates'] as $polygon) { $singlePolygon = ['type' => 'Polygon', 'coordinates' => $polygon]; @@ -91,6 +118,7 @@ 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); } } } @@ -173,7 +201,7 @@ private function insertSitePolygon(string $polygonUuid, array $properties, float } $insertionSchemaSuccess = $this->insertCriteriaSite($polygonUuid, $SCHEMA_CRITERIA_ID, $validSchema); $insertionDataSuccess = $this->insertCriteriaSite($polygonUuid, $DATA_CRITERIA_ID, $validData); - + $sitePolygon = new SitePolygon(); $sitePolygon->project_id = $properties['project_id'] ?? null; $sitePolygon->proj_name = $properties['proj_name'] ?? null; @@ -274,7 +302,7 @@ public function uploadShapefile(Request $request) return response()->json(['error' => 'Only ZIP files are allowed'], 400); } $directory = storage_path('app/public/shapefiles/' . uniqid('shapefile_')); - mkdir($directory, 0755, true); + mkdir($directory, 0755, true); // Extract the contents of the ZIP file $zip = new \ZipArchive(); diff --git a/app/Models/V2/Sites/SitePolygon.php b/app/Models/V2/Sites/SitePolygon.php index a5aa9341a..3143d7d23 100644 --- a/app/Models/V2/Sites/SitePolygon.php +++ b/app/Models/V2/Sites/SitePolygon.php @@ -29,6 +29,7 @@ class SitePolygon extends Model 'distr', 'num_trees', 'est_area', + 'country' ]; public function polygonGeometry() 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 6fb03c771..8280dcc18 100644 --- a/database/migrations/2024_04_2_121238_create_sites_polygons.php +++ b/database/migrations/2024_04_2_121238_create_sites_polygons.php @@ -33,6 +33,7 @@ public function up() $table->integer('num_trees')->nullable(); $table->float('est_area')->nullable(); $table->date('date_modified')->nullable(); + $table->string('country')->nullable(); $table->string('created_by')->nullable(); $table->string('last_modified_by')->nullable(); $table->softDeletes();