is "zoom to selected feature" possible in arcpy.mapping?

2852
9
12-09-2011 08:04 AM
JoshuaChan
New Contributor III
I'd like to have the user input a value as an argument that would then be used to do a "select by attribute" against a point dataset. Is it possible to "zoom to selected feature" using arcpy.mapping or some other python module?
0 Kudos
9 Replies
JasonScheirer
Occasional Contributor III
Yes, see the zoomToSelectedFeatures method. There are samples there to get you started.
0 Kudos
JeffBarrette
Esri Regular Contributor
You can use the zoomToSelectedFeatures on the DataFrame class:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/DataFrame/00s300000003000000/

or you can use the getSelectedExtent method on the Layer class (and apply that extent to the data frame object).
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/

Jeff
0 Kudos
JoshuaChan
New Contributor III
Thanks. It seemed to work the first time but it zoomed me into the wrong location. Since then I've been trying to trouble shoot but in doing so, my script seems to have gotten broken and I can't get it to work anymore. Now I'm having trouble even with simple things like getting a count of my selected set just to see if I can see how many features are being selected before it zooms in. the GetCount_management returns an error of  'Result' object has no attribute 'getOuput'.  Yet if I copy & paste those exact same lines into a PythonWin interactive window the script returns the correct number of selected features instead of an error message.

Must be Monday. I can't see what's going on here.




You can use the zoomToSelectedFeatures on the DataFrame class:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/DataFrame/00s300000003000000/

or you can use the getSelectedExtent method on the Layer class (and apply that extent to the data frame object).
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/

Jeff
0 Kudos
JoshuaChan
New Contributor III
still couldn't get the zoomToSelectedFeature thing to work. But my coworker did this successfully with dataframe.PanToExtent(lyr.getSelectedExtent)




You can use the zoomToSelectedFeatures on the DataFrame class:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/DataFrame/00s300000003000000/

or you can use the getSelectedExtent method on the Layer class (and apply that extent to the data frame object).
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/

Jeff
0 Kudos
DanielAbera
New Contributor III
Hi Guys,

I was trying to use python for select by attribute using variables from user input. It works fine just for a single variable, but when it comes to more than one variable using the "AND" its giving me error.

here part of the code :
MER  = sys.argv[1]
TWP  = sys.argv[2]
RNG  = sys.argv[3]
SEC  = sys.argv[4]

Expression ="MER= "+MER "AND" "TWP= "+TWP "AND" "RNG= "+RNG "AND" "SEC= "+SEC

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", Expression)

Any idea please ???

Thanks,

Dan
0 Kudos
DanielAbera
New Contributor III
still couldn't get the zoomToSelectedFeature thing to work. But my coworker did this successfully with dataframe.PanToExtent(lyr.getSelectedExtent)



This should work :

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
lyr = arcpy.mapping.ListLayers(mxd, "Layer Name", df)[0]

df.extent = lyr.getSelectedExtent(True)
df.scale *= 1.5
arcpy.RefreshActiveView()
0 Kudos
NobbirAhmed
Esri Regular Contributor
I was trying to use python for select by attribute using variables from user input.


This thread is about 'zoom to selected feature' issue. You have introduced a very different question in it. People who are not interested about 'zoom to selected' will not check this thread out and will not see your question.

If you can, please start a new thread with your question. That will help other users with similar problem also.
0 Kudos
JeffBarrette
Esri Regular Contributor
I would change

Expression ="MER= "+MER "AND" "TWP= "+TWP "AND" "RNG= "+RNG "AND" "SEC= "+SEC

to

Expression ="MER= "+ MER + " AND TWP= " + TWP + " AND RNG= " + RNG + " AND SEC= " + SEC

or

Expression = "MER= {0} AND TWP= {1} AND RNG= {2} AND SEC= {3}".format(MER,TWP,RNG,SEC)


Jeff
0 Kudos
DanielAbera
New Contributor III
Thanks All !!
0 Kudos