-
Notifications
You must be signed in to change notification settings - Fork 0
/
placekeyAlgorithm.py
580 lines (552 loc) · 21.7 KB
/
placekeyAlgorithm.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
# -*- coding: utf-8 -*-
"""
/***************************************************************************
placekey
A QGIS plugin
Processing plugin for placekey
-------------------
begin : 2020-11-12
copyright : (C) 2020 by Riccardo Klinger
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
import traceback
import math
import time
from PyQt5.QtCore import (QCoreApplication, QUrl, QVariant)
from qgis.core import (Qgis,
QgsCoordinateTransform,
QgsFeature,
QgsFeatureSink,
QgsFields,
QgsField,
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFeatureSink,
QgsProject,
QgsMapLayer,
QgsProcessingException,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterBoolean,
QgsProcessingParameterMapLayer,
QgsProcessingParameterField,
QgsCoordinateReferenceSystem,
QgsMessageLog,
QgsWkbTypes,
QgsSettings)
from qgis.utils import iface
import requests
import json
class placekeyAlgorithm(QgsProcessingAlgorithm):
def __init__(self):
super().__init__()
INPUT = 'INPUT'
OUTPUT = 'OUTPUT'
CityField = 'City Field'
Mode = 'Mode'
LocationField = 'Location Name Field'
AddressField = 'Address Field'
RegionField = 'Region Name Field'
PostalField = 'Postal Code Field'
CountryField = 'ISO Country Code Field'
copyAttributes = 'Copy Attributes'
DropGeo = 'Use Attributes only'
def createInstance(self):
return type(self)()
def group(self):
return "Add placekey"
def groupId(self):
return "addplacekey"
class addPlacekey(placekeyAlgorithm):
"""This is an example algorithm that takes a vector layer and
creates a new one just with just those features of the input
layer that are selected.
It is meant to be used as an example of how to create your own
algorithms and explain methods and variables used to do it. An
algorithm like this will be available in all elements, and there
is not need for additional work.
All Processing algorithms should extend the GeoAlgorithm class.
"""
def tr(self, string):
"""
Returns a translatable string with the self.tr() function.
"""
return QCoreApplication.translate('Processing', string)
def name(self):
"""This is the provired full name.
"""
return 'addPlacekey'
def displayName(self):
"""This is the provired full name.
"""
return 'Add placekey'
def shortHelpString(self):
"""
Returns a localised short helper string for the algorithm. This string
should provide a basic description about what the algorithm does and
the parameters and outputs associated with it..
"""
return self.tr(
"""This algorithm adds a placekey to your table. The placekey is
defined either by a set of:
- (lat, lon) taken from the geometry
- (street_address, city, region, country) or
- (street_address, region, postal_code, country)
and a placename if needed.
<a href="https://docs.placekey.io/">Placekey API Documentation</a>
______________
Parameters:
- Location Name Field: the name of the place / "Twin Peaks Petroleum"
- City Name Field: The city where the place is located / "San Francisco"
- Address Field: The street address of the place / "1543 Mission Street"
- Region Name Field: The second-level administrative region below nation for the place. In the US, this is the state / "California" or "CA"
- Postal Code: The postal code for the place / "94105"
- Country Code: The ISO 2-letter Country Code for the place. Defaults to 'US' if none given.
- Copy all Attributes: If checked we will copy all attributes from the input layer to the output. If false, we will only copy the feature id.
- Use Attributes Only: If checked we will not use lat/lon as input atributes which then will
The output layer will be stored in EPSG 4326.
Make sure to have your placekey API key added to the options using 'Manage placekey API keys"
""")
def initAlgorithm(self, config=None):
"""Here we define the inputs and output of the algorithm, along
with some other properties.
"""
self.addParameter(
QgsProcessingParameterMapLayer(
self.INPUT,
self.tr('Input table'),
#[QgsProcessing.TypeFile,
# QgsProcessing.TypeVector,
# QgsProcessing.TypeVectorPoint,
# QgsProcessing.TypeVectorAnyGeometry,
# QgsProcessing.TypeMapLayer]
)
)
self.addParameter(
QgsProcessingParameterField(
self.LocationField,
self.tr('Location Name Field'),
parentLayerParameterName=self.INPUT,
defaultValue="",
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.CityField,
self.tr('City Name Field'),
parentLayerParameterName=self.INPUT,
defaultValue="",
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.AddressField,
self.tr('Address Field'),
parentLayerParameterName=self.INPUT,
defaultValue="",
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.RegionField,
self.tr('Region Name Field'),
parentLayerParameterName=self.INPUT,
defaultValue="",
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.PostalField,
self.tr('Postal Code Field'),
parentLayerParameterName=self.INPUT,
defaultValue="",
optional=True
)
)
self.addParameter(
QgsProcessingParameterField(
self.CountryField,
self.tr('ISO Country Code Field'),
parentLayerParameterName=self.INPUT,
defaultValue="",
optional=True
)
)
self.addParameter(
QgsProcessingParameterBoolean(
self.copyAttributes,
self.tr('Copy all Attributes')
)
)
self.addParameter(
QgsProcessingParameterBoolean(
self.DropGeo,
self.tr('Use Attributes Only')
)
)
self.addParameter(
QgsProcessingParameterFeatureSink(
self.OUTPUT,
self.tr('Layer with Placekeys')
)
)
def loadCredFunctionAlg(self):
creds = {}
try:
s = QgsSettings()
creds["key"] = s.value("placekey/api_key", None)
except BaseException:
print("no API key found!")
return creds
def valueCheck(self, string):
if string == "nan" or string == "NULL" or string == "0":
return ""
else:
return string
def addPayloadItem(self, parameters, source, context, feature, feedback):
"""getting field names"""
locationName = self.parameterAsString(
parameters,
self.LocationField,
context
)
cityName = self.parameterAsString(
parameters,
self.CityField,
context
)
addressName = self.parameterAsString(
parameters,
self.AddressField,
context
)
regionName = self.parameterAsString(
parameters,
self.RegionField,
context
)
zipCode = self.parameterAsString(
parameters,
self.PostalField,
context
)
country = self.parameterAsString(
parameters,
self.CountryField,
context
)
geometry = self.parameterAsString(
parameters,
self.DropGeo,
context
)
item = {
"query_id": str(feature.id()),
}
if locationName != "":
item["location_name"] = self.valueCheck(
str(feature[locationName]))
if addressName != "":
item["street_address"] = self.valueCheck(str(feature[addressName]))
if cityName != "":
item["city"] = self.valueCheck(str(feature[cityName]))
if zipCode != "":
item["postal_code"] = self.valueCheck(str(feature[zipCode]))
if regionName != "":
item["region"] = self.valueCheck(str(feature[regionName]))
if country != "":
item["iso_country_code"] = self.valueCheck(str(feature[country]))
if country == "":
item["iso_country_code"] = "US"
if geometry == "false":
featGeometry = feature.geometry().centroid()
sourceCrs = source.sourceCrs()
if source.sourceCrs != QgsCoordinateReferenceSystem(4326):
destCrs = QgsCoordinateReferenceSystem(4326)
tr = QgsCoordinateTransform(sourceCrs, destCrs, QgsProject.instance())
featGeometry.transform(tr)
if math.isnan(featGeometry.asPoint().y()) is False:
item["latitude"] = featGeometry.asPoint().y()
item["longitude"] = featGeometry.asPoint().x()
else:
print("strange geometry found at feature with id " + str(feature.id()))
return item
def getKeys(self, payload, result, key, feedback):
url = "https://api.placekey.io/v1/placekeys"
headers = {
'apikey': key,
'Content-Type': 'application/json',
'user-agent': 'placekey-qgis/1.0 batchMode'
}
response = requests.request(
"POST",
url,
headers=headers,
data=json.dumps(payload)
)
if response.status_code == 401:
feedback.pushInfo(
"""check your API key. Seems like you're unauthorized!"""
)
raise QgsProcessingException(
"invalid API key")
if response.status_code == 429:
for retry in range(1, 11):
feedback.pushInfo('waiting 10s, rate limit exceeded')
feedback.pushInfo("trying again" + str(retry) + "/10 ")
time.sleep(10)
response = requests.request(
"POST",
url,
headers=headers,
data=json.dumps(payload)
)
if response.status_code == 200:
break
if retry == 10:
raise QgsProcessingException(
"tried 10 times, cancelling processing"
)
if response.status_code == 200:
if "error" in response.json():
for entry in payload["queries"]:
result.append(json.loads(
"""{"query_id": """ +
str(entry["query_id"]) +
""", "placekey": "Invalid address"}"""))
else:
for item in response.json():
if "placekey" in item:
result.append(item)
if "error" in item:
result.append(json.loads(
"""{"query_id": """ +
str(item["query_id"]) +
""", "placekey": "Invalid address"}"""))
if response.status_code == 400:
try:
if response.json()["error"][0:2] == "All":
for entry in payload["queries"]:
result.append(json.loads(
"""{"query_id": """ +
str(entry["query_id"]) +
""", "placekey": "Invalid address"}"""))
except BaseException:
raise QgsProcessingException(
"""No proper request sent. Details in the python log,
make sure to have a proper set off attributes.
If you're using a point layer, make sure to have
valid geometries for all features! Response was: """ +
response.text)
return result
def inputCheck(self, source, parameters, context, feedback):
locationName = self.parameterAsString(
parameters,
self.LocationField,
context
)
cityName = self.parameterAsString(
parameters,
self.CityField,
context
)
addressName = self.parameterAsString(
parameters,
self.AddressField,
context
)
regionName = self.parameterAsString(
parameters,
self.RegionField,
context
)
zipCode = self.parameterAsString(
parameters,
self.PostalField,
context
)
country = self.parameterAsString(
parameters,
self.CountryField,
context
)
geometry = self.parameterAsString(
parameters,
self.DropGeo,
context
)
# no attributes given:
if source.wkbType() in [0, 3, 4, 100]:
feedback.pushInfo(
"using a layer with no geometry. only attributes are used!"
)
if (locationName == "" and
cityName == "" and
addressName == "" and
regionName == "" and
zipCode == "" and
country == "" and geometry == "true"):
raise QgsProcessingException(
"""You disabled geometry. Please provide additional
information like city name, zipcode and address /
housenumber to conszruct the WHERE part."""
)
if cityName == "" and zipCode == "" and geometry == "true":
raise QgsProcessingException(
"please provide either city name or postal code"
)
def processAlgorithm(self, parameters, context, feedback):
"""checking for key"""
key = self.loadCredFunctionAlg()["key"]
if key == "" or key is None:
feedback.reportError("no API key found!", True)
raise QgsProcessingException(
"no API key found! ")
"""getting the input table"""
source = self.parameterAsSource(
parameters,
self.INPUT,
context
)
if source.wkbType() in [1004, 2004, 3004]:
raise QgsProcessingException(
"MultiPoint layer are not supported!")
if source is None:
raise QgsProcessingException(
self.invalidSourceError(
parameters, self.INPUT
)
)
self.inputCheck(source, parameters, context, feedback)
"""predefining the output layer"""
copy = self.parameterAsString(
parameters,
self.copyAttributes,
context
)
geometry = self.parameterAsString(
parameters,
self.DropGeo,
context
)
fields = source.fields()
if copy == "false":
fields.clear()
fields.append(QgsField("origin_id", QVariant.LongLong))
fields.append(QgsField("placekey", QVariant.String))
(sink, dest_id) = self.parameterAsSink(
parameters,
self.OUTPUT,
context,
fields,
QgsWkbTypes.Point,
source.sourceCrs()
)
if sink is None:
raise QgsProcessingException(
self.invalidSinkError(
parameters, self.OUTPUT))
feedback.pushInfo(
'{} points for placekey finding'.format(
source.featureCount()))
features = source.getFeatures()
"""lining up the entries"""
payload = {"queries": []}
batches = []
result = []
if source.featureCount() < 100:
"""small mode"""
for current, feature in enumerate(features):
if feedback.isCanceled():
break
payload["queries"].append(self.addPayloadItem(parameters,
source,
context,
feature,
feedback))
batches.append(payload)
result = self.getKeys(batches[0], result, key, feedback)
else:
"""batch mode"""
index = 0
for current, feature in enumerate(features):
if feedback.isCanceled():
break
index += 1
if index % 100 != 0 and index != source.featureCount():
payloadItem = self.addPayloadItem(parameters,
source,
context,
feature,
feedback)
payload["queries"].append(payloadItem)
if index % 100 == 0:
payloadItem = self.addPayloadItem(parameters,
source,
context,
feature,
feedback)
payload["queries"].append(payloadItem)
batches.append(payload)
payload = {"queries": []}
if index == source.featureCount():
payloadItem = self.addPayloadItem(parameters,
source,
context,
feature,
feedback)
payload["queries"].append(payloadItem)
batches.append(payload)
payload = {"queries": []}
currentBatch = 0
for batch in batches:
if feedback.isCanceled():
break
currentBatch += 1
result = self.getKeys(batch, result, key, feedback)
feedback.setProgress(int(currentBatch / len(batches) * 100))
feedback.pushInfo('gathering placekeys finished...')
'''processing result'''
features = source.getFeatures()
feedback.pushInfo('merging source with placekeys...')
if geometry == "true":
feedback.pushInfo('attributes used for inputs, resulting geometry is the centroid of input geometries')
if geometry == "false":
feedback.pushInfo('centroids of geometries used for inputs and resulting geomtry of placekey layer')
for current, feature in enumerate(features):
if feedback.isCanceled():
break
#fet = QgsFeature()
fet = QgsFeature(fields)
if copy == "true":
attributes = feature.attributes()
try:
fet.setGeometry(feature.geometry().centroid())
except BaseException:
feedback.pushInfo('no geometry available')
for index in range(0, len(result)):
if str(result[index]["query_id"]) == str(feature.id()):
placekey = result[index]["placekey"]
if copy == "true":
attributes.append(placekey)
fet.setAttributes(attributes)
else:
fet.setAttribute(0, feature.id())
fet.setAttribute(1, placekey)
result.pop(index)
break
sink.addFeature(fet, QgsFeatureSink.FastInsert)
feedback.setProgress(int(current / source.featureCount()) * 100)
return {self.OUTPUT: dest_id}