1

I'm new to Python, but know this tool can solve my problem. I have a large dataset and I want to extract layers based on the attributes of two columns.

For example, my dataset has columns A-N. In this exercise, columns D and C are of relevance. These columns both have variables ranging from 1-9. I want to loop through this data set and extract every combination based on these two columns (D=1 and C=1, D=2 and C=1, D=1 and C=2, etc.).

I assume I need to use a combination of the 'select by attribute' tool and 'make a feature layer' tool, but am having trouble putting together the two.

Can you provide a basic script structure for this solution?

closed as off-topic by PolyGeo Apr 10 '18 at 22:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Dissolve them using these 2 fields. – FelixIP Apr 10 '18 at 19:25
  • Make feature layer is temporary; do you want a more permanent extract? – smiller Apr 10 '18 at 19:26
  • @TVEC_GIS Do you know the values for C and D in advance, or do you need to determine this from the dataset during the loop? – smiller Apr 10 '18 at 19:31
2

Here is an untested and very dirty method that uses itertools.permutaions to generate a where clause for every combination of values for fields C and D (if they are integers 1-9 and don't contain null values). Since there were a bunch of details not explained I just took a guess. Sorry if it's not what you are looking for. I am also not sure of the where clause statements validity.

import itertools, arcpy

#if the only values available in both coloumns are a integers 1-9
numbers = [1,2,3,4,5,6,7,8,9]

#generates a list of lists for every combination of the two fields
possibleCominations = [x for x in itertools.permutations(numbers, 2)]

out_path = "pointFeatureTest.gdb"

#builds a list of the where clauses
where_clauses = []
for combo in possibleCominations :
    where_clauses.append("D = {D} AND C = {C}".format(D=combo[0],C=combo[1]))

lyr_name = 'featureClass'
arcpy.MakeFeatureLayer_management (in_features, lyr_name)
for whereClause in where_clauses:
    #formats the where clause so that it can be used as the feature class name
    fc_name = whereClause.replace(' ','_').replace('=','eq')
    print fc_name
    arcpy.FeatureClassToFeatureClass_conversion (lyr_name, out_path, fc_name, where_clause = whereClause)

I have not tested this and did it from memory so I don't guarantee it to work.

Also, you could change it so that the where clause is with the arcpy.MakeFeatureLayer_management and then get a count with arcpy.GetCount_management before exporting so that you aren't exporting an empty feature class if the combination of C and D doesn't exist. Like below:

 import itertools, arcpy

    #if the only values available in both coloumns are a integers 1-9
    numbers = [1,2,3,4,5,6,7,8,9]

    #generates a list of lists for every combination of the two fields
    x = [x for x in itertools.permutations(numbers, 2)]

    out_path = "pointFeatureTest.gdb"

    #builds a list of the where clauses
    where_clauses = []
    for list in [x for x in itertools.permutations(list1, 2)]:
        where_clauses.append("D = {D} AND C = {C}".format(D=list[0],C=list[1]))

    lyr_name = 'featureClass'

    for whereClause in where_clauses:
        #formats the where clause so that it can be used as the feature class name
        lyr = arcpy.MakeFeatureLayer_management (in_features, lyr_name,where_clause = whereClause)
        if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:
            fc_name = whereClause.replace(' ','_').replace('=','eq')
            print fc_name
            arcpy.FeatureClassToFeatureClass_conversion (lyr_name, out_path, fc_name)
        #cleanup
        del lyr

Not the answer you're looking for? Browse other questions tagged or ask your own question.