Skip to content

Commit 92e6825

Browse files
9.0.1
1 parent 0ffec20 commit 92e6825

34 files changed

Lines changed: 2211 additions & 4629 deletions

core/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
first_install = signature_plot = spectral_signature_plotter = None
3939
scatter_plot = scatter_plotter = scp_processing_provider = None
4040
signature_importer = usgs_spectral_lib = multiple_roi = None
41-
smtp_notification = smtp_server = smtp_user = smtp_password = None
41+
smtp_notification = smtp_server = smtp_user = smtp_pass = None
4242
smtp_recipients = main_menu = simplified = rs_version = None
4343
# welcome url
4444
first_reply = second_reply = second_url = None
@@ -217,8 +217,8 @@
217217
qgis_registry[reg_smtp_emails] = ''
218218
reg_smtp_user = 'SemiAutomaticClassificationPlugin/smtp_user'
219219
qgis_registry[reg_smtp_user] = ''
220-
reg_smtp_password = 'SemiAutomaticClassificationPlugin/smtp_password'
221-
qgis_registry[reg_smtp_password] = ''
220+
reg_smtp_pass = 'SemiAutomaticClassificationPlugin/smtp_pass'
221+
qgis_registry[reg_smtp_pass] = ''
222222
# raster variable name
223223
reg_raster_variable_name = 'SemiAutomaticClassificationPlugin/raster_var_name'
224224
raster_variable_name_def = 'raster'

core/messages.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
ui_name = 'semiautomaticclassificationplugin'
3131

32+
3233
# message box information
3334
def msg_box(title, message):
3435
QMessageBox.information(cfg.iface.mainWindow(), str(title), str(message))
@@ -66,7 +67,7 @@ def msg_bar_critical(title, message):
6667
# Message bar error
6768
def msg_bar_error(message):
6869
# noinspection PyTypeChecker
69-
msg_bar_critical(QApplication.translate(ui_name, 'Error'),message=message)
70+
msg_bar_critical(QApplication.translate(ui_name, 'Error'), message=message)
7071

7172

7273
# message bar warning
@@ -137,7 +138,7 @@ def msg_inf_4():
137138

138139
def msg_inf_5():
139140
# noinspection PyTypeChecker
140-
msg_bar_info( QApplication.translate(ui_name, 'Enter class values'))
141+
msg_bar_info(QApplication.translate(ui_name, 'Enter class values'))
141142

142143

143144
def msg_inf_6():
@@ -213,6 +214,7 @@ def msg_err_9(layer=None):
213214
QApplication.translate(ui_name, f'Unable to find layer {layer}')
214215
)
215216

217+
216218
""" Warnings """
217219

218220

@@ -248,8 +250,8 @@ def msg_war_4():
248250
def msg_war_5():
249251
# noinspection PyTypeChecker
250252
msg_bar_warning(
251-
QApplication.translate( ui_name,
252-
'Select a training input; input is not loaded')
253+
QApplication.translate(ui_name,
254+
'Select a training input; input is not loaded')
253255
)
254256

255257

core/ui_utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
# If not, see <https://www.gnu.org/licenses/>.
2222

2323

24-
import os
25-
import sys
2624
import ssl
2725
import smtplib
26+
import subprocess
2827

