Select to view content in your preferred language

change definition query on layers in mxd file

797
1
03-08-2011 10:10 AM
RafiqBasaria
Emerging Contributor
Hi everyone. Ive done a bunch of searches, and have gotten much farther than I would have otherwise, but I am still getting stuck. I am trying to create a script that will change the definition query on a speicific layer in an MXD file. The script will then select by location, intersected with the new features from the layer that the definition query was changed for. Then these selected features will be copied to a new feature class or shapefile. Here is the code I have at this point. Thank you in advance for any help!!

import arcpy

mxd = arcpy.mapping.MapDocument("C:/Rafig_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/DataPrep.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == "CountyCommission_TriCounty":
        layer.definitionQuery = "[DISTRICT] = '1' AND [COUNTY] = 'ORANGE'"

arcpy.SelectLayerByLocation_management("BusRoutes_Ridership","INTERSECT","CountyCommission_TriCounty","","NEW_SELECTION")

arcpy.CopyFeatures_management("BusRoutes_Ridership","C:/Rafig_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/Or_Com1.shp")
Tags (2)
0 Kudos
1 Reply
RafiqBasaria
Emerging Contributor
Additionally, how can I make it so that the layer having the definition query changed, the definition query itself, and the output are all user defined parameters? (I will be changing the definition query mutliple times, and I will be querying 2 different layers separately. Each selection will have its own output, for a total of something like 30 layers at the end.) Thank you.
0 Kudos