Hi All,
I'm new to python scripting and I was wondering if there is a way to select features with a unique combination of values from multiple fields. I would like to take each selection set of features and complete additional geoprocessing tasks on them and move on to the next selection of features with the same field values and repeat.
The closest I have got so far is to produce a list of values that are unique in the feature class. I feel like this is not heading in the direction I would like it to. I am open to running the python in or outside of ArcGIS Pro.
import arcpy
import numpy
fc = "C:\Temp\ProjectModel.gdb\TempAvgs_ExportFeatures1"
arr = arcpy.da.FeatureClassToNumPyArray(fc, ['GeologicUnit', 'Analyte'])
uniqueRows = numpy.unique(arr)
print uniqueRows
Solved! Go to Solution.
The arcpy way of doing this:
import arcpy
fc = "C:\Temp\ProjectModel.gdb\TempAvgs_ExportFeatures1"
fields = ['GeologicUnit', 'Analyte']
unique_combinations = {
row
for row in arcpy.da.SearchCursor(fc, fields)
}
for unit, analyte in unique_combinations:
print(unit, analyte)
# create a layer with all rows of that values combination
sql = f"GeologicUnit = {unit} AND Analyte = {analyte}" # add single quotes around {unit} and {analyte} if those fields are strings
layer = arcpy.management.MakeFeatureLayer(fc, f"{unit}_{analyte}", sql)
# do your geoprocessing on this layer
arcpy.analysis.Intersect([layer, some_other_layer], f"Intersect_{unit}_{analyte}")
concatenate the two fields into a new field (comma and or space separated), then export to an array. Bring over the original fields and any other fields that you want for the analysis
Or, you can do the searchcursor thing, or use SplitByAttribute tool if you want to process as separate files (using the new field as the split field)
Split By Attributes (Analysis)—ArcGIS Pro | Documentation
But if you aren't working with complex geometry functionality, it is sometimes easier to just do the work in numpy (or even pandas). You can always use arcpy's ExtendTable to bring back tabular results into Pro
The arcpy way of doing this:
import arcpy
fc = "C:\Temp\ProjectModel.gdb\TempAvgs_ExportFeatures1"
fields = ['GeologicUnit', 'Analyte']
unique_combinations = {
row
for row in arcpy.da.SearchCursor(fc, fields)
}
for unit, analyte in unique_combinations:
print(unit, analyte)
# create a layer with all rows of that values combination
sql = f"GeologicUnit = {unit} AND Analyte = {analyte}" # add single quotes around {unit} and {analyte} if those fields are strings
layer = arcpy.management.MakeFeatureLayer(fc, f"{unit}_{analyte}", sql)
# do your geoprocessing on this layer
arcpy.analysis.Intersect([layer, some_other_layer], f"Intersect_{unit}_{analyte}")
Thanks Johannes! That worked great!