[GIS] ‘Select by attribute’ using python in ArcMap 10

arcgis-10.0arcmappythonsql

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)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()

Best Answer

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
arcpy.RefreshActiveView()

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.