Skip to content

Instantly share code, notes, and snippets.

@danielmitd
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save danielmitd/2c7591de52f296b9ca50 to your computer and use it in GitHub Desktop.

Select an option

Save danielmitd/2c7591de52f296b9ca50 to your computer and use it in GitHub Desktop.
mapping match post codes
<?php
// read in translated city nam
$translations = [];
$languages = [ 1 => 'de', 2 => 'fr', 3 => 'it', 4 => 'rm' ];
$fp = fopen($argv[1], 'r');
while(false !== ($line = fgetcsv($fp, 1024, "\t"))) {
list(
// Primary key for postcode/town
$classificationNumber,
// incremental number,
$classificationIncrement,
// 2: allowed for addressing, mainly translations or alternative namings
// 3: regional description, used in address but not with postcode
$descriptionType,
// 1: german
// 2: french
// 3: italian
// 4: romansh
$languageCode,
// locality name max. 18 characters long
$localityName18,
// locality name max. 27 characters long
$localityName27
) = array_map('utf8_encode', $line);
if ($descriptionType != 2) {
continue;
}
$translations[$classificationNumber][$languages[$languageCode]] = $localityName27;
}
$municipalCities = [];
$cities = [];
$fp = fopen($argv[2], 'r');
while(false !== ($line = fgetcsv($fp, 1024, "\t"))) {
list(
// Primary key for postcode/town
$classificationNumber,
// 10 = domicile and P.O. box addresses
// 20 = only domicile addresses
// 30 = only P.O. box
// 40 = company postcodes
// 80 = internal postcodes (indicates the delivery post office on bundle slips or on bag labels)
$postcodeType,
// Address postcode.
// 3 = routing district (Berne)
// 34 = routing area (Burgdorf)
// 343 = route (Burgdorf – Langnau)
// 3436 = post office number (Zollbrück)
$postcode,
// Additional postcode digits
$postcodeDigits,
// locality name max. 18 characters long
$localityName18,
// locality name max. 27 characters long
$localityName27,
// Official abbreviations
$canton,
// Language (language majority) within a postcode area. 1 = German, 2 = French, 3 = Italian, 4 = Romansh.
$languageCode,
// Additional language within a postcode.
$languageCodeAlt,
// ndicates if the postcode is included in the «sort file»
$presentInSortFile,
// Indicates the post office (ONRP) that delivers most of the letters
$mailDeliveryBy,
// Numbering used by the Federal Statistical Office for municipalities
$municipalityNumber,
// Indicates as of when the dates are valid (format YYYYMMDD)
$validityDate
) = array_map('utf8_encode', $line);
if (!in_array($postcodeType, [10, 20])) {
continue;
}
if (!isset($municipalCities[$municipalityNumber])) {
$municipalCities[$municipalityNumber] = $classificationNumber;
}
$strippedName = trim(preg_replace('/\d/', '', $localityName27));
if (isset($cities["$postcode-$canton-$strippedName"])) {
continue;
}
// if translation is available
$translation = [];
$translation[$languages[$languageCode]] = $localityName27;
if (isset($translations[$municipalCities[$municipalityNumber]])) {
$translation = $translations[$municipalCities[$municipalityNumber]];
}
$cities["$postcode-$canton-$strippedName"] = [
'code' => $postcode,
'canton' => $canton,
'name' => $localityName27,
'locale' => $languages[$languageCode],
'alt_names' => $translation
];
}
file_put_contents('post-cities.json', json_encode(array_values($cities), JSON_PRETTY_PRINT).PHP_EOL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment