ArcGIS Engine空間高效查詢(IIdentify方法)
本文共分兩部分:
第一部分 實現IIdentify
第二部分 實現IRowIdentifyObj-克服IIdentify介面的缺點
第一部分 實現IIdentify
轉自http://www.gisall.com/?uid-121719-action-viewspace-itemid-2918
利用ArcEngine空間查詢,大多數人會馬上想到利用IQueryFilter介面,的確,IQueryFilter介面是我們經常使用的查詢介面而且使用比較簡單,但是在大資料量查詢資料,尤其是空間查詢時效率會很低,導致速度會很慢。
給大家推薦一個介面,給剛剛入門或者還不知道該介面的朋友提供一點參考。
IIdentify
// IIdentify幫助介紹
The IIdentify interface can be used to identify features at the specified location. When this interface is on a map layer, the Identify method returns an array of FeatureIdentifyObject objects.
On a FeatureIdentifyObject, you can do a QI to the IIdentifyObj
IIdentify繼承了FeatureLayer、RasterLayer、CadFeatureLayer
缺點:只有與查詢圖形相交一種查詢方式沒有IQueryfilter的查詢方式多樣
//獲得查詢圖形
IActiveView act = myMapControl.ActiveView.FocusMap as IActiveView;
IPoint pp = act.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y);
//QI FeatureLayer QI IIdentif
IIdentify pIdentify = needLayer as IIdentify;
IArray pIDs = pIdentify.Identify((IGeometry)pp);
if (pIDs == null || pIDs.Count == 0)
{
return;
}
//取第一個實體
IFeatureIdentifyObj pFeatIdObj = pIDs.get_Element(0) as IFeatureIdentifyObj;
// 1:獲得IFeature物件
IFeature pFea= pFeatIdObj.Feature
myMapControl.FlashShape(needFeat.Shape, 3, 300, null);
//2:獲得IRow物件
IRowIdentifyObject pRowObj = pFeatIdObj as IRowIdentifyObject;
IRow pRow= = pRowObj.Row ;
第二部分 實現IRowIdentifyObj
http://hxb1127.blog.sohu.com/16443963.html
本例要演示的是如何查詢Feature的屬性資訊。實現後的結果為選擇了UI Tool Control後,在要查詢的Feature上單擊滑鼠,查詢的結果將顯示在彈出的窗體上。
l 要點
首先需要得到要查詢的Feature物件。使用IIdentify介面的Identify方法可以對給定的位置進行查詢,得到結果為IIdentifyObj物件的陣列。然後通過為IIdentifyObj物件設定IFeatureIdentifyObj查詢介面,即可進一步得到Feature物件。因為IFeatureIdentifyObj介面的Feature屬性具有隻寫(write only)屬性,故又用到另一個介面IRowIdentifyObj。
得到Feature物件後即可操作其Fields屬性和Value屬性,得到其屬性欄位名和值。
l 程式說明
在窗體上使用了MSFlexGrid Control 6.0來顯示查詢結果。所以本例也演示了MSFlexGrid控制元件的使用方法。
窗體名: frmResult
MSFlexGrid控制元件名: flxAttr
標籤控制元件名: lblLocation (標籤用來顯示查詢位置的地理座標)
l 程式碼
Private Sub UIT_Identify_MouseDown(ByVal button As Long, ByVal shift As Long, _ ByVal x As Long, ByVal y As Long)
Dim pMxApplication As IMxApplication
Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pPoint As IPoint
Dim pIDArray As IArray
Dim pIdentify As IIdentify
Dim pFeatureIdentifyObj As IFeatureIdentifyObj
Dim pIdentifyObj As IIdentifyObj
Dim pRowIdentifyObj As IRowIdentifyObject
Dim pFeature As IFeature
Dim pFields As IFields
Dim pField As IField
Dim iFieldIndex As Integer
Dim iLayerIndex As Integer
Dim sShape As String
On Error GoTo ErrorHandler
Set pMxApplication = Application
Set pMxDocument = Application.Document
Set pMap = pMxDocument.FocusMap
'Identify from TOP layer to BOTTOM, exit loop since one Feature identified
For iLayerIndex = 0 To pMap.LayerCount - 1
Set pIdentify = pMap.Layer(iLayerIndex)
'Convert x and y to map units
Set pPoint = pMxApplication.Display.DisplayTransformation.ToMapPoint(x, y)
'Set label on the form, coordinates would have 6 digits behind decimal point
frmResult.lblLocation = "Location:(" & Format(pPoint.x, "##0.000000") & "," _& Format(pPoint.y, "##0.000000") & ")"
Set pIDArray = pIdentify.Identify(pPoint)
'Get the FeatureIdentifyObject
If Not pIDArray Is Nothing Then
Set pFeatureIdentifyObj = pIDArray.Element(0)
Set pIdentifyObj = pFeatureIdentifyObj
pIdentifyObj.Flash pMxApplication.Display
'Feature property of FeatureIdentifyObject has write only access
Set pRowIdentifyObj = pFeatureIdentifyObj
Set pFeature = pRowIdentifyObj.Row
Set pFields = pFeature.Fields
'Set the MSFlexGrid control on form te display identify result
With frmResult.flxAttr
.AllowUserResizing = flexResizeColumns
.ColAlignment(1) = AlignmentSettings.flexAlignLeftCenter
.ColWidth(0) = 1500
.ColWidth(1) = 1800
'Add header to MSFlexGrid control
.Rows = pFields.FieldCount + 1
.Cols = 2
.FixedRows = 1
.FixedCols = 0
.TextMatrix(0, 0) = "Field"
.TextMatrix(0, 1) = "Value"
For iFieldIndex = 0 To pFields.FieldCount - 1
Set pField = pFields.Field(iFieldIndex)
'Set field "Field" of the MSFlex control
.TextMatrix(iFieldIndex + 1, 0) = pField.Name
'Set field "Value" of the MSFlex control
Select Case pField.Type
Case esriFieldTypeOID
.TextMatrix(iFieldIndex + 1, 1) = pFeature.OID
Case esriFieldTypeGeometry
'The function QueryShapeType return a String that
' correspond with the esriGeoemtryType const
sShape = QueryShapeType(pField.GeometryDef.GeometryType) .TextMatrix(iFieldIndex + 1, 1) = sShape
Case Else
.TextMatrix(iFieldIndex + 1, 1) = pFeature.Value(iFieldIndex)
End Select
Next iFieldIndex
End With
frmResult.Show modal
Exit Sub
End If
Next iLayerIndex
'If code goes here, no Feature was indentified, clear the MSFlex control's content
' and show a message
frmResult.flxAttr.Clear
MsgBox "No feature identified."
Exit Sub
ErrorHandler:
MsgBox Err.Description
End Sub
Public Function QueryShapeType(ByVal enuGeometryType As esriGeometryType) As String
Dim sShapeType As String
Select Case enuGeometryType
Case esriGeometryPolyline
sShapeType = "Polyline"
Case esriGeometryPolygon
sShapeType = "Polygon"
Case esriGeometryPoint
sShapeType = "Point"
Case esriGeometryMultipoint
sShapeType = "Multipoint"
Case esriGeometryNull
sShapeType = "Unknown"
Case esriGeometryLine
sShapeType = "Line"
Case esriGeometryCircularArc
sShapeType = "CircularArc"
Case esriGeometryEllipticArc
sShapeType = "EllipticArc"
Case esriGeometryBezier3Curve
sShapeType = "BezierCurve"
Case esriGeometryPath
sShapeType = "Path"
Case esriGeometryRing
sShapeType = "Ring"
Case esriGeometryEnvelope
sShapeType = "Envelope"
Case esriGeometryAny
sShapeType = "Any valid geometry"
Case esriGeometryBag
sShapeType = "GeometryBag"
Case esriGeometryMultiPatch
sShapeType = "MultiPatch"
Case esriGeometryTriangleStrip
sShapeType = "TriangleStrip"
Case esriGeometryTriangeFan
sShapeType = "TriangleFan"
Case esriGeometryRay
sShapeType = "Ray"
Case esriGeometrySphere
sShapeType = "Sphere"
Case Else
sShapeType = "Unknown!"
End Select
QueryShapeType = sShapeType
End Function
引用與:http://blog.sina.com.cn/s/blog_4d780fc10101460r.html