Select to view content in your preferred language

Zoom to selected features

2029
10
06-01-2010 12:26 AM
NehayaArabeiat
Emerging Contributor
Hi Everybody,

I build an ArcGIS Engine Application in vb.net language, I want to select a field in a feature layer and zoom to it, I try the following code but when I click the button it zooms to nothing and the map disappear, any help please??

Shared Sub SelectLayersFeatures(ByVal pFeatureLayer As IFeatureLayer, ByVal WhereClause As String, ByVal pActiveview As IActiveView, ByVal blockclass As IFeatureClass)
        Dim pFeatureSelection As IFeatureSelection = pFeatureLayer
        Dim pfeaturezoomlayer As IFeatureLayer

        If Not pFeatureSelection Is Nothing Then
            Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
            pQueryFilter.WhereClause = WhereClause
            pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

            pActiveview.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            pfeaturezoomlayer = pFeatureSelection

            pfeaturezoomlayer.FeatureClass = BlockClass

            Dim pFSel As IFeatureSelection

            pFSel = pFeatureLayer

            Dim pSelSet As ISelectionSet
            pSelSet = pFSel.SelectionSet

            Dim pEnumGeom As IEnumGeometry
            Dim pEnumGeomBind As IEnumGeometryBind

            pEnumGeom = New EnumFeatureGeometry
            pEnumGeomBind = pEnumGeom
            pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)

            Dim pGeomFactory As IGeometryFactory
            pGeomFactory = New GeometryEnvironment

            Dim pGeom As IGeometry
            pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

            pActiveview.Extent = pGeom.Envelope

          


        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        Dim where As String
        where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"
        Dim pfeaturelayer As IFeatureLayer
        pfeaturelayer = New FeatureLayer
        pfeaturelayer.FeatureClass = BlockClass
        ' Dim blockclass2 As IFeatureClass
        Dim pActiveview As IActiveView = AxPageLayoutControl1.ActiveView

        'blockclass2 = pFeatureworkspace.OpenFeatureClass("Jerash_Blocks")
        'pfeaturelayer.FeatureClass = blockclass2

        SelectLayersFeatures(pfeaturelayer, where, pActiveview, BlockClass)

        '  AxMapControl1.ActiveView.Extent = pGeom.Envelope

        AxPageLayoutControl1.Refresh()
        AxMapControl1.Refresh()


    End Sub
0 Kudos
10 Replies
PrahladYeri
Emerging Contributor
Hi,

Just before refreshing the map, when you set the Extent property of the Active View, try doing one more thing.

Try to set the map scale using the Map object in your code.

pActiveview.Extent = pGeom.Envelope
AxMapControl.FocusMap.MapScale = 1250

I'm not using an ActiveX control, but directly control the ArcMap application through an iCommand. Hence, I get the above variable using _EsriDocument.FocusMap.MapScale where _EsriDocument is a variable of iMxDocument.

If this does'nt solve the problem, let me know. We'll have to try something else.

Cheers,

Prahlad
0 Kudos
NehayaArabeiat
Emerging Contributor
Hi prahlad1981,

Thanks for your reply

I have made some changes in the code that I wrote procedure for select features and another one for the zoom then I try your suggestion but the following error appeared:



this is the code:

Public Sub SelectFeatures()
        Try

            pActiveview = AxPageLayoutControl1.ActiveView
            Dim pMap, pmap2 As IMap
            pmap2 = pActiveview.FocusMap
            pMap = AxTOCControl1.ActiveView.FocusMap
            pmap2.ClearSelection()
            pActiveview.Refresh()

            If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
            pfeaturelayer = pMap.Layer(0)
            pFeatureSelection = pfeaturelayer 'QI

            Dim where As String
             where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"

            If Not pFeatureSelection Is Nothing Then
                Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
                pQueryFilter.WhereClause = where
                pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

                AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub ZoomToSelectedFeatures()

        Dim pMap As IMap
      
        pActiveview = AxPageLayoutControl1.ActiveView
        pMap = pActiveview.FocusMap
        Dim pFSel As IFeatureSelection

        pFSel = pMap.FeatureSelection()
      
        Dim pSelSet As ISelectionSet
        pSelSet = pFSel.SelectionSet

        Dim pEnumGeom As IEnumGeometry
        Dim pEnumGeomBind As IEnumGeometryBind

        pEnumGeom = New EnumFeatureGeometry
        pEnumGeomBind = pEnumGeom
        pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)

        Dim pGeomFactory As IGeometryFactory
        pGeomFactory = New GeometryEnvironment

        Dim pGeom As IGeometry
        pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

        pActiveview.Extent = pGeom.Envelope

        AxMapControl1.ActiveView.FocusMap.MapScale = 1250
        AxPageLayoutControl1.ActiveView.FocusMap.MapScale = 1250

        AxMapControl1.Refresh()
        AxPageLayoutControl1.Refresh()

    End Sub

    Private Sub Blocks_List_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Blocks_List.SelectedIndexChanged

        SelectFeatures()
     
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Try

            ZoomToSelectedFeatures()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub
