I have a shapefile that contains a text field "Company" that i would like to use to run a definition query to create a layer file. I want to be able to create a layer for each company.
I had a look around and I found Creating new layers using ArcPy to define Definition Query for each field value? which seems to detail the process.
In my code i am trying to write out a layer file for each unique value that determines a definition query. My source file contains a shapefile of shops , within the shp file there is a field that contains the company that owns the shop. I want to run a definition query on each unique company contained within the company field
i have been able to get the unique companies into a list( as pointed out , i needed to use a set to get the unique values of the search curosr) , that i would like to loop through. My code is as follows
import arcpy
from arcpy import env
# This is where the file is being picked up from
arcpy.env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"
# The file to be processed
Source_File = "SDE_COMPANY_LIST.shp"
Company_Bulk_Company_List = [row[0] for row in arcpy.da.SearchCursor(Source_File,"COMPANY")]
UniqueCompany = set(Company_Bulk_Company_List)
print UniqueCompany
# Getting the list out
for company_query in UniqueCompany:
env.workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"
out_layer0 = str(company_query)+"lyr"
in_layer = out_layer0
out_layer = in_layer +"lyr"
#MakeFeatureLayer variables
in_features = "SDE_COMPANY_LIST.shp"
where_clause = '"COMPANY" = \'company_query\''
print where_clause
workspace = "S:\\AppsData\\GIS\\PROJECT\\COMPANIES\\SHAPEFILES\\"
try:
# Execute MakeFeatureLayer
arcpy.MakeFeatureLayer_management(in_features, out_layer0, where_clause, workspace)
# Execute SaveToLayerFile
arcpy.SaveToLayerFile_management(in_layer, out_layer, "ABSOLUTE")
except:
print arcpy.GetMessages()
I the layers are being created , but there is a problem with the definition query as what i am in getting inside the defination box for the layer is
company = ' company_query'
where is i like to see company = ' variable that is inside company_query'