ZoomToLayer does not work as expected

2855
7
03-27-2012 05:03 AM
by Anonymous User
Not applicable
Original User: Kaspatoo

Hello,

I implemented a ZoomToLayer method.
I did like this:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//00480000016r000000

There its done this way:
      activeView.Extent = layer.AreaOfInterest;
      activeView.Refresh();

The layer is from the selectedItem in TOC.
In my case I still have Layer throuh a search.
Imap.get_Layers()

using
layer.AreaOfInterest
ArcMap.Document.ActiveView

Later my implementation:
        public static void zoomTo(IEnvelope extent, IActiveView activeView)
        {
            activeView.Extent = extent;
            activeView.Refresh();
        }


Well theres is some kind of "zoom" but not the right one. Because it doesnt matter how many features are in this layer, the zoom is always done to the same scale.
After I did my zoom and then clickling right in TOC on this layer and chose "ZoomToLayer" ArcMap zooms perfectly.
I guess the problem is ILayer.AreaOfInterest. Because its describbed as "The default area of interest for the layer.". Well if the featureClass changes, this layer is pointing to and now there are less features and the overall area is much smaller than before in my mind the area of Interest shrinked. But I could imagine that ArcMap is not thinking this way and is still holding the maximum are it ever had for ever.
But then I wonder why the arcMap function ZoomToLayer is able to handle this.

Maybe an additional information. I am adding features to the specific fetatureclass but only the new added ones will be shown by the layer due to a definition query. thats why I am thinking of the max area ever because the features still exists in featureclass but not in the layer.

Hope someone can help me. Thanks.

my zoom:
[ATTACH=CONFIG]13022[/ATTACH]
ArcMaps zoom:
[ATTACH=CONFIG]13024[/ATTACH]
Reply
0 Kudos
7 Replies
GeorgeFaraj
Occasional Contributor III
If you are zooming to an area based on features selected (or some other criteria that are marked before you begin the zoom) then what you do is to loop thru the features and build a new envelope with the union of all the selected feature shapes (IEnvelope.Union().)  When you have that envelope then you expand it (IEnvelope.Expand()) to ensure a surrounding buffer and then set the ActiveView.Extent and Refresh.
Reply
0 Kudos
by Anonymous User
Not applicable
Original User: Kaspatoo

yes, for single feature im still doing this and this works fine.
But I want to implement exactly the logic of what ArcMaps "ZoomToLayer" does.
The layer is in the TOC which I am searching as described above and which uses a DefinitionQuery.
Reply
0 Kudos
NeilClemmons
Regular Contributor III
One way to do this is to query the feature layer (use IFeatureLayer.Search so that it respects the definition query) and add the feature geometries to a geometry bag.  Then zoom to the envelope of the geometry bag.  You can also use IEnumGeometryBind together with ITopologicalOperator.ConstructUnion to union the features together, after which you would zoom to the envelope of the resulting polygon.
Reply
0 Kudos
by Anonymous User
Not applicable
Original User: Kaspatoo

Hi,

yes this were possible I think altough it is more complex.
I do not really understand why my ZoomToLayer works different from ArcMaps ZoomToLayer.
There must be a difference. I cannot imagine that ArcMap is working like you suggested.
I really would prefer to find the correct solution also for better understanding ArcGIS work.
Reply
0 Kudos
NeilClemmons
Regular Contributor III
Yours doesn't work the same because the AreaOfInterest property returns the spatially referenced extent of the layer, not the minimum bounding extent of the features within the layer.  If you want to do the exact same thing that ArcMap does then you can call the actual ArcMap command.  To do this you'll need to set the context item reference on the current contents view.  The code below is a VBA macro that shows how to do this.  It uses the selected layer in the TOC to set the context item but you can modify it to use any layer reference you want.

Sub ZoomToLayer()
    Dim mxDoc As IMxDocument
    Set mxDoc = ThisDocument
   
    Dim layer As IFeatureLayer
    Set layer = mxDoc.SelectedLayer
   
    mxDoc.CurrentContentsView.ContextItem = layer
    Dim uid As uid
    Set uid = New uid
    uid.Value = "{18DF94D9-0F8A-11D2-94B1-080009EEBECB}:7"
    Dim commandItem As ICommandItem
    Set commandItem = Application.Document.CommandBars.Find(uid)
    commandItem.Execute
End Sub
Reply
0 Kudos
by Anonymous User
Not applicable
Original User: Kaspatoo

Hi thanks for this,

a further solution.
But one question any more.
Do you not know what exactly this Button does internally or are you not allowed to tell internal functionality (of this button)?.
Well I just say because in my mind the best and clearest programming were to implement the right code directly and not reference other buttons (by which this implementation loses portability) or implementing some code which runs over several corners.

But thanks for your help.

edit:
another question to the featureLayer.Search
when I use a queryFilter of null will I get all features in featureClass or only the shown ones in the layer (so is the query defininition still expected or have I to add it in the query filter?)
Reply
0 Kudos
YuanLiu
Occasional Contributor

For whoever having similar problem, IGeodataSet.Extent (http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//0025000003m6000000 ) might be interesting

To update the extent, we can use IFeatureClassManage.UpdateExtent (http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#/IFeatureClassManage_Int... )