仿照ArcMap实现空间查询功能(不够完善,能实现主体功能)
仿照ArcMap实现空间查询功能(不够完善,能实现主体功能):

Public Class Select_By_LocationClass Select_By_Location
Private pMap As IMap
Private pLayer1, pLayer2 As ILayer
Private pFeaturelayer2 As IFeatureLayer '目标关系图层
Private QueryEnum As ESRI.ArcGIS.Carto.esriSelectionResultEnum
Private SpatialRel As esriSpatialRelEnum
Private LayerCount As Integer
Private Array() As Integer '一个存储1和0值的数组,表示listview中的图层是否能在combbox中显示
Private Array2() As Integer
Dim ArrayList_all As ArrayList
Dim ArrayList_check As ArrayList
Dim button1_Enable1 As Boolean
Dim Buffer_distance As Double
Dim pMapUnits As esriUnits


Public Sub New()Sub New()
' 此调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化。
InitializeInterface()
End Sub

Private Sub InitializeInterface()Sub InitializeInterface()
Dim i As Integer
pMap = ArcGIS.AxMapControl1.Map
LayerCount = pMap.LayerCount
Comb_Layer2.Items.Clear()
Comb_QueryEnum.SelectedIndex = 0
Comb_SpatialRel.SelectedIndex = 0
ListView_Layers1.Items.Clear()
ReDim Array(LayerCount)
ReDim Array2(LayerCount)
ArrayList_all = New ArrayList
If pMap.LayerCount > 0 Then '只要map中有featurelayer,listview中就会显示;ArrayList_all中就有layer
For i = 0 To pMap.LayerCount - 1
If TypeOf pMap.Layer(i) Is IFeatureLayer Then
ListView_Layers1.Items.Add(pMap.Layer(i).Name)
Array(i) = 1
Array2(i) = 0
ArrayList_all.Add(pMap.Layer(i))
End If
Next
End If
If ListView_Layers1.Items.Count > 1 Then 'listview中图层数大于1,combobox中才显示
For i = 0 To ListView_Layers1.Items.Count - 1 '显示combobox——目标关系图层
If ListView_Layers1.Items.Item(i).Checked = False Then
Comb_Layer2.Items.Add(pMap.Layer(i).Name)
End If
Next

If Comb_Layer2.Items.Count > 0 Then
Comb_Layer2.SelectedIndex = 0
End If

Dim tempLayer As ILayer
For i = 0 To ArrayList_all.Count - 1
tempLayer = ArrayList_all(i)
If Comb_Layer2.Text = tempLayer.Name Then
pLayer2 = ArrayList_all(i) '得到目标图层
End If
Next
pFeaturelayer2 = pLayer2
Dim pFeatureSelection As IFeatureSelection
pFeatureSelection = pFeaturelayer2
If Not pFeatureSelection.SelectionSet Is Nothing Then
Label6.Text = pFeatureSelection.SelectionSet.Count & " features selected"
Else
Label6.Text = "0 features selected"
End If
Label7.Text = Comb_Layer2.Text
Else
Label6.Text = "0 features selected"
Label7.Text = ""
End If
Button1.Enabled = False
TextBox_distance.Enabled = False
Comb_unit.Enabled = False
SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
QueryEnum = esriSelectionResultEnum.esriSelectionResultNew
End Sub


Private Sub Display_Comb_Layer2()Sub Display_Comb_Layer2()
Comb_Layer2.Items.Clear()
Dim i As Integer
For i = 0 To ListView_Layers1.Items.Count - 1
If Array(i) = 1 Then
Comb_Layer2.Items.Add(pMap.Layer(i).Name)
End If
Next
If Comb_Layer2.Items.Count > 0 Then
Comb_Layer2.SelectedIndex = 0
ElseIf Comb_Layer2.Items.Count = 0 Then
Comb_Layer2.Text = ""
End If
For i = 0 To LayerCount - 1
If Array2(i) = 1 Then
button1_Enable1 = True
Exit For
End If
Next
If i = LayerCount Then
button1_Enable1 = False
End If
If button1_Enable1 = True And Comb_Layer2.Items.Count > 0 Then
Button1.Enabled = True
Else
Button1.Enabled = False
End If
End Sub

