use python to create definition query

23833
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
RafiqBasaria
New Contributor II
Been working on this more. I chaged a couple of parts, but am now running into the issue of how the layer is seeing the definition query. Since the layer reads it using single and double quotes, python cannot correctly enter the query. Here is the new code.

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":
        lyr.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")


As you can see, the lyr.definitionQuery =  line is not correctly syntaxed. I cannot figure out how to get python to read that entire query definition as a string to insert into the definition query of the layer.
0 Kudos
ChrisMathers
Occasional Contributor III
Just escape some of hte quotes and wrap it in more quotes:

'"DISTRICT" = \'1\' AND "COUNTY" = \'ORANGE\''
0 Kudos
RafiqBasaria
New Contributor II
That worked perfectly. Thanks!! I was unaware that this could be done. Thank you.
0 Kudos
ChrisMathers
Occasional Contributor III
Your welcome. All where clauses and definition queries need to be strings so sometimes escaping is needed to make it work.
0 Kudos
DaveVerbyla
New Contributor III
rbasaria;87366 wrote:
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


Query = "[DISTRICT] = '1' AND [COUNTY] = 'ORANGE'"
arcpy.MakeFeatureLayer(inFC,layername,Query)
0 Kudos
justinperez
New Contributor II
sql = "OWNER=" "'" +  "NuStar GP Holdings LLC" + "'"

That's how I do it where Owner is the field name and NuStar GP Holdings LLC is the value...this is on SQL Server 2008..on Oracle I think it may differ a bit.  If I was going to use a variable for the value it would be something like..

sql = "OWNER=" "'" +  value_variable + "'"
0 Kudos
justinperez
New Contributor II
sql = "OWNER='" +  "NuStar GP Holdings LLC" + "'"

That's how I do it where Owner is the field name and NuStar GP Holdings LLC is the value...this is on SQL Server 2008..on Oracle I think it may differ a bit.

If I want to vary the value...notice how I dropped the double quotes around the value

sql = "OWNER='" +  value_variable + "'"

Note the single quote after Owner= and also inside the last double quotes.
0 Kudos
JonathanCusick
New Contributor II
I've been trying to use a definition query just like rbasaria, and have used the syntax that clm42 suggested, but keep running into trouble. For now, I just want to be able to query out various zones from a larger map, and then run further geoprocessing functions on them.

import arcpy
from arcpy import env
import os

inzones = (.../RevisedGroundZones)
for lyr in inzones:
     if lyr.name == "RevisedGroundZones":
          lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''



When running it from the python window in ArcMap, I get an error message that says AttributeError: 'str' object has no attribute 'name' and when I run it as a script tool in ArcCatalog, I get an error saying that there is no definition Query function for 'lyr'


Any help would be very appreciated, thanks!
0 Kudos
MathewCoyle
Frequent Contributor
I've been trying to use a definition query just like rbasaria, and have used the syntax that clm42 suggested, but keep running into trouble. For now, I just want to be able to query out various zones from a larger map, and then run further geoprocessing functions on them.

import arcpy
from arcpy import env
import os

inzones = (.../RevisedGroundZones)
for lyr in inzones:
     if lyr.name == "RevisedGroundZones":
          lyr.definitionQuery = ' "Grounds_GSC_Zones" = \'2\''



When running it from the python window in ArcMap, I get an error message that says AttributeError: 'str' object has no attribute 'name' and when I run it as a script tool in ArcCatalog, I get an error saying that there is no definition Query function for 'lyr'


Any help would be very appreciated, thanks!


Can you post the rest of your code? How you create the mxd object etc. You need to make a layer object to get the name property from it if you are running this out of process.
0 Kudos