rbasaria

use python to create definition query

Discussion created by rbasaria on Mar 22, 2011
Latest reply on Jul 16, 2013 by rzufelt
Hi. Ive been trying to figure out how to accomplish this, but Ive run into some trouble. Im not sure if I am coding this correctly. My goal, overall, is to select features from a layer that intersect with features from another layer. The features in this second layer need to be defined by a definition query that I do not want to manually change. The selected features will then be exported to a new shapefile. I have been able to get the script to select and export, but I cannot get it to create the definition query. Any assistance will be greatly appreciated. Here is the code that I have, so far. Thanks!!

import arcpy

mxd = arcpy.mapping.MapDocument("C:/Rafiq_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:/Rafiq_GIS/PYTHONTESTING/2011/2011_CountyComm/Data/Orange/Or_Com1.shp")

Outcomes