Saturday, February 12, 2011

Zoom in to feature in ArcGIS Engine using ArcGIS Server and IFind

The second blog post on how to use ArcGIS Server from ArcGIS Engine. This post show how to zoom in on a feature using a IFind searching for a specific value on an attribute.

Basic steps include getting the layer from the map and casting the layer to the IFind interface and searching the layer for the feature. The project are expecting that the layer href="http://server.arcgisonline.com/ArcGIS/rest/services/Demographics/USA_Median_Household_Income/MapServer is loaded in the map.

 Private Sub FindUsingIFindToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindUsingIFindToolStripMenuItem.Click
OnFind()
End Sub

Private Sub OnFind()
Dim state As String = "Alaska"
Dim attribute As String = "Name"
Dim layername As String = "States"
Dim control As AxMapControl = axMapControl1
Dim layer As ILayer = getLayer(layername)
Dim stateGeometry As IGeometry = Me.Find(layer, attribute, state, control.ActiveView)
control.ActiveView.Extent = stateGeometry.Envelope
control.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, Nothing, Nothing)
End Sub



The code for getting the layer from the map.

 Private Function getLayer(ByVal name As String) As ILayer
Dim retLayer As ILayer = Nothing
Dim control As AxMapControl = axMapControl1
For i As Integer = 0 To control.Map.LayerCount - 1
Dim layer As ILayer = control.Map.Layer(i)
retLayer = Me.getLayer(name, layer)
If Not retLayer Is Nothing Then
Exit For
End If
Next
Return retLayer
End Function

Private Function getLayer(ByVal name As String, ByVal layer As ILayer) As ILayer
Dim retLayer As ILayer = Nothing
If TypeOf layer Is ICompositeLayer Then
Dim gLayer As ICompositeLayer = CType(layer, ICompositeLayer)
For i As Integer = 0 To gLayer.Count - 1
Dim l As ILayer = gLayer.Layer(i)
'söker rekursivt efter lagret
retLayer = Me.getLayer(name, l)
If Not retLayer Is Nothing Then
Exit For
End If
Next
Else
If layer.Name.Equals(name) Then
retLayer = layer
End If
End If
Return retLayer
End Function



The code that does the actual IFind. We cast the layer to IFind and use the Find method on the interface. Important to note is that it only work when you search for a value on an attribute, you can't do a custom query on serveral attributes.
 Private Function Find(ByVal layer As ILayer, ByVal field As String, ByVal value As String, ByVal activeView As IActiveView) As IGeometry5
Dim findLayer As IFind = CType(layer, IFind)
Dim fields As String() = {field}
'Söker med IFind efter features med attributet satt till value.
Dim array As IArray = findLayer.Find(value, True, fields, activeView.ScreenDisplay.CancelTracker)
Dim polygon As IPolygon5 = Nothing
For i As Integer = 0 To array.Count - 1
Dim featureFindData As IFeatureFindData2 = CType(array.Element(i), IFeatureFindData2)
Dim featureFields As IFields = featureFindData.Feature.Fields
Dim feature As IFeature = featureFindData.Feature
If (polygon Is Nothing) Then
polygon = CType(feature.Shape, IPolygon5)
Else
Dim topOp As ITopologicalOperator6 = CType(feature.Shape, ITopologicalOperator6)
polygon = CType(topOp.UnionEx(CType(polygon, IGeometry), True), IPolygon5)
End If
Next
Return CType(polygon, IGeometry5)
End Function



The same result as I got in the previous blog post, load the layer showing whole US:



And after running the find tool it shows Alaska:

1 comment:

Unknown said...

KING CASINO, LLC GIVES A $100 FREE BET
KING CASINO, LLC GIVES A $100 FREE BET https://febcasino.com/review/merit-casino/ to try. Visit us today and herzamanindir.com/ receive a $100 FREE BET! Sign ventureberg.com/ up at apr casino our new site! worrione.com