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.

Joseph
76.6k8 gold badges173 silver badges286 bronze badges
asked Mar 9, 2016 at 4:21

3 Answers 3

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.

answered Mar 9, 2016 at 4:36
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().

answered Mar 9, 2016 at 6:16
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

answered Mar 9, 2016 at 6:50

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.