C# code to display result of spatial query on arcmap.

1095
1
12-18-2013 12:09 AM
cynthiasinha
New Contributor
Hi all,
The problem is i have to display those points on arcmap that lies inside the envelope.I am explaining what i have done so far.
I have one point feature class say featureclass ,open it and used one ARCGIS Snippet i.e. GetActiveviewFromArcmap() and constructed one envelope as-:


Iactiveview pactiveview=GetActiveviewFromArcmap(m_application);
Ienvelope penvelope=new EnvelopeClass();
penvelope=pactiveview.extent;
penvelope.QueryCoord(out minx, out miny,out maxX,out maxY);

Ispatialfilter sp=New IspatialfilterClass();
sp.geometry=penvelope;
sp.geometryfield=featureclass.shapefieldname;
sp.spatialrel=esrispatialrelenum.esrispatialrelIntersects;



Ifeaturecursor fc=featureclass.Search(sp,false);
Ifeature feature=featurecursor.nextfeature();

What i have to do is to display the output of this spatial query on arcmap.how i will retrieve the selectionset of spatialfilter query and how to diaplay it on arcmap.
0 Kudos
1 Reply
swapnabhide
New Contributor
Hi Cynthia,

For your post regarding selecting the feature inside envelope, I think the code below will be useful. It uses ISpatialFilter and IFeatureSelection interface to query and select the points respectively. I have the code in VBA, you might have to modify it as per your requirement. The code is written on button's click event and envelope needs to be selected by tracking a rectangle using cursor. The points inside the envelope are selected and number of selected points is also given in message box.
------------------------
Private Sub UIButtonControl1_Click()
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pEnv As IEnvelope
Dim pRubber As IRubberBand
Set pRubber = New RubberEnvelope

Dim pActiveView As IActiveView
Set pActiveView = pMxDoc.FocusMap
Set pEnv = pRubber.TrackNew(pActiveView.ScreenDisplay, Nothing)

Dim pSpatialFilter As ISpatialFilter
Set pSpatialFilter = New SpatialFilter
Set pSpatialFilter.Geometry = pEnv
pSpatialFilter.SpatialRel = esriSpatialRelIntersects

Dim lPoints As Long
Dim pLayer As IFeatureLayer
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim i As Long
For i = 0 To pMxDoc.FocusMap.LayerCount - 1
    If (TypeOf pMxDoc.FocusMap.Layer(i) Is IGeoFeatureLayer) Then
        Set pLayer = pMxDoc.FocusMap.Layer(i)
        pSpatialFilter.GeometryField = pLayer.FeatureClass.ShapeFieldName
        Set pFeatureCursor = pLayer.Search(pSpatialFilter, True)
        Set pFeature = pFeatureCursor.NextFeature
        Do Until (pFeature Is Nothing)
            Select Case pFeature.Shape.GeometryType
                Case esriGeometryPoint
                    lPoints = lPoints + 1
            End Select
            Set pFeature = pFeatureCursor.NextFeature
        Loop
    End If
Next i
Dim pFS As IFeatureSelection
Set pFS = pLayer
pFS.SelectFeatures pSpatialFilter, esriSelectionResultNew, False
pActiveView.Refresh
MsgBox "Features Found:" & vbCrLf & lPoints & " Points "

End Sub
-----------------------
Hope this helps.

Regards,
Swapna.
0 Kudos