-1

I'm trying to create a python script that'll apply a definition query to all 5 feature classes. Each feature class is a condominium floor footprint called "Condo_1st_level", "Condo_2nd_level", etc. Each of these feature classes has a unique field called "SUBCODE" which represents the unique subdivision name. I'd like simply change the "SUBCODE" in this script to display the unique records for all 5 feature classes at once that are depended on that "SUBCODE".

Is this possible?

UPDATE: I was able to create a python script that allows me to apply a definition query to all 5 feature classes in my map document and therefore answering my own question. This script was created using ArcMap (Python version 2). Here's an example of the end result:

mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"ASSESSOR.ASR.CONDO_1ST_LEVEL*")
#replace subcode here
QUERY = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"ASSESSOR.ASR.CONDO_2ND_LEVEL*")
#replace subcode here
QUERY = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"ASSESSOR.ASR.CONDO_3RD_LEVEL*")
#replace subcode here
QUERY = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"ASSESSOR.ASR.CONDO_4TH_LEVEL*")
#replace subcode here
QUERY = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd,"ASSESSOR.ASR.CONDO_5TH_LEVEL*")
#replace subcode here
QUERY = '"SUBCODE" =\'1172\''
layers[0].definitionQuery=query
print layers [0].definitionQuery

Probably not the cleanest, but it works for me.

3
  • 2
    Your question is confusing, Do you want to apply a definition query to show only certain records, or apply symbology to the dataset to represent that data dependant on Subcode? – MacroZED Oct 31 '17 at 17:28
  • 1
    I've edited my question. I don't feel that it was confusing to begin with and certainly doesn't deserves a negative response. Didn't realize how brutal people can be on this site! Just looking for some friendly help. – pac_co Oct 31 '17 at 19:05
  • For questions about code please always include a code snippet that illustrates what you have tried and where you are stuck. – PolyGeo Oct 31 '17 at 20:19
2
import arcpy
# you can copy/paste this into the Python window inside ArcMap
#https://gis.stackexchange.com/questions/28553/add-definition-query-to-layers-in-a-group/28562#28562

#Variables to form definition query
field = '"SUBCODE"'
value = "'ParkView'"
#concatenate query syntax
queryStr = str(field) + "=" + str(value)
#Specify the MXD project (CURRENT), dataframe (Layers)
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#Apply defintion query to each Layer
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if lyr.supports("DEFINITIONQUERY"): 
        lyr.definitionQuery = queryStr
arcpy.RefreshActiveView()
del mxd

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