arcpy: maximum of a field of a feature class

386
11
04-26-2019 05:34 AM
RamB
by
Occasional Contributor II

Hi

I would like to get the maximum of a field in a feature class.  If no features are selected, I get the right value. But if some features are selected, I am getting the maximum of a field from within these selected features. However, I would like to get maximum of a field from the whole table i.e., from both selected and un-selected features. 

how may I do it?

Thank you

Reply
0 Kudos
11 Replies
DanPatterson_Retired
MVP Esteemed Contributor
in_fc = 'C:/Your_spaceless_path/CoordGeom.gdb/Polygons'

arr = arcpy.da.TableToNumPyArray(in_fc, 'Shape_Area') # pick a field

# nan functions account for nulls, cast to the dtype of the field using astype
np.nanmax(arr.astype('float'))

155.0
Reply
0 Kudos
RamB
by
Occasional Contributor II

Thank you, may be I was not clear. Your code gives same answer as mine. Let me try again. 

Imagine you have 3 features [0,1,2] in a fc. They have a income field which is an integer [10,12,14] respectively . 

case1: I have selected no feature selected, I ask for max of fc , I get 14 >> correct

case2: I have selected features [0,1], i ask for max of fc , I get 12 >> incorrect, I still want 14, because I am asking max of fc, not max of selection of fc. 

case3: I have selected features [1,2], i ask for max of fc, I get 14, >> correct

I hope it clarifies a bit?

Reply
0 Kudos
RamB
by
Occasional Contributor II

I mean, I am asking max of income field in fc.

Reply
0 Kudos
JoeBorgione
MVP Esteemed Contributor

Just need to ask: have you actually looked at the values of your selected versus non-selected and done the summation manually?  Sounds like one of your fields is 0 or None value.  What is your objective of comparing your selected sum versus the non-selected?  If you know the sum of the income field without any selections, why not write that value to a variable if you need to make comparisons to it?

can't wait to retire....
Reply
0 Kudos
RamB
by
Occasional Contributor II

User selects few features, sets their income to max(income in whole of  fc) +1 , so now the max of income of whole fc has changed. User goes to another area in the extent, selects a few polygons, sets their income to max(income in whole of fc) + 1, so the max of income in the whole fc has changed again. This continues till all polygons are finished.

Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Just use a Describe object to retrieve the underlying feature class of the feature layer.

featlyr_name = "rangerdistrict" # name of existing feature layer
fld_max = "GIS_ACRES" # field for determining maximum value

max(fld for fld, in arcpy.da.SearchCursor(featlyr_name,fld_max))

fc = arcpy.Describe(featlyr_name).featureclass.catalogPath
max(fld for fld, in arcpy.da.SearchCursor(fc,fld_max))
DanPatterson_Retired
MVP Esteemed Contributor

My answer gives you the maximum whether you have a selection or not.

If you need that maximum, that is the way to get it without worrying about selections or not.

If you need maximum-ish (ie there is a selection) then work with arcpy

If you want the best of both worlds, they you can work with both approaches

Reply
0 Kudos
RamB
by
Occasional Contributor II

would your solution of accessing a disk file be affected by:

1. lock imposed by arcmap

2. hard-coded path on the file system?

Reply
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

If you see the requirements for the function, it just requires the path to the featureclass's table.  Locks aren't for reading, only modifying a featureclass.

You could try it and report back