diff --git a/scripts/addressdb/premstaetten/adb-to-object.php b/scripts/addressdb/premstaetten/adb-to-object.php new file mode 100644 index 000000000..1f701e69d --- /dev/null +++ b/scripts/addressdb/premstaetten/adb-to-object.php @@ -0,0 +1,26 @@ +#!/usr/bin/php + $netzgebiet_extref]); + if(!$netzgebiet) { + echo "netzgebiet not found\n"; + exit; + } + + if($execution_state == "Canceled") { + continue; + } + + if($btype == "Greenfield") { + continue; + } + + $adrcd = false; + if($adrcd_subcd) { + $adrparts = explode("-", $adrcd_subcd); + $adrcd = $adrparts[0]; + } + + $gem_name = trim($csv[59]); + $plz_string = trim($csv[88]); + $ort_name = trim($csv[31]); + $strasse_hausnummer = trim($csv[22]); + + $strasse_name = ""; + $hausnummer_name = ""; + + $m = []; + if(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]); + } + } + + + /* + $adrcd = trim($csv[0]); + $gem_kz = trim($csv[2]); + $gem_name = trim($csv[3]); + $ort_kz = trim($csv[5]); + $plz_string = trim($csv[4]); + $ort_name = trim($csv[6]); + $skz = trim($csv[7]); + $strasse_name = trim($csv[8]); + $hausnummer_string = trim($csv[9]); + $hausnummer_extref = trim($csv[0]); + if(in_array($hausnummer_extref, $h_extrefs)) { + die("Hausnummer extref doppelt! $hausnummer_extref\n"); + } + $h_extrefs[] = $hausnummer_extref; + */ + //$lat = str_replace(",",".",trim($csv[7])); + //$long = str_replace(",",".",trim($csv[8])); + + + if(!$adrcd) { + continue; + } + + if(!is_numeric($adrcd)) { + echo "Invalid adrcd ($adrcd)\n"; + continue; + } + + if(!$hausnummer_extref) { + die("!!! Keine Hausnummer Extref\n"); + } + + + if($hausnummer_extref == "25012262") { + $unit_count = 45; + } + if($hausnummer_extref == "25012676") { + continue; + } + + if($hausnummer_extref == "25012153" || $hausnummer_extref == "25012155" || $hausnummer_extref == "25012157") { + continue; + } + if($hausnummer_extref == "25012159") { + $unit_count = 61; + } + + if($hausnummer_extref == "25010649") { + continue; + } + if($hausnummer_extref == "25010723" || $hausnummer_extref == "25010816") { + continue; + } + + $existing_hausnummer = ADBHausnummerModel::getFirst(["extref" => $hausnummer_extref]); + if(!$existing_hausnummer) { + $existing_hausnummer = ADBHausnummerModel::getFirst(["adrcd" => $adrcd]); + } + if(!$existing_hausnummer) { + //echo "$hausnummer_extref not found $strasse_name $hausnummer_name\n"; + //continue; + + // find hausnummer + //echo "$strasse_name $hausnummer_name\n"; + + if($strasse_name == "Toepferring") $strasse_name = "Töpferring"; + if($strasse_name == "Foehrenweg") $strasse_name = "Föhrenweg"; + + $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'"; + $res = $db->query($sql); + + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $existing_hausnummer = new ADBHausnummer($data->hausnummer_id); + $existing_hausnummer->netzgebiet_id = $netzgebiet->id; + + if(!$existing_hausnummer->adrcd) { + $existing_hausnummer->adrcd = $adrcd; + } + + $existing_hausnummer->extref = $hausnummer_extref; + $existing_hausnummer->grund_nr = $grund_nr; + $existing_hausnummer->gdaeigenschaft = $gdaeigenschaft; + $existing_hausnummer->rimo_fcp_name = $fcp_name; + if(!$existing_hausnummer->oaid) { + $existing_hausnummer->oaid = $existing_hausnummer->getNewOAID(); + } + $existing_hausnummer->save(); + + } else { + echo "Address not found (straße: $strasse_name, hausnummer: $hausnummer_name, adrcd: $adrcd, extref: $hausnummer_extref, unit_count: $unit_count)\n"; + } + } else { + // found hausnummer + $existing_hausnummer->netzgebiet_id = $netzgebiet->id; + if(!$existing_hausnummer->adrcd) { + $existing_hausnummer->adrcd = $adrcd; + } + + $existing_hausnummer->extref = $hausnummer_extref; + $existing_hausnummer->grund_nr = $grund_nr; + $existing_hausnummer->gdaeigenschaft = $gdaeigenschaft; + $existing_hausnummer->rimo_fcp_name = $fcp_name; + if(!$existing_hausnummer->oaid) { + $existing_hausnummer->oaid = $existing_hausnummer->getNewOAID(); + } + $existing_hausnummer->save(); + } + continue; + + // add new units + $existing_units_count = ADBWohneinheitModel::count(['hausnummer_id' => $existing_hausnummer->id]); + if($existing_units_count != $unit_count) { + /*if(($existing_units_count - $unit_count) > 1) { + echo "========================================================================\n"; + echo "=============== [".$existing_hausnummer->id."] Need to delete ".($existing_units_count - $unit_count)." units\n"; + echo "========================================================================\n"; + }*/ + if($existing_units_count < $unit_count) { + echo "=============== Adding ".($unit_count - $existing_units_count)." units\n"; + + $new_units_count = $unit_count - $existing_units_count; + $last_unit_num = 0; + + + foreach(ADBWohneinheitModel::search(['hausnummer_id' => $existing_hausnummer->id]) as $tmp_unit) { + if($tmp_unit->num > $last_unit_num) { + $last_unit_num = $tmp_unit->num; + } + } + + // create wohneinheiten + for($i = 1; $i <= $new_units_count; $i++) { + $num = $last_unit_num + $i; + //echo "$existing_units_count create wohneinheit $num\n"; + $unit_data = [ + 'hausnummer_id' => $existing_hausnummer->id, + 'num' => $num, + ]; + $wohneinheit = ADBWohneinheitModel::create($unit_data); + $wohneinheit_id = $wohneinheit->save(); + if(!$wohneinheit_id) { + die("Cannot save Wohneinheit\n"); + } + $wohneinheit->oaid = $wohneinheit->getNewOAID(); + if(!$wohneinheit->oaid) { + die("Error generating OAID for wohneinheit ".$wohneinheit->id); + } + //var_dump($wohneinheit); + $wohneinheit->save(); + $w++; + } + + } + } + + //echo "$hausnummer_extref: $adrcd, $netzgebiet_extref, $grund_nr, $unit_count, $gdaeigenschaft\n"; + +} \ No newline at end of file