2

I am using the python code found in Programmatically finding polygons which are >90% overlapped by another vector polygon layer using QGIS?.

I get the following message

error: NameError: name 'area1' is not defined

Here is the concerned part of the code:

for i, feat1 in enumerate(feats_lyr1):
    for j, feat2 in enumerate(feats_lyr2):
        if feat1.geometry().intersects(feat2.geometry()):
            area1 = feat1.geometry().intersection(feat2.geometry()).area()
            area2 = feat1.geometry().area()             
    crit =area1/area2
    if crit > 0.9:
        selected_feats.append(feat1)

After checking lots of things (geometry, CRS, ..), the only thing I see is that i have null area for couple of polygons.

  • Is that possible that small areas, close to zero, are considered to be zero if considered as integer number?
  • how to ensure the use of decimal number?
  • 1
    I think you should review your indentation because the last three lines of that code look like they should go in a level. – PolyGeo Dec 19 '17 at 19:45
  • 1
    Put crit =area1/area2 if crit > 0.9: selected_feats.append(feat1) inside second for – Fran Raga Dec 19 '17 at 19:49
2

It was an indentation problem:

for i, feat1 in enumerate(feats_lyr1):
    for j, feat2 in enumerate(feats_lyr2):
        if feat1.geometry().intersects(feat2.geometry()):
            area1 = feat1.geometry().intersection(feat2.geometry()).area()
            area2 = feat2.geometry().area()
            crit =area1/area2
            if crit > 0.9:
                selected_feats.append(feat2)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.