1

Could someone tell me how to classify/populate fields with QGIS FieldPyculator Plugin?

In the Attribute Table, I have a field named POPULACAO with population data:

enter image description here

I want to apply a weight to classify these fields. I've installed QGIS FieldPyculator Plugin via repository. Below the Python code to complete this task in ArcGIS 10:


def reclass(POP):

if (POP <= 10000):
return 1
elif (POP > 10001 and POP <= 30000):
return 2
elif (POP > 30001 and POP <= 50000):
return 3
elif (POP > 50001 and POP <= 100000):
return 4
elif (POP > 100001 and POP <= 200000):
return 5
elif (POP > 200001 and POP <= 500000):
return 6
elif (POP > 500001 and POP <= 1000000):
return 7
else:
return 8

In the box below:

reclass( !POPULACAO! )

The ArcGIS 10 Python code works like a charm. Data shapefile: http://bit.ly/xBW8Pz | In FieldPyculator, I did wrong, I know:

enter image description here

Please help me. Jorge Santos

2 Answers 2

2

Well, the error message says that the indentation is wrong. The code is not correctly indented at all. In Python, indentation matters and everything after the first line would have to be indented, as well as all the loops etc.

1

Standard Python indentation is two or four spaces.
Try using the following indented code:

def reclass(POP):
  if (POP <= 10000):
    return 1
  elif (POP > 10001 and POP <= 30000):
    return 2
  elif (POP > 30001 and POP <= 50000):
    return 3
  elif (POP > 50001 and POP <= 100000):
    return 4
  elif (POP > 100001 and POP <= 200000):
    return 5
  elif (POP > 200001 and POP <= 500000):
    return 6
  elif (POP > 500001 and POP <= 1000000):
    return 7
  else:
    return 8

2
  • 1
    Ei!!! Kisses for Anita "@underdarkgis" Graser, hugs for Fezter! Python need to insert more spaces... Sorry for all, boys and girls. In the box below: reclass( !POPULACAO! ) exchange for reclass ( <POPULACAO> ) Commented May 28, 2012 at 23:23
  • Now the FieldPyculator rocks: goo.gl/Zbe5Q Thanks for all, Jorge Santos Commented May 29, 2012 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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