-
Notifications
You must be signed in to change notification settings - Fork 0
/
algomaps_qgis.py
1146 lines (937 loc) · 51.2 KB
/
algomaps_qgis.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
/***************************************************************************
AlgoMapsPlugin
A QGIS plugin
Plugin Algolytics do standaryzacji danych adresowych i geokodowania
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2024-09-05
git sha : $Format:%H$
copyright : (C) 2024 by Algolytics Technologies
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. *
* *
***************************************************************************/
"""
from qgis.PyQt.QtCore import QSettings, QTranslator, QCoreApplication, Qt
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QTableWidgetItem, QComboBox, QMessageBox
from qgis.core import (
QgsApplication,
Qgis,
QgsMessageLog,
QgsProject,
QgsRectangle, QgsPointXY, QgsCoordinateReferenceSystem, QgsCoordinateTransform,
QgsField, QgsFeature, QgsVectorLayer,
QgsGeometry
)
# Initialize Qt resources from file resources.py
from .resources import *
# Import the code for the DockWidget
from .algomaps_qgis_dockwidget import AlgoMapsPluginDockWidget
import os
import sys
import json
CONFIG_PATH = 'dq_config.json'
DEBUG_MODE = False # Verbose messages
def find_python():
def _log_python_path(ppath, tag='-'):
if DEBUG_MODE:
QgsMessageLog().logMessage(message=f'[{tag}] Found python executable: {ppath}',
tag='AlgoMaps',
level=Qgis.MessageLevel.Info)
pass
import platform
if platform.system() != "Windows": # -> 'Linux' or 'Darwin' (MacOS)
if 'python' in sys.executable:
_log_python_path(sys.executable, tag='UNIX')
return sys.executable
if platform.system() == "Darwin":
if DEBUG_MODE:
py_exe = os.path.join(os.path.dirname(sys.executable), 'bin', 'python3')
_log_python_path(py_exe, tag='MacOS')
return py_exe
if platform.system() == 'Linux':
...
if platform.system() == "Windows":
for path in sys.path: # TODO: check correctness
assumed_path = os.path.join(path, "python.exe")
if os.path.isfile(assumed_path):
_log_python_path(assumed_path, tag='Windows')
return assumed_path
return None
def _get_package_manager_command():
import platform
if platform.system() == 'Linux':
os_info = platform.freedesktop_os_release().get('ID', 'linux')
if os_info in ('ubuntu', 'debian', 'linuxmint', 'kubuntu', 'xubuntu', 'lubuntu', 'pop', 'peppermint', 'mx'):
return ['apt', 'install']
if os_info in ('fedora', 'centos', 'rhel', 'fedoraremixforwsl'):
return ['yum', 'install']
if os_info in ('arch', 'manjaro'):
return ['pacman', '-S']
if os_info in ('opensuse', 'suse'):
return ['zypper', 'install']
if os_info in ('gentoo', 'funtoo', 'chromeos'):
return ['emerge']
if platform.system() == 'Darwin':
return ['brew', 'install']
def version_is_lower(version: str, to_compare: tuple):
"""
A simple function to compare module versions
works fine with simple examples like 1.3.10 or 2.0.3a but may produce false results with versions lik
0.1rc1 (-> 0.11) or 0.0.3beta2 (-> 0.0.32)
"""
import re
ver_str = re.sub(r'[^0-9.]', '', version).split(".") # Good enough for us
ver_tuple = tuple(map(int, ver_str))
return ver_tuple < to_compare
class AlgoMapsPlugin:
"""QGIS Plugin Implementation."""
def __init__(self, iface):
"""Constructor.
:param iface: An interface instance that will be passed to this class
which provides the hook by which you can manipulate the QGIS
application at run time.
:type iface: QgsInterface
"""
# Save reference to the QGIS interface
self.batch_header_type = None
self.batch_quote = None
self.batch_sep = None
self.include_gus = None
self.include_teryt = None
self.include_buildinfo = None
self.include_financial = None
self.flags = {}
self.iface = iface
self.canvas = self.iface.mapCanvas()
self.batch_combo_widgets = [] # List of column-role comboBoxes for Batch processing
self.csv_path = None
self.csv_path_output = None
self.taskManager = QgsApplication.taskManager()
# Initialize plugin directory
self.plugin_dir = os.path.dirname(__file__)
self.config_path = os.path.join(self.plugin_dir, CONFIG_PATH)
# Initialize locale
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'AlgoMapsPlugin_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&AlgoMaps')
# TODO: We are going to let the user set this up in a future iteration
self.toolbar = self.iface.addToolBar(u'AlgoMapsPlugin')
self.toolbar.setObjectName(u'AlgoMapsPlugin')
self.pluginIsActive = False
self.dockwidget = None
self.qproj = None
if Qgis.versionInt() > 33800:
from qgis.PyQt.QtCore import QMetaType
self._field_string_type = QMetaType.QString
self._field_int_type = QMetaType.Int
self._field_double_type = QMetaType.Double
else:
from qgis.PyQt.QtCore import QVariant
self._field_string_type = QVariant.String
self._field_int_type = QVariant.Int
self._field_double_type = QVariant.Double
# Read config file
try:
with open(self.config_path) as f:
conf = json.load(f)
self.dq_user = conf.get("dq_user")
self.dq_token = conf.get("dq_token")
self.api_key = conf.get("api_key")
self.default_chk_teryt = conf.get("default_chk_teryt")
self.default_chk_gus = conf.get("default_chk_gus")
self.default_chk_buildinfo = conf.get("default_chk_buildinfo")
self.default_chk_financial = conf.get("default_chk_financial")
except Exception as e:
iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(u'Cannot read config file, check details in "Log Messages" tab.'),
level=Qgis.MessageLevel.Critical)
QgsMessageLog().logMessage(repr(e), tag='AlgoMaps', level=Qgis.MessageLevel.Critical)
# noinspection PyMethodMayBeStatic
def tr(self, message):
"""Get the translation for a string using Qt translation API.
We implement this ourselves since we do not inherit QObject.
:param message: String for translation.
:type message: str, QString
:returns: Translated version of message.
:rtype: QString
"""
# noinspection PyTypeChecker,PyArgumentList,PyCallByClass
return QCoreApplication.translate('AlgoMapsPlugin', message)
def install_module(self, module_name, upgrade=False, force_external=False, curr_depth=0):
if curr_depth > 3: # Recursion failsafe
return
import subprocess
if DEBUG_MODE:
QgsMessageLog().logMessage(message=f'Install {module_name}',
tag='AlgoMaps',
level=Qgis.MessageLevel.Info)
arg_list = [find_python(), '-m', 'pip', 'install', module_name]
if upgrade:
arg_list.insert(4, '--upgrade')
if force_external:
arg_list.insert(4, '--break-system-packages')
if DEBUG_MODE:
QgsMessageLog().logMessage(message=' '.join(arg_list),
tag='AlgoMaps',
level=Qgis.MessageLevel.Info)
result = subprocess.run(arg_list,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return_code = result.returncode
if DEBUG_MODE:
QgsMessageLog().logMessage(message=f'Return code: {return_code}',
tag='AlgoMaps',
level=Qgis.MessageLevel.Info)
if return_code == 1:
pip_message = result.stdout.decode(encoding="utf-8").lower()
if 'no module named pip' in pip_message:
if DEBUG_MODE:
QgsMessageLog().logMessage(message=f'Trying to install pip...',
tag='AlgoMaps',
level=Qgis.MessageLevel.Info)
# Install pip via official get-pip.py script
# print('Try to install pip via official get-pip.py script')
d_script = "import urllib.request; import os; urllib.request.urlretrieve('https://bootstrap.pypa.io/get-pip.py', 'algomaps_get-pip.py')"
try:
download_pip_python = subprocess.run(
[sys.executable,
'-c',
d_script
]
)
install_pip_res = subprocess.run([find_python(), 'algomaps_get-pip.py'])
except:
QgsMessageLog().logMessage(message=f'Could not install pip to install modules. Try to execute '
f'the troubleshooting steps (README.md) or contact the '
f'developer team (e.g. post an issue on GitHub). You can use'
f' the plugin but Batch functionality will not work.',
tag='AlgoMaps',
level=Qgis.MessageLevel.Critical)
# print('ERRORS! download pip and install get-pip.py')
return
# Check if pip has been installed
if download_pip_python.returncode == 0 and install_pip_res.returncode == 0:
subprocess.run(['rm', '~/algomaps_get-pip.py']) # cleanup
# print('OK, installed pip')
else:
# Try to install pip with apt/other package manager
# print('Try to install pip with apt/other package manager')
package_manager_cmd = _get_package_manager_command()
args_list = [*package_manager_cmd,
'python3-pip',
'python3-setuptools', # for zypper
'python3-wheel'] # for yum and zypper
if package_manager_cmd[0] == 'pacman':
args_list = [*package_manager_cmd, 'python-pip']
apt_res = subprocess.run(args_list)
# print(args_list)
if apt_res.returncode != 0:
QgsMessageLog().logMessage(message=f'Could not install pip to install modules. Try to execute '
f'the troubleshooting steps (README.md) or contact the '
f'developer team (e.g. post an issue on GitHub). You can use'
f' the plugin but Batch functionality will not work.',
tag='AlgoMaps',
level=Qgis.MessageLevel.Critical)
return
# Try reinstalling the Python module
self.install_module(module_name, upgrade=upgrade, curr_depth=curr_depth + 1)
if 'externally managed environment' in pip_message or 'externally-managed-environment' in pip_message:
if force_external: # We tried to force install before and it didn't work
QgsMessageLog().logMessage(message=f'Could not use pip to install modules. Batch will not work!',
tag='AlgoMaps',
level=Qgis.MessageLevel.Critical)
return
# Try force install module to system-wise Python
msg_box = QMessageBox()
msg_box.setIcon(QMessageBox.Question)
msg_box.setWindowTitle(f"Instalacja biblioteki {module_name.split('>')[0]}") # nazwa bez nr wersji
msg_box.setText("Twoja instalacja Python jest zarządzana zewnętrznie (externally-managed). Instalacja "
"dodatkowych bibliotek może w rzadkich przypadkach spowodować niekompatybilność innch "
f"modułów. Czy na pewno chcesz zainstalować bibliotekę {module_name.split('>')[0]}?")
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
ret_value = msg_box.exec()
if ret_value == QMessageBox.Ok:
self.install_module(module_name, upgrade=upgrade, force_external=True, curr_depth=curr_depth + 1)
return
if return_code != 0:
QgsMessageLog().logMessage(message=f'Unknown error code. Stdout: {result.stdout.decode(encoding="utf-8")}',
tag='AlgoMaps',
level=Qgis.MessageLevel.Warning)
return return_code
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
"""Add a toolbar icon to the toolbar.
:param icon_path: Path to the icon for this action. Can be a resource
path (e.g. ':/plugins/foo/bar.png') or a normal file system path.
:type icon_path: str
:param text: Text that should be shown in menu items for this action.
:type text: str
:param callback: Function to be called when the action is triggered.
:type callback: function
:param enabled_flag: A flag indicating if the action should be enabled
by default. Defaults to True.
:type enabled_flag: bool
:param add_to_menu: Flag indicating whether the action should also
be added to the menu. Defaults to True.
:type add_to_menu: bool
:param add_to_toolbar: Flag indicating whether the action should also
be added to the toolbar. Defaults to True.
:type add_to_toolbar: bool
:param status_tip: Optional text to show in a popup when mouse pointer
hovers over the action.
:type status_tip: str
:param parent: Parent widget for the new action. Defaults None.
:type parent: QWidget
:param whats_this: Optional text to show in the status bar when the
mouse pointer hovers over the action.
:returns: The action that was created. Note that the action is also
added to self.actions list.
:rtype: QAction
"""
icon = QIcon(icon_path)
action = QAction(icon, text, parent)
action.triggered.connect(callback)
action.setEnabled(enabled_flag)
if status_tip is not None:
action.setStatusTip(status_tip)
if whats_this is not None:
action.setWhatsThis(whats_this)
if add_to_toolbar:
self.toolbar.addAction(action)
if add_to_menu:
self.iface.addPluginToMenu(
self.menu,
action)
self.actions.append(action)
return action
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
icon_path = ':/plugins/algomaps_qgis/icon.png'
self.add_action(
icon_path,
text=self.tr(u'AlgoMaps - standaryzacja i geokodowanie adresów'),
callback=self.run,
parent=self.iface.mainWindow())
self.qproj = QgsProject.instance()
def onClosePlugin(self):
"""Cleanup necessary items here when plugin dockwidget is closed"""
# disconnects
self.dockwidget.closingPlugin.disconnect(self.onClosePlugin)
# remove this statement if dockwidget is to remain
# for reuse if plugin is reopened
# Commented next statement since it causes QGIS crashe
# when closing the docked window:
# self.dockwidget = None
self.pluginIsActive = False
def unload(self):
"""Removes the plugin menu item and icon from QGIS GUI."""
for action in self.actions:
self.iface.removePluginMenu(
self.tr(u'&AlgoMaps'),
action)
self.iface.removeToolBarIcon(action)
# Remove the toolbar
del self.toolbar
def run(self):
"""Run method that loads and starts the plugin"""
if not self.pluginIsActive:
self.pluginIsActive = True
# dockwidget may not exist if:
# first run of plugin
# removed on close (see self.onClosePlugin method)
if self.dockwidget is None:
# Check imports
try:
import pandas as pd
import dq
# Check Pandas version
version = pd.__version__
if version_is_lower(version, (1, 3, 0)):
raise ModuleNotFoundError
except ModuleNotFoundError as e:
msg_box = QMessageBox()
msg_box.setIcon(QMessageBox.Question)
msg_box.setWindowTitle("Instalacja bibliotek Python")
msg_box.setText("Czy chcesz zainstalować dodatkowe bilioteki Python? \n - pandas \n - dq-client\n\n"
"Są one potrzebne do poprawnego działania funkcji Batch (przetwarzanie wsadowe).\n"
"Bez ich instalacji nie będzie można przetwarzać plików CSV.")
msg_box.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
ret_value = msg_box.exec()
self._msg_install_clicked(ret_value)
# Create the dockwidget (after translation) and keep reference
self.dockwidget = AlgoMapsPluginDockWidget()
# Fill the `config.json` values into UI
self.populate_dq_api_settings_ui()
# Set default checkboxes values
self.populate_checkbox_settings_ui()
# Connect the buttons
self.dockwidget.btn_settings_save.clicked.connect(self.save_settings)
self.dockwidget.btn_settings_reset.clicked.connect(self.reset_settings)
self.dockwidget.btn_geocode_general.clicked.connect(self.clicked_geocode_general)
self.dockwidget.btn_geocode_details.clicked.connect(self.clicked_geocode_details)
self.dockwidget.btn_batch_process.clicked.connect(self.clicked_batch_process)
# Conenct settings checkboxes
self.dockwidget.chk_teryt.stateChanged.connect(self.settings_chkbox_changed)
self.dockwidget.chk_gus.stateChanged.connect(self.settings_chkbox_changed)
self.dockwidget.chk_buildinfo.stateChanged.connect(self.settings_chkbox_changed)
self.dockwidget.chk_financial.stateChanged.connect(self.settings_chkbox_changed)
# Batch UI
self.dockwidget.progress_batch.setVisible(False)
self.dockwidget.btn_cancel_batch.setVisible(False)
self.dockwidget.tableWidget_batch.setVisible(False)
self.dockwidget.group_csv_info.setVisible(False)
self.dockwidget.group_batch.setVisible(False)
self.dockwidget.file_batch_load.fileChanged.connect(self.file_batch_load_changed)
self.dockwidget.file_batch_save.fileChanged.connect(self.file_batch_save_changed)
self.dockwidget.txt_sep.textEdited.connect(self.txt_sep_changed)
self.dockwidget.txt_quotechar.textEdited.connect(self.txt_quotechar_changed)
# connect to provide cleanup on closing of dockwidget
self.dockwidget.closingPlugin.connect(self.onClosePlugin)
# show the dockwidget
# TODO: fix to allow choice of dock location
self.iface.addDockWidget(Qt.RightDockWidgetArea, self.dockwidget)
self.dockwidget.show()
def populate_dq_api_settings_ui(self):
self.dockwidget.txt_dq_user.setText(self.dq_user)
self.dockwidget.txt_dq_token.setText(self.dq_token)
self.dockwidget.txt_api_key.setText(self.api_key)
def populate_checkbox_settings_ui(self):
self.dockwidget.chk_financial.setChecked(self.default_chk_financial)
self.dockwidget.chk_teryt.setChecked(self.default_chk_teryt)
self.dockwidget.chk_gus.setChecked(self.default_chk_gus)
self.dockwidget.chk_buildinfo.setChecked(self.default_chk_buildinfo)
if self.dockwidget.chk_financial.isChecked():
self.dockwidget.chk_gus.setEnabled(False)
def reset_settings(self):
# Set previous DQ/API data
self.populate_dq_api_settings_ui()
# Set default checkbox values
self.populate_checkbox_settings_ui()
if DEBUG_MODE:
QgsMessageLog().logMessage(message="Reset ustawień.", tag='AlgoMaps', level=Qgis.MessageLevel.Info)
def save_settings(self):
try:
new_settings = {
# Save new DQ/API data
"dq_user": self.dockwidget.txt_dq_user.text(),
"dq_token": self.dockwidget.txt_dq_token.text(),
"api_key": self.dockwidget.txt_api_key.text(),
# Save the checkbox values
"default_chk_teryt": self.dockwidget.chk_teryt.isChecked(),
"default_chk_gus": self.dockwidget.chk_gus.isChecked(),
"default_chk_buildinfo": self.dockwidget.chk_buildinfo.isChecked(),
"default_chk_financial": self.dockwidget.chk_financial.isChecked(),
}
with open(self.config_path, 'w') as f:
json.dump(new_settings, f)
self.dq_user = new_settings['dq_user']
self.dq_token = new_settings['dq_token']
self.api_key = new_settings['api_key']
self.default_chk_teryt = new_settings["default_chk_teryt"]
self.default_chk_gus = new_settings["default_chk_gus"]
self.default_chk_buildinfo = new_settings["default_chk_buildinfo"]
self.default_chk_financial = new_settings["default_chk_financial"]
if DEBUG_MODE:
QgsMessageLog().logMessage(message="Zapisano ustawienia", tag='AlgoMaps', level=Qgis.MessageLevel.Success)
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(u'Zapisano ustawienia'),
level=Qgis.MessageLevel.Success)
except:
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(u'Zapis ustawień nie powiódł się'),
level=Qgis.MessageLevel.Warning)
def clicked_geocode_general(self):
if DEBUG_MODE:
QgsMessageLog().logMessage(message="Geokoduj (jedno pole adresowe)", tag='AlgoMaps', level=Qgis.MessageLevel.Info)
dane_ogolne = self.dockwidget.txt_generaldata.text()
# API request
req_data = {
"generalData": dane_ogolne
}
result_json = self.send_single_algomaps_request(req_data,
self.include_teryt,
self.include_gus,
self.include_buildinfo,
self.include_financial)
if result_json is None:
return
self.dockwidget.txt_outputstand.setText(
json.dumps(result_json, indent=4, ensure_ascii=False).encode('utf8').decode())
if 'latitude' in result_json and 'longitude' in result_json:
self.add_response_to_map(result_json, dane_ogolne, self.include_teryt,
self.include_gus, self.include_buildinfo, self.include_financial)
else:
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(u'Brak geokodowania dla podanego adresu'),
level=Qgis.MessageLevel.Warning)
def clicked_geocode_details(self):
if DEBUG_MODE:
QgsMessageLog().logMessage(message="Geokoduj (dane szczegółowe)", tag='AlgoMaps', level=Qgis.MessageLevel.Info)
w = self.dockwidget.txt_voivodeship.text()
p = self.dockwidget.txt_county.text()
g = self.dockwidget.txt_commune.text()
m = self.dockwidget.txt_city.text()
k = self.dockwidget.txt_postal.text()
u = self.dockwidget.txt_street.text()
n = self.dockwidget.txt_houseno.text()
l = self.dockwidget.txt_flatno.text()
# API request
req_data = {
"voivodeshipName": w,
"countyName": p,
"communeName": g,
"cityName": m,
"postalCode": k,
"streetName": u,
"streetNumber": n,
"apartmentNumber": l
}
result_json = self.send_single_algomaps_request(req_data,
self.include_teryt,
self.include_gus,
self.include_buildinfo,
self.include_financial)
if result_json is None: # Error
return
self.dockwidget.txt_outputstand.setText(
json.dumps(result_json, indent=4, ensure_ascii=False).encode('utf8').decode())
if 'latitude' in result_json and 'longitude' in result_json:
input_text = f'{w}|{p}|{g}|{m}|{k}|{u}|{n}|{l}'
self.add_response_to_map(result_json, input_text, self.include_teryt,
self.include_gus, self.include_buildinfo, self.include_financial)
else:
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(u'Brak geokodowania dla podanego adresu'),
level=Qgis.MessageLevel.Warning)
def send_single_algomaps_request(self, req_data, teryt=False, gus=False, buildinfo=False, financial=False):
import requests
active_modules = ["ADDRESSES"] if not financial else ["ADDRESSES", "FINANCES"]
gus = gus if not financial else True # If using financial data, we need GUS identifiers
input_json = {
"inputRows": [req_data],
"processParameters": {
"activeModules": active_modules,
"includeBuildingsInfo": buildinfo,
"includeSymbolicNames": teryt,
"includeDiagnosticInfo": True,
"includeGeographicCoordinates": True,
"includeGusZones": gus
}
}
if DEBUG_MODE:
QgsMessageLog().logMessage(message='SEND: ' + repr(input_json), tag='AlgoMaps', level=Qgis.MessageLevel.Info)
headers = {
'Content-Type': 'application/json',
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': self.api_key
}
response = requests.post('https://api.algolytics.pl/dqo/api/v1/rows',
json=input_json,
headers=headers)
if response.status_code == 200:
return response.json()[0]
else:
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(u'Nie udało się pobrać danych. Sprawdź poprawność klucza API w '
u'Ustawieniach'),
level=Qgis.MessageLevel.Critical)
return None
def add_response_to_map(self, result_json, input_data=None, teryt=False, gus=False, buildinfo=False,
financial=False):
default_fields_names = ["inputData", "voivodeshipName", "countyName", "communeName", "postalCode",
"cityName", "cityDistrictName", "streetAttribute", "streetName",
"streetNameMajorPart", "streetNameMinorPart", "streetNumber", "apartmentNumber",
"addressId", "numberOfApartments", "numberOfJuridicalPersons",
"latitude", "longitude", "statusMatch", "statusGeocoding", "statusOther"]
default_fields = []
for field in default_fields_names:
if field in ["latitude", "longitude"]:
field_type = self._field_double_type
elif field in ["numberOfApartments", "numberOfJuridicalPersons"]:
field_type = self._field_int_type
else:
field_type = self._field_string_type
default_fields.append(self._define_field(field, field_type, result_json))
lat = result_json.get('latitude')
lon = result_json.get('longitude')
status = result_json.get('statuses')
status_match, status_geocode, status_other = self._parse_statuses(status)
default_definitions = [QgsField(x[0], x[1]) for x in default_fields]
default_values = dict([[x[0], x[2]] for x in default_fields]) # Field-value map
default_values['inputData'] = input_data
default_values['statusMatch'] = status_match
default_values['statusGeocoding'] = status_geocode
default_values['statusOther'] = status_other
# Additional fields (checkboxes selected)
additional_fields = []
if teryt:
additional_fields.append(self._define_field("communeSymbol", self._field_string_type, result_json))
additional_fields.append(self._define_field("communeTypeSymbol", self._field_int_type, result_json))
additional_fields.append(self._define_field("communeTypeName", self._field_string_type, result_json))
additional_fields.append(self._define_field("citySymbol", self._field_string_type, result_json))
additional_fields.append(self._define_field("cityDistrictSymbol", self._field_string_type, result_json))
additional_fields.append(self._define_field("streetSymbol", self._field_string_type, result_json))
if gus:
additional_fields.append(
self._define_field("statisticalRegionSymbol", self._field_string_type, result_json))
additional_fields.append(self._define_field("censusCircuitSymbol", self._field_string_type, result_json))
if buildinfo:
# Integer fields
for field in ["numberOfInhabitedApartmentsByGUS", "numberOfInhabitedApartmentsByPESEL",
"numberOfInhabitantsByGUS", "numberOfInhabitantsByPESEL",
"numberOfMen",
"numberOfMenBetween_0_4_yearsOld", "numberOfMenBetween_5_9_yearsOld",
"numberOfMenBetween_10_14_yearsOld", "numberOfMenBetween_15_19_yearsOld",
"numberOfMenBetween_20_24_yearsOld", "numberOfMenBetween_25_29_yearsOld",
"numberOfMenBetween_30_34_yearsOld", "numberOfMenBetween_35_39_yearsOld",
"numberOfMenBetween_40_44_yearsOld", "numberOfMenBetween_45_49_yearsOld",
"numberOfMenBetween_50_54_yearsOld", "numberOfMenBetween_55_59_yearsOld",
"numberOfMenBetween_60_64_yearsOld", "numberOfMenOver_65_yearsOld",
"numberOfWomen",
"numberOfWomenBetween_0_4_yearsOld", "numberOfWomenBetween_5_9_yearsOld",
"numberOfWomenBetween_10_14_yearsOld", "numberOfWomenBetween_15_19_yearsOld",
"numberOfWomenBetween_20_24_yearsOld", "numberOfWomenBetween_25_29_yearsOld",
"numberOfWomenBetween_30_34_yearsOld", "numberOfWomenBetween_35_39_yearsOld",
"numberOfWomenBetween_40_44_yearsOld", "numberOfWomenBetween_45_49_yearsOld",
"numberOfWomenBetween_50_54_yearsOld", "numberOfWomenBetween_55_59_yearsOld",
"numberOfWomenBetween_60_64_yearsOld", "numberOfWomenOver_65_yearsOld",
"numberOfMicroEntrepreneurs"]:
additional_fields.append(self._define_field(field, self._field_int_type, result_json))
# String field
additional_fields.append(self._define_field("taxationAuthority", self._field_string_type, result_json))
if financial:
# Double precision fields
for field in ["individualClientFraudStatistic", "individualClientFraudScore",
"individualClientDefaultStatistic", "individualClientDefaultScore",
"businessClientFraudStatistic", "businessClientFraudScore",
"businessClientDefaultStatistic", "businessClientDefaultScore",
"entrepreneurFraudStatistic", "entrepreneurFraudScore",
"entrepreneurDefaultStatistic", "entrepreneurDefaultScore",
"averageIncome", "incomePercentile5", "incomePercentile25", "incomePercentile50",
"incomePercentile75", "incomePercentile95"]:
additional_fields.append(self._define_field(field, self._field_double_type, result_json))
additional_values = dict([[x[0], x[2]] for x in additional_fields]) # Field-value map
additional_definitions = [QgsField(x[0], x[1]) for x in
additional_fields] # Field definitions for data provider
# Add to map
layer_name = "AlgoMaps standaryzacja i geokodowanie"
layer_find = QgsProject.instance().mapLayersByName(layer_name)
if len(layer_find) == 0:
# Create layer if not exists
vl = QgsVectorLayer("Point?crs=epsg:4326", layer_name, "memory")
self.qproj.addMapLayer(vl)
pr = vl.dataProvider()
pr.addAttributes([*default_definitions,
*additional_definitions])
vl.updateFields()
else:
vl = layer_find[0]
pr = vl.dataProvider()
# Create feature
f = QgsFeature()
f.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(lon, lat)))
# Set feature fields as layer fields
f.setFields(vl.fields())
# Populate fields values
for key, value in default_values.items():
f.setAttribute(key, value)
cnt_invalid = 0
for key, value in additional_values.items():
if key in vl.fields().names():
f.setAttribute(key, value)
else:
cnt_invalid += 1
if DEBUG_MODE:
QgsMessageLog().logMessage(message=f'Can\'t add {key}', tag='AlgoMaps', level=Qgis.MessageLevel.Warning)
if cnt_invalid > 0:
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(
f'Nie udało się wypełnić {cnt_invalid} atrybutów. Rozważ usunięcie/zmianę nazwy warstwy, aby poprawnie utworzyć obiekt ze wszystkimi żądanymi atrybutami.'),
level=Qgis.MessageLevel.Warning)
pr.addFeature(f)
vl.updateFields()
vl.updateExtents()
# Recenter the map
self.recenter_at_xy(lon, lat)
def _define_field(self, field_name, field_type, result_json):
if field_type == 'str' or field_type == 'string':
field_type = self._field_string_type
if field_type == 'int':
field_type = self._field_int_type
if field_type == 'double' or field_type == 'float':
field_type = self._field_double_type
return [field_name, field_type, result_json.get(field_name)]
def recenter_at_xy(self, lon, lat, srs=4326):
original_rect = QgsRectangle(QgsPointXY(lon, lat), QgsPointXY(lon, lat))
# We need to transform the point lat/lon to map's CRS
source_crs = QgsCoordinateReferenceSystem(f"EPSG:{srs}")
dest_crs = self.qproj.crs()
transform_context = QgsProject.instance().transformContext()
coordinate_transform = QgsCoordinateTransform(source_crs, dest_crs, transform_context)
transformed_rect = coordinate_transform.transformBoundingBox(original_rect)
# Center map at the point
self.canvas.setExtent(transformed_rect)
self.canvas.refresh()
def settings_chkbox_changed(self, i):
self.include_teryt = True if self.dockwidget.chk_teryt.isChecked() else False
self.include_gus = True if self.dockwidget.chk_gus.isChecked() else False
self.include_buildinfo = True if self.dockwidget.chk_buildinfo.isChecked() else False
if self.dockwidget.chk_financial.isChecked():
self.include_financial = True
self.include_gus = True
self.dockwidget.chk_gus.setChecked(True)
self.dockwidget.chk_gus.setEnabled(False)
else:
self.dockwidget.chk_gus.setEnabled(True)
self.include_financial = False
if DEBUG_MODE:
include_txt = str([self.include_teryt, self.include_gus, self.include_buildinfo, self.include_financial])
QgsMessageLog().logMessage(message='Checkbox state: [TERYT, GUS, BUILDINFO, FINANCIAL]', tag='AlgoMaps',
level=Qgis.MessageLevel.Info)
QgsMessageLog().logMessage(include_txt, tag='AlgoMaps', level=Qgis.MessageLevel.Info)
self.flags = {
'gus': self.include_gus,
'teryt': self.include_teryt,
'buildinfo': self.include_buildinfo,
'financial': self.include_financial
}
@staticmethod
def _parse_statuses(status):
# Split status into three separate strings (match, geocode, others)
import re
matches = re.findall(r'(<dopasowanie: [^>]+>)|(<geokodowanie: [^>]+>)|(<[^>]+>)',
status.strip())
status_dop = None
status_geo = None
status_other = []
for match in matches:
if match[0]:
status_dop = match[0]
elif match[1]:
status_geo = match[1]
elif match[2]:
status_other.append(match[2])
# Concatenate all "other" matches into a single string
status_other = ''.join(status_other) if status_other else None
return status_dop, status_geo, status_other
def file_batch_load_changed(self):
from .csv_utils import identify_header, identify_delimiter_and_quotechar
if DEBUG_MODE:
QgsMessageLog().logMessage(message='BATCH FILE PATH CHANGED', tag='AlgoMaps', level=Qgis.MessageLevel.Info)
try:
self.csv_path = self.dockwidget.file_batch_load.filePath()
if not self.csv_path: # Empty fileWidget path
return
# Infer the csv separator and quoting char
sep, quotechar = identify_delimiter_and_quotechar(self.csv_path)
if DEBUG_MODE:
QgsMessageLog().logMessage(message=f'Separator: {sep} / Quotechar: {quotechar}', tag='AlgoMaps', level=Qgis.MessageLevel.Info)
self.batch_sep = str(sep) if sep else ','
self.batch_quote = str(quotechar) if quotechar else '"'
self.batch_header_type = identify_header(self.csv_path, sep=sep)
self.dockwidget.txt_sep.setText(self.batch_sep)
self.dockwidget.txt_quotechar.setText(self.batch_quote)
self.show_csv_table(self.csv_path, sep=self.batch_sep, header_type=self.batch_header_type, quotechar=self.batch_quote)
except Exception as e:
raise
# QgsMessageLog().logMessage(str(e), tag='AlgoMaps', level=Qgis.MessageLevel.Warning)
# pass
def clicked_batch_process(self):
column_roles = [cb.currentText() for cb in self.batch_combo_widgets]
required_cols = ['DANE_OGOLNE', 'KOD_POCZTOWY', 'MIEJSCOWOSC']
any_value = any(item in required_cols for item in column_roles)
# Check 1: At least one of `DANE_OGOLNE`, `KOD_POCZTOWY` or `MIEJSCOWOSC` is present
if not any_value:
msg_box = QMessageBox()
msg_box.setIcon(QMessageBox.Warning)
msg_box.setWindowTitle(f"Brak wymaganych kolumn") # nazwa bez nr wersji
msg_box.setText("Musisz wybrać co najmniej jedną z kolumn: (DANE_OGOLNE, KOD_POCZTOWY, MIEJSCOWOSC)")
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec()
return
# Check 2: Duplicates
duplicates = []
for role in column_roles:
if column_roles.count(role) > 1 and role not in duplicates and role not in ('PRZEPISZ', 'POMIN'):
duplicates.append(role)
if len(duplicates) > 0:
msg_box = QMessageBox()
msg_box.setIcon(QMessageBox.Warning)
msg_box.setWindowTitle(f"Zduplikowane kolumny") # nazwa bez nr wersji
msg_box.setText(f"Kolumny (z wyjątkiem POMIN i PRZEPISZ) nie mogą się powtarzać! \n\nDuplikaty: {duplicates}")
msg_box.setStandardButtons(QMessageBox.Ok)
msg_box.exec()
return
# TODO: check if save as csv and filepath not empty
if DEBUG_MODE:
QgsMessageLog().logMessage(message='CLICKED PROCESS', tag='AlgoMaps', level=Qgis.MessageLevel.Info)
QgsMessageLog().logMessage(message=f'COLUMN ROLES:\n{column_roles}', tag='AlgoMaps', level=Qgis.MessageLevel.Info)
try:
from .BatchGeocoder import BatchGeocoder
except Exception as e:
self.iface.messageBar().pushMessage(self.tr(u'AlgoMaps'),
self.tr(
u'Cannot initialize batch processing - error in code. Contact the AlgoMaps developers'),
level=Qgis.MessageLevel.Critical)
if DEBUG_MODE:
QgsMessageLog().logMessage(str(e), tag='AlgoMaps', level=Qgis.MessageLevel.Warning)
return
geocoder = BatchGeocoder(csv_path=self.csv_path,
column_roles=column_roles,
iface=self.iface,
dock_handle=self.dockwidget,
qproj=self.qproj,
dq_user=self.dq_user,
dq_token=self.dq_token,