Last active
May 28, 2020 15:48
-
-
Save tostercx/43ec2e6fd72ec8e2be5f226aa83c7365 to your computer and use it in GitHub Desktop.
Convert ARIS VZD XML to CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$csv = ""; | |
function wcsv($a) { | |
global $csv; | |
$csv .= '#' . implode('#;#', $a) . "#\r\n"; | |
} | |
$filemap = [ | |
'HouseCoordinateData' => 'AW_EKA_GEO', | |
'StreetData' => 'AW_IELA', | |
'ParishData' => 'AW_PAGASTS', | |
'CountyData' => 'AW_NOVADS', | |
'HouseData' => 'AW_NLIETA', | |
'TownData' => 'AW_PILSETA', | |
'VillageData' => 'AW_CIEMS', | |
]; | |
$hmap = [ | |
'HouseCoordinateData' => [ | |
'Mslink' => 'mslink', | |
'ARCode' => 'vieta_cd', | |
'XValue' => 'koord_x', | |
'YValue' => 'koord_y', | |
], | |
'StreetData' => [ | |
'Code' => 'kods', | |
'Type' => 'tips_cd', | |
'Name' => 'nosaukums', | |
'ParentCode' => 'vkur_cd', | |
'ParentType' => 'vkur_tips', | |
'Approved' => 'apstipr', | |
'ApproveDegree' => 'apst_pak', | |
'Status' => 'statuss', | |
'SortName' => 'sort_nos', | |
'DateCreated' => 'dat_sak', | |
'DateModified' => 'dat_mod', | |
'DateDeleted' => 'dat_beig', | |
'StreetCode' => 'atrib' | |
], | |
'ParishData' => [ | |
'Code' => 'kods', | |
'Type' => 'tips_cd', | |
'Name' => 'nosaukums', | |
'ParentCode' => 'vkur_cd', | |
'ParentType' => 'vkur_tips', | |
'Approved' => 'apstipr', | |
'ApproveDegree' => 'apst_pak', | |
'Status' => 'statuss', | |
'SortName' => 'sort_nos', | |
'DateCreated' => 'dat_sak', | |
'DateModified' => 'dat_mod', | |
'DateDeleted' => 'dat_beig', | |
'ATUCode' => 'atrib' | |
], | |
'CountyData' => [ | |
'Code' => 'kods', | |
'Type' => 'tips_cd', | |
'Name' => 'nosaukums', | |
'ParentCode' => 'vkur_cd', | |
'ParentType' => 'vkur_tips', | |
'Approved' => 'apstipr', | |
'ApproveDegree' => 'apst_pak', | |
'Status' => 'statuss', | |
'SortName' => 'sort_nos', | |
'DateCreated' => 'dat_sak', | |
'DateModified' => 'dat_mod', | |
'DateDeleted' => 'dat_beig', | |
'ATUCode' => 'atrib' | |
], | |
'HouseData' => [ | |
'Code' => 'kods', | |
'Type' => 'tips_cd', | |
'Status' => 'statuss', | |
'Approved' => 'apstipr', | |
'ApproveDegree' => 'apst_pak', | |
'ParentCode' => 'vkur_cd', | |
'ParentType' => 'vkur_tips', | |
'Name' => 'nosaukums', | |
'SortName' => 'sort_nos', | |
'PostIndex' => 'atrib', | |
'PostOfficeCode' => 'pnod_cd', | |
'DateCreated' => 'dat_sak', | |
'DateModified' => 'dat_mod', | |
'DateDeleted' => 'dat_beig', | |
'ForBuild' => 'for_build' | |
], | |
'TownData' => [ | |
'Code' => 'kods', | |
'Type' => 'tips_cd', | |
'Name' => 'nosaukums', | |
'ParentCode' => 'vkur_cd', | |
'ParentType' => 'vkur_tips', | |
'Approved' => 'apstipr', | |
'ApproveDegree' => 'apst_pak', | |
'Status' => 'statuss', | |
'SortName' => 'sort_nos', | |
'DateCreated' => 'dat_sak', | |
'DateModified' => 'dat_mod', | |
'DateDeleted' => 'dat_beig', | |
'ATUCode' => 'atrib' | |
], | |
'VillageData' => [ | |
'Code' => 'kods', | |
'Type' => 'tips_cd', | |
'Name' => 'nosaukums', | |
'ParentCode' => 'vkur_cd', | |
'ParentType' => 'vkur_tips', | |
'Approved' => 'apstipr', | |
'ApproveDegree' => 'apst_pak', | |
'Status' => 'statuss', | |
'SortName' => 'sort_nos', | |
'DateCreated' => 'dat_sak', | |
'DateModified' => 'dat_mod', | |
'DateDeleted' => 'dat_beig', | |
'VillageCode' => 'atrib', | |
], | |
]; | |
foreach(glob('xml/*.xml') as $file) { | |
echo " ---- $file ----\n"; | |
$type = basename($file, '.xml'); | |
if (!isset($hmap[$type])) { | |
echo "File type not supported, skipping!\n"; | |
continue; | |
} | |
$xml = simplexml_load_file($file); | |
$node = $xml->children('env', true)->Body->children('n1', true); | |
$node = ((array)$node)[array_keys((array)$node)[0]]; | |
$node = ((array)$node)[array_keys((array)$node)[0]]; | |
$node = ((array)$node)[array_keys((array)$node)[0]]; | |
$header = $hmap[$type]; | |
wcsv($header); | |
foreach ($node as $line) { | |
$line = (array)$line; | |
$l2 = []; | |
foreach($header as $k => $h) { | |
$val = isset($line[$k]) ? $line[$k] : ""; | |
if ($val && strpos($h, 'dat_') === 0) { | |
$pt = explode(' ', $val); | |
$d = explode('-', $pt[0]); | |
$d = array_reverse($d); | |
$val = implode('.', $d) . ' ' . $pt[1]; | |
} | |
$l2[$h] = $val; | |
} | |
wcsv($l2); | |
} | |
file_put_contents('csv/' . $filemap[$type] . '.CSV', iconv('UTF-8', 'CP1257', $csv)); | |
$csv = ''; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment