Classify values

339
13
09-02-2011 07:47 AM
MatthiasAbele
New Contributor II
Hallo,

I ve got a column with values between 0 - 2. I would like to classify the values in classes between 1 - 5 according to the following scheme. The class for each value should be written in another column.

Class 1   0.0 - 0.2
Class 2   0.2 - 0.4
Class 3   0.4 - 0.6
Class 4   0.6 - 0.8
Class 5   0.8 - 1.0
Class 4   1.0 - 1.2
Class 3   1.2 - 1.4
Class 2   1.4 - 1.6
Class 1   1.6 - 9.0

How can I do this with python for several feature classes?

Thanx for any hints,

Matthias
Tags (2)
0 Kudos
13 Replies
MatthiasAbele
New Contributor II
Hi StacyRendall,

two things I have recognized:

- the class breaks must contain a last element, which is the upper limit of all values, and so have one element more than the class values. Your code checks if both are equal; if not it interrupts the script...so I changed it to and it is working....

if len(classBreaks)- 1 != len(classValues):
 arcpy.AddError('Class Values and Class Breaks must be the same length.')


- if I choose a geodatabase or feature dataset and there a features inside which do have the value field but not the class field, the code stops.(also for those which have both fields) If I delete those with only a value field, the code works again. What could be the reason?

Yours,

Matthias
0 Kudos
StacyRendall1
Occasional Contributor III
Hi Matthias,

Good spotting on the first point.

Not sure what you mean on the second one. Every feature class in your workspace needs to have both fields, or else it will fail.
0 Kudos
MatthiasAbele
New Contributor II
Hi StacyRendall,

why does every feature in the gdb must have both fields? Is it possible to configure it in a way, so that it just scips features which do not have to given fields?

Yours,

Matthias
0 Kudos
StacyRendall1
Occasional Contributor III
Sure, with ListFields() and then a conditional to check if the field is actually in the feature class...

Change the last bit of code (from for fc in listFCs onwards):
for fc in listFCs:
        fcFieldList = arcpy.ListFields(fc)
        if (valueField in fcFieldList) & (classField in fcFieldList):
  rows = arcpy.UpdateCursor(fc)
  for row in rows:
   row.setValue(classField, calc(row.getValue(valueField))) # looks complex, but is just getting the value from valueField, passing it to CALC then writing the output to the classField
   rows.updateRow(row)

  try: del row
  except NameError: pass
         del rows


If the feature class doesn't have both fields, it will skip the file. It would also be possible to add the fields, if that was what you wanted.