Skip to content

Commit

Permalink
Merge pull request #601 from wri/feat/TM-1401-cleaned-tree-species
Browse files Browse the repository at this point in the history
[TM-1401] Import cleaned tree species associations.
  • Loading branch information
roguenet authored Dec 6, 2024
2 parents 2b7366c + cb17f0f commit b05d9cb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
1 change: 0 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: pull-request
on:
pull_request:
branches: [main, staging, release/**]
jobs:
lintTest:
runs-on: ubuntu-latest
Expand Down
80 changes: 80 additions & 0 deletions app/Console/Commands/ImportTreeSpeciesAssociations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Console\Commands;

use App\Console\Commands\Traits\Abortable;
use App\Models\V2\TreeSpecies\TreeSpecies;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;

class ImportTreeSpeciesAssociations extends Command
{
use Abortable;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import-tree-species-associations {file}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Imports a CSV that links UUIDs from v2_tree_species to taxon_ids from tree_species_research';

protected int $treeSpeciesUuidColumn;

protected int $taxonIdColumn;

/**
* Execute the console command.
*/
public function handle()
{
$this->executeAbortableScript(function () {
$process = new Process(['wc', '-l', $this->argument('file')]);
$process->run();
$this->assert($process->isSuccessful(), "WC failed {$process->getErrorOutput()}");

$lines = ((int)explode(' ', $process->getOutput())[0]) - 1;

$fileHandle = fopen($this->argument('file'), 'r');
$this->parseHeaders(fgetcsv($fileHandle));

$this->withProgressBar($lines, function ($progressBar) use ($fileHandle) {
while ($csvRow = fgetcsv($fileHandle)) {
$treeSpeciesUuid = $csvRow[$this->treeSpeciesUuidColumn];
$taxonId = $csvRow[$this->taxonIdColumn];

if ($taxonId != 'NA') {
TreeSpecies::isUuid($treeSpeciesUuid)->update(['taxon_id' => $taxonId]);
}
$progressBar->advance();
}

$progressBar->finish();
});

fclose($fileHandle);
});
}

protected function parseHeaders(array $headerRow): void
{
foreach ($headerRow as $index => $header) {
if ($header == 'tree_species_uuid') {
$this->treeSpeciesUuidColumn = $index;
} elseif ($header == 'taxon_id') {
$this->taxonIdColumn = $index;
}
}

$this->assert(
$this->treeSpeciesUuidColumn != null && $this->taxonIdColumn != null,
'Not all required columns were found'
);
}
}
9 changes: 6 additions & 3 deletions app/Models/V2/TreeSpecies/TreeSpecies.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ class TreeSpecies extends Model implements EntityRelationModel
'speciesable_id',
'collection',
'hidden',

'old_id',
'old_model',
'taxon_id',
];

public const COLLECTION_DIRECT_SEEDING = 'direct-seeding';
Expand Down Expand Up @@ -82,6 +80,11 @@ public function speciesable()
return $this->morphTo();
}

public function taxonomicSpecies()
{
return $this->belongsTo(TreeSpeciesResearch::class, 'taxon_id');
}

public function getRouteKeyName()
{
return 'uuid';
Expand Down

0 comments on commit b05d9cb

Please sign in to comment.