[Delete] Doesn't use Bounding Boxes in code.

This commit is contained in:
guillaume91 2024-05-22 16:23:42 +02:00
parent 8e143badc5
commit a4f48d04a4
6 changed files with 100 additions and 229 deletions

View file

@ -36,14 +36,12 @@ public function __construct(ProjectDownload $download, Carbon $date,)
*/
public function handle(): void
{
logger(json_encode($this->download->project->getBoundingBoxesAsArray()));
$command = [
sprintf('%srunpython.sh', base_path('../')),
sprintf('--date=%s', $this->date->format('Y-m-d')),
sprintf('--days=%d', $this->days),
sprintf('--project_dir=%s', $this->download->project->download_path),
sprintf('--bbox=%s', json_encode($this->download->project->getBoundingBoxesAsArray())),
];
$process = new Process($command);

View file

@ -3,10 +3,8 @@
namespace App\Livewire\Projects;
use App\Models\Project;
use App\Models\ProjectBoundingBox;
use App\Models\ProjectEmailRecipient;
use App\Rules\HarvestFile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Livewire\Component;
@ -27,9 +25,9 @@ class ProjectManager extends Component
public $showMailSettingsModal = false;
public $projectIdBeingDeleted;
public $span_json_path;
public $pivot_json_path;
public $harvest_json_path;
// public $span_json_path;
// public $pivot_json_path;
// public $harvest_json_path;
public array $pivotFiles;
public array $spanFiles;
@ -61,13 +59,13 @@ public function editMailSettings(Project $project)
private function loadFormData(Project $project)
{
$this->formData = $project->toArray();
$this->pivot_json_path = $this->formData['pivot_json_path'];
$this->span_json_path = $this->formData['span_json_path'];
$this->harvest_json_path = $this->formData['harvest_json_path'];
$this->formData['pivot_json_path'] = null;
$this->formData['span_json_path'] = null;
$this->formData['harvest_json_path'] = null;
$this->formData['boundingBoxes'] = $project->boundingBoxes->toArray();
// $this->pivot_json_path = $this->formData['pivot_json_path'];
// $this->span_json_path = $this->formData['span_json_path'];
// $this->harvest_json_path = $this->formData['harvest_json_path'];
// $this->formData['pivot_json_path'] = null;
// $this->formData['span_json_path'] = null;
// $this->formData['harvest_json_path'] = null;
// $this->formData['boundingBoxes'] = $project->boundingBoxes->toArray();
$this->formData['mail_recipients'] = $project->emailRecipients->toArray() ?: [
[
'name' => '',
@ -82,10 +80,11 @@ public function openCreateProjectModal()
$this->showProjectModal = true;
}
public function saveProject(Request $request)
public function saveProject()
{
$this->resetErrorBag();
dd($this->validateForm());
$this->mergeFormData();
$this->validateForm();
Project::saveWithFormData($this->formData);
$this->resetFormData();
$this->showProjectModal = false;
@ -102,18 +101,6 @@ public function saveMailSettings()
$this->dispatch('saved');
}
public function addBoundingBox()
{
$this->formData['boundingBoxes'][] =
[
'name' => '',
'top_left_latitude' => '',
'top_left_longitude' => '',
'bottom_right_latitude' => '',
'bottom_right_longitude' => '',
];
}
public function addEmailRecipient()
{
$this->formData['mail_recipients'][] =
@ -123,17 +110,6 @@ public function addEmailRecipient()
];
}
public function deleteBoundingBox($index)
{
if (array_key_exists('id', $this->formData['boundingBoxes'][$index])) {
ProjectBoundingBox::whereId($this->formData['boundingBoxes'][$index]['id'])->delete();
}
unset($this->formData['boundingBoxes'][$index]);
$this->formData['boundingBoxes'] = array_values($this->formData['boundingBoxes']);
$this->dispatch('saved');
}
public function deleteEmailRecipient($index)
{
if (array_key_exists('id', $this->formData['mail_recipients'][$index])) {
@ -190,10 +166,6 @@ private function resetFormData()
{
$this->formData = [
'name' => '',
'pivot_json_path' => '',
'span_json_path' => '',
'harvest_json_path' => '',
'boundingBoxes' => [],
'mail_subject' => '',
'mail_template' => '',
'mail_frequency' => '',
@ -203,7 +175,6 @@ private function resetFormData()
$this->pivotFiles = [];
$this->spanFiles = [];
$this->harvestDataFiles = [];
$this->addBoundingBox();
$this->addEmailRecipient();
}
@ -213,10 +184,9 @@ private function validateForm()
return Validator::make([
'name' => $this->formData['name'],
'pivot_file' => $this->pivotFiles[0] ?? null,
'span_file' => $this->spanFiles[0] ?? null,
'harvest_file' => $this->harvestDataFiles[0] ?? null,
'boundingBoxes' => $this->formData['boundingBoxes'],
'pivot_file' => $this->formData['pivot_file'],
'span_file' => $this->formData['span_file'],
'harvest_file' => $this->formData['harvest_file'],
], [
'name' => [
'required',
@ -236,12 +206,6 @@ private function validateForm()
}
}],
'harvest_file' => ['sometimes', new HarvestFile],
'boundingBoxes' => ['required', 'array', 'min:1'],
'boundingBoxes.*.name' => ['required', 'string', 'max:255'],
'boundingBoxes.*.top_left_latitude' => ['required', 'string'],
'boundingBoxes.*.top_left_longitude' => ['required', 'string'],
'boundingBoxes.*.bottom_right_latitude' => ['required', 'string'],
'boundingBoxes.*.bottom_right_longitude' => ['required', 'string'],
])->validate();
}
@ -263,4 +227,14 @@ private function validateEmailSettingsForm()
'mail_recipients.*.email' => ['required', 'email'],
])->validateWithBag('saveEmailSettingsForm');
}
/**
* @return mixed
*/
public function mergeFormData(): void
{
$this->formData['pivot_file'] = $this->pivotFiles[0] ?? null;
$this->formData['span_file'] = $this->spanFiles[0] ?? null;
$this->formData['harvest_file'] = $this->harvestDataFiles[0] ?? null;
}
}

