点选要素(完整版)

点选法选择要素(2007-07-01 15:28:27)
  分类:ArcGIS学习
  下面是用点选法选择地图中的要素,首先在鼠标点击点周围建立较小范围的缓冲区,然后用这个缓冲区与地图中的要素类进行空间过滤计算,得到选择的要素。下面的例子仅在一个图层中选择,并且局限于选择点要素,其他要素类似。

Private Sub AxMapControl1_OnMouseDown(ByVal sender As System.Object, ByVal e As AxesriMapControl.IMapControlEvents2_OnMouseDownEvent) Handles AxMapControl1.OnMouseDown
        Dim pMap As IMap
        pMap = AxMapControl1.Map

获取点图层
        Dim pFeatLyr As IFeatureLayer
        pFeatLyr = pMap.Layer(0)

获取鼠标点击点
        Dim pPoint As IPoint
        pPoint = New Point
        pPoint.PutCoords(e.mapX, e.mapY)
        Dim pGeometry As IGeometry
        pGeometry = pPoint
        Dim pFeatClass As IFeatureClass
        pFeatClass = pfeatlyr.FeatureClass

进行缓冲区运算:
        Dim pTopo As ITopologicalOperator
        pTopo = pGeometry

        设定缓冲区距离:
        Dim length As Double
        length = ConvertPixelToMapUnits(pMap, 5)

        得到缓冲区多边形pBuffer:
        Dim pBuffer As IGeometry
        pBuffer = pTopo.Buffer(length)

        得到缓冲区的包络线:
        pGeometry = pBuffer.Envelope

下面进行空间过滤运算:
        Dim pSpatialFilter As ISpatialFilter

        pSpatialFilter = New SpatialFilter
        pSpatialFilter.Geometry = pGeometry

       下面设置为三种不同要素类型的图层的空间过滤运算:
        Select Case pFeatClass.ShapeType
            Case esriGeometryType.esriGeometryPoint
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains
            Case esriGeometryType.esriGeometryPolyline
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses
            Case esriGeometryType.esriGeometryPolygon
                pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects
        End Select
        pSpatialFilter.GeometryField = pFeatClass.ShapeFieldName

下面得到空间过滤运算的结果指针:
        Dim pCursor As IFeatureCursor
        pCursor = pFeatClass.Search(pSpatialFilter, False)

得到空间过滤运算的结果要素
        Dim pfeat As IFeature
        pfeat = pCursor.NextFeature

下面把选择到的要素显示出来
        Dim pColor As IRgbColor
        pColor = New RgbColor
        pColor.Red = 110
        pColor.Green = 120
        pColor.Blue = 210
        Dim pMarkSym As ISimpleMarkerSymbol
        pMarkSym = New SimpleMarkerSymbol
        pMarkSym.Style = esriSimpleMarkerStyle.esriSMSCircle
        pMarkSym.Size = 10
        pMarkSym.Color = pColor
        Do While Not pfeat Is Nothing
            pMap.SelectFeature(pfeatlyr, pfeat)
            AxMapControl1.DrawShape(pfeat.Shape, pMarkSym)

            'pfeat.Delete()加上这一句可以删除选中的要素
            pfeat = pCursor.NextFeature
        Loop

    End Sub

这个函数是用来得到一个最佳缓冲距离:
    Private Function ConvertPixelToMapUnits(ByVal pActiveView As IActiveView, ByVal pixelUnits As Double) As Double
        Dim realWorldDisplayExtent As Double
        Dim pixelExtent As Integer
        Dim sizeOfOnePixel As Double

        pixelExtent = pActiveView.ScreenDisplay.DisplayTransformation.DeviceFrame.right - pActiveView.ScreenDisplay.DisplayTransformation.DeviceFrame.left
        realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width
        sizeOfOnePixel = realWorldDisplayExtent / pixelExtent
        Return pixelUnits * sizeOfOnePixel
    End Function

原文地址:https://www.cnblogs.com/zhangjun1130/p/1431476.html