use python to create definition query

23981
13
03-22-2011 05:46 AM
RafiqBasaria
New Contributor II
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")
Tags (2)
0 Kudos
13 Replies
JonathanCusick
New Contributor II
Ah I didn't realize I had to do so. That's all of the code referring to the query. The rest of it begins to run the clip function along with others down the line. So I have to define the mxd that this is happening in in order to call upon the attributes of the Ground Zones feature class? Is there a way to run this just in Catalog?

I tried defining the mxd as shown in the post at the top of the thread, but am still having problems. Whenever I try to set mxd to the full pathname of the map, I get an error saying Invalid MXD Filename. When just using the keyword "CURRENT" to define the mxd, it at least ran, but then I got that same 'str' object has no attribute 'name'.

import arcpy
from arcpy import env
import os

mxd = arcpy.mapping.MapDocument("CURRENT")
inzones = (.../RevisedGroundZones)
for lyr in inzones:
     if lyr.name == "RevisedGroundZones":
          lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''
0 Kudos
RhettZufelt
MVP Frequent Contributor
Ah I didn't realize I had to do so. That's all of the code referring to the query. The rest of it begins to run the clip function along with others down the line. So I have to define the mxd that this is happening in in order to call upon the attributes of the Ground Zones feature class? Is there a way to run this just in Catalog?

I tried defining the mxd as shown in the post at the top of the thread, but am still having problems. Whenever I try to set mxd to the full pathname of the map, I get an error saying Invalid MXD Filename. When just using the keyword "CURRENT" to define the mxd, it at least ran, but then I got that same 'str' object has no attribute 'name'.

import arcpy
from arcpy import env
import os

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "RevisedGroundZones", df)[0]
lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''


Think you just need to define the layer.  If it is just one layer, no need to iterate.

R_
0 Kudos
JonathanCusick
New Contributor II
Thanks for the help. I tried running the script with the updates you made, and while the script runs all the way (doesn't receive any error messages), it doesn't actually apply the definition query. When I look at the newly clipped layer in ArcMap, it is clipped to the entire Ground Zones layer as opposed to just ground zone 1 or 2 for example.

Also, what is the function of the [0] at the end of the 6th and 7th lines?
0 Kudos
RhettZufelt
MVP Frequent Contributor

import arcpy
from arcpy import env
import os


mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "RevisedGroundZones", df)[0]
lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''



So, similar to your code, could do it this way as well if you wanted to iterate through the list instead of just grabbing the first item (normally first of one):

for lyr in arcpy.mapping.ListLayers(mxd, "*"):
     if lyr.name == "RevisedGroundZones":
          lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''



The "List"Layers and "List"DataFrames create a list of matching objects, so, all Dataframes with name "Layers" would be appended to the df list, and all layers, within the specified dataframe, that match "RevisedGroundZones" would be appended to the lyr list.  However, putting the [0] actually makes it grab the first item in the list.  I am assuming here that you only have one dataframe named "Layers" and one layer within that named "RevisedGroundZones".  That way, there would only be one item in each list, so putting the [0] on the end grabs it without having to iterate through a list of one.

I have successfully ran this and it applies the definition query (of course, this is for text field, if your RevisedGroundZones attribute is a number field, the quotes need to dissapear from the definition query.

So, if you only have one dataframe name "Layers" (or "layers" as list function here is not case sensitive), and within that only one layer named RevisedGroundZones, it should be applying the definition query.  If you have more than one with this name, it would only apply to the first on in the list.  Would then need to iterate through if you wanted it applied to all with that name.

Of course, you can always throw a mxd.save() in there after the definition query, then open the mxd with arc map and see what the actual def query being applied is (and if there are any drawing errors related).

Once you get the def query working, then let it move on to the clipping.

R_
0 Kudos