Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #36 from minvws/import
Browse files Browse the repository at this point in the history
Updated import to use the spkkl database
  • Loading branch information
jaytaph authored Mar 9, 2023
2 parents 01df0fe + 3d56893 commit e1b7745
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ When validation has failed:
# Importing data

Data is currently not specified, but will be in the future. For now, the data is imported from a CSV file that
can be found at: https://www.postnl.nl/Images/testset_pct2020_tcm10-185443.zip
can be found at: https://www.spikkl.nl/postcode-database/download/spikkl-postal-code-level-csv-demo.zip

To import:

```shell
$ php bin/console app:import reeks-1000-records.csv
$ php bin/console app:import <file.csv>
```
44 changes: 22 additions & 22 deletions app/Services/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ public function import(string $file, ?callable $outputCallback): void
return;
}

fread($file, 3); // Read and discard BOM

$headers = fgetcsv($file);
if (!$headers) {
$headers = [];
}
$headers = array_map('strtolower', $headers);
$headers = array_map('trim', $headers);


$idx = 0;
while (!feof($file)) {
$idx++;
Expand All @@ -38,31 +35,34 @@ public function import(string $file, ?callable $outputCallback): void
$data = array_combine($headers, $line);
$data = array_map('trim', $data);

// Skip non-build streets
if ($data['reeksindicatie'] === '') {
continue;
}

if (empty($data['lettercombinatie'])) {
continue;
}

$street = Street::firstOrCreate([
'name' => $data['straatnaam officieel'],
'name' => $data['street'],
]);

$city = City::firstOrCreate([
'name' => $data['woonplaatsnaam nen'],
'name' => $data['city'],
]);

$postcode = Postcode::create([
'postcode' => $data['wijkcode'] . $data['lettercombinatie'],
'odd' => $data['reeksindicatie'] === '0',
'from_number' => $data['codebreekpunt-van'],
'to_number' => $data['codebreekpunt-t/m'],
'city_id' => $city->id,
'street_id' => $street->id,
]);
// ranges can be comma separated like: 2-4,8,10-12,9-13,17
$ranges = explode(',', $data['street_number_range']);
foreach ($ranges as $range) {
if (str_contains($range, '-')) {
[$from, $to] = explode('-', $range);
} else {
$from = $to = $range;
}

$isOdd = $from % 2 === 1;

$postcode = Postcode::create([
'postcode' => $data['postal_code'],
'odd' => $isOdd,
'from_number' => $from,
'to_number' => $to,
'city_id' => $city->id,
'street_id' => $street->id,
]);
}

if ($outputCallback !== null) {
$outputCallback($idx, $postcode);
Expand Down

0 comments on commit e1b7745

Please sign in to comment.