Thursday 22 September 2016

arcpy - definition query with 3 plus variables to loop with python and save layer files


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



Answer



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

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...