2

I am new to ModelBuilder and Python scripting.

I am trying to create a model which selects each feature within a dataset, uses a select by location to select all features within a 50m radius, and from the selection results, return the maximum value in particular field.

I have setup a model: Model

And the settings + python script I am using in the Calculate field is: Calculate Field Settings

fc = "Sample_Area_Layer"

field = "nNCC_1"

Cursor = arcpy.SearchCursor(fc)

row = Cursor.next()

MaxValue = 0

CurrentValue = 0

for row in Cursor:

    CurrentValue = row.getValue(field)

    if CurrentValue > MaxValue:

        MaxValue = CurrentValue

    Cursor.next

The results which are being returned are inconsistent. Some have values which are not in the selection, others have values which are not the maximum value of the selected values - so although the Maximum Value may infact be 9 the formula is only picking up a maximum value of 4.

2

Firstly, you shouldn't be using arcpy.SearchCursor. As of 10.1, you can use arcpy.da.SearchCursor which is more efficient.

However, having said that, once you have the selection, could you not just run Summary Statistics? Add that to your model. From the documentation:

When using layers, only the currently selected features are used to calculate statistics.

You'll get another table with the maximum value. You can grab that value and update your field with that value. No need for python.

2

You are skipping half of the records, because

for row in Cursor

automatically fetches the next record. No need to call next(). See examples on the ArcGIS help page on SearchCursor. To solve the problem comment out both lines with next().

1

I had the "Calculate Field" connected to the results of the Select by Location in Model Builder, rather than the individual feature which was originally selected.

When I moved the Calculate Field to the original selected feature, the results were correct.

enter image description here

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.