2

I have a script that takes an input feature class and uses a field within this class to create a definition query for each unique field. A question on how to do this is listed below saving layers for each unique attribute

My Script works when I have one file.

I am now interested in using more than one field variable within the query. Ideally I like to use three.

My Fields are going to be COUNTRY , SPORT , MEDIA

The definition query would be something like

"Country"= 'a_country' AND "Sport"= 'a_sport' AND "MEDIA "= 'a_Media_type'

Now the problem is that this definition will only work in some cases. Not all countries will do a given sport and therefore will not have a media type associated with that sport. Also if a country does have a sport, it might not have that media type. So i need some way of not creating layers if these relationships between the fields do not exist.

At the moment the below code will create layer files for all the possible examples, many of which don't exist. See code below

import arcpy
from arcpy import env


# This is where the environment is being set , and the file is being picked up from

location = r"U:\Stage_Area\GIS\Users\Team_Members\TWATER\DATA_SPORTS.gdb"

arcpy.env.workspace = location

# the feature class 
Source_File = "OVERVIEW"

# Field to be used 
User_Feild_1 = "COUNTRY"
User_Feild_2 = "SPORT"
User_Feild_3 = "MEDIA"

## This is the part where a seperate list of unique values are being created for each feild 


The_list_User_Feild_1 = [row[0] for row in arcpy.da.SearchCursor  (Source_File,User_Feild_1)]
# All of the values are listed and they need to be stripped down to only unique   ones , which can be used within the lopp 
Unique_list_1 = set(The_list_User_Feild_1)
print Unique_list_1

 The_list_User_Feild_2 = [row[0] for row in arcpy.da.SearchCursor(Source_File,User_Feild_2)]
 # All of the values are listed and they need to be stripped down to only unique ones , which can be used within the lopp 
 Unique_list_2 = set(The_list_User_Feild_2)
 print Unique_list_2


 The_list_User_Feild_3 = [row[0] for row in arcpy.da.SearchCursor(Source_File,User_Feild_3)]
 # All of the values are listed and they need to be stripped down to only unique   ones , which can be used within the lopp 
Unique_list_3 = set(The_list_User_Feild_3)
print Unique_list_3

## The Where_clause builds the defination query inside the last loop 

for value in Unique_list_2:

    for Second_Value in Unique_list_1:

        for thrid_value in The_list_User_Feild_3:




            #This the name the layer that is being temporily created 

            name_of_layer = str(Second_Value)+'_'+ str(value) + '_'+ str(thrid_value)

            out_layer = name_of_layer 

            #MakeFeatureLayer variables
            in_features = Source_File




            where_clause = '"' + User_Feild_1 + '"' + " = " + "'" + Second_Value  + "'" + " AND " + '"' + User_Feild_2 + '"' + " = " + "'" + value + "'" + " AND " + '"' + User_Feild_3 + '"' + " = " + "'" + thrid_value + "'"

            print '\n',where_clause
            arcpy.env.workspace = r"U:\Stage_Area\GIS\Users\Team_Members\TWATER\DATA_SPORTS.gdb"

            try:
            # Execute MakeFeatureLayer
                arcpy.MakeFeatureLayer_management(in_features, name_of_layer, where_clause)

             # Execute SaveToLayerFile
                ## this is the location where i like the files to be saved out 
                arcpy.env.workspace = r"U:\Stage_Area\GIS\Users\Team_Members\TWATER\DATA_SPORTS"
                arcpy.SaveToLayerFile_management(name_of_layer, out_layer, "ABSOLUTE")
            except:
                    print arcpy.GetMessages()

I was wondering some had some approaches, ideas of working with these variables. I found a similar post here.

This has a definition, but it seems to be specific to the tool it's using. Also i was struggling to understand the concept

similar question

2

I would add an if statement and a GetCount_management before your SaveToLayerFile_mangement. If the feature layer has no records, you can skip the iteration.

Maybe:

if int(arcpy.GetCount_mangement (name_of_layer).getOutput (0)) == 0:
    continue
1

I Was able to find Solution. In my Original code I was three separate list that were created from a search cursor. Each of these list consisted of a unique values within the fields. But really what I am interested in is the unique value of three fields combined

So instead of doing three separate Search cursor's

I did one but extracted the three fields that I was interested in. I then appended these cursor values to a list. Made a unique set for this list.

Once I had the unique list. I could iterate over this and then call the separate fields for use in my Layer name and Where_clause

fc = r"U:\Stage_Area\GIS\Users\Team_Members\TWATER\DATA_SPORTS.gdb\data"
fields = [User_Feild_1, User_Feild_2 ,User_Feild_3]

List_of_all_values =[]
with arcpy.da.SearchCursor(fc, fields) as cursor:
for row in cursor:
        #print("{0}, {1}".format(row[0], row[1]))
        curse = str(row[0]), str(row[1]), str(row[2])
        #print curse
        List_of_all_values.append(curse)
        del curse
         #print List_of_all_values



 Uniquelist = set(List_of_all_values)





# Getting the list out 

for items in Uniquelist:




    name_of_layer = str(items[0])+ '_' + str(items[1])+ '_' + str(items[2]) 

    out_layer = name_of_layer 

    #MakeFeatureLayer variables
    in_features = Source_File



    where_clause = '"' + User_Feild_1 + '"' + " = " + "'" + (items[0])  + "'" + " AND " + '"' + User_Feild_2 + '"' + " = " + "'" + (items[1])  + "'" + " AND " + '"' + User_Feild_3 + '"' + " = " + "'" + (items[2])  + "'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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