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
7667def 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
557608def 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