Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()
share|improve this question

2 Answers 2

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.

share|improve this answer
3  
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

 
discard

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.