Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# script zum verlinken der schulen und zuweisen der URI aus dem AREG
from script_helper import DBTools, APITools
from configparser import ConfigParser
if __name__ == '__main__':
print("geocoding dataset now")
# datenbank mit den schuldaten
conn_config = ConfigParser()
conn_config.read("db_conn.cfg")
conn_str = f"""
host={conn_config['connection']['host']}
port={conn_config['connection']['port']}
dbname={conn_config['connection']['dbname']}
user={conn_config['connection']['user']}
password={conn_config['connection']['pwd']}"""
table = conn_config['connection']['table']
url = conn_config['connection']['url']
# logfile = r"geocoder.log"
my_db = DBTools(conn_str)
my_api = APITools(url)
schulen = my_db.select_adress(table)
adresse = {}
maxfeatures = len(schulen)
numfeatures = 0
errorfeatures = 0
missinfeatures = 0
log = []
for item in schulen:
adresse["hnr"] = item[1]
adresse["stn"] = item[2]
adresse["plz"] = item[3]
adresse["ort"] = item[4]
areg_response = my_api.geocode(adresse)
if areg_response.get("uri") is not None:
areg_uri = areg_response["uri"]
my_db.update_uri(str(item[0]), areg_uri, table)
numfeatures += 1
else:
areg_response_key = list(areg_response.keys())
if areg_response_key[0] == "MISSING":
missinfeatures += 1
if areg_response_key[0] == "ERROR":
errorfeatures += 1
progress_msg = (f"{areg_response_key[0]} : " +
f"{areg_response[areg_response_key[0]]} | " +
f"betroffene schule: [{item[0]}]\t{adresse['hnr']};" + # noqa
f" {adresse['stn']}; {adresse['plz']}; " +
f"{adresse['ort']}")
print(progress_msg)
my_db.closecursor()
# sorgt auf dem cloudserver fuer fehler wegen permission
# with open(logfile, "w", encoding='utf-8') as out:
# for item in log:
# out.write(item)
percentage = round((numfeatures * 100) / maxfeatures, 2)
statistics = (f"statistics:\n {numfeatures} from {maxfeatures} linked" +
f" >> {percentage}%\n {missinfeatures} features with " +
f"missing attributes\n {errorfeatures} features with " +
f"errors\n")
print(statistics)
print("linking dataset finished, closing programm")