Skip to content

Commit

Permalink
population import update
Browse files Browse the repository at this point in the history
  • Loading branch information
kaizokou committed Oct 7, 2024
1 parent f253dda commit 7f86246
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/Command/CityImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->success('Population purge completed successfully.');

$this->cityService->importData($populationFilePath, 'population', ';');
$this->cityService->aggregatePopulationForMainCities();
} elseif ($coordinatesOnly) {
$io->writeln('<info>Purging all coordinates data...</info>');
$this->cityService->purgeCoordinates();
Expand All @@ -97,6 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->cityService->importData($citiesFilePath, 'city', ';');
$this->cityService->importData($coordinateFilePath, 'coordinates');
$this->cityService->importData($populationFilePath, 'population', ';');
$this->cityService->aggregatePopulationForMainCities();
}

$end = new DateTime();
Expand Down
73 changes: 68 additions & 5 deletions src/Service/CityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Entity\City;
use App\Entity\Department;
use App\Entity\Drug;
use App\Entity\Entity;
use App\Entity\Region;
use App\Enum\DepartmentType;
Expand Down Expand Up @@ -474,12 +473,37 @@ private function processPopulation(array $data): void

// Try fallback methods to find the city
if ($this->verbose) {
$this->output->writeln("<comment>No main city found for INSEE code: $inseeCode. Attempting to match by city name: $communeName</comment>");
$this->output->writeln("<comment>No main city found for INSEE code: $inseeCode. Attempting to find a sub city</comment>");
}

$subCities = $this->em->getRepository(City::class)->findBy(['inseeCode' => $inseeCode,]);

if ($subCities && count($subCities) === 1) {
$subCity = $subCities[0];
$subCity->setPopulation((int)$totalPopulation);
$this->populationUpdates[] = $subCity;
$this->nbSuccessPopulation++;

if (count($this->populationUpdates) >= $this->batchSize) {
$this->flushBatch();
}

if ($this->verbose) {
$this->output->writeln("<info>Updated population for sub city: {$subCity->getSubCityName()} (INSEE: $inseeCode)</info>");
}
return;
}

// Try fallback methods to find the city
if ($this->verbose) {
$this->output->writeln("<comment>No sub city found for INSEE code: $inseeCode. Attempting to match by city name: $communeName</comment>");
}

// Try to match by communeName in altName, subCityName, or subCityAltName
$matchingCities = $this->em->getRepository(City::class)->createQueryBuilder('c')
->where('LOWER(c.name) = :name OR LOWER(c.rawName) = :name OR LOWER(c.subCityName) = :name OR LOWER(c.rawSubName) = :name')
->where(
'LOWER(c.name) = :name OR LOWER(c.rawName) = :name OR LOWER(c.subCityName) = :name OR LOWER(c.rawSubName) = :name'
)
->setParameter('name', strtolower($communeName))
->getQuery()
->getResult();
Expand All @@ -502,14 +526,53 @@ private function processPopulation(array $data): void
}
return;
} elseif ($numMatches > 1) {
$this->output->writeln("<error>Ambiguous matches found for commune name: $communeName (INSEE: $inseeCode)</error>");
$this->output->writeln(
"<error>Ambiguous matches found for commune name: $communeName (INSEE: $inseeCode)</error>"
);
} else {
$this->output->writeln("<error>No main city found for INSEE code: $inseeCode and no city matched the commune name: $communeName</error>");
$this->output->writeln(
"<error>No main city found for INSEE code: $inseeCode and no city matched the commune name: $communeName</error>"
);
}

$this->nbFailedPopulation++;
}

public function aggregatePopulationForMainCities(): void
{
// Fetch all main cities (mainCity is NULL) with no population set (population is NULL)
$mainCities = $this->em->getRepository(City::class)->createQueryBuilder('c')
->where('c.mainCity IS NULL')
->andWhere('c.population IS NULL')
->getQuery()
->getResult();

// Iterate through each main city and calculate the total population of its subtitles
/** @var City $mainCity */
foreach ($mainCities as $mainCity) {
$subCities = $mainCity->getSubCities();
$totalPopulation = 0;
foreach ($subCities as $subCity) {
if ($subCity->getPopulation() !== null) {
$totalPopulation += $subCity->getPopulation();
}
}

if ($totalPopulation > 0) {
$mainCity->setPopulation($totalPopulation);
$this->em->persist($mainCity);

if ($this->verbose) {
$this->output->writeln("<info>Updated population for main city: {$mainCity->getName()} to $totalPopulation</info>");
}
}
}

$this->em->flush();
$this->output->writeln("<info>Population aggregation completed for main cities.</info>");
}


private function normalizeCityName(string $name): string
{
// Remove leading articles at the start
Expand Down

0 comments on commit 7f86246

Please sign in to comment.