空间查询的实现

空间查询是一个经常要用到的功能,它是通过给定一定的范围,查询得到在这个空间范围内的要素的查询方式。下面的代码是在返回鼠标点击点周围长宽100个地图单位的矩形范围内的要素。
过程描述
Dim pMap As IMap
Dim pPoint As IPoint
Set pMap = MapControl1.Map
Dim pFeatureLayer As IFeatureLayer
Set pFeatureLayer = pMap.Layer(1)
Set pPoint = MapControl1.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

Dim pEnv As IEnvelope
Set pEnv = New Envelope
Set pEnv = MapControl1.ActiveView.Extent
pEnv.Height = 100
pEnv.Width = 100
pEnv.CenterAt pPoint
MsgBox pPoint.x
MsgBox pEnv.XMax

Dim pGeometry As IGeometry
Set pGeometry = pEnv
Set pGeometry.SpatialReference = pMap.SpatialReference

pFeatureLayer.Selectable = True
Dim pSFilter As ISpatialFilter
Set pSFilter = New SpatialFilter
With pSFilter
Set .Geometry = pGeometry
.GeometryField = pFeatureLayer.FeatureClass.ShapeFieldName
.SpatialRel = esriSpatialRelIntersects
End With
Dim b As Boolean
b = pSFilter.FilterOwnsGeometry
Dim pFeatureCursor As IFeatureCursor

Set pFeatureCursor = pFeatureLayer.Search(pSFilter, False)

你可能感兴趣的:(查询)