Hello All,
We have just installed ArcGIS 10.1 and MS Visual Studio 10 to develop automation tools for select by attribute and select by Location.
We developed a new add-in button in ArcMAP, and clicking it should do 4 queries, 2 of them are attribute queries and other 2 are spatial queries. order (1 attribute, 1 spatial, 1 attribute, 1 spatial)
Suffering from very slow performance when using our code to perform the spatial queries.
Please help!!
Here after is the used code:
Public Function p_GenSelectByLocation(ByVal SourceLayerName As String, ByVal TargetLayerName As String, ByVal Whereclause As String, ByVal SelectionType As esriSelectionResultEnum, ByVal RelationType As esriSpatialRelEnum) As Integer
Try
Dim pboolSrcFound As Boolean = False
Dim pboolTrgtFound As Boolean = False
Dim pEnumLayer As IEnumLayer = My.ArcMap.Document.ActiveView.FocusMap.Layers(Nothing, True)
Dim pSrcFLayer As IFeatureLayer = Nothing
Dim pTrgtFLayer As IFeatureLayer = Nothing
Dim pLoopLayer As ILayer = pEnumLayer.Next()
pEnumLayer.Reset()
Do Until pLoopLayer Is Nothing
If TypeOf pLoopLayer Is IFeatureLayer Then
If pLoopLayer.Name = SourceLayerName Then
pSrcFLayer = pLoopLayer
pboolSrcFound = True
End If
If pLoopLayer.Name = TargetLayerName Then
pTrgtFLayer = pLoopLayer
pboolTrgtFound = True
End If
End If
If pboolSrcFound = True And pboolTrgtFound = True Then Exit Do
pLoopLayer = pEnumLayer.Next()
Loop
If pSrcFLayer Is Nothing Or pTrgtFLayer Is Nothing Then Exit Try
Dim pSrcFeatureSelection As IFeatureSelection = pSrcFLayer
Dim pGeomToUnion As IGeometryCollection = New GeometryBag()
If pSrcFeatureSelection.SelectionSet.Count = 0 Then Exit Try
'Get union of Geometry of all selected features
Dim pCursor As ICursor = Nothing
pSrcFeatureSelection.SelectionSet.Search(Nothing, False, pCursor)
If pCursor Is Nothing Then Exit Try
Dim pfeatureCursor As IFeatureCursor = pCursor
Dim pFeature As IFeature = pfeatureCursor.NextFeature
Dim pGeometry As IGeometry
pGeometry = pFeature.Shape
Dim pCount As Integer = 0
Do Until pFeature Is Nothing
pCount += 1
pGeometry = pFeature.Shape
pGeomToUnion.AddGeometry(pGeometry)
pFeature = pfeatureCursor.NextFeature
Loop
Dim pEnumGeometry As IEnumGeometry = pGeomToUnion
'Dim pgeometrybag As IGeometryBag = pEnumGeometry
Dim pResultGeom As ITopologicalOperator = Nothing
If pGeometry.GeometryType = esriGeometryType.esriGeometryPolyline Then
pResultGeom = New Polyline
pResultGeom.ConstructUnion(pEnumGeometry)
ElseIf pGeometry.GeometryType = esriGeometryType.esriGeometryPolygon Then
pResultGeom = New Polygon
pResultGeom.ConstructUnion(pEnumGeometry)
End If
If pResultGeom Is Nothing Then Exit Try
'define the spatial filter and select the features based on the union Geometry
Dim pSFilter As ISpatialFilter = New SpatialFilter
'If pTrgtFLayer Is Nothing Then Exit Try
Dim pnameOfShapeField As System.String = pTrgtFLayer.FeatureClass.ShapeFieldName
pSFilter.GeometryField = pnameOfShapeField
pSFilter.Geometry = TryCast(pResultGeom, IGeometry)
pSFilter.SpatialRel = RelationType
Dim pTrgtFeatureSelection As IFeatureSelection = pTrgtFLayer
pTrgtFeatureSelection.SelectFeatures(TryCast(pSFilter, ESRI.ArcGIS.Geodatabase.ISpatialFilter), SelectionType, False)
Return pTrgtFeatureSelection.SelectionSet.Count
Catch ex As Exception
pFuncReturnDebug(ex)
Return -1
End Try
End Function