0 Kudos
PrahladYeri
Emerging Contributor
Hi prahlad1981,

Thanks for your reply

I have made some changes in the code that I wrote procedure for select features and another one for the zoom then I try your suggestion but the following error appeared:



this is the code:

Public Sub SelectFeatures()
        Try

            pActiveview = AxPageLayoutControl1.ActiveView
            Dim pMap, pmap2 As IMap
            pmap2 = pActiveview.FocusMap
            pMap = AxTOCControl1.ActiveView.FocusMap
            pmap2.ClearSelection()
            pActiveview.Refresh()

            If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
            pfeaturelayer = pMap.Layer(0)
            pFeatureSelection = pfeaturelayer 'QI

            Dim where As String
             where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"

            If Not pFeatureSelection Is Nothing Then
                Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
                pQueryFilter.WhereClause = where
                pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

                AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Public Sub ZoomToSelectedFeatures()

        Dim pMap As IMap
      
        pActiveview = AxPageLayoutControl1.ActiveView
        pMap = pActiveview.FocusMap
        Dim pFSel As IFeatureSelection

        pFSel = pMap.FeatureSelection()
      
        Dim pSelSet As ISelectionSet
        pSelSet = pFSel.SelectionSet

        Dim pEnumGeom As IEnumGeometry
        Dim pEnumGeomBind As IEnumGeometryBind

        pEnumGeom = New EnumFeatureGeometry
        pEnumGeomBind = pEnumGeom
        pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)

        Dim pGeomFactory As IGeometryFactory
        pGeomFactory = New GeometryEnvironment

        Dim pGeom As IGeometry
        pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

        pActiveview.Extent = pGeom.Envelope

        AxMapControl1.ActiveView.FocusMap.MapScale = 1250
        AxPageLayoutControl1.ActiveView.FocusMap.MapScale = 1250

        AxMapControl1.Refresh()
        AxPageLayoutControl1.Refresh()

    End Sub

    Private Sub Blocks_List_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Blocks_List.SelectedIndexChanged

        SelectFeatures()
     
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Try

            ZoomToSelectedFeatures()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub


Hi Nehaya,

On which line in code is this error occuring?

If it occurs while casting into IFeatureSelection (pFSel = pMap.FeatureSelection()), then try your previous approach if it works for you.

Cheers,

Prahlad.
0 Kudos
NehayaArabeiat
Emerging Contributor
Hi prahlad1981,

Yes the error appeared in this line so I tried my previous code but it zoomed to nothing and the map control became blank !!!

Here's my final code:

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        Try

            pActiveview = AxPageLayoutControl1.ActiveView
            Dim pMap, pmap2 As IMap
            pmap2 = pActiveview.FocusMap
            pMap = AxTOCControl1.ActiveView.FocusMap
            pmap2.ClearSelection()
            pActiveview.Refresh()

            If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
            pfeatureLayer = pMap.Layer(0)
            pfeatureselection = pfeatureLayer 'QI

            Dim where As String
            where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"

            If Not pfeatureselection Is Nothing Then
                Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
                pQueryFilter.WhereClause = where
                pfeatureselection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

                 Dim pSelSet As ISelectionSet

                pSelSet = pfeatureselection.SelectionSet


                Dim pEnumGeom As IEnumGeometry

                Dim pEnumGeomBind As IEnumGeometryBind


                pEnumGeom = New EnumFeatureGeometry

                pEnumGeomBind = pEnumGeom

                pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)


                Dim pGeomFactory As IGeometryFactory

                pGeomFactory = New GeometryEnvironment


                Dim pGeom As IGeometry

                pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

                 AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

                AxPageLayoutControl1.ActiveView.Extent = pGeom.Envelope
             
                AxPageLayoutControl1.ActiveView.Refresh()


            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Thank you very much
0 Kudos
PrahladYeri
Emerging Contributor
Hi prahlad1981,

Yes the error appeared in this line so I tried my previous code but it zoomed to nothing and the map control became blank !!!

