Select to view content in your preferred language

Counting Features in a Feature Class with the Field Calculator using Python

3287
3
01-21-2013 03:34 AM
by Anonymous User
Not applicable
Original User: max1987

Hi experts,

first things first: I'm a Python-noob, so keep the answers as simple as possible 😉

My problem is:
I have a Shapefile that represents Drill-Core-Data. The column "UK_GES" represents the Z-value for a geological layer.
I have to interpolate missing values between existing values. Until now, I'm working on the DBF-file in Excel (as shown in the attached png). To get the interpolation done, I use a function that includes the Excel-standard-function "COUNT" (in german: "ANZAHL")... As you may know, importing Excel-Data back into ArcGIS sometimes leads to errors especially when you're working with confusingly huge Excel-files (in most cases these errors are caused by the operator, not by the system...). So I'd like to implement the function (that is shown in the attached png) directly in ArcGIS to avoid conversion-steps and the mentioned  operator-made errors.
For that, I would like to use the Field Calculator.

What I need:
I need a Python-script for an linear interpolation that can be scripted and used with the Field Calculator. It should work only for selected rows/Featuers. It should express the following:

[ A + ( C - A ) ] / [ COUNT ( A to B ) ]

(explanation: A,B,C are rows/Features in a selected column; A= Z-value of the 1st Feature in a Feature Class; B= Z-value of the 2nd Feature in a Feature Class; C= Z-value of the 3rd Feature in a Feature Class)

The attached png should show wich values have to be interpolated (the selected ones).

I think this shouldn't be that challanging for Python-Pro's 😉 For me it is, alas...
Thanks in advance!!!
0 Kudos
3 Replies
T__WayneWhitley
Honored Contributor
Yes, that is easy to do, particularly if you know where the known values fall (where in the sort) in your table.
If you don't know that beforehand, then an initial cursor processing 'sweep' will be necessary to 'gather' the values, probably load the key:value pair in a Python dictionary, COUNT:Z-VALUE.  Then calculate the missing values and load them on a 2nd sweep.

This is simple math - are you somewhat familiar with cursors?
0 Kudos
by Anonymous User
Not applicable
Original User: max1987

Hi Mr. Whitley,
thank you so much for your reply!
Yes I'm theoretically familiar with cursors, but I haven't used them practically so far...
I don't think I'll need them because: An operater is needed (and wanted) anyway to identify the "known values" manually because he has to decide wether an automatic linear interpolation between "known values" is appropriate or an manual assignment of values for "unknown values" is necessary.
By the way, the position of the known values in the table is random.
As I've said, I'm a python-greenhorn 😉 (The only thing I've done so far is an classification via IF-ELIF...)
I've attached another screenshot and a test-shapefile as an example:
Since my problem is apparently easy to fix for a python-pro, I would be very happy if you could tell me what I have to type in the Field Calculator mask to apply an linear interpolation to the three selected features/rows (field "Z"). Maybe I'll understand how the operators work when I have the script.

In the example the results for the "Z"-field should look like this:
The 1st row has the "known value" "1". The 5th row has the "known value" "5".
The 3 "unknown values" between the mentioned rows should have the values "2", "3" and "4".  
Of course, the calculation should also work for data-type "float"/"double" ...
Thanks in advance!
0 Kudos
T__WayneWhitley
Honored Contributor
Well, here is some 'ugly' python code that does the basic interpolation for a single double field in a feature class, just for use as an example - so experiment with it as you wish, understanding it contains no error trapping.  It does function, not optimally and it has some significant limitations, but since you have not much python coding experience, maybe you'll find it interesting - have to enter 2 parameter variables as instructed, one for the feature class path, the other for the field name.  Values to interpolate by have to be entered on the first and last records (one of the aforementioned limitations).  See below, you can copy/paste to a text file, saving with a py extension in order to execute with your installed Python:
import arcpy
fc = r'< enter pathname to fc, e.g. C:\theGDB.gdb\fcName >'
field = '< enter your double field name >'

rows = arcpy.SearchCursor(fc)
i = 0
index = []
vals = []
for row in rows:
        val = row.getValue(field)
        if val != None and val != 0.0:
                vals.append(val)
                index.append(i)
                i = 0
        else:
                i += 1


rows = arcpy.UpdateCursor(fc)
j = 1
i = -1
for row in rows:
        val = row.getValue(field)
        if val != None and val != 0.0:
                i+=1
                j=1
        else:
                inter = vals + float(j)*((vals[i+1] - vals)/float(index[i+1]+1))
                j += 1
                row.setValue(field, inter)
                rows.updateRow(row)

del row, rows


Note:
Replace "< enter pathname to fc, e.g. C:\theGDB.gdb\fcName >" with your feature class pathname.
Replace "< enter your double field name >" with your field name containing the double values to interpolate.
When replacing params, remove all contained within the single quotes but do not remove the single quotes.
0 Kudos