Skip to content

Commit

Permalink
add country in site_polygon migration and log error of site polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
egrojMonroy committed Apr 16, 2024
1 parent 389f67b commit 995c22c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions app/Models/V2/Sites/SitePolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SitePolygon extends Model
'distr',
'num_trees',
'est_area',
'country'
];

public function polygonGeometry()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 995c22c

Please sign in to comment.