-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
172 lines (143 loc) · 5.63 KB
/
utils.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
# -*- coding: utf-8 -*-
import os
import re
import shutil
import subprocess
import sys
from datetime import datetime
from PyQt5.QtCore import Qt
from chardet import detect
from qgis.PyQt.QtWidgets import QComboBox, QApplication, QProgressDialog, \
QMessageBox
from qgis.core import QgsProject, QgsVectorLayer, \
QgsRasterLayer
from qgis.utils import iface
project = QgsProject.instance()
class CreateTemporaryLayer(QgsVectorLayer):
parent_layer = None
def __init__(self, *args, **kwargs):
super(CreateTemporaryLayer, self).__init__(*args, **kwargs)
def set_layer_fields(self, fields):
self.dataProvider().addAttributes(fields)
self.updateFields()
def repair_comboboxes(dlg):
dialog_obj_list = [dlg.__getattribute__(obj) for obj in dlg.__dir__()]
combo_list = list(
filter(lambda elem: isinstance(elem, QComboBox), dialog_obj_list))
if not combo_list:
return
for combo in combo_list:
combo.installEventFilter(dlg)
temp_value = combo.isEditable()
combo.setEditable(True)
combo_css = 'QComboBox { combobox-popup: 0; }'
combo.setStyleSheet(f"{combo.styleSheet()} {combo_css}")
combo.setMaxVisibleItems(10)
combo.view().setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)
combo.setEditable(temp_value)
def get_project_settings(parameter, key, default=''):
return project.readEntry(parameter, key, default)[0]
def set_project_settings(parameter, key, value):
return project.writeEntry(parameter, key, value)
def create_progress_bar(max_len, title='Proszę czekać',
txt='Trwa przetwarzanie danych.', start_val=0,
auto_close=True, cancel_btn=None, silent=False):
progress_bar = QProgressDialog()
progress_bar.setFixedWidth(500)
progress_bar.setWindowTitle(title)
progress_bar.setLabelText(txt)
progress_bar.setMaximum(max_len)
progress_bar.setValue(start_val)
progress_bar.setAutoClose(auto_close)
progress_bar.setCancelButton(cancel_btn)
QApplication.processEvents()
if silent:
progress_bar.close()
return progress_bar
def change_progressbar_value(progress, last_progress_value,
value, last_step=False, silent=False):
if not silent:
progress.show()
QApplication.processEvents()
last_progress_value += value
if last_progress_value == 100 or last_step:
progress.setValue(100)
progress.close()
else:
progress.setValue(last_progress_value)
def add_layer_into_map(layer, group_name, parent_name=None, position=0):
root = project.layerTreeRoot()
if parent_name and root.findGroup(parent_name):
group = root.findGroup(parent_name).findGroup(group_name)
else:
group = root.findGroup(group_name)
if not group:
project.addMapLayer(layer)
return
QApplication.processEvents()
project.addMapLayer(layer, False)
if group_name:
group.insertLayer(position, layer)
group.setExpanded(False)
def add_rasters_to_project(group_name, list_of_rasters, symbology=None):
QApplication.processEvents()
group_import = project.layerTreeRoot().findGroup(group_name)
if not group_import:
project.layerTreeRoot().addGroup(group_name)
for raster_path in list_of_rasters:
QApplication.processEvents()
rlayer = QgsRasterLayer(raster_path, os.path.basename(raster_path))
add_layer_into_map(rlayer, group_name)
if symbology:
rlayer.loadNamedStyle(symbology)
rlayer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(rlayer.id())
def add_vectors_to_project(group_name, list_of_vectors, symbology=None):
QApplication.processEvents()
group_import = project.layerTreeRoot().findGroup(group_name)
if not group_import:
project.layerTreeRoot().addGroup(group_name)
for vector_path in list_of_vectors:
QApplication.processEvents()
if isinstance(vector_path, QgsVectorLayer):
vlayer = vector_path
else:
vlayer = QgsVectorLayer(vector_path, os.path.basename(vector_path),
"ogr")
add_layer_into_map(vlayer, group_name)
if symbology:
vlayer.loadNamedStyle(symbology)
vlayer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(vlayer.id())
def open_other_files(filepath):
if sys.platform.startswith('darwin'):
subprocess.call(('open', filepath))
elif os.name == 'nt':
try:
os.startfile(filepath)
except WindowsError:
ext = os.path.splitext(filepath)[-1]
QMessageBox.critical(
None, 'Analiza NMT',
f'''Błąd przy otwieraniu pliku z rozszerzeniem *.{ext}.
Zainstaluj program obsługujący format *.{ext} i spróbuj ponownie.''',
QMessageBox.Ok)
return
elif os.name == 'posix':
subprocess.call(('xdg-open', filepath))
def standarize_path(path):
return os.path.normpath(os.sep.join(re.split(r'\\|/', path)))
def repair_encoding(tmp_dir, infile):
tmp_file = os.path.join(
tmp_dir,
f"{datetime.now().strftime('%H_%M_%S_%f')[:-3]}.{os.path.splitext(infile)[-1]}"
)
shutil.copyfile(infile, tmp_file)
os.remove(infile)
with open(tmp_file, 'rb') as file:
rawdata = rb''.join([file.readline() for _ in range(150)])
with open(tmp_file,
'r', encoding=detect(rawdata)['encoding']) as input_file, \
open(infile, 'w', encoding='utf-8') as output_file:
text = input_file.read()
output_file.write(text)