|
ArcGIS接口详细说明之个人开发心得(转载)
1. 关于IField接口(esriGeoDatabase)
IField接口的第一个属性AliasName(只读,获得字段的别名)
IField接口的第二个方法CheckValue(Value)(方法,对于指定的属性字段,基于字段类型判断参数值是否有效,有效,则返回True,否则返回False)
例子代码:
'Get Field
Set pField = pFields.Field(6)
bChkField = pField.CheckValue(45.86)
If bChkField = True Then
'Add data to selected rows for the field selected.
End If
IField接口的其他属性均为只读属性,常用有Name(只读,获得字段的名称)
例子代码:
Dim pFields As IFields
Dim pField As IField
Dim pGeoDef As IGeometryDef
Dim pDomain As IDomain
Dim i As Long
Set pFields = pFeatClass.Fields
For i = 0 To pFields.FieldCount - 1
Set pField = pFields.Field(i)
If pField.Type = esriFieldTypeGeometry Then
Set pGeoDef = pField.GeometryDef
Else
Debug.Print pField.AliasName
Debug.Print pField.DefaultValue
Set pDomain = pField.Domain
Debug.Print pField.Editable
Debug.Print pField.IsNullable
Debug.Print pField.Length
Debug.Print pField.Name
Debug.Print pField.Precision
Debug.Print pField.Required
Debug.Print pField.Scale
Debug.Print pField.Type
Debug.Print pField.VarType
End If
Next
2. 关于IFieldEdit接口(esriGeoDatabase)
所有该接口的属性均为可读可写,经常用与对新建字段的设置,因为字段一旦被设置,其基本属性就不能被更改,所以就需要该接口类型的变量去转换,方法为:
IFeatureClass pFC_SCP_PT;
IFieldEdit editPT = new FieldClass();
pFC_SCP_PT.AddField((IField)editPT);
如果在vb中去编写代码,则赋值和获取均为同一属性,而在C#中,为了区分设置和获取,属性均有两个,类似于Name和Name_2,这样就可以区分了,普遍用设置的带有_2的那个属性。
IFieldEdit接口的第一个属性Name (读写,设置或者获取该变量类型变量字段的名称)
IFieldEdit接口的第二个属性Precision(读写,设置或者获取该变量类型变量字段的长度)
IFieldEdit接口的第三个属性Scale(读写,设置或者获取该变量类型变量字段的精度)
IFieldEdit接口的第四个属性Type(读写,设置或者获取该变量类型变量字段的类型)
例子代码:
IFeatureClass pFC_SCP_PT;
editPT = new FieldClass();
editPT.Precision_2 = 8;
editPT.Scale_2 = 3;
editPT.Name_2 = "ELEV1";
editPT.Type_2 = esriFieldType.esriFieldTypeDouble;
3. 关于IFields接口(esriGeoDatabase)
IFields接口的第一个属性Field(Index) (只读,以用于获取具体的字段,返回类型为IField)
IFields接口的第二个属性FieldCount(只读,以用于获取属性的数量)
利用上面两个接口并用索引去依次循环获得每一列的属性pField(Ifield接口)
例子代码:
Dim i As Long
Dim pField As IField
For i = 0 To (pFields.FieldCount - 1)
Set pField = pFields.Field(i)
Debug.Print pField.Name & ": " & pField.Type
Next i
IFields接口的第三个方法FindField(Name)(方法,输入想要查找的属性域字段的名称,如果有,则返回该属性域字段在此Fields的索引,没有则返回-1)
例子代码:
Dim i As Integer
Dim pFields As IFields
Dim pField As IField
'Get Fields
Set pFields = pFeatClass.Fields
'Find the field named "average_income"
i = pFields.FindField("average_income")
'Set the current field
Set pField = pFields.Field(i)
'Delete field from featureclass
pFeatClass.DeleteField pField
IFields接口的第四个方法FindFieldByAliasName(Name)(方法,与第三个方法类似,此时输入的为该列属性字段的别名,此方法不经常用)
例子代码:
Dim i As Integer
Dim pFields As IFields
Dim pField As IField
'Get Fields
Set pFields = pFeatClass.Fields
'Find the field with the aliasname "current population"
i = pFields.FindFieldByAliasName("current population")
'Set the current field
Set pField = pFields.Field(i)
'Delete field from featureclass
pFeatClass.DeleteField pField
4. 关于IPoint接口(esriGeometry)
IPoint接口的第一个方法PutCoords(X,Y)(方法,设置该点的坐标)或者直接调用可以读写的属性X和Y,将坐标赋值给X和Y
例子代码:
Dim pPoint As IPoint
Set pPoint = New Point
pPoint.PutCoords 100, 100
复制代码
IPoint接口的第二个方法QueryCoords(X,Y) (方法,得到该点的坐标)
例子代码:
Dim pPoint as IPoint
Dim dX as Double, dY as Double
pPoint.QueryCoords dX, dY
IPoint接口的第三个方法ConstrainAngle (constraintAngle, anchor, allowOpposite ) (方法,如果第三个参数allowOpposite为True,则将第二个参数anchor这个点作为一个原点,然后以第一个参数constraintAngle为与x轴的角度,做一条直线,再将调用此参数的点向该直线做垂线并交于一个新点,并将调用此方法的点移动到该点)
例子代码:
'Finds the closes point to line from (0,0) with angles
'defined by steps of pi/4 (Note all angles in radians)
Dim pApoint As IPoint
Dim pNpoint As IPoint
Dim pi As Double
Dim dAngle As Double
Dim i As Long
Set pApoint = New Point
pi = 4 * Atn(1)
dAngle = 0
pApoint.PutCoords 0, 0
Set pNpoint = New Point
For i = 0 To 7
pNpoint.PutCoords 1, 0
dAngle = i * pi / 4
pNpoint.ConstrainAngle dAngle, pApoint, True
MsgBox "angle = " & i & "*pi/4" & vbCrLf & pNpoint.X & "," & pNpoint.Y
Next i
IPoint接口的第四个方法ConstrainDistance (constraintRadius, anchor ) (方法,以第二个参数anchor这个点为圆心,然后以第一个参数constraintRadius为半径做一个圆,将调用此参数的点移动到该点与圆心做线段交于该圆的交点上)
例子代码:
Public Sub t_constraindistance()
Dim pPoint As IPoint
Dim pNPoint As IPoint
Dim dRadius As Double
Set pPoint = New Point
pPoint.PutCoords 0, 0
Set pNPoint = New Point
pNPoint.PutCoords 2, 2
dRadius = 1.4142135623731
pNPoint.ConstrainDistance dRadius, pPoint
MsgBox "Radius = " & dRadius & " x,y = " & pNPoint.X & "," & pNPoint.Y
End Sub
5. 关于IPointArray接口(esriGeometry)
IPointArray接口的第一个方法Add(p) (方法,向该类型的数组变量添加Point)
IPointArray接口的第二个属性Count (只读,获得该数组变量中Point的个数,返回Long类型变量)
IPointArray接口的第三个属性Element(Index) (只读,获得该数组变量中位于参数Index索引位置的点Point,返回一个Point类型的变量)
IPointArray接口的第四个方法Insert (Index, p ) (方法,向索引位置Index插入一个点Point)
IPointArray接口的第五个方法Remove (Index ) (方法,移除索引位置Index的点Point)
IPointArray接口的第六个方法RemoveAll (方法,移除所有在此数组中的点)
6. 关于IPointCollection接口(esriGeometry)
IPointCollection接口的第一个方法AddPoint(inPoint [,before] [,after]) (方法,向该类型的点集变量添加Point,第一个参数为添加的Point,第二个第三个参数为可选择的参数,默认添加进点集的末尾)
IPointCollection接口的第二个属性Point(i) (只读,获得该点集变量中第i个位置的Point,返回IPoint类型变量,i从0计算开始)
IPointCollection接口的第三个属性PointCount (只读,获得该点集变量中点的个数,返回Long类型变量,切记,如果一个PointCollection变量是由闭合的Geometry转换而来的话,那么点的个数比节点数多一个,因为是闭合的,所以首位节点是同一个点)
7. 关于IPolyline接口(esriGeometry)
IPolyline接口的第一个属性FromPoint与ToPoint(读写,设置或者读取该点的起始点和终止点,返回都是IPoint类型的变量)
IPolyline接口的第二个方法QueryFromPoint (from )(方法,返回IPoint类型的变量到参数from)
IPolyline接口的第三个方法QueryToPoint (to ) (方法,返回IPoint类型的变量到参数to)
Public Sub t_ICurve_QueryPoints()
Dim pID As New UID
pID = "esriEditor.editor"
Dim pEditor As IEditor
Dim pApp As IApplication
Set pApp = MxApplication
Set pEditor = pApp.FindExtensionByCLSID(pID)
If pEditor.SelectionCount <> 1 Then
MsgBox "select one Curve"
Exit Sub
End If
Dim pEnumFeat As IEnumFeature
Dim pFeature As IFeature
Set pEnumFeat = pEditor.EditSelection
Dim pCurve As ICurve
Dim pPointFrom As IPoint
Dim pPointTo As IPoint
Set pPointFrom = New Point
Set pPointTo = New Point
Set pFeature = pEnumFeat.Next
While Not pFeature Is Nothing
If pFeature.Shape.GeometryType = esriGeometryPolyline Or _
esriGeometryPolyline Or esriGeometryLine Then
Set pCurve = pFeature.Shape
pCurve.QueryFromPoint pPointFrom
pCurve.QueryToPoint pPointTo
MsgBox "+++ICurve properties..." & vbCrLf _
& "Curve.QueryFromPoint (x,y) = " & pPointFrom.X & "," & pPointFrom.Y & vbCrLf _
& "Curve.QueryToPoint (x,y) = " & pPointTo.X & "," & pPointTo.Y & vbCrLf
End If
Set pFeature = pEnumFeat.Next
Wend
End Sub
IPolyline接口的第四个方法Generalize (maxAllowableOffset ) (方法,用道格拉斯普克发来简化polyline)
IPolyline接口的第五个方法Weed (maxAllowableOffsetFactor ) (方法,和方法Generalize类似,均为简化polyline的方法,不同的是参数。)
8. 关于IGeometry接口(esriGeometry)
Public Sub t_IGeometry_polygon()
Dim pID As New UID
pID = "esriEditor.editor"
Dim pEditor As IEditor
Dim pApp As IApplication
Set pApp = Application
Set pEditor = pApp.FindExtensionByCLSID(pID)
If pEditor.SelectionCount <> 1 Then
MsgBox "select one polygon"
Exit Sub
End If
Dim pEnumFeat As IEnumFeature
Dim pFeature As IFeature
Set pEnumFeat = pEditor.EditSelection
Dim pGeometry As IGeometry
Set pFeature = pEnumFeat.Next
While Not pFeature Is Nothing
If pFeature.Shape.GeometryType = esriGeometryPolygon Then
(通过pFeature.Shape获得Geometry)
Set pGeometry = pFeature.Shape
MsgBox "+++Polygon::IGeometry properties..." & vbCrLf _
& "Dimension = " & pGeometry.Dimension & vbCrLf _
& "Geometry type = " & pGeometry.GeometryType & vbCrLf _
& "Envelope = " & pGeometry.Envelope.XMin & "," & pGeometry.Envelope.YMin & "," _
& pGeometry.Envelope.XMax & "," & pGeometry.Envelope.YMin & vbCrLf _
& "IsEmpty = " & pGeometry.IsEmpty & vbCrLf _
& "SpatialReference = " & pGeometry.SpatialReference.Name
End If
Set pFeature = pEnumFeat.Next
Wend
End Sub
复制代码
IGeometry接口的第一个属性Dimension(只读,返回一个类型为esriGeometryDimension的该图形的几何维度)
-1 esriGeometryNoDimension
1 esriGeometry0Dimension
2 esriGeometry1Dimension
4 esriGeometry2Dimension
5 esriGeometry25Dimension
6 esriGeometry3Dimension
IGeometry接口的第二个属性Extent(只读,返回一个类型为IEnvelope的该图形的几何范围的最大边框)
IGeometry接口的第三个属性GeometryType(只读,返回一个类型为esriGeometryType的该图形的几何类型)
esriGeometryNull = 0
esriGeometryPoint = 1
esriGeometryMultipoint = 2
esriGeometryPolyline = 3
esriGeometryPolygon = 4
esriGeometryEnvelope = 5
esriGeometryPath = 6
esriGeometryAny = 7
esriGeometryMultiPatch = 9
esriGeometryRing = 11
esriGeometryLine = 13
esriGeometryCircularArc = 14
esriGeometryBezier3Curve = 15
esriGeometryEllipticArc = 16
esriGeometryBag = 17
esriGeometryTriangleStrip = 18
esriGeometryTriangleFan = 19
esriGeometryRay = 20
esriGeometrySphere = 21
9. 关于IArea接口(esriGeometry)
Public Sub t_IArea_polygon()
Dim pID As New UID
pID = "esriEditor.editor"
Dim pEditor As IEditor
Dim pApp As IApplication
Set pApp = Application
Set pEditor = pApp.FindExtensionByCLSID(pID)
If pEditor.SelectionCount <> 1 Then
MsgBox "select one polygon"
Exit Sub
End If
Dim pEnumFeat As IEnumFeature
Dim pFeature As IFeature
Dim i As Long
Set pEnumFeat = pEditor.EditSelection
Dim pArea As IArea
Dim pCenter As IPoint
Dim pLabel As IPoint
Set pCenter = New Point
Set pLabel = New Point
Set pFeature = pEnumFeat.Next
While Not pFeature Is Nothing
If pFeature.Shape.GeometryType = esriGeometryPolygon Then
Set pArea = pFeature.Shape
MsgBox "+++Polygon::IArea properties..." & vbCrLf _
& "Area = " & pArea.Area & vbCrLf _
& "Center.X = " & pArea.Centroid.X & vbCrLf _
& "Center.Y = " & pArea.Centroid.Y & vbCrLf _
& pArea.LabelPoint.X & vbCrLf _
& "LabelPoint.Y = " & pArea.LabelPoint.Y
pArea.QueryCentroid pCenter
pArea.QueryLabelPoint pLabel
MsgBox "+++Polygon::IArea Queries..." & vbCrLf _
& "Center = " & pCenter.X & "," & pCenter.Y & vbCrLf _
& "Label = " & pLabel.X & "," & pLabel.Y & vbCrLf
End If
Set pFeature = pEnumFeat.Next
Wend
End Sub
复制代码
IArea接口的第一个属性Area(只读,返回一个double类型的数值,为此Area的面积)
IArea接口的第二个属性Centroid(只读,返回一个IPoint类型的变量,为此Area的重心)
IArea接口的第三个属性LablePoint(只读,返回一个IPoint类型的变量,为此Area的标签的位置,一般都在此Area的内部)
IArea接口的第四个方法QueryCentroid (Center ) (方法,Center参数为一个IPoint类型的变量,通过调用此方法将重心点赋值给参数Center)
IArea接口的第五个方法QueryLablePoint (LablePoint ) (方法,LablePoint参数为设置IPoint类型的变量,通过调用此方法将标签点赋值给参数LablePoint)
10. 关于IEnvelope接口(esriGeometry)
应用:(中心放大)
Public Sub ZoomInCenter()
Dim pMxDocument As IMxDocument
Dim pActiveView As IActiveView
Dim pDisplayTransform As IDisplayTransformation
Dim pEnvelope As IEnvelope
Dim pCenterPoint As IPoint
Set pMxDocument = Application.Document
Set pActiveView = pMxDocument.FocusMap
Set pDisplayTransform = pActiveView.ScreenDisplay.DisplayTransformation
Set pEnvelope = pDisplayTransform.VisibleBounds
'In this case, we could have set pEnvelope to IActiveView::Extent
'Set pEnvelope = pActiveView.Extent
Set pCenterPoint = New Point
pCenterPoint.x = ((pEnvelope.XMax - pEnvelope.XMin) / 2) + pEnvelope.XMin
pCenterPoint.y = ((pEnvelope.YMax - pEnvelope.YMin) / 2) + pEnvelope.YMin
pEnvelope.width = pEnvelope.width / 2
pEnvelope.height = pEnvelope.height / 2
pEnvelope.CenterAt pCenterPoint
pDisplayTransform.VisibleBounds = pEnvelope
pActiveView.Refresh
End Sub
复制代码
IEnvelope接口的第一个方法CenterAt(pPoint) (方法,将这个矩形的边框移动到参数pPoint的位置,但是其他属性不变,如它的Width和Height)
例子代码:
' The example shows how to move an Envelope to a new
' center point (pPoint).
Public Sub t_EnvCenterAt()
Dim pEnv1 As IEnvelope
Dim pPoint As IPoint
Set pEnv1 = New Envelope
Set pPoint = New Point
pEnv1.PutCoords 100, 100, 200, 200
pPoint.PutCoords 0, 0
pEnv1.CenterAt pPoint
Dim dXmin As Double, dYmin As Double, dXmax As Double, dYmax As Double
pEnv1.QueryCoords dXmin, dYmin, dXmax, dYmax
If pEnv1.IsEmpty Then
MsgBox "envelope is empty"
Else
MsgBox dXmin & "," & dYmin & "," & dXmax & "," & dYmax
End If
End Sub
复制代码
IEnvelope接口的长宽属性Height和Width属性(读写,可以通过该属性获取或设置该边框的长和宽)
IEnvelope接口的4个顶点属性UpperLeft、UpperRight、LowerLeft和LowerRight(读写,返回IPoint类型的四个顶点,比直接获得最值坐标更加方便严谨)
例子代码:
Private Sub Form_Load()
Set m_pEnveLope = New Envelope
Set m_pCPoint = New Point
m_pEnveLope.XMin = 0
m_pEnveLope.YMin = 0
m_pEnveLope.XMax = 0
m_pEnveLope.YMax = 0
m_pCPoint.X = 0
m_pCPoint.Y = 0
Set m_pLowerLeft = New Point
Set m_pLowerRight = New Point
Set m_pUpperLeft = New Point
Set m_pUpperRight = New Point
update_props
End Sub
Private Sub update_props()
Set m_pLowerLeft = m_pEnveLope.LowerLeft
edtLlx.Text = m_pLowerLeft.X
edtLly.Text = m_pLowerLeft.Y
Set m_pLowerRight = m_pEnveLope.LowerRight
edtLrx.Text = m_pLowerRight.X
edtLry.Text = m_pLowerRight.Y
Set m_pUpperLeft = m_pEnveLope.UpperLeft
edtUlx.Text = m_pUpperLeft.X
edtUly.Text = m_pUpperLeft.Y
Set m_pUpperRight = m_pEnveLope.UpperRight
edtUrx.Text = m_pUpperRight.X
edtUry.Text = m_pUpperRight.Y
End Sub
复制代码
IEnvelope接口的最值坐标属性XMax、XMin、YMax和YMin(读写,可以通过该属性获取或设置该边框的四个顶点的坐标)
IEnvelope接口的第五个方法Union (inEnvelope ) (方法,将参数输入的几何边框和调用该方法的几何边框求并集,并将结果赋值给第一个边框,即调用此方法的object)
例子代码:
Public Sub t_EnvUnion()
Dim pEnv1 As IEnvelope
Dim pEnv2 As IEnvelope
Set pEnv1 = New Envelope
Set pEnv2 = New Envelope
pEnv1.PutCoords 100, 100, 200, 200
pEnv2.PutCoords 150, 150, 250, 250
pEnv1.Union pEnv2
Dim dXmin As Double, dYmin As Double, dXmax As Double, dYmax As Double
pEnv1.QueryCoords dXmin, dYmin, dXmax, dYmax
End Sub
复制代码
IEnvelope接口的第六个方法Union (inEnvelope ) (方法,返回与输入参数相交的区域的几何边框,并将结果赋值给第一个边框,即调用此方法的object)
例子代码:
' The example shows how to intersect 2 envelopes. The result is put in
' the first envelope.
Public Sub t_EnvIntersect()
Dim pEnv1 As IEnvelope
Dim pEnv2 As IEnvelope
Set pEnv1 = New Envelope
Set pEnv2 = New Envelope
pEnv1.PutCoords 100, 100, 200, 200
pEnv2.PutCoords 150, 150, 250, 250
pEnv1.Intersect pEnv2
Dim dXmin As Double, dYmin As Double, dXmax As Double, dYmax As Double
pEnv1.QueryCoords dXmin, dYmin, dXmax, dYmax
If pEnv1.IsEmpty Then
MsgBox "envelope is empty"
Else
MsgBox dXmin & "," & dYmin & "," & dXmax & "," & dYmax
End If
End Sub
复制代码
IEnvelope接口的第七个方法PutCoords (XMin, YMin,XMax,YMax) (方法,将新建的一个边框的4个极坐标设置为输入的参数)
例子代码:
Public Function CreateEnvXY(dblXMin As Double, dblYMin As Double, _
dblXMax As Double, dblYMax As Double) As IEnvelope
Set CreateEnvXY = New esriGeometry.Envelope
CreateEnvXY.PutCoords dblXMin, dblYMin, dblXMax, dblYMax
End Function
复制代码
IEnvelope接口的第八个方法QueryCoords (XMin, YMin,XMax,YMax)(方法,将已有的一个边框的4个极坐标输出到参数当中以备后用)
IEnvelope接口的第九个方法Expand (dx, dy, asRatio) (方法,按照输入的dx与dy参数来放大或者缩小当前的边框,用与对ArcMap窗体的中心放大或缩小,或者点击屏幕获得点击点的坐标,并将中心点设置成点击点,并进行一定比例的放大或者缩小)
例子代码:
Public Sub t_EnvExpand()
Dim pEnv1 As IEnvelope
Set pEnv1 = New Envelope
pEnv1.PutCoords 100, 100, 200, 200
pEnv1.Expand 0.5, 0.5, True
Dim dXmin As Double, dYmin As Double, dXmax As Double, dYmax As Double
pEnv1.QueryCoords dXmin, dYmin, dXmax, dYmax
If pEnv1.IsEmpty Then
MsgBox "envelope is empty"
Else
MsgBox dXmin & "," & dYmin & "," & dXmax & "," & dYmax
End If
End Sub
复制代码
注意!!!一般情况设置为True,来控制倍数的放大
Expand scales the size of the Envelope. If asRatio = FALSE, the expansion is additive.
XMin = XMin - dx
YMin = YMin - dy
XMax = XMax + dx
YMax = YMax + dy
If asRatio = TRUE, the expansion is multiplicative.
XMin = (XMin - dx*Width)/2
YMin = (YMin - dy*Height)/2
XMax = (XMax + dx*Width)/2
YMax = (YMax + dy*Height)/2
The Envelope remains centered at the same position.
复制代码
IEnvelope接口的第十个方法Offset (X, Y) (方法,将已有的一个边框的按照输入参数的大小来进行水平竖直的移动)
例子代码:
Private Sub btnOffset_Click()
m_pEnveLope.Offset 10, 20
update_props
End Sub
注意!!!
The new position of the Envelope is as follows:
new XMin= old XMin + X
new YMin = old YMin + Y
new XMax = old XMax + X
new YMax = old YMax + Y
复制代码
11. 关于IFeature接口(esriGeoDatabase)
IFeature接口的第一个属性Class(只读)
IFeature接口的第二个方法Delete(方法,删除该行。因为一个Feature在表格中对应的就是一行数据,删除该行就能相应的删除这个Feature)
IFeature接口的第三个属性Extent(只读,获取该Feature要素在地图上的一个矩形范围,返回值为IEnvelope类型)
IFeature接口的第四个属性FeatureType(只读,获取该Feature要素的要素类型,返回值为枚举类型的esriFeatureType)
IFeature接口的第五个属性Fields(只读,获取该Feature要素的字段集合,返回值为IFields类型)
IFeature接口的第六个属性Shape(读写,获取该Feature要素的图形,返回值为IGeometry类型,或者各种实体化的类型,如IPolyline)
IFeature接口的第七个属性ShapeCopy(只读,克隆该Feature要素的几何图形,返回值为IGeometry类型)
IFeature 接口的第八个方法Store(方法,保存该行。)
此属性可用于对Feature要素的几何图形进行操作,步骤如下:
用IFeature.ShapeCopy方法获取一个已经存在的Geometry,或者新建一个Geometry
对Geometry进行操作
通过IFeature.Shape属性将Geometry写入
通过IFeature.Store方法保存该Feature要素
例子代码:
Dim pFeature As IFeature
Dim pGeo As IGeometry
Set pGeo = pFeature.ShapeCopy
'Change the shape
pFeature.Shape = pGeo
pFeature.Store
复制代码
IFeature接口的第九个属性Value(读写,利用字段的索引进行对该要素该字段的值的读写)
注意,索引Index是从0开始的。
object.Value(Index ) = [ value ]
IFeature 接口的第十个属性Table(只读,将该行要素转换成ITable格式的数据,即可对一张表进行数据操作,具体方法查看ITable接口)
例子代码:
Dim pTable As ITable
Set pTable = pRow.Table
复制代码
12. 关于IRow接口(esriGeoDatabase)
IRow接口的第一个方法Delete(方法,删除该行)
IRow接口的第二个属性Fields(只读,获取该Feature要素的字段集合,返回值为IFields类型)此方法类似于IFeature接口的Fields属性
IRow 接口的第三个方法Store(方法,保存该行。)此方法类似于IFeature接口的Store方法
IRow接口的第四个属性Table(只读,获取该行所在的表格,返回值为ITable类型)
例子代码:
Dim pTable As ITable
Set pTable = pRow.Table
复制代码
IRow接口的第五个属性Value(Index) (读写,获取该行在参数索引的字段的值,注意,索引Index是从0开始的。)
object.Value(Index ) = [ value ]
IRow接口的第六个属性HasOID(只读,判断指出该行是否有OID)
IRow接口的第七个属性OID(只读,获取该行的OID值)
例子代码:
If pRow.HasOID Then
Debug.Print pRow.OID
End If
复制代码
13. 关于IFeatureClass接口(esriGeoDatabase)
Dim pFeatcls As IFeatureClass
Dim pFeatLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As IMap
Set pDoc = ThisDocument
Set pMap = pDoc.Maps.Item(0)
Set pFeatLayer = pMap.Layer(0)
Set pFeatcls = pFeatLayer.FeatureClass
复制代码
IFeatureClass接口的第一个方法AddField(Field) (方法,增加一个属性字段到这个要素类,其中传入的参数为一个IField接口的变量,此变量可以由其他要素类获得并赋值给要操作的要素类,可用IFeilds接口的Field属性来获得)
IFeatureClass接口的第二个方法DeleteField(Field) (方法,删除一个属性字段,其中传入的参数为一个IField接口的变量)
IFeatureClass接口的第三个属性Fields(只读,获取该要素类的全部属性字段,返回一个IFields类型的变量)
例子代码:
'Assume we have a reference to a feature class, pFC
Dim pFields As IFields
Dim pField As IField
Set pFields = pFC.Fields
Set pField = pFields.Field(pFields.FindField("MyField"))
pFC.DeleteField pField
复制代码
IFeatureClass接口的第四个方法FindField(Name) (方法,去查找在该要素类里面是否含有参数名字的属性字段,如果有,则返回索引,没有,则返回-1)
IFeatureClass接口的第五个属性AreaField(只读,获取属性字段为geometry的那一个Field)
例子代码:
Dim pFeatcls As IfeatureClass
Dim pFeatLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As Imap
Set pDoc = ThisDocument
Set pMap = pDoc.Maps.Item(0)
Set pFeatLayer = pMap.Layer(0)
Set pFeatcls = pFeatLayer.FeatureClass
Dim pFld As IField
Set pFld = pFeatcls.AreaField
If Not pFld Is Nothing Then
MsgBox pFld.Name
End If
复制代码
IFeatureClass接口的第六个方法Search (filter, Recycling) (方法,去得到一个IFeatureCursor类型的游标,该游标由filter来控制赛选,如果filter等于null,则返回整个featureclass的游标,再用IfeatureCursor的NextFeature的方法依次得到每一个Feature)
例子代码:
Dim pFeatcls As IFeatureClass
Dim pFeatLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As IMap
Set pDoc = ThisDocument
Set pMap = pDoc.Maps.Item(0)
Set pFeatLayer = pMap.Layer(0)
Set pFeatcls = pFeatLayer.FeatureClass
' +++ create the query filter, and give
' +++ it a where clause
Dim pQFilt As IQueryFilter
Dim pFeatCur As IFeatureCursor
Set pQFilt = New QueryFilter
pQFilt.WhereClause = "subtype = 'COM'"
Set pFeatCur = pFeatcls.Search(pQFilt, False)
' +++ get the area field
Dim pFlds As IFields
Dim pFld As IField
Dim lAIndex As Long
Set pFlds = pFeatcls.Fields
lAIndex = pFlds.FindField ("Area")
Set pFld = pFlds.Field(lAIndex)
' +++ a variable to hold the total area
Dim dtotArea As Double
dtotArea = 0#
' +++ loop through all of the features and
' +++ calculate the sum of all of the areas
Dim pFeat As IFeature
Set pFeat = pFeatCur.NextFeature
Do
dtotArea = dtotArea + pFeat.Value(lAIndex)
Set pFeat = pFeatCur.NextFeature
Loop Until pFeat Is Nothing
' +++ send the total area to a message box
MsgBox dtotArea
复制代码
IFeatureClass接口的第七个方法Insert(useBuffering) (方法,去得到一个IFeatureCursor类型的游标,来用作插入新的Features,useBuffering是一个布尔型参数,当为True时即可以插入新的Feature,再用IFeatureCursor的InsertFeature (buffer )的方法去插入一个新的Feature)
例子代码:
Dim pFeatcls As IFeatureClass
Dim pFeatLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As Imap
Set pDoc = ThisDocument
Set pMap = pDoc.Maps.Item(0)
Set pFeatLayer = pMap.Layer(0)
Set pFeatcls = pFeatLayer.FeatureClass
Dim pFeatCur As IFeatureCursor
Dim pFeatBuf As IFeatureBuffer
Dim v As Variant
Set pFeatCur = pFeatcls.Insert(True)
Set pFeatBuf = pFeatcls.CreateFeatureBuffer
v = pFeatCur.InsertFeature(pFeatBuf)
复制代码
IFeatureClass接口的第八个方法CreateFeatureBuffer(方法,新建一个缓冲,返回一个IFeatureBuffer类型的变量,然后再对这个变量进行操作)
例子代码:
Dim pFeatcls As IFeatureClass
Dim pFeatLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As IMap
Set pDoc = ThisDocument
Set pMap = pDoc.Maps.Item(0)
Set pFeatLayer = pMap.Layer(0)
Set pFeatcls = pFeatLayer.FeatureClass
'create a feature cursor and feature buffer interface
Dim pFeatCur As IFeatureCursor
Dim pFeatBuf As IFeatureBuffer
'open the feature cursor and feature buffer
Set pFeatCur = pFeatcls.Insert(True)
Set pFeatBuf = pFeatcls.CreateFeatureBuffer
'get the list of fields
Dim pFlds As IFields
Dim pFld As IField
Dim i As Long
Dim pPolygon As IPolygon
Dim pPolyline As IPolyline
Dim pPt As IPoint
Set pPolygon = New Polygon
Set pPolyline = New Polyline
Set pPt = New Point
'find the geometry field, based on the shape type,
'set the value for the field to the appropriate object
Set pFlds = pFeatcls.Fields
For i = 1 To pFlds.FieldCount - 1
Set pFld = pFlds.Field(i)
If (pFld.Type = esriFieldTypeGeometry) Then
Dim pGeom As IGeometry
Select Case pFeatcls.ShapeType
Case esriGeometryPolygon
Set pGeom = pPolygon
Case esriGeometryPolyline
Set pGeom = pPolyline
Case esriGeometryPoint
Set pGeom = pPt
End Select
'set the value in the feature buffer
pFeatBuf.Value(i) = pGeom
'if it is not a geometry column, determine what kind of
'field it is, and insert the equivalent of a null value
'for that field type
Else
If pFld.Type = esriFieldTypeInteger Then
pFeatBuf.Value(i) = CLng(0)
ElseIf pFld.Type = esriFieldTypeDouble Then
pFeatBuf.Value(i) = CDbl(0)
ElseIf pFld.Type = esriFieldTypeSmallInteger Then
pFeatBuf.Value(i) = CInt(0)
ElseIf pFld.Type = esriFieldTypeString Then
pFeatBuf.Value(i) = ""
Else
MsgBox "Need to handle this field type"
End If
End If
Next i
'insert the feature from the buffer into the database
pFeatCur.InsertFeature pFeatBuf
复制代码
14. 关于ITable接口(esriGeoDatabase)
ITable是把要素类当成一个表格来看,每一列对应一个字段(Field),每一行对应一个要素(Feature),所以对要素类(Ifeatureclass)接口的操作均可以类似的在Itable接口中找到。
两个接口可以进行如下强制转化:
VB语言
Dim pFC As IFeatureClass
Dim pTable As ITable
Set pTable = pFC
复制代码
C#语言
IFeatureClass pFC;
ITable pTable;
pTable = (ITable)pFC;
复制代码
ITable接口的第一个方法AddField(Field) (方法,增加一个属性字段到这个表,其中传入的参数为一个IField接口的变量,此变量可以由其他表获得并赋值给要操作的表,可用IFeilds接口的Field属性来获得)
ITable接口的第二个方法GetRow(OID) (方法,通过OID来从表格数据库中获取一行,返回一个IRow接口的变量)此方法类似于IFeatureClass接口的GetFeature方法
例子代码:
Dim pWorkspace As IWorkspace
Dim pFact As IWorkspaceFactory
' This example uses an SDE connection. This code works the
' same for any open IWorkspace.
Dim pPropset As IPropertySet
Set pPropset = New PropertySet
With pPropset
.SetProperty "Server", "fred"
.SetProperty "Instance", "5203"
.SetProperty "Database", "sdedata"
.SetProperty "user", "test"
.SetProperty "password", "test"
.SetProperty "version", "sde.DEFAULT"
End With
Set pFact = New SdeWorkspaceFactory
Set pWorkspace = pFact.Open(pPropset, Me.hWnd)
Dim pFeatureWorkspace As IFeatureWorkspace
Set pFeatureWorkspace = pWorkspace
Dim pTable As ITable
Set pTable = pFeatureWorkspace.OpenTable("avement")
Dim pRow As IRow
Set pRow = pTable.GetRow(59)
Debug.Print pRow.Value(2)
复制代码
ITable接口的第三个方法GetRows(oids, Recycling) (方法,得到一个游标ICursor,通过一个oids的OID数组参数和一个Recycling的布尔类型的参数,一般为True)此方法类似于IFeatureClass接口的GetFeatures方法
例子代码:
Dim iOIDList() As Long
Dim iOIDListCount As Long
iOIDListCount = 5
ReDim iOIDList(iOIDListCount)
iOIDList(0) = 1
iOIDList(1) = 2
iOIDList(2) = 3
iOIDList(3) = 4
iOIDList(4) = 50
Dim pCursor As ICursor
Set pCursor = pTable.GetRows(iOIDList, True)
Dim pRow As IRow
Set pRow = pCursor.NextRow
While Not pRow Is Nothing
Debug.Print pRow.Value(2)
Set pRow = pCursor.NextRow
Wend
复制代码
ITable接口的第四个方法RowCount(QueryFilter) (方法,得到满足查询过滤器条件的行数。此方法IFeatureClass接口没有,所以是一个很好的有条件查询要素数量的一个方法)
本篇文章来源于 网址:http://www.gissky.net/Article/1879.htm |
|