View file

@ -5,7 +5,6 @@
use App\Jobs\ProjectDownloadTiffJob;
use App\Jobs\ProjectMosiacGeneratorJob;
use App\Jobs\ProjectReportGeneratorJob;
use App\Livewire\Forms\MailingForm;
use Carbon\CarbonPeriod;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@ -28,52 +27,28 @@ class Project extends Model
'download_path',
'pivot_json_path',
'span_json_path',
'harvest_json_data'
'harvest_json_path'
];
public static function saveWithFormData(mixed $formData)
{
$uniqueIdentifier = ['id' => $formData['id'] ?? null];
$project = Project::updateOrCreate($uniqueIdentifier, $formData);
if ($formData['pivot_json_path']) {
$path = $formData['pivot_json_path']->storeAs($project->download_path .'/Data', 'pivot.geojson');
$project->update(['pivot_json_path' => $path]);
$baseFrom = 'livewire-tmp/';
$baseTo = $project->download_path.'/Data/';
if ($formData['pivot_file']) {
Storage::copy($baseFrom.$formData['pivot_file']['tmpFilename'],$baseTo.'pivot.geojson');
$project->update(['pivot_json_path' => $baseTo.'pivot.geojson']);
}
if ($formData['span_json_path']) {
$path = $formData['span_json_path']->storeAs($project->download_path .'/Data', 'span.geojson');
$project->update(['span_json_path' => $path]);
if ($formData['span_file']) {
Storage::copy($baseFrom.$formData['span_file']['tmpFilename'],$baseTo.'span.geojson');
$project->update(['span_json_path' => $baseTo.'span.geojson']);
}
if ($formData['harvest_json_data']) {
$path = $formData['harvest_json_path']->storeAs($project->download_path .'/Data', 'harvest.xlsx');
$project->update(['harvest_json_path' => $path]);
if ($formData['harvest_file']) {
Storage::copy($baseFrom.$formData['harvest_file']['tmpFilename'],$baseTo.'harvest.'.$formData['harvest_file']['extension']);
$project->update(['harvest_json_path' => $baseTo.'harvest.'.$formData['harvest_file']['extension']]);
}
$project->upsertBoundingBox($formData);
$project->upsertMailRecipients($formData);
}
private function upsertBoundingBox($formData)
{
$boundingBoxesData = array_map(function ($boundingBox) {
$boundingBox['project_id'] = $this->id;
unset($boundingBox['created_at']);
unset($boundingBox['updated_at']);
$boundingBox['id'] ??= null;
return $boundingBox;
}, $formData['boundingBoxes'] ?? []);
ProjectBoundingBox::upsert(
$boundingBoxesData,
['id', 'project_id'],
[
'name',
'top_left_latitude',
'top_left_longitude',
'bottom_right_latitude',
'bottom_right_longitude'
]
);
}
private function upsertMailRecipients($formData)
{
$mailRecipientsData = array_map(function ($mailRecipient) {
@ -95,7 +70,7 @@ public function getMosaicPath()
return sprintf('%s/%s', $this->download_path, 'weekly_mosaic');
}
public function getMosaicList(): \Illuminate\Support\Collection
public function getMosaicList(): Collection
{
return collect(Storage::files($this->getMosaicPath()))
->filter(fn($file) => Str::endsWith($file, '.tif'))
@ -113,7 +88,6 @@ protected static function boot()
parent::boot(); // TODO: Change the autogenerated stub
static::deleting(function ($project) {
$project->boundingBoxes()->delete();
$project->emailRecipients()->delete();
$project->mailings()->each(function ($mailing) {
$mailing->attachments()->delete();
@ -135,11 +109,6 @@ public function getAttachmentPathAttribute()
return '/storage/'.$this->download_path.'/attachments';
}
public function boundingBoxes()
{
return $this->hasMany(ProjectBoundingBox::class);
}
public function emailRecipients()
{
return $this->hasMany(ProjectEmailRecipient::class);
@ -184,7 +153,7 @@ public function allMosaicsPresent(Carbon $endDate): bool
throw new \Exception($message);
}
public function getMosaicFilenameListByEndDate(Carbon $endDate): \Illuminate\Support\Collection
public function getMosaicFilenameListByEndDate(Carbon $endDate): Collection
{
$result = collect([]);
for ($i = 0; $i < 4; $i++) {
@ -370,10 +339,4 @@ public function getMosaicsFor($year, $startWeekNumber)
return $return;
}
public function getBoundingBoxesAsArray() {
return $this->boundingBoxes->map(function ($boundingBox) {
return $boundingBox->toArrayCustom();
})->toArray();
}
}

View file

@ -1,34 +0,0 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProjectBoundingBox extends Model
{
use HasFactory;
protected $fillable = [
'name',
'top_left_latitude',
'top_left_longitude',
'bottom_right_latitude',
'bottom_right_longitude',
];
public function project()
{
return $this->belongsTo(Project::class);
}
public function toArrayCustom()
{
return [
(float) $this->top_left_latitude,
(float) $this->top_left_longitude,
(float) $this->bottom_right_latitude,
(float) $this->bottom_right_longitude
];
}
}

View file

@ -0,0 +1,34 @@
<?php
use App\Models\Project;
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('project_bounding_boxes');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::create('project_bounding_boxes', function (Blueprint $table) {
$table->id();
$table->foreignIdFor(Project::class);
$table->string('name');
$table->string('top_left_latitude');
$table->string('top_left_longitude');
$table->string('bottom_right_latitude');
$table->string('bottom_right_longitude');
$table->timestamps();
});
}
};

View file

@ -60,76 +60,12 @@
<span class="bg-red-100 text-red-400 p-1 rounded">{{ $message }}</span>
@enderror
</div>
@foreach($projectManager->formData['boundingBoxes'] as $key => $boundingBox)
{{-- <div wire:key="bounding_box_{{ $key }}">--}}
<div class="col-span-6 sm:col-span-4">
{{ __('Bounding box') }}
@if( count($projectManager->formData['boundingBoxes']) > 1)
<button
class="cursor-pointer ml-6 text-sm text-red-500"
type="button"
wire:click="deleteBoundingBox({{ $key }})"
wire:confirm="{{ __('Are you sure you want to delete this BoundingBox?') }}"
>
Delete
</button>
@endif
</div>
<div class="col-span-6 sm:col-span-4">
<x-label for="bounding_box_{{ $key }}_name" value="{{ __('Name') }}"/>
<x-input id="bounding_box_{{ $key }}_name" type="text" class="mt-1 block w-full"
wire:model="formData.boundingBoxes.{{ $key }}.name" autofocus/>
<x-input-error for="boundingBoxes.{{ $key }}.name" class="mt-2"/>
</div>
<div class="col-span-6 grid grid-cols-6 gap-1">
<div class="col-span-3">
<x-label for="bounding_box_{{ $key }}_top_left_latitude"
value="{{ __('Top left Latitude') }}"/>
<x-input id="bounding_box_{{ $key }}_top_left_latitude" type="text"
class="block w-full"
wire:model="formData.boundingBoxes.{{ $key }}.top_left_latitude"
autofocus/>
<x-input-error for="boundingBoxes.{{ $key }}.top_left_latitude" class="mt-2"/>
</div>
<div class="col-span-3">
<x-label for="bounding_box_{{ $key }}_top_left_longitude" value="{{ __('Longitude') }}"/>
<x-input id="name" type="text" class="block w-full"
wire:model="formData.boundingBoxes.{{ $key }}.top_left_longitude"
autofocus/>
<x-input-error for="boundingBoxes.{{ $key }}.top_left_longitude" class="mt-2"/>
</div>
<div class="col-span-3">
<x-label for="bounding_box_{{ $key }}_bottom_right_latitude"
value="{{ __('Bottom right Latitude') }}"/>
<x-input id="bounding_box_{{ $key }}_bottom_right_latitude" type="text"
class="block w-full"
wire:model="formData.boundingBoxes.{{ $key }}.bottom_right_latitude"
autofocus/>
<x-input-error for="boundingBoxes.{{ $key }}.bottom_right_latitude" class="mt-2"/>
</div>
<div class="col-span-3">
<x-label for="boundingBox_{{ $key }}_bottom_right_longitude" value="{{ __('Longitude') }}"/>
<x-input id="boundingBox_{{ $key }}_bottom_right_longitude" type="text"
class="block w-full"
wire:model="formData.boundingBoxes.{{ $key }}.bottom_right_longitude"
autofocus/>
<x-input-error for="boundingBoxes.{{ $key }}.bottom_right_longitude" class="mt-2"/>
</div>
</div>
{{-- </div>--}}
@endforeach
</x-slot>
<x-slot name="actions">
<x-action-message class="mr-3" on="saved">
{{ __('Saved.') }}
</x-action-message>
<x-secondary-button class="mr-3"
type="button"
wire:click="addBoundingBox"
>
{{ __('Add Bounding box') }}
</x-secondary-button>
<x-secondary-button class="mr-3"
type="button"
x-on:click="$wire.showProjectModal = false"