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-1760] add delete data in the parse #723

Merged
merged 2 commits into from
Mar 3, 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
31 changes: 25 additions & 6 deletions app/Console/Commands/NormalizeSitePolygonDataCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class NormalizeSitePolygonDataCommand extends Command
'single-line,partial' => 'partial,single-line',
'single-line,full' => 'full,single-line',
'full, single-line' => 'full,single-line',
'Null' => null,
'partial coverage_perimetral' => 'partial',
'Partial plantation, Line plantation' => 'partial,single-line',
'N/A' => null,
'partial,planting-in-patches' => 'partial',
'' => null,
];

protected $targetSysMapping = [
Expand All @@ -58,6 +64,10 @@ class NormalizeSitePolygonDataCommand extends Command
'Open natural ecosystem or Grasslands' => 'natural-forest',
'Agroforestry' => 'agroforest',
'Tree Planting' => 'natural-forest',
'N/A' => null,
'Null' => null,
'' => null,
'open-natural-ecosystem' => 'natural-forest',
];

protected $practiceMapping = [
Expand Down Expand Up @@ -95,6 +105,15 @@ class NormalizeSitePolygonDataCommand extends Command
'tree-planting,direct-seeding' => 'direct-seeding,tree-planting',
'tree-planting,direct-seeding,applied-nucleation' => 'direct-seeding,tree-planting',
'tree-planting,direct-seeding,applied-nucleation,cutting' => 'direct-seeding,tree-planting',
'N/A' => null,
'Null' => null,
'' => null,
'agroforestry-/tree-planting' => 'tree-planting',
'enrichment-planting' => null,
'enrichment-planting,assisted-natural-regeneration' => 'assisted-natural-regeneration',
'tree planting/RNA' => 'tree-planting',
'tree-planting, direct-seeding, cutting' => 'direct-seeding,tree-planting',
'tree-planting, assisted-natural-regeneration, direct-seeding' => 'assisted-natural-regeneration,direct-seeding,tree-planting',
];

/**
Expand Down Expand Up @@ -164,7 +183,7 @@ protected function normalizeDistrColumn($isDryRun = false)
continue;
}

$count = SitePolygon::where('distr', $oldValue)->count();
$count = SitePolygon::withTrashed()->where('distr', $oldValue)->count();
if ($count > 0) {
$valuesToUpdate[$oldValue] = [
'new_value' => $newValue,
Expand Down Expand Up @@ -193,7 +212,7 @@ protected function normalizeDistrColumn($isDryRun = false)

$this->info('Updating distr values...');
foreach ($valuesToUpdate as $oldValue => $info) {
SitePolygon::where('distr', $oldValue)
SitePolygon::withTrashed()->where('distr', $oldValue)
->update(['distr' => $info['new_value']]);

$this->info("Updated {$info['count']} records from '{$oldValue}' to " .
Expand All @@ -214,7 +233,7 @@ protected function normalizeTargetSysColumn($isDryRun = false)
continue;
}

$count = SitePolygon::where('target_sys', $oldValue)->count();
$count = SitePolygon::withTrashed()->where('target_sys', $oldValue)->count();
if ($count > 0) {
$valuesToUpdate[$oldValue] = [
'new_value' => $newValue,
Expand All @@ -241,7 +260,7 @@ protected function normalizeTargetSysColumn($isDryRun = false)

$this->info('Updating target_sys values...');
foreach ($valuesToUpdate as $oldValue => $info) {
SitePolygon::where('target_sys', $oldValue)
SitePolygon::withTrashed()->where('target_sys', $oldValue)
->update(['target_sys' => $info['new_value']]);

$this->info("Updated {$info['count']} records from '{$oldValue}' to '{$info['new_value']}'");
Expand All @@ -261,7 +280,7 @@ protected function normalizePracticeColumn($isDryRun = false)
continue;
}

$count = SitePolygon::where('practice', $oldValue)->count();
$count = SitePolygon::withTrashed()->where('practice', $oldValue)->count();
if ($count > 0) {
$valuesToUpdate[$oldValue] = [
'new_value' => $newValue,
Expand Down Expand Up @@ -290,7 +309,7 @@ protected function normalizePracticeColumn($isDryRun = false)

$this->info('Updating practice values...');
foreach ($valuesToUpdate as $oldValue => $info) {
SitePolygon::where('practice', $oldValue)
SitePolygon::withTrashed()->where('practice', $oldValue)
->update(['practice' => $info['new_value']]);

$this->info("Updated {$info['count']} records from '{$oldValue}' to " .
Expand Down
21 changes: 20 additions & 1 deletion app/Services/PolygonService.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,24 @@ protected function orderCommaSeparatedPropertiesAlphabetically(string $commaSepa
return implode(',', $properties);
}

protected function validateTargetSys(string $targetSys): ?string
{
$validValues = [
'agroforest',
'mangrove',
'natural-forest',
'peatland',
'riparian-area-or-wetland',
'silvopasture',
'urban-forest',
'woodlot-or-plantation',
];

$targetSys = trim($targetSys);

return in_array($targetSys, $validValues, true) ? $targetSys : null;
}

public function validateSitePolygonProperties(string $polygonUuid, array $properties)
{
// Avoid trying to store an invalid date string or int in the DB, as that will throw an exception and prevent
Expand All @@ -419,14 +437,15 @@ public function validateSitePolygonProperties(string $polygonUuid, array $proper

$practicesValidValues = ['assisted-natural-regeneration', 'direct-seeding','tree-planting'];
$properties['practice'] = $this->orderCommaSeparatedPropertiesAlphabetically($properties['practice'] ?? '', $practicesValidValues);
$properties['target_sys'] = $this->validateTargetSys($properties['target_sys'] ?? '');

return [
'poly_name' => $properties['poly_name'] ?? null,
'site_id' => $properties['site_id'] ?? null,
'plantstart' => $properties['plantstart'],
'plantend' => $properties['plantend'],
'practice' => $properties['practice'],
'target_sys' => $properties['target_sys'] ?? null,
'target_sys' => $properties['target_sys'],
'distr' => $properties['distr'],
'num_trees' => $properties['num_trees'],
'calc_area' => $properties['area'] ?? null,
Expand Down
Loading