5

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()
6

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.

  • 3
    You could skip the ListLayers and just issue arcpy.SelectLayerByAttribute_management("Parcels", "NEW_SELECTION", whereClause) – Jason Scheirer May 27 '11 at 21:20
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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