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

[TM-1424] download current polygons of project #663

Merged
merged 2 commits into from
Jan 27, 2025
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
45 changes: 45 additions & 0 deletions app/Helpers/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,49 @@ public static function getPolygonGeojson($uuid)
'geometry' => $geometry,
];
}

public static function generateGeoJSON($project = null, $siteUuid = null)
{
$query = SitePolygon::query();

if ($project) {
$query->whereHas('site', function ($query) use ($project) {
$query->where('project_id', $project->id);
});
}

if ($siteUuid) {
$query->where('site_id', $siteUuid);
}

$sitePolygons = $query->active()->get();

$features = [];
foreach ($sitePolygons as $sitePolygon) {
$polygonGeometry = PolygonGeometry::where('uuid', $sitePolygon->poly_id)
->select(DB::raw('ST_AsGeoJSON(geom) AS geojsonGeom'))
->first();

if (! $polygonGeometry) {
throw new \Exception('No polygon geometry found for the given UUID.');
}

$fieldsToValidate = ['poly_name', 'plantstart', 'plantend', 'practice', 'target_sys', 'distr', 'num_trees', 'site_id', 'uuid'];
$properties = [];
foreach ($fieldsToValidate as $field) {
$properties[$field] = $sitePolygon->$field;
}

$features[] = [
'type' => 'Feature',
'geometry' => json_decode($polygonGeometry->geojsonGeom),
'properties' => $properties,
];
}

return [
'type' => 'FeatureCollection',
'features' => $features,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\V2\Exports;

use App\Exports\V2\EntityExport;
use App\Helpers\GeometryHelper;
use App\Http\Controllers\Controller;
use App\Models\V2\Forms\Form;
use App\Models\V2\Nurseries\Nursery;
Expand Down Expand Up @@ -70,17 +71,13 @@ private function getModelClass(string $entity)

private function exportShapefiles(Project $project)
{
$filename = storage_path('./'.Str::of($project->name)->replace(['/', '\\'], '-') . ' Sites Shapefiles - ' . now() . '.zip');
$zip = new \ZipArchive();
$zip->open($filename, \ZipArchive::CREATE);
$geoJson = GeometryHelper::generateGeoJSON($project);
$filename = Str::of($project->name)->replace(['/', '\\'], '-') . ' Sites GeoJSON - ' . now() . '.geojson';
$path = storage_path('./' . $filename);

rescue(function () use ($project, $zip) {
$this->addSiteShapefiles($project, $zip);
});
file_put_contents($path, json_encode($geoJson, JSON_PRETTY_PRINT));

$zip->close();

return response()->download($filename)->deleteFileAfterSend();
return response()->download($path)->deleteFileAfterSend();
}

private function addSiteShapefiles(Project $project, \ZipArchive $mainZip): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,45 +1073,9 @@ public function getAllPolygonsAsGeoJSONDownload(Request $request)
{
try {
$siteUuid = $request->query('uuid');
$polygonsUuids = SitePolygon::where('site_id', $siteUuid)
->active()
->pluck('poly_id');
$features = [];
foreach ($polygonsUuids as $polygonUuid) {
$feature = [];
$polygonGeometry = PolygonGeometry::where('uuid', $polygonUuid)
->select(DB::raw('ST_AsGeoJSON(geom) AS geojsonGeom'))
->first();
if (! $polygonGeometry) {
return response()->json(['message' => 'No polygon geometry found for the given UUID.'], 404);
}

$sitePolygon = SitePolygon::where('poly_id', $polygonUuid)->first();
if (! $sitePolygon) {
return response()->json(['message' => 'No site polygon found for the given UUID.'], 404);
}

$properties = [];
$fieldsToValidate = ['poly_name', 'plantstart', 'plantend', 'practice', 'target_sys', 'distr', 'num_trees', 'site_id', 'uuid'];
foreach ($fieldsToValidate as $field) {
$properties[$field] = $sitePolygon->$field;
}

$propertiesJson = json_encode($properties);
$geoJson = GeometryHelper::generateGeoJSON(null, $siteUuid);

$feature = [
'type' => 'Feature',
'geometry' => json_decode($polygonGeometry->geojsonGeom),
'properties' => json_decode($propertiesJson),
];
$features[] = $feature;
}
$featureCollection = [
'type' => 'FeatureCollection',
'features' => $features,
];

return response()->json($featureCollection);
return response()->json($geoJson);
} catch (\Exception $e) {
return response()->json(['message' => 'Failed to generate GeoJSON.', 'error' => $e->getMessage()], 500);
}
Expand Down
Loading