Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to select a feature using SQL before zooming to it in python. What I'm looking for are replacements for the commented lines in this code:

import arcpy
parcel_num = "123456"
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
# df.selectFeatures("SELECT * FROM LandRecords.GISDBA.Parcels "
#                   "WHERE PARCEL_NUM='%s'" % parcel_num)
share|improve this question

Assuming you have "LandRecords.GISDBA.Parcels" as a layer in your map document and the layer name is set to "Parcels" you should be able to do this:

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
parcels = arcpy.mapping.ListLayers(mapDoc, "Parcels", df)[0]

whereClause = "PARCEL_NUM ='%s'" % parcel_num
arcpy.SelectLayerByAttribute_management(parcels, "NEW_SELECTION", whereClause)
df.extent = parcels.getSelectedExtent(True)
df.scale *= 1.5

Note: my experience so far is changing the map extents automatically refreshes the map. I only call arcpy.RefreshActiveView() when I manually manipulate the df.scale.

share|improve this answer
You could skip the ListLayers and just issue arcpy.SelectLayerByAttribute_management("Parcels", "NEW_SELECTION", whereClause) – Jason Scheirer May 27 '11 at 21:20

I'm not up to scratch with the ArcPy module (not using version 10 yet) but previous python code would require you to get a handle on a layer then do a select on it. I grabbed this from the online help:

import arcpy 
arcpy.MakeFeatureLayer_management ("C:/data/data.mdb/states", "stateslyr")
arcpy.SelectLayerByAttribute_management ("stateslyr", "NEW_SELECTION", " [NAME] = 'California' ")

Click here to go to online Help

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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