Here's my final code:

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

        Try

            pActiveview = AxPageLayoutControl1.ActiveView
            Dim pMap, pmap2 As IMap
            pmap2 = pActiveview.FocusMap
            pMap = AxTOCControl1.ActiveView.FocusMap
            pmap2.ClearSelection()
            pActiveview.Refresh()

            If Not TypeOf pMap.Layer(0) Is IFeatureLayer Then Exit Sub
            pfeatureLayer = pMap.Layer(0)
            pfeatureselection = pfeatureLayer 'QI

            Dim where As String
            where = "GeoCode2 = '" & Blocks_List.SelectedItem & "'"

            If Not pfeatureselection Is Nothing Then
                Dim pQueryFilter As IQueryFilter = New QueryFilterClass()
                pQueryFilter.WhereClause = where
                pfeatureselection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

                 Dim pSelSet As ISelectionSet

                pSelSet = pfeatureselection.SelectionSet


                Dim pEnumGeom As IEnumGeometry

                Dim pEnumGeomBind As IEnumGeometryBind


                pEnumGeom = New EnumFeatureGeometry

                pEnumGeomBind = pEnumGeom

                pEnumGeomBind.BindGeometrySource(Nothing, pSelSet)


                Dim pGeomFactory As IGeometryFactory

                pGeomFactory = New GeometryEnvironment


                Dim pGeom As IGeometry

                pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

                 AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

                AxPageLayoutControl1.ActiveView.Extent = pGeom.Envelope
             
                AxPageLayoutControl1.ActiveView.Refresh()


            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

Thank you very much


Hi Nehaya,

Now that you've set the FeatureSelection object using a different approach (SelectFeatures() func.), has it solved the error ?

Cheers,

Prahlad.
0 Kudos
NehayaArabeiat
Emerging Contributor
Hi prahlad1981,

I tried it but the same problem, what can I do??



Thanks for your assistance
0 Kudos
JamesCrandall
MVP Alum
Hi prahlad1981,

I tried it but the same problem, what can I do??

Thanks for your assistance


It looks like you have a typo in "esr iViewDrawPhase".  Also, in my implementation (an ICommand .dll which works for ArcMap) I am using esriViewGeography instead.  You can try this by replacing,

AxPageLayoutControl1.ActiveView.PartialRefresh(esr iViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

with,

AxPageLayoutControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, Nothing)
0 Kudos
NehayaArabeiat
Emerging Contributor
Hi jamesfreddyc,

Thanks for your reply...

I tried your solution but the same problem appeared, what's now???
0 Kudos
dlldelhi
New Contributor
I found its solution from esri site only, some times back...
I was using ArcMap 9.3, I was having selected FID of feature...then tried this and it worked

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim str
            Dim pFeatureLayerLayer As IFeatureLayer
            Dim pActiveView As IActiveView
            Dim pFeatureSelection As IFeatureSelection
            Dim pQueryFilter As IQueryFilter

            pActiveView = pMap
            pQueryFilter = New QueryFilter
            pFeatureSelection = pFeatureLayer

            str = "FID" & " = " & ComboBox2.SelectedIndex
            pQueryFilter.WhereClause = str

            'Invalidate only the selection cache
            'Flag the original selection
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)
            'Perform the selection
            pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)
            'Flag the new selection
            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

            ZoomToSelected(2, True) 'modify last two arguments here to change zoom expansion

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "ERROR")
            Exit Sub
        End Try

    End Sub

    Public Sub ZoomToSelected(ByVal expandFactor As Double, ByVal bAsRatio As Boolean)
        Dim pDoc As IMxDocument
        pDoc = pMxDoc
        Dim pEnumFeat As IEnumFeature
        pEnumFeat = pDoc.FocusMap.FeatureSelection
        Dim pEnv As IEnvelope
        pEnumFeat.Reset()

        Dim pFeat As IFeature
        pFeat = pEnumFeat.Next
        Do Until pFeat Is Nothing
            If pEnv Is Nothing Then
                pEnv = pFeat.Shape.Envelope
            Else
                pEnv.Union(pFeat.Shape.Envelope)
            End If
            pFeat = pEnumFeat.Next
        Loop

        If pEnv Is Nothing Then
            MsgBox("no selection")
            Exit Sub
        End If

        Dim pActiveView As IActiveView
        pActiveView = pDoc.ActiveView

        Dim pCP As IPoint
        If pEnv.Width <> 0 Or pEnv.Height <> 0 Then
            pEnv.Expand(expandFactor, expandFactor, bAsRatio)
            pActiveView.Extent = pEnv
        Else
            ' if it's a point, leave current extent size, and just center on it
            ' you can do something else here if you want
            pCP = New Point
            pCP.PutCoords(0.5 * (pEnv.XMax + pEnv.XMin), 0.5 * (pEnv.YMax + pEnv.YMin))

            pEnv = pActiveView.Extent
            pEnv.CenterAt(pCP)
            pActiveView.Extent = pEnv
        End If
        pActiveView.Refresh()
    End Sub
0 Kudos