-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
231 lines (186 loc) · 6.51 KB
/
app.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import os
import time
import requests
from flask import Flask, abort, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
if app.debug:
url = "postgresql+psycopg2://postgres:password@db:5432/postgres"
time.sleep(10)
from pathlib import Path
api_key = Path("keys.txt").read_text()
else:
url = os.environ["DATABASE_URL"]
api_key = os.environ["HERE_KEY"]
app.config["SQLALCHEMY_DATABASE_URI"] = url
db = SQLAlchemy(app)
class Location(db.Model):
# TODO: drop?
__table_args__ = (
db.UniqueConstraint(
"city", "county", "state", "country", 'district', 'address', name="location_index"
),
)
id = db.Column(db.Integer, primary_key=True)
country = db.Column(db.Text(), nullable=False)
state = db.Column(db.Text(), nullable=False)
county = db.Column(db.Text())
city = db.Column(db.Text())
address = db.Column(db.Text())
district = db.Column(db.Text())
latitude = db.Column(db.Float(), nullable=False)
longitude = db.Column(db.Float(), nullable=False)
provider = db.Column(db.Text(), nullable=False)
result_house_number = db.Column(db.Text())
result_street = db.Column(db.Text())
result_postal_code = db.Column(db.Text())
result_district = db.Column(db.Text())
result_city = db.Column(db.Text())
result_county = db.Column(db.Text())
@app.cli.command()
def resetdb():
db.drop_all()
print("resetdb successful!")
db.create_all()
db.create_all()
def geocode_here(q):
qq = ";".join([f"{k}={v}" for k, v in q.items() if v != None and k != "city" and k != "address"])
q_city = q["city"]
if 'address' in q and q['address'] is not None and len(q['address']) > 0:
q_city = q['address'] + ', ' + q_city
url = f"https://geocode.search.hereapi.com/v1/geocode?q={q_city}&qq={qq}&apiKey={api_key}&lang=de-de&in=countryCode:DEU&limit=1"
print(url, flush=True)
r = requests.get(url, timeout=30)
r.raise_for_status()
items = r.json()["items"]
if len(items) == 0:
return None
item = items[0]
if (
item["resultType"] == "administrativeArea"
and not item["administrativeAreaType"] == "county"
):
return None
if not item["resultType"] in [
"administrativeArea",
"locality",
"street",
"place",
"houseNumber",
]:
return None
adr = item["address"]
county = adr["county"]
city = adr["city"] if "city" in adr else None
district = adr["district"] if "district" in adr else None
street = adr["street"] if "street" in adr else None
postal_code = adr["postalCode"] if "postalCode" in adr else None
house_number = adr["houseNumber"] if "houseNumber" in adr else None
return (
*item["position"].values(),
county,
city,
district,
street,
postal_code,
house_number,
)
def geocode(q, p):
if p == "here":
return geocode_here(q)
return None
def get_location(q, p):
location = (
Location.query.filter(Location.city == q["city"])
.filter(Location.county == q["county"])
.filter(Location.address == q["address"])
.filter(Location.district == q["district"])
.filter(Location.state == q["state"])
.filter(Location.country == q["country"])
.filter(Location.provider == p)
.first()
)
if location is None:
geocode_result = geocode(q, p)
if geocode_result is None:
return None
(
latitude,
longitude,
r_county,
r_city,
r_district,
r_street,
r_postal_code,
r_house_number,
) = geocode_result
location = Location(
**q,
latitude=latitude,
longitude=longitude,
result_county=r_county,
result_city=r_city,
result_district=r_district,
result_street=r_street,
result_postal_code=r_postal_code,
result_house_number=r_house_number,
provider=p,
)
db.session.add(location)
db.session.commit()
return location
@app.route("/", methods=["GET"])
def index_get():
city = request.args.get("city")
county = request.args.get("county")
state = request.args.get("state")
country = request.args.get("country")
provider = request.args.get("provider")
district = request.args.get("district")
address = request.args.get("address")
# city & county are optional
if None in [state, country, provider]:
abort(
400,
"please construct your requests as follows /?provider=here&city=Haldensleben&county=Börde&state=Sachsen-Anhalt&country=Deutschland",
)
query = {"state": state, "country": country, "city": city, "county": county, 'address': address, 'district': district}
provider = provider.lower()
location = get_location(query, provider)
if location is None:
abort(400, f"geolocation failed for `{query}` and `{provider}`")
return {
"latitude": location.latitude,
"longitude": location.longitude,
"county": location.result_county,
"city": location.result_city,
"district": location.result_district,
"street": location.result_street,
"house_number": location.result_house_number,
"postal_code": location.result_postal_code,
"state": location.state,
"country": location.country,
}
@app.route("/", methods=["POST"])
def index_post():
data = request.get_json()
if data is None or "locations" not in data or len(data["locations"]) == 0:
abort(
400,
'post data like this: `{"provider": "here", "locations": [{"query": {"city":"Haldensleben", "county": "Börde", "state": "Sachsen-Anhalt", "country": "Deutschland"}}]}`',
)
provider = data["provider"].lower()
for x in data["locations"]:
location = get_location(x["query"], provider)
if location is not None:
x["latitude"] = location.latitude
x["longitude"] = location.longitude
x["county"] = location.result_county
x["city"] = location.result_city
x["district"] = location.result_district
x["street"] = location.result_street
x["house_number"] = location.result_house_number
x["postal_code"] = location.result_postal_code
x["state"] = location.state
x["country"] = location.country
return data