May-16-2020, 10:44 AM
I have a while loop in which I check if a certain geographic point is located within any of the 7 regions of a given province (using PostGis ST_WITHIN).
'a' is region number, 'k' is the last region number and 'PointFound' = is a boolean.
region 1 --> (False,)
region 2 --> (False,)
region 3 --> (True,)
region 4 --> (False,)
region 5 --> (False,)
region 6 --> (False,)
region 7 --> (False,)
As shown, the point is located within region number 3.
However, I now want to update the region number into my database, so I changed my code as follows (for testing purposes : later on new print() will have to be replaced by an UPDATE query to database):
region 1 --> (False,)
region 2 --> (False,)
region 3 --> (True,)
Place my UPDATE sql query here
region 4 --> (False,)
region 5 --> (False,)
region 6 --> (False,)
region 7 --> (False,)
However, I am getting :
region 1 --> (False,)
Place my UPDATE sql query here
region 2 --> (False,)
Place my UPDATE sql query here
region 3 --> (True,)
Place my UPDATE sql query here
region 4 --> (False,)
Place my UPDATE sql query here
region 5 --> (False,)
Place my UPDATE sql query here
region 6 --> (False,)
Place my UPDATE sql query here
region 7 --> (False,)
Place my UPDATE sql query here
notwithstanding the additional print instruction is placed after 'PointFound' is fetched for every new 'a' BUT before I increment my 'a' value with '1'
Whatever I tried (simple if, if.. elif ..else, while, …. etcetera), nothing seems to solve my problem.
What am I doing wrong here ? Why is my 'if PointFound:' condition not doing what it is expected to do ?
'a' is region number, 'k' is the last region number and 'PointFound' = is a boolean.
cusor.execute(query) PointFound = cursor.fetchone() print('region ' + str(a) + ' --> ' + str(PointFound)) a += 1 if a > k: breakThis works fine and is giving me the expected output :
region 1 --> (False,)
region 2 --> (False,)
region 3 --> (True,)
region 4 --> (False,)
region 5 --> (False,)
region 6 --> (False,)
region 7 --> (False,)
As shown, the point is located within region number 3.
However, I now want to update the region number into my database, so I changed my code as follows (for testing purposes : later on new print() will have to be replaced by an UPDATE query to database):
cusor.execute(query) PointFound = cursor.fetchone() print('region ' + str(a) + ' --> ' + str(PointFound)) if PointFound: print('Place my UPDATE sql query here') a += 1 if a > k: breakI was expecting following result :
region 1 --> (False,)
region 2 --> (False,)
region 3 --> (True,)
Place my UPDATE sql query here
region 4 --> (False,)
region 5 --> (False,)
region 6 --> (False,)
region 7 --> (False,)
However, I am getting :
region 1 --> (False,)
Place my UPDATE sql query here
region 2 --> (False,)
Place my UPDATE sql query here
region 3 --> (True,)
Place my UPDATE sql query here
region 4 --> (False,)
Place my UPDATE sql query here
region 5 --> (False,)
Place my UPDATE sql query here
region 6 --> (False,)
Place my UPDATE sql query here
region 7 --> (False,)
Place my UPDATE sql query here
notwithstanding the additional print instruction is placed after 'PointFound' is fetched for every new 'a' BUT before I increment my 'a' value with '1'
Whatever I tried (simple if, if.. elif ..else, while, …. etcetera), nothing seems to solve my problem.
What am I doing wrong here ? Why is my 'if PointFound:' condition not doing what it is expected to do ?