370 lines
13 KiB
PHP
Executable File
370 lines
13 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
|
|
//require 'vendor/autoload.php';
|
|
require("../config/config.php");
|
|
|
|
define('FRONKDB_SQLDEBUG',false);
|
|
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
|
|
|
|
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
|
|
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
|
|
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
|
|
|
|
$me = new User(1);
|
|
|
|
$filename = BASEDIR."/scripts/import/premstaetten_buildings.csv";
|
|
|
|
$db = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
|
|
$log = mfLoghandler::singleton();
|
|
|
|
$input = fopen($filename, "r");
|
|
|
|
$h_extrefs = [];
|
|
|
|
$i = 0;
|
|
while($csv = fgetcsv($input, 0, ";")) {
|
|
$i++;
|
|
if($i == 1) continue;
|
|
|
|
|
|
$hausnummer = false;
|
|
|
|
if(!trim($csv[0])) {
|
|
continue;
|
|
} else {
|
|
//echo implode(", ", $csv)."\n";
|
|
$gem_kz = trim($csv[17]);
|
|
$gem_name = trim($csv[18]);
|
|
//$ort_kz = trim($csv[2]);
|
|
$plz_name = trim($csv[5]);
|
|
$ort_name = trim($csv[6]);
|
|
$strasse_hausnummer = trim($csv[4]);
|
|
$hausnummer_extref = trim($csv[1]);
|
|
if(in_array($hausnummer_extref, $h_extrefs)) {
|
|
die("Hausnummer extref doppelt!\n");
|
|
}
|
|
$h_extrefs[] = $hausnummer_extref;
|
|
|
|
$lat = str_replace(",",".",trim($csv[7]));
|
|
$long = str_replace(",",".",trim($csv[8]));
|
|
|
|
$netzgebiet_extref = trim($csv[14]);
|
|
if(!$netzgebiet_extref) continue;
|
|
$netzgebiet = new ADBNetzgebiet();
|
|
$netzgebiet->loadByExtref($netzgebiet_extref);
|
|
if(!$netzgebiet->id) {
|
|
die("Netzgebiet nicht gefunden: $netzgebiet_extref\n");
|
|
}
|
|
//var_dump($netzgebiet);exit;
|
|
|
|
$nutzung = trim($csv[9]);
|
|
$funktion = trim($csv[20]);
|
|
if($nutzung != "Greenfield" && $funktion == "Greenfield") {
|
|
$nutzung = "Greenfield";
|
|
}
|
|
|
|
$unit_count = $csv[10];
|
|
if(!$unit_count) $unit_count = 1;
|
|
|
|
if(!$strasse_hausnummer) continue;
|
|
|
|
//echo "strasse: $strasse\n";
|
|
//echo "strasse_building: $strasse_building\n";
|
|
|
|
$strasse_name = "";
|
|
$hausnummer_name = "";
|
|
$addresszusatz = "";
|
|
|
|
$m = [];
|
|
// (?:\s+((?:gesch(?:ae|ä)ft|betrieb und wohnungen|paketlogistik|cafe|pavillon|pfarrheim|[^ ]*haus|[^ ]*geb(?:ae|ä)ude|[^ ]*halle)(?:\s+[a-z0-9]+)?))?
|
|
if(preg_match('/^(\D+)\s+(\d+[a-z0-9\/&#._-]*)(?:\s+((?:gesch(?:ae|ä)ft|betrieb und wohnungen|stg|paketlogistik|cafe|pavillon|pfarrheim|[^ ]*haus|[^ ]*geb(?:ae|ä)ude|[^ ]*halle)(?:\s+[a-z0-9]+)?))?/i', $strasse_hausnummer, $m)) {
|
|
$strasse_name = trim($m[1]);
|
|
$hausnummer_name = trim($m[2]);
|
|
if(array_key_exists(3, $m)) {
|
|
$addresszusatz = trim($m[3]);
|
|
}
|
|
} elseif(preg_match('/^(.+)\s+(\d+[a-z0-9\/&#._-]*)(.+)?/i', $strasse_hausnummer, $m)) {
|
|
$strasse_name = trim($m[1]);
|
|
$hausnummer_name = trim($m[2]);
|
|
if(array_key_exists(3, $m)) {
|
|
$addresszusatz = trim($m[3]);
|
|
}
|
|
} elseif(preg_match('/^(.+)/i', $strasse_hausnummer, $m)) {
|
|
// ignore GST objects
|
|
continue;
|
|
$strasse_name = trim($m[1]);
|
|
}
|
|
|
|
if($strasse_name == "Dr.Karl-Renner-Weg") $strasse_name = "Dr.-Karl-Renner-Weg";
|
|
if($strasse_name == "Bierbaumer Strasse") $strasse_name = "Bierbaumerstrasse";
|
|
if($strasse_name == "GST") continue;
|
|
if(preg_match('/^L\d+/',$strasse_name)) continue;
|
|
|
|
/*if($strasse_name == "Hauptstrasse 103 Geschaeft" && $hausnummer_name == "1") {
|
|
$strasse_name = "Hauptstrasse";
|
|
$hausnummer_name = "103";
|
|
$addresszusatz = "Geschaeft 1";
|
|
}
|
|
|
|
if($strasse_name == "Hauptstrasse 131 Stg") {
|
|
$strasse_name = "Hauptstrasse";
|
|
$addresszusatz = "Stg $hausnummer_name";
|
|
$hausnummer_name = "131";
|
|
}*/
|
|
|
|
if(!$strasse_name && !$nutzung == "Greenfield") {
|
|
die("Unbekanntes Adressformat\n");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if(!$gem_name || !$plz_name || !$ort_name || !$strasse_name) {
|
|
die("!! Konnte Adresse nicht parsen\n");
|
|
}
|
|
|
|
if(!$hausnummer_extref) {
|
|
die("!!! Keine Hausnummer Extref\n");
|
|
}
|
|
|
|
if(!$netzgebiet_extref) {
|
|
die("!!! Keine Netzgebiet Extref\n");
|
|
}
|
|
if($nutzung == "Greenfield") continue;
|
|
//echo "Netzgebiet extref: \033[1m$netzgebiet_extref\033[0m | Hausnummer extref: \033[1m$hausnummer_extref\033[0m | gem: \033[1m$gem_name\033[0m | plz: \033[1m$plz_name\033[0m | ort: \033[1m$ort_name\033[0m | strasse: \033[1m$strasse_name\033[0m | hausnummer: \033[1m$hausnummer_name\033[0m |";
|
|
//echo "Zusatz: \033[1m$addresszusatz\033[0m | (\033[1m$nutzung\033[0m)\n";
|
|
//continue;
|
|
// find address in AddressDB and update extrefs
|
|
|
|
$strasse_name = $db->escape($strasse_name);
|
|
$hausnummer_name = $db->escape($hausnummer_name);
|
|
|
|
$strasse_search = [$strasse_name];
|
|
|
|
if(strpos($strasse_name, ' ') !== false) $strasse_search[] = str_replace(' ', '-', $strasse_name);
|
|
if(strpos($strasse_name, '-') !== false) $strasse_search[] = str_replace('-', ' ', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.', '. ', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.', '.-', $strasse_name);
|
|
if(strpos($strasse_name, '. ') !== false) $strasse_search[] = str_replace('. ', '.', $strasse_name);
|
|
if(strpos($strasse_name, '. ') !== false) $strasse_search[] = str_replace('. ', '.-', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.-', '.', $strasse_name);
|
|
if(strpos($strasse_name, '.') !== false) $strasse_search[] = str_replace('.-', '. ', $strasse_name);
|
|
|
|
foreach($strasse_search as $search) {
|
|
if(strpos($search, 'ß') !== false) $strasse_search[] = str_replace('ß', 'ss', $search);
|
|
if(strpos($search, 'ä') !== false) $strasse_search[] = str_replace('ä', 'ae', $search);
|
|
if(strpos($search, 'ö') !== false) $strasse_search[] = str_replace('ö', 'oe', $search);
|
|
if(strpos($search, 'ü') !== false) $strasse_search[] = str_replace('ü', 'ue', $search);
|
|
|
|
if(strpos($search, 'ss') !== false) $strasse_search[] = str_replace('ss', 'ß', $search);
|
|
if(strpos($search, 'ae') !== false) $strasse_search[] = str_replace('ae', 'ä', $search);
|
|
if(strpos($search, 'oe') !== false) $strasse_search[] = str_replace('oe', 'ö', $search);
|
|
if(strpos($search, 'ue') !== false) $strasse_search[] = str_replace('ue', 'ü', $search);
|
|
}
|
|
|
|
|
|
$sql = "SELECT * FROM view_hausnummer WHERE gemeinde_id = 1 AND strasse IN ('". implode("', '", $strasse_search)."') AND hausnummer='$hausnummer_name'";
|
|
/*if(preg_match('/^Tobelbader/i',$strasse_name)) {
|
|
echo "$sql\n";
|
|
}*/
|
|
$res = $db->query($sql);
|
|
|
|
if(!$db->num_rows($res)) {
|
|
// adresse nicht gefunden => anlegen
|
|
|
|
if($nutzung == "Greenfield") continue;
|
|
if(!$nutzung || $nutzung == "Transmitting station") continue;
|
|
|
|
echo "creating $strasse_name $hausnummer_name $ort_name\n";
|
|
|
|
$ort_search = [$ort_name];
|
|
|
|
if(strpos($ort_name, 'ß') !== false) $ort_search[] = str_replace('ß', 'ss', $ort_name);
|
|
if(strpos($ort_name, 'ä') !== false) $ort_search[] = str_replace('ä', 'ae', $ort_name);
|
|
if(strpos($ort_name, 'ö') !== false) $ort_search[] = str_replace('ö', 'oe', $ort_name);
|
|
if(strpos($ort_name, 'ü') !== false) $ort_search[] = str_replace('ü', 'ue', $ort_name);
|
|
|
|
if(strpos($ort_name, 'ss') !== false) $ort_search[] = str_replace('ss', 'ß', $ort_name);
|
|
if(strpos($ort_name, 'ae') !== false) $ort_search[] = str_replace('ae', 'ä', $ort_name);
|
|
if(strpos($ort_name, 'oe') !== false) $ort_search[] = str_replace('oe', 'ö', $ort_name);
|
|
if(strpos($ort_name, 'ue') !== false) $ort_search[] = str_replace('ue', 'ü', $ort_name);
|
|
|
|
$ortschaft = ADBOrtschaftModel::getFirst(['name' => $ort_search]);
|
|
if(!$ortschaft->id) die("Ortschaft nicht gefunden: $ort_name\n");
|
|
|
|
$strasse_id = false;
|
|
|
|
$strsql = "SELECT * FROM view_hausnummer WHERE gemeinde_id=1 AND ortschaft IN ('". implode("', '", $ort_search)."') AND strasse IN ('". implode("', '", $strasse_search)."')";
|
|
//echo "$strsql\n";exit;
|
|
$strres = $db->query($strsql);
|
|
if(!$db->num_rows($strres)) {
|
|
echo("Strasse in adressdb nicht gefunden, wird erstellt: $strsql\n");
|
|
|
|
// create strasse
|
|
|
|
$strasse_data = [
|
|
'ortschaft_id' => $ortschaft->id,
|
|
'gemeinde_id' => 1,
|
|
'name' => $strasse_name
|
|
];
|
|
$strasse = ADBStrasseModel::create($strasse_data);
|
|
//$strasse_id = $strasse->save();
|
|
//if(!$strasse_id) die("Cannot create strasse: $strasse_name\n");
|
|
} else {
|
|
$data = $db->fetch_object($strres);
|
|
$strasse_id = $data->strasse_id;
|
|
}
|
|
/*
|
|
if(!$strasse_id) {
|
|
die("keine strasse_id");
|
|
}*/
|
|
/*
|
|
$plz = ADBPlzModel::getFirst(['plz' => $plz_name]);
|
|
if(!$plz) die("PLZ nicht gefunden: $plz_name\n");
|
|
|
|
$hausnummer_data = [
|
|
'netzgebiet_id' => $netzgebiet->id,
|
|
'extref' => $hausnummer_extref,
|
|
'ortschaft_id' => $ortschaft->id,
|
|
'plz_id' => $plz->id,
|
|
'strasse_id' => $strasse_id,
|
|
'hausnummer' => $hausnummer_name,
|
|
'zusatz' => $addresszusatz,
|
|
'gps_lat' => $lat,
|
|
'gps_long' => $long,
|
|
'freigabe' => 'voll'
|
|
];
|
|
|
|
//var_dump($hausnummer_data);exit;
|
|
$hausnummer = ADBHausnummerModel::create($hausnummer_data);
|
|
$hausnummer_id = $hausnummer->save();
|
|
|
|
if(!$hausnummer_id) die("Error creating hausnummer");
|
|
*/
|
|
} else {
|
|
|
|
$data = $db->fetch_object($res);
|
|
$hausnummer = new ADBHausnummer($data->hausnummer_id);
|
|
|
|
if($hausnummer->netzgebiet_id != $netzgebiet->id) {
|
|
$hausnummer->netzgebiet_id = $netzgebiet->id;
|
|
$hausnummer->save();
|
|
}
|
|
if($hausnummer->extref != $hausnummer_extref) {
|
|
$hausnummer->extref = $hausnummer_extref;
|
|
$hausnummer->save();
|
|
}
|
|
$hausnummer->gps_lat = (float)$lat;
|
|
$hausnummer->gps_long = (float)$long;
|
|
$hausnummer->freigabe = "voll";
|
|
$hausnummer->save();
|
|
|
|
}
|
|
/*
|
|
if($hausnummer) {
|
|
// create missing wohneinheiten
|
|
$new_unit_count = 0;
|
|
$last_unit_tuer = 0;
|
|
|
|
$units = ADBWohneinheitModel::search(['hausnummer_id' => $hausnummer->id]);
|
|
if(count($units)) {
|
|
foreach($units as $unit) {
|
|
if($unit->tuer > $last_unit_tuer) {
|
|
$last_unit_tuer = $unit->tuer;
|
|
}
|
|
}
|
|
}
|
|
echo $hausnummer->strasse->name." ".$hausnummer->hausnummer."(".$hausnummer->id."): ".count($units)." vorhanden vs. csv ".$unit_count."\n";
|
|
if(count($units) < $unit_count) {
|
|
$new_unit_count = $unit_count - count($units);
|
|
}
|
|
|
|
if($new_unit_count && count($units) === 1) {
|
|
$unit = $units[0];
|
|
if(!$unit->zusatz && !$unit->tuer) {
|
|
$unit->tuer = 1;
|
|
if(!$unit->save()) {
|
|
die("Couldn't set tuer to 1 (wohneinheit ".$unit->id.")");
|
|
}
|
|
$last_unit_tuer = 1;
|
|
}
|
|
}
|
|
|
|
if($new_unit_count == 1 && $nutzung != "Transmitting station") {
|
|
// create single wohneinheit
|
|
echo "Creating single Wohneiheit (".$hausnummer->strasse->name." ".$hausnummer->hausnummer." - hausnummer_id: ".$hausnummer->id.")\n";
|
|
$unit_data = [
|
|
'hausnummer_id' => $hausnummer->id,
|
|
'num' => 1,
|
|
'nutzung' => "Wohnung"
|
|
];
|
|
if($last_unit_tuer) {
|
|
$unit_data['tuer'] = ++$last_unit_tuer;
|
|
}
|
|
$unit = ADBWohneinheitModel::create($unit_data);
|
|
//var_dump($unit);exit;
|
|
$unit->save();
|
|
}
|
|
|
|
if($new_unit_count > 1) {
|
|
echo "Creating $new_unit_count Wohneiheiten (".$hausnummer->strasse->name." ".$hausnummer->hausnummer." - hausnummer_id: ".$hausnummer->id.")\n";
|
|
for($i = 0; $i < $new_unit_count; $i++) {
|
|
if($nutzung == "Transmitting station") continue;
|
|
|
|
$unit_data = [
|
|
'hausnummer_id' => $hausnummer->id,
|
|
'num' => 1,
|
|
'tuer' => ++$last_unit_tuer,
|
|
'nutzung' => "Wohnung"
|
|
];
|
|
$unit = ADBWohneinheitModel::create($unit_data);
|
|
|
|
$unit->save();
|
|
}
|
|
//if($hausnummer->id != 1723 && $nutzung != "Transmitting station") { var_dump($unit);exit; }
|
|
}
|
|
}*/
|
|
|
|
/*
|
|
|
|
$data = $db->fetch_object($res);
|
|
$csv_ort = str_replace(['ß','ä','ö','ü'], ['ss','ae','oe','ue'], $ort_name);
|
|
$db_ort = str_replace(['ß','ä','ö','ü'], ['ss','ae','oe','ue'], $data->ortschaft);
|
|
*/
|
|
// update ortschaft if needed
|
|
/*
|
|
if($csv_ort != $db_ort && $nutzung != "Greenfield") {
|
|
|
|
if($strasse_name != "Hauptstrasse" && $hausnummer_name != "133") {
|
|
|
|
echo "Netzgebiet extref: \033[1m-------\033[0m | Hausnummer extref: \033[1m--------\033[0m | gem: \033[1m$data->gemeinde\033[0m | plz: \033[1m$data->plz\033[0m | ort: \033[1m$db_ort\033[0m | strasse: \033[1m$data->strasse\033[0m | hausnummer: \033[1m$data->hausnummer\033[0m\n";
|
|
// find ortschaft
|
|
|
|
$ortschaft = ADBOrtschaftModel::getFirst(['name' => $ort_name]);
|
|
if(!$ortschaft) {
|
|
$ort_data = [];
|
|
$ort_data['gemeinde_id'] = 1;
|
|
$ort_data['name'] = $ort_name;
|
|
$ort_data['plz'] = $plz_name;
|
|
$ortschaft = ADBOrtschaftModel::create($ort_data);
|
|
$ortschaft_id = $ortschaft->save();
|
|
if(!$ortschaft_id) {
|
|
die("Konnte Ortschaft ($ort_name) nicht erstellen.");
|
|
}
|
|
}
|
|
|
|
// update ortschaft in hausnummer
|
|
$hausnummer = new ADBHausnummer($data->hausnummer_id);
|
|
if(!$hausnummer->id) {
|
|
die("Hausnummer nicht gefunden für Ort update");
|
|
}
|
|
$hausnummer->ortschaft_id = $ortschaft->id;
|
|
$hausnummer->save();
|
|
}
|
|
}*/
|
|
|
|
//echo "$data->ortschaft, $data->plz, $data->strasse, $data->hausnummer\n";
|
|
|
|
|
|
} |