Select to view content in your preferred language

Zoom to Selected Features Extent in a Layer within a Map Service

992
5
08-21-2011 07:21 PM
HenryKo
Deactivated User
Hi,

I have a map document, and it has layers coming from non-cached map services. The map services can have features selected.

How do I zoom to the selected features for a layer (i.e. a map service) in arcpy?

The script, map services and map document are all ArcGIS 10 SP2.

Thanks.
Tags (2)
0 Kudos
5 Replies
StephanieWendel
Esri Contributor
This can be done by using the Method of zoomToSelectedFeatures () for data frames or Layer.getSelectedExtent for layers.

For more information, see this link on the data frame method: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/DataFrame/00s300000003000000/

and this link for more on the layer method:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/

There is a short sample and explanations that should help give you an idea of how each works.
0 Kudos
HenryKo
Deactivated User
Thank you for your reply.

I created a very simple MXD with a base map (a cached map service), and a non-cached map service (which it has a layer called "Rail Roads"). I ran the code below and it crashes. Is this a known issue?



import arcpy
m = arcpy.mapping.MapDocument("test3.mxd")
d = arcpy.mapping.ListDataFrames(m)[0]
lyrs = arcpy.mapping.ListLayers(m, "", d)
for l in lyrs:
     print l.name
     if (l.name == "Rail Roads"):
         d.extent = l.getExtent()
d.zoomToSelectedFeatures()
0 Kudos
StephanieWendel
Esri Contributor
Make sure in this line you use the full path the the map document:
m = arcpy.mapping.MapDocument("test3.mxd")


For example:
m = arcpy.mapping.MapDocument(r"C:\Data\MapDocs\test3.mxd")


The way you have it right now, it is unsure of where to find that document.
0 Kudos
HenryKo
Deactivated User
Thanks.

I tried it, still have the same results (i.e. the  l.getExtent() part crashes if the layer is part of a non-cached map service).

In fact, I tried it in the Python window within ArcMap with

m = arcpy.mapping.MapDocument("CURRENT")

and it did not work.
0 Kudos
MathewCoyle
Honored Contributor
Shot in the dark, but did you try setting "False" to get the geometric extent instead of symbolized extent?

d.extent = l.getExtent(False)

I doubt that would be an issue, but like I said, shot in the dark.

Another thing, will there always be a selection? There is another method to get an extent of selected features.
d.extent = l.getSelectedExtent()

You could also try getting just the layer you want instead of going through all of them recursively,
l = arcpy.mapping.ListLayers(mxd, "Rail Roads", d)[0]
0 Kudos