-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_apis.py
322 lines (262 loc) · 11 KB
/
data_apis.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import copy
import json
import urllib.parse
from abc import ABC, abstractmethod
from qgis.core import (Qgis, QgsCoordinateReferenceSystem,
QgsCoordinateTransform, QgsNetworkAccessManager,
QgsPointXY, QgsProject, QgsSettings)
from qgis.PyQt.QtCore import QUrl
from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.utils import iface
from .utils.logger import Logger
Log = Logger()
class APIQueryStrategy(ABC):
source = None
@abstractmethod
def query(self, selected_tags, x_min, y_min, x_max, y_max):
pass
@abstractmethod
def extractElements(self, data):
pass
@abstractmethod
def getAttributeMappings(self):
pass
@abstractmethod
def extractLatLon(self, element):
pass
@abstractmethod
def getGeometryType(self, element):
pass
def transformTo4326(self, x, y):
if x is not None and y is not None:
project = QgsProject.instance()
project_crs = project.crs()
target_crs = QgsCoordinateReferenceSystem("EPSG:4326")
transform = QgsCoordinateTransform(project_crs, target_crs, project)
pt = QgsPointXY(x, y)
pt = transform.transform(pt)
return pt.x(), pt.y()
return None, None
def transformCoordinates(self, x, y):
if x is not None and y is not None:
project = QgsProject.instance()
api_crs = QgsCoordinateReferenceSystem("EPSG:4326")
target_crs = QgsProject.instance().crs()
transform = QgsCoordinateTransform(api_crs, target_crs, project)
pt = QgsPointXY(x, y)
pt = transform.transform(pt)
return pt.y(), pt.x()
return None, None
class OverpassAPIQueryStrategy(APIQueryStrategy):
source = "Open Street Map"
def restructure_data(self, new_data):
nodes = {
element["id"]: (element["lat"], element["lon"])
for element in new_data["elements"]
if element["type"] == "node"
}
ways = {
element["id"]: element
for element in new_data["elements"]
if element["type"] == "way"
}
# Build a set of all possible node IDs of all ways
possible_node_ids = set()
for way_id, node_ids in ways.items():
possible_node_ids.update(node_ids["nodes"])
# Filter out nodes where their ID is not in the list of possible node IDs
new_data["elements"] = [
element
for element in new_data["elements"]
if element["type"] != "node" or element["id"] not in possible_node_ids
]
# Move nodes under the corresponding way element
for way_id, node_ids in ways.items():
way_nodes = [
({"lon": nodes[node_id][1], "lat": nodes[node_id][0]})
for node_id in node_ids["nodes"]
if node_id in nodes
]
ways[way_id]["nodes"] = way_nodes
return new_data
def query(self, x_min, y_min, x_max, y_max):
x_min, y_min = self.transformTo4326(x_min, y_min)
x_max, y_max = self.transformTo4326(x_max, y_max)
selected_cultural_tags = QgsSettings().value("/KgrFinder/osm_tags", [])
custom_osm_tags = QgsSettings().value("/KgrFinder/custom_osm_tags", [])
selected_tags = selected_cultural_tags + custom_osm_tags
overpass_query = self.createOverpassQuery(
selected_tags, x_min, y_min, x_max, y_max
)
url = f"https://overpass-api.de/api/interpreter?data={overpass_query}"
Log.log_debug(f"called url {url}")
request = QNetworkRequest(QUrl(url))
reply = QgsNetworkAccessManager.instance().blockingGet(request)
if reply.error():
if reply.errorString():
Log.log_error(reply.errorString())
if reply.content():
data = str(reply.content(), "utf-8")
data = json.loads(data)
new_data = copy.deepcopy(data)
new_data = self.restructure_data(new_data)
return new_data
return None
def createOverpassQuery(self, tags, x_min, y_min, x_max, y_max):
overpass_query = "[out:json];("
for search_term in tags:
key, sep, value = search_term.partition("=")
key, value = [f"'{value}'" if value else "" for value in (key, value)]
query = f"node[{key}{sep}{value}]({y_min},{x_min},{y_max},{x_max});"
query += f"way[{key}{sep}{value}]({y_min},{x_min},{y_max},{x_max});>;"
# query += f'relation["{tag}"]({y_min},{x_min},{y_max},{x_max});'
overpass_query += query
overpass_query += ");out;"
return overpass_query
def getAttributeMappings(self):
return {
"name": "tags.name",
"description": "tags.description",
"type": "type",
"id": "id",
"tags": "tags",
"lat": "lat",
"lon": "lon"
}
def extractElements(self, data):
if not data:
return []
# Extract elements from the Overpass API response
return data.get("elements", [])
def extractLatLon(self, element):
lat = element.get("lat")
lon = element.get("lon")
if lon is not None and lat is not None:
lat, lon = self.transformCoordinates(lon, lat)
return lat, lon
else:
return None, None
def extractPolygonNodes(self, element):
nodes = element.get("nodes")
if (
nodes is not None and len(nodes) >= 2
): # Make sure there are at least 3 nodes to form a polygon
coordinates = [(node["lon"], node["lat"]) for node in nodes]
transformed_coordinates = [
self.transformCoordinates(lat, lon) for lat, lon in coordinates
] # Switched lat and lon
transformed_points = [
QgsPointXY(lon, lat) for lat, lon in transformed_coordinates
] # Switched lon and lat
return transformed_points
else:
return None
def getGeometryType(self, element):
if element["type"] == "node":
return "point"
elif element["type"] == "way":
return "polygon"
else:
return "unknown"
class iDAIGazetteerAPIQueryStrategy(APIQueryStrategy):
source = "iDAI.Gazetteer"
def query(self, x_min, y_min, x_max, y_max):
x_min, y_min = self.transformTo4326(x_min, y_min)
x_max, y_max = self.transformTo4326(x_max, y_max)
idai_gazetteer_filter = QgsSettings().value("/KgrFinder/idai_gazetteer_filter", "None")
custom_gazetteer_tags = QgsSettings().value("/KgrFinder/custom_gazetteer_tags", [])
BASE_URL = "https://gazetteer.dainst.org/search.json?q="
options = ""
# Build idai_gazetteer_filter_str
idai_gazetteer_filter_str = '{"match":{"types":"'+idai_gazetteer_filter+'"}}' if idai_gazetteer_filter != "None" else ""
# Build idai_gazetteer_custom_tags_str
idai_gazetteer_custom_tags_str = ', '.join(['{"match":{"tags":"'+tag+'"}}' for tag in custom_gazetteer_tags])
# Combine filter and custom tags if both are present
if idai_gazetteer_filter_str and idai_gazetteer_custom_tags_str:
idai_gazetteer_filter_str += ','
# Construct the options JSON string
options = '{"bool":{"must":['+idai_gazetteer_filter_str+idai_gazetteer_custom_tags_str+']}}'
options = urllib.parse.quote_plus(options)
url = BASE_URL + options
q_string = "&fq=_exists_:prefLocation.coordinates OR _exists_:prefLocation.shape"
q_string += "&polygonFilterCoordinates="
q_string += f'{x_min}&polygonFilterCoordinates={y_min}&polygonFilterCoordinates={x_max}+'
q_string += f'&polygonFilterCoordinates={y_min}&polygonFilterCoordinates={x_max}&polygonFilterCoordinates={y_max}'
q_string += f'&polygonFilterCoordinates={x_min}&polygonFilterCoordinates={y_max}'
q_string += "&limit=1000&type=extended&pretty=true"
url = url + q_string
request = QNetworkRequest(QUrl(url))
Log.log_debug(f"called url {url}")
reply = QgsNetworkAccessManager.instance().blockingGet(request)
if reply.error():
if reply.errorString():
Log.log_debug(reply.errorString())
iface.messageBar().pushMessage(
"KGR", reply.errorString(), level=Qgis.Critical, duration=3
)
if reply.content():
data = str(reply.content(), "utf-8")
data = json.loads(data)
new_data = copy.deepcopy(data)
return new_data
return None
def getAttributeMappings(self):
return {
'name': 'prefName.title',
# 'description': 'types',
'type': 'types',
"id": "@id",
# 'tags': 'tags',
"lat": "prefLocation.coordinates[1]",
"lon": "prefLocation.coordinates[0]",
}
def extractElements(self, data):
if not data:
return []
# Extract elements from the Overpass API response
return data.get("result", [])
def extractPolygonNodes(self, element):
def recursive_extract_coordinates(shape):
coordinates = []
if isinstance(shape, list):
if (
len(shape) == 2
and isinstance(shape[0], (int, float))
and isinstance(shape[1], (int, float))
):
return [(shape[0], shape[1])] # Assuming it's a coordinate pair
else:
for sub_shape in shape:
coordinates.extend(recursive_extract_coordinates(sub_shape))
return coordinates
shape = element.get("prefLocation", {}).get("shape", [])
if shape is not None:
coordinates = recursive_extract_coordinates(shape)
if coordinates:
transformed_coordinates = [
self.transformCoordinates(lon, lat) for lon, lat in coordinates
]
transformed_points = [
QgsPointXY(lon, lat) for lat, lon in transformed_coordinates
]
return transformed_points
return None
def extractLatLon(self, element):
pref_location = element.get("prefLocation", {})
coordinates = pref_location.get("coordinates", [])
if len(coordinates) == 2:
lat = coordinates[1]
lon = coordinates[0]
lat, lon = self.transformCoordinates(lon, lat)
return lat, lon
else:
return None, None
def getGeometryType(self, element):
coordinates = element.get("prefLocation", {}).get("coordinates", [])
shape = element.get("prefLocation", {}).get("shape", [])
if shape:
return "polygon"
elif len(coordinates) == 2:
return "point"
else:
return "unknown"