Geographic Information Systems Asked on December 3, 2021
I am very rusty/inexperienced in Python.
I would like to create a more compact script that prints min and max values of each numeric field from a feature class. Right now I have the code below repeated for each field that contains numeric values.
How would I code this to identify if a field has numeric values and then print the min and max values to the python shell or even better to a .csv/text file?
import arcpy
data = "C:/Workfolder/Input.gdb/FeatureClass"
listname = []
cursor = arcpy.da.SearchCursor(data, "field1")
for row in cursor:
listname.append(int(row[0]))
print 'Field1_min, {0}'.format(min(listname))
print 'Field2_max, {0}'.format(max(listname))
I think one of the efficient options would be using where
and sql_clause
arguments of arcpy.da
, for example:
field_to_find_min_max = "MyFieldName"
min_value = arcpy.da.SearchCursor(data, field_to_find_min_max, "{} IS NOT NULL".format(field_to_find_min_max), sql_clause = (None, "ORDER BY {} ASC".format(field_to_find_min_max))).next()[0]
max_value = arcpy.da.SearchCursor(data, field_to_find_min_max, "{} IS NOT NULL".format(field_to_find_min_max), sql_clause = (None, "ORDER BY {} DESC".format(field_to_find_min_max))).next()[0]
This also makes sure the only non-NULL values are processed (where clause).
Answered by fatih_dur on December 3, 2021
Using the list is clever, or at least it seems until you get to a very large feature class. Instead of the list:
import arcpy
data = "C:/Workfolder/Input.gdb/FeatureClass"
cursor = arcpy.da.SearchCursor(data, "field1")
FirstRecord = True
for row in cursor:
if FirstRecord:
FirstRecord = False
MinValue = int(row[0])
MaxValue = int(row[0])
else:
MinValue = min(int(row[0]),MinValue)
MaxValue = max(int(row[0]),MaxValue)
print 'Field1_min, {0}'.format(MinValue)
print 'Field2_max, {0}'.format(MaxValue)
Or using Summary Statistics:
import arcpy
data = "C:/Workfolder/Input.gdb/FeatureClass"
arcpy.Statistics_analysis(data,r"c:WorkFolderInput.gdbStatTable",[["Field1","min"],["Field1","max"]])
cursor = arcpy.da.SearchCursor(r"c:WorkFolderInput.gdbStatTable",["Field1_min","Field1_max"])
row = cursor.next()
print "Field1 Min = %s, Field1 Max = %s" % (str(row[0]), str(row[1]))
del row
del cur
arcpy.Delete_management(r"c:WorkFolderInput.gdbStatTable")
Be careful that field1 is, or can be converted to, an integer. If it's a date or string field you're gonna have problems!
Answered by Michael Stimson on December 3, 2021
Another way to calculate the minimum is to use the SummaryStatistics function.
To iterate through the fields, use the ListFields function:
# Create a list of fields using the ListFields function
fields = arcpy.ListFields(feature_class)
# Iterate through the list of fields
for field in fields:
if(field.type == 'Integer' or field.type == "Double"):
#calculate the minimum using SummaryStatistics
Answered by Stephen Lead on December 3, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP