MaxGit32 commited on
Commit
ffce005
1 Parent(s): 1f72699

Create Location.py

Browse files
Files changed (1) hide show
  1. pages/Location.py +85 -0
pages/Location.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from functools import partial
2
+ from geopy.geocoders import Nominatim # Openstreatmaps
3
+ import requests
4
+ import json
5
+ import pandas as pd
6
+
7
+ class Location:
8
+ def __init__(self, location):
9
+ self.location = location
10
+ self.df = self.jsonToDf()
11
+
12
+ def jsonToDf(self):
13
+ plz = pd.read_json('plz.json')
14
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00f6','Ö')
15
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00fc','ü')
16
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00df','ß')
17
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00e4','ä')
18
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00c4','Ä')
19
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00d6','Ö')
20
+ plz["plz_name"] = plz['plz_name'].str.replace('\u00dc','Ü')
21
+ plz["plz_name"] = plz['plz_name'].str.replace('Halle/ Saale','Halle (Saale)')
22
+
23
+ mask = plz['plz_name'] == "Halle"
24
+ plz.loc[mask, 'plz_name'] = "Halle (Weserbergland)"
25
+
26
+ return plz
27
+
28
+ def getPostalJson(self):
29
+ # Postleitzahlen per API erzahlten
30
+ url = 'https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/georef-germany-postleitzahl/exports/json?select=plz_name%2C%20name&lang=de&timezone=UTC&use_labels=false&epsg=4326' # Ersetze dies durch die tatsächliche API-URL
31
+ response = requests.get(url)
32
+
33
+ # Überprüfen, ob die Anfrage erfolgreich war (Status-Code 200)
34
+ if response.status_code == 200:
35
+ # JSON-Antwort aus der API
36
+ json_data = response.json()
37
+
38
+ # Speichern der JSON-Antwort in einer Datei
39
+ with open('plz.json', 'w') as file:
40
+ json.dump(json_data, file)
41
+
42
+ print('Daten erfolgreich in "api_response.json" gespeichert.')
43
+ return json_data
44
+ else:
45
+ print('Fehler bei der API-Anfrage.')
46
+
47
+ ## return Postleitzahl
48
+ def getPostalCode(self):
49
+ try:
50
+ geolocator = Nominatim(user_agent="LocationApiPruefen")
51
+ geocode = partial(geolocator.geocode, language="de")
52
+ postleitzahl = geocode(self.location).raw.get("display_name")
53
+ x_split = postleitzahl.split(", ")
54
+ post_sub = x_split[4]
55
+ columns = ['plz_name', 'name']
56
+ df = pd.DataFrame(columns=columns)
57
+ df.loc[0] = [self.location, post_sub]
58
+
59
+ if post_sub.isdigit():
60
+ return df
61
+ else:
62
+ raise Exception
63
+ except:
64
+ # Dataframe PLZ durchsuchen
65
+ gesuchter_wert = self.location
66
+ ergebnisse = self.df[self.df['plz_name'] == gesuchter_wert]
67
+
68
+ if ergebnisse.empty:
69
+ gesuchter_wert_erw = gesuchter_wert+ " "
70
+ ergebnisse = self.df[self.df['plz_name'].str.contains(gesuchter_wert_erw)]
71
+
72
+ if ergebnisse.empty:
73
+ neue_zeile = pd.Series({'plz_name': "", 'name': ''})
74
+ #ergebnisse = ergebnisse.append(neue_zeile, ignore_index=True)
75
+ ergebnisse = pd.concat([neue_zeile, ergebnisse], ignore_index=True)
76
+ #ergebnisse = ergebnisse.drop(1)
77
+ #print(ergebnisse)
78
+ return ergebnisse
79
+ #print(ergebnisse)
80
+ return ergebnisse
81
+
82
+
83
+ ## Testing
84
+ #l1 = Location("Wartenberg").getPostalCode()
85
+ #print(type(l1))