2928
import qgis.core as qgis_core
3029
from PyQt6.QtCore import QSize
@@ -313,7 +312,7 @@ def send_smtp_message(subject: str = None, message: str = None):
313312
server = smtplib.SMTP(cfg.smtp_server, 587)
314313
context = ssl.create_default_context()
315314
server.starttls(context=context)
316-
server.login(cfg.smtp_user, cfg.smtp_password)
315+
server.login(cfg.smtp_user, cfg.smtp_pass)
317316
tolist = cfg.smtp_recipients.split(',')
318317
if subject is None:
319318
subject = 'completed process'
@@ -353,7 +352,8 @@ def beeps(frequency: int, duration: float):
353352
if cfg.system_platform.startswith('win'):
354353
winsound.Beep(frequency, int(duration * 1000))
355354
elif cfg.system_platform.startswith('linux'):
356-
os.system(
357-
'play --no-show-progress --null --channels 1 synth %s sine %s'
358-
% (str(duration), str(frequency))
355+
subprocess.run(
356+
['play', '--no-show-progress', '--null', '--channels', '1',
357+
'synth', str(duration), 'sine', str(frequency),
358+
], check=True,
359359
)

core/utils.py

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@
6363
""" download functions """
6464

6565

66-
# check Remotior Sensus version
67-
def check_remotior_sensus_version():
68-
request = QNetworkRequest(
69-
QUrl('https://pypi.org/pypi/remotior_sensus/json')
70-
)
71-
cfg.remotior_reply = QgsNetworkAccessManager.instance().get(request)
72-
cfg.remotior_reply.finished.connect(remotior_label)
73-
74-
7566
# check Remotior Sensus version
7667
def remotior_label():
7768
try:
@@ -553,6 +544,66 @@ def random_points_in_grid(point_number, x_min, x_max, y_min, y_max):
553544
return points
554545

555546

547+
# evaluate expression for dynamic use
548+
def evaluate_expression(expression, array):
549+
if '>=' in expression:
550+
left, right = expression.split('>=')
551+
return array >= float(right)
552+
if '<=' in expression:
553+
left, right = expression.split('<=')
554+
return array <= float(right)
555+
if '>' in expression:
556+
left, right = expression.split('>')
557+
return array > float(right)
558+
if '<' in expression:
559+
left, right = expression.split('<')
560+
return array < float(right)
561+
if '==' in expression:
562+
left, right = expression.split('==')
563+
return array == float(right)
564+
if '!=' in expression:
565+
left, right = expression.split('!=')
566+
return array != float(right)
567+
raise ValueError('Invalid expression')
568+
569+
570+
def split_expression(expression, operator):
571+
expressions = []
572+
depth = 0
573+
current = ''
574+
i = 0
575+
while i < len(expression):
576+
if expression[i] == '(':
577+
depth += 1
578+
elif expression[i] == ')':
579+
depth -= 1
580+
if depth == 0 and expression[i:i+len(operator)] == operator:
581+
expressions.append(current)
582+
current = ''
583+
i += len(operator)
584+
continue
585+
current += expression[i]
586+
i += 1
587+
expressions.append(current)
588+
return expressions
589+
590+
591+
def evaluate_condition(condition, context):
592+
name, a = next(iter(context.items()))
593+
expression = condition.replace(name, 'a').strip()
594+
or_parts = split_expression(expression, '|')
595+
or_result = None
596+
for part in or_parts:
597+
and_parts = split_expression(part, '&')
598+
and_result = None
599+
for and_part in and_parts:
600+
cond = evaluate_expression(and_part, a)
601+
and_result = cond if and_result is None else (and_result & cond)
602+
or_result = and_result if or_result is None else (or_result
603+
| and_result)
604+
return or_result
605+
606+
556607
# calculate random points with condition
557608
def random_points_with_condition(
558609
raster_path, point_number, condition, x_min, y_top
@@ -562,10 +613,14 @@ def random_points_with_condition(
562613
points = []
563614
# band array
564615
_array = cfg.util_gdal.read_raster(raster_path)
616+
context = {
617+
cfg.qgis_registry[cfg.reg_raster_variable_name]: _array
618+
}
565619
condition = condition.replace(
566620
cfg.qgis_registry[cfg.reg_raster_variable_name], '_array'
567621
)
568-
condition = eval(condition, {'__builtins__': None}, {'_array': _array})
622+
_array = context[cfg.qgis_registry[cfg.reg_raster_variable_name]]
623+
condition = evaluate_condition(condition, context)
569624
arr = np.argwhere(condition)
570625
rng = np.random.default_rng()
571626
try:

0 commit comments

Comments
 (0)