2

I'm working with QGis 2.8, editing the attribute table of a SHP file, named "myTable". I need to populate field_4 with the conditions listed below:


For 1 to n (it runs across records of myTable)                                              
  select record n                                   
    set var1 = field_1.value (it puts the value of field_1 in var1) 
      if field_2 >1 then field_4.value = 'OK'                   
      else                              
      SELECT MAX(field_2) FROM myTable WHERE field_1.value = var1 (it selects the record with the higher value of those who have field_1=var1)
      set var3 = field_3.value (it copies field_3 value into var3)                  
      end                               
    select record n (it come's back to current record)                              
    set field_4.value = var3 (it populates field_4 with var3 value)                 
next n          

myTable will be like this after running:

geometry, field_1, field_2, field_3, field_4

1       1       10          a       ok
2       1       1           b       c
3       1       80          c       ok
4       2       60          d       ok
5       2       20          e       ok
6       2       1           f       d
7       2       5           g       ok
8       1       1           h       c
9       1       8           i       ok
10      2       5           l       ok
11      2       5           m       ok
12      2       4           n       ok
13      3       39          o       ok
14      3       1           p       r
15      3       10          q       ok
16      3       50          r       ok

I'd like write this script in Python, but my skill is very low !! (note that it calls some SQL functions) I don't know how to use "cursor" tecnique into Python. Can someone help me writing this procedure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.