'//////单击listview中checkbox,动态改变combobox中图层显示(通过数组Array值实现)
Private Sub ListView_Layers1_ItemChecked()Sub ListView_Layers1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView_Layers1.ItemCheck
If e.CurrentValue = CheckState.Unchecked And e.NewValue = CheckState.Checked Then
Array(e.Index) = 0
Array2(e.Index) = 1
End If
If e.CurrentValue = CheckState.Checked And e.NewValue = CheckState.Unchecked Then
Array(e.Index) = 1
Array2(e.Index) = 0
End If
Display_Comb_Layer2()
End Sub

Private Sub Button2_Click()Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub


Private Sub Comb_QueryEnum_SelectedIndexChanged()Sub Comb_QueryEnum_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Comb_QueryEnum.SelectedIndexChanged
Select Case Comb_QueryEnum.SelectedIndex
Case 0
QueryEnum = esriSelectionResultEnum.esriSelectionResultNew
Case 1
QueryEnum = esriSelectionResultEnum.esriSelectionResultAdd
Case 2
QueryEnum = esriSelectionResultEnum.esriSelectionResultSubtract
Case 3
QueryEnum = esriSelectionResultEnum.esriSelectionResultAnd
End Select
End Sub


Private Sub Comb_SpatialRel_SelectedIndexChanged()Sub Comb_SpatialRel_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Comb_SpatialRel.SelectedIndexChanged
Select Case Comb_SpatialRel.SelectedIndex
Case 0
SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
Case 1
SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
Case 2
SpatialRel = esriSpatialRelEnum.esriSpatialRelContains '完全包括
Case 3
SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin
Case 4
SpatialRel = esriSpatialRelEnum.esriSpatialRelUndefined
Case 5
SpatialRel = esriSpatialRelEnum.esriSpatialRelUndefined
Case 6
SpatialRel = esriSpatialRelEnum.esriSpatialRelTouches
Case 7
SpatialRel = esriSpatialRelEnum.esriSpatialRelUndefined
Case 8
SpatialRel = esriSpatialRelEnum.esriSpatialRelEnvelopeIntersects
Case 9
SpatialRel = esriSpatialRelEnum.esriSpatialRelContains '只能对polygon操作
Case 10
SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin
End Select
If Comb_SpatialRel.SelectedIndex = 1 Then
CheckBox3.CheckState = CheckState.Checked
TextBox_distance.Enabled = True
Comb_unit.Enabled = True
Else
CheckBox3.CheckState = CheckState.Unchecked
TextBox_distance.Enabled = False
Comb_unit.Enabled = False
End If
End Sub
'//////查询函数
Function Query()Function Query(ByVal pQueryFeatureLayer As IFeatureLayer, ByVal pRelationFeatureLayer As IFeatureLayer) As Boolean
Dim pFeatureSelection As IFeatureSelection
Dim pSelectionSet As ISelectionSet
Dim pFeatureCursor As IFeatureCursor
Dim pGeoCollection As IGeometryCollection
Dim pFeature As IFeature
Dim pTopo As ITopologicalOperator
Dim pGeometry As IGeometry
Dim pBuffer As IGeometry
'/////////得到目标关系图层选择集或所有feature的一个GeoCollection
pFeatureSelection = pRelationFeatureLayer
pSelectionSet = pFeatureSelection.SelectionSet
If pSelectionSet.Count > 0 Then
pSelectionSet.Search(Nothing, False, pFeatureCursor) '必须是False,否则出现异常“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”
Else
pFeatureCursor = pRelationFeatureLayer.FeatureClass.Search(Nothing, False)
End If
pGeoCollection = New GeometryBag


If CheckBox3.CheckState = CheckState.Unchecked Then
pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pGeoCollection.AddGeometry(pFeature.Shape)
pFeature = pFeatureCursor.NextFeature
Loop
Else
Try
If TextBox_distance.Text <> "" Then
If pMapUnits = esriUnits.esriMeters Then
If Comb_unit.SelectedIndex = 0 Then '得到缓冲距离
Buffer_distance = CDbl(TextBox_distance.Text) * 1000
ElseIf Comb_unit.SelectedIndex = 1 Then
Buffer_distance = CDbl(TextBox_distance.Text)
ElseIf Comb_unit.SelectedIndex = 2 Then
Buffer_distance = CDbl(TextBox_distance.Text)
End If
ElseIf pMapUnits = esriUnits.esriKilometers Then
If Comb_unit.SelectedIndex = 0 Then
Buffer_distance = CDbl(TextBox_distance.Text)
ElseIf Comb_unit.SelectedIndex = 1 Then
Buffer_distance = CDbl(TextBox_distance.Text) / 1000
ElseIf Comb_unit.SelectedIndex = 2 Then
Buffer_distance = CDbl(TextBox_distance.Text)
End If
ElseIf pMapUnits = esriUnits.esriUnknownUnits Then
Buffer_distance = CDbl(TextBox_distance.Text)
End If
Else
Buffer_distance = 0
End If
Catch ex As Exception
Buffer_distance = 0
End Try

pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pGeometry = pFeature.Shape
pTopo = pGeometry
pBuffer = pTopo.Buffer(Buffer_distance)
pGeoCollection.AddGeometry(pBuffer)
pFeature = pFeatureCursor.NextFeature
Loop
End If
'MsgBox(pGeoCollection.GeometryCount)
'/////////定义SpatialFilter
Dim pFilter As ISpatialFilter
pFilter = New SpatialFilter
With pFilter
.Geometry = pGeoCollection
.GeometryField = "shape"
.SpatialRel = SpatialRel
End With
'////////得到待选图层
Dim pSelectFeatures As IFeatureSelection
pSelectFeatures = pQueryFeatureLayer
'///////查询
pSelectFeatures.SelectFeatures(pFilter, QueryEnum, False)
If Not pSelectFeatures.SelectionSet Is Nothing Then
pSelectFeatures.SelectionSet.Refresh()
ArcGIS.AxMapControl1.ActiveView.Refresh()
Return True
Exit Function
End If
Return False
pGeoCollection = Nothing
pFilter = Nothing
End Function

Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim LayerIndex As ListView.CheckedIndexCollection
Dim pFeatureLayer1 As IFeatureLayer
LayerIndex = ListView_Layers1.CheckedIndices
Dim i As Integer
ArrayList_check = New ArrayList
For i = 0 To LayerIndex.Count - 1
ArrayList_check.Add(ArrayList_all(LayerIndex.Item(i))) '得到待查寻图层,可能有多个
Next
For i = 0 To ArrayList_check.Count - 1
pLayer1 = ArrayList_check(i)
pFeatureLayer1 = pLayer1
'MsgBox(pFeatureLayer1.Name)
'MsgBox(pFeatureLayer2.Name)
Query(pFeatureLayer1, pFeatureLayer2)
Next
End Sub


Private Sub Comb_Layer2_SelectedIndexChanged()Sub Comb_Layer2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Comb_Layer2.SelectedIndexChanged
Dim i As Integer
Dim tempLayer As ILayer
For i = 0 To ArrayList_all.Count - 1
tempLayer = ArrayList_all(i)
If Comb_Layer2.Text = tempLayer.Name Then
pLayer2 = ArrayList_all(i) '得到目标图层
End If
Next
pFeaturelayer2 = pLayer2
Dim pFeatureSelection As IFeatureSelection
pFeatureSelection = pFeaturelayer2
If Not pFeatureSelection.SelectionSet Is Nothing Then
Label6.Text = pFeatureSelection.SelectionSet.Count & " features selected"
Else
Label6.Text = "0 features selected"
End If
Label7.Text = Comb_Layer2.Text
End Sub

Private Sub CheckBox3_CheckedChanged()Sub CheckBox3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
If CheckBox3.CheckState = CheckState.Unchecked Then
TextBox_distance.Enabled = False
Comb_unit.Enabled = False
Else
TextBox_distance.Enabled = True
Comb_unit.Enabled = True
End If
If Comb_unit.Enabled = True Then
Comb_unit.SelectedIndex = 1
End If
End Sub

Private Sub Select_By_Location_Load()Sub Select_By_Location_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
'Dim pArcGIS As New My_ArcGis.ArcGIS
Dim sMapUnits As String = Nothing
pMapUnits = ArcGIS.AxMapControl1.MapUnits
End Sub
End Class
浙公网安备 33010602011771号