ArcGIS接口详细说明之个人开发心得

1.        关于IField接口(esriGeoDatabase)3楼

2.        关于IFieldEdit接口(esriGeoDatabase)4楼

3.        关于IFields接口(esriGeoDatabase)5楼

4.        关于IPoint接口(esriGeometry)6楼

5.        关于IPointArray接口(esriGeometry)7楼

6.        关于IPointCollection接口(esriGeometry)8楼        

7.        关于IPolyline接口(esriGeometry)9楼

8.        关于IGeometry接口(esriGeometry)10楼

9.        关于IArea接口(esriGeometry)11楼

10.        关于IEnvelope接口(esriGeometry)12楼

11.        关于IFeature接口(esriGeoDatabase)13楼

12.        关于IRow接口(esriGeoDatabase)14楼

13.        关于IFeatureClass接口(esriGeoDatabase)15楼

14.        关于ITable接口(esriGeoDatabase)16楼

15.        关于IFeatureCursor接口(esriGeoDatabase)17楼

16.        关于IQueryFilter接口(esriGeoDatabase)18楼

17.        关于IFeatureLayer接口(esriCarto)19楼

18.        关于IFeatureSelection接口(esriCarto)20楼

19.        关于IMap接口(esriCarto) 21楼

20.        关于IPropertySet接口(esriSystem) 22楼

21.        关于IFeatureWorkspace接口(esriGeoDatabase)23楼

22.        关于IWorkspaceEdit接口(esriGeoDatabase)24楼

23.        关于IWorkspaceFactory接口(esriGeoDatabase)25楼

24.        关于ITopologicalOperator接口(esriGeometry)26楼

25.        创建Buffer并选择里面的要素 27楼

26.        Merge要素Union要素  28楼

27.        怎样从Table中获取具体需求值的Row 42楼

28.        怎样ZoomInCenter 43楼

29.        怎样读取一个字段内的所有值 44楼

30.        怎样编辑更改属性字段的值 45楼

31.        怎样将MapControl中的Map复制到PageLayoutControl中 47楼

32.        怎样判断是否出于编辑状态 63楼

33.        怎样用点创建一个Polygon 64楼

34.        怎样运用属性来计算总面积 65楼

35.        关于属性域的一些心得 82楼

36.        怎样实现翻折Flip方法 83楼

37.        回答cumtbGIS关于ITopologicalOperator接口Clip方法的问题 87楼

38.        回答机器猫FJJ关于ISpatialFilter接口方法的问题(完整函数) 91楼

39.        回答网友韶华响当当关于更改符号的代码(完整函数) 100楼

40.        回答网友韶华响当当关于显示属性的代码(完整函数) 101楼

41.        回答gjw1015关于IFeature变量添加进List数组里的问题 112楼


1.        关于IField接口(esriGeoDatabase)

IField接口的第一个属性AliasName(只读,获得字段的别名)

IField接口的第二个方法CheckValue(Value)(方法,对于指定的属性字段,基于字段类型判断参数值是否有效,有效,则返回True,否则返回False)

例子代码: 

IFeatureClass pFC_SCP_PT;

editPT = new FieldClass();

editPT.Precision_2 = 8;

editPT.Scale_2 = 3;

editPT.Name_2 = "ELEV1";

editPT.Type_2 = esriFieldType.esriFieldTypeDouble;

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.PrintpField.AliasName

    Debug.PrintpField.DefaultValue

    Set pDomain= pField.Domain

    Debug.PrintpField.Editable

    Debug.PrintpField.IsNullable

    Debug.PrintpField.Length

    Debug.PrintpField.Name

    Debug.PrintpField.Precision

    Debug.PrintpField.Required

    Debug.PrintpField.Scale

    Debug.PrintpField.Type

    Debug.PrintpField.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.PrintpField.Name & ": " & pField.Type

Next i

IFields接口的第三个方法FindField(Name)(方法,输入想要查找的属性域字段的名称,如果有,则返回该属性域字段在此Fields的索引,没有则返回-1)

例子代码:

Dim i AsInteger

Dim pFields AsIFields

Dim pField AsIField

'Get Fields

Set pFields = pFeatClass.Fields

'Find the fieldnamed"average_income"

i =pFields.FindField("average_income")

'Set the currentfield

Set pField = pFields.Field(i)

'Delete field fromfeatureclass

pFeatClass.DeleteFieldpField

 

IFields接口的第四个方法FindFieldByAliasName(Name)(方法,与第三个方法类似,此时输入的为该列属性字段的别名,此方法不经常用)

例子代码:

Dim i AsInteger

Dim pFields AsIFields

Dim pField AsIField

'Get Fields

Set pFields = pFeatClass.Fields

'Find the fieldwiththealiasname"currentpopulation"

i =pFields.FindFieldByAliasName("currentpopulation")

'Set the currentfield

Set pField = pFields.Field(i)

'Delete field fromfeatureclass

pFeatClass.DeleteFieldpField

 

4.        关于IPoint接口(esriGeometry)

IPoint接口的第一个方法PutCoords(X,Y)(方法,设置该点的坐标)或者直接调用可以读写的属性X和Y,将坐标赋值给X和Y

例子代码:

Dim pPoint AsIPoint

Set pPoint = NewPoint

pPoint.PutCoords100,100

IPoint接口的第二个方法QueryCoords(X,Y)(方法,得到该点的坐标)

例子代码:

Dim pPoint asIPoint

Dim dX asDouble,dYasDouble

pPoint.QueryCoordsdX,dY

IPoint接口的第三个方法ConstrainAngle (constraintAngle, anchor, allowOpposite ) (方法,如果第三个参数allowOpposite为True,则将第二个参数anchor这个点作为一个原点,然后以第一个参数 constraintAngle为与x轴的角度,做一条直线,再将调用此参数的点向该直线做垂线并交于一个新点,并将调用此方法的点移动到该点)

例子代码:

'Finds the closespointtolinefrom(0,0)withangles

 'defined by stepsofpi/4(Noteallanglesinradians)

   Dim pApointAsIPoint

   Dim pNpointAsIPoint

   Dim piAsDouble

   Dim dAngleAsDouble

   Dim iAsLong

   Set pApoint =NewPoint

  pi = 4 *Atn(1)

  dAngle = 0

  pApoint.PutCoords 0,0

   Set pNpoint =NewPoint

   For i =0To7

    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 pPointAsIPoint

   Dim pNPointAsIPoint

   Dim dRadiusAsDouble

 

   Set pPoint =NewPoint

  pPoint.PutCoords 0,0

 

   Set pNPoint =NewPoint

  pNPoint.PutCoords 2,2

  dRadius = 1.4142135623731

 

  pNPoint.ConstrainDistance dRadius,pPoint

   MsgBox "Radius =" &dRadius& " x,y = " &pNPoint.X& "," & pNPoint.Y

 EndSub

 

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 pIDAsNewUID

  pID = "esriEditor.editor"

   Dim pEditorAsIEditor

   Dim pAppAsIApplication

   Set pApp =MxApplication

   Set pEditor =pApp.FindExtensionByCLSID(pID)

 

  If   pEditor.SelectionCount<> 1 Then

     MsgBox"selectoneCurve"

     ExitSub

   End If

 

   Dim pEnumFeatAsIEnumFeature

   Dim pFeatureAsIFeature

 

   Set pEnumFeat =pEditor.EditSelection

 

   Dim pCurveAsICurve

   Dim pPointFromAsIPoint

   Dim pPointToAsIPoint

 

   Set pPointFrom =NewPoint

   Set pPointTo =NewPoint

 

   Set pFeature =pEnumFeat.Next

 

   While NotpFeatureIsNothing

     IfpFeature.Shape.GeometryType= esriGeometryPolyline Or_

    esriGeometryPolyline OresriGeometryLineThen

       SetpCurve =pFeature.Shape

      pCurve.QueryFromPoint pPointFrom

      pCurve.QueryToPoint pPointTo

       MsgBox"+++ICurveproperties..."& vbCrLf _

        & "Curve.QueryFromPoint(x,y) =" &pPointFrom.X& "," & pPointFrom.Y &vbCrLf_

        & "Curve.QueryToPoint(x,y) =" &pPointTo.X& "," & pPointTo.Y &vbCrLf

     EndIf

     SetpFeature =pEnumFeat.Next

   Wend

End Sub

 

IPolyline接口的第四个方法Generalize (maxAllowableOffset ) (方法,用道格拉斯普克发来简化polyline)

IPolyline接口的第五个方法Weed (maxAllowableOffsetFactor ) (方法,和方法Generalize类似,均为简化polyline的方法,不同的是参数。)

 

8.        关于IGeometry接口(esriGeometry)

Public Sub t_IGeometry_polygon()

   Dim pIDAsNewUID

  pID = "esriEditor.editor"

   Dim pEditorAsIEditor

   Dim pAppAsIApplication

   Set pApp =Application

   Set pEditor =pApp.FindExtensionByCLSID(pID)

 

   If pEditor.SelectionCount<>  1Then

     MsgBox"selectonepolygon"

     ExitSub

   End If

 

   Dim pEnumFeatAsIEnumFeature

   Dim pFeatureAsIFeature

   Set pEnumFeat =pEditor.EditSelection

   Dim pGeometryAsIGeometry

   Set pFeature =pEnumFeat.Next

   While NotpFeatureIsNothing

     IfpFeature.Shape.GeometryType= esriGeometryPolygon Then

(通过pFeature.Shape获得Geometry)

       SetpGeometry =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

     EndIf

     SetpFeature =pEnumFeat.Next

   Wend

 EndSub

 

IGeometry接口的第一个属性Dimension(只读,返回一个类型为esriGeometryDimension的该图形的几何维度)

-1    esriGeometryNoDimension

   esriGeometry0Dimension

   esriGeometry1Dimension

   esriGeometry2Dimension

   esriGeometry25Dimension

   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 pIDAsNewUID

  pID = "esriEditor.editor"

   Dim pEditorAsIEditor

   Dim pAppAsIApplication

   Set pApp =Application

   Set pEditor =pApp.FindExtensionByCLSID(pID)

   If pEditor.SelectionCount<>  1Then

     MsgBox"selectonepolygon"

     ExitSub

   End If

 

   Dim pEnumFeatAsIEnumFeature

   Dim pFeatureAsIFeature

   Dim iAsLong

 

   Set pEnumFeat =pEditor.EditSelection

 

   Dim pAreaAsIArea

   Dim pCenterAsIPoint

   Dim pLabelAsIPoint

   Set pCenter =NewPoint

   Set pLabel =NewPoint

 

   Set pFeature =pEnumFeat.Next

 

   While NotpFeatureIsNothing

     IfpFeature.Shape.GeometryType= esriGeometryPolygon Then

       SetpArea =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

     EndIf

     SetpFeature =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 pMxDocumentAsIMxDocument

  Dim pActiveViewAsIActiveView

  Dim pDisplayTransformAsIDisplayTransformation

  Dim pEnvelopeAsIEnvelope

  Dim pCenterPointAsIPoint

 

  Set pMxDocument =Application.Document

  Set pActiveView =pMxDocument.FocusMap

  Set pDisplayTransform= pActiveView.ScreenDisplay.DisplayTransformation

  Set pEnvelope =pDisplayTransform.VisibleBounds

  'In thiscase,wecouldhavesetpEnvelopetoIActiveView::Extent

  'Set pEnvelope =pActiveView.Extent

  Set pCenterPoint =NewPoint

 

  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.CenterAtpCenterPoint

  pDisplayTransform.VisibleBounds= pEnvelope

  pActiveView.Refresh

End Sub

 

IEnvelope接口的第一个方法CenterAt(pPoint) (方法,将这个矩形的边框移动到参数pPoint的位置,但是其他属性不变,如它的Width和Height)

例子代码:

 

' The example showshowtomoveanEnvelopetoanew

'centerpoint(pPoint).

Public Sub t_EnvCenterAt()

  Dim pEnv1AsIEnvelope

  Dim pPointAsIPoint

 

  Set pEnv1 =NewEnvelope

  Set pPoint =NewPoint

 

  pEnv1.PutCoords100,100,200,200

  pPoint.PutCoords0,0

 

  pEnv1.CenterAtpPoint

 

  Dim dXminAsDouble,dYminAsDouble,dXmaxAsDouble,dYmaxAsDouble

  pEnv1.QueryCoordsdXmin,dYmin,dXmax,dYmax

 

  If pEnv1.IsEmptyThen

    MsgBox "envelopeisempty"

  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 =NewEnvelope

   Set m_pCPoint =NewPoint

  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 =NewPoint

   Set m_pLowerRight =NewPoint

   Set m_pUpperLeft =NewPoint

   Set m_pUpperRight =NewPoint

  update_props

 EndSub

 

 PrivateSubupdate_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

 EndSub

 

IEnvelope接口的最值坐标属性XMax、XMin、YMax和YMin(读写,可以通过该属性获取或设置该边框的四个顶点的坐标)

IEnvelope接口的第五个方法Union (inEnvelope ) (方法,将参数输入的几何边框和调用该方法的几何边框求并集,并将结果赋值给第一个边框,即调用此方法的object)

例子代码:

 

Public Sub t_EnvUnion()

   Dim pEnv1AsIEnvelope

   Dim pEnv2AsIEnvelope

 

   Set pEnv1 =NewEnvelope

   Set pEnv2 =NewEnvelope

 

  pEnv1.PutCoords 100,100,200,200

  pEnv2.PutCoords 150,150,250,250

 

  pEnv1.Union pEnv2

 

   Dim dXminAsDouble,dYminAsDouble,dXmaxAsDouble,dYmaxAsDouble

  pEnv1.QueryCoords dXmin,dYmin,dXmax,dYmax

End Sub

 

IEnvelope接口的第六个方法Union (inEnvelope ) (方法,返回与输入参数相交的区域的几何边框,并将结果赋值给第一个边框,即调用此方法的object)

例子代码:

 

' The example showshowtointersect2envelopes.Theresultisputin

'thefirstenvelope.

 PublicSubt_EnvIntersect()

   Dim pEnv1AsIEnvelope

   Dim pEnv2AsIEnvelope

 

   Set pEnv1 =NewEnvelope

   Set pEnv2 =NewEnvelope

 

  pEnv1.PutCoords 100,100,200,200

  pEnv2.PutCoords 150,150,250,250

 

  pEnv1.Intersect pEnv2

 

   Dim dXminAsDouble,dYminAsDouble,dXmaxAsDouble,dYmaxAsDouble

  pEnv1.QueryCoords dXmin,dYmin,dXmax,dYmax

 

   If pEnv1.IsEmptyThen

     MsgBox"envelopeisempty"

   Else

     MsgBoxdXmin &","& dYmin & "," &dXmax &","& dYmax

   End If

End Sub

 

IEnvelope接口的第七个方法PutCoords (XMin, YMin,XMax,YMax) (方法,将新建的一个边框的4个极坐标设置为输入的参数)

例子代码:

 

Public Function CreateEnvXY(dblXMinAsDouble,dblYMinAsDouble,_

                          dblXMax As Double,dblYMaxAsDouble)AsIEnvelope

   Set CreateEnvXY =NewesriGeometry.Envelope

   CreateEnvXY.PutCoordsdblXMin,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 pEnv1AsIEnvelope

   Set pEnv1 =NewEnvelope

  pEnv1.PutCoords 100,100,200,200

 

  pEnv1.Expand 0.5, 0.5,True

 

   Dim dXminAsDouble,dYminAsDouble,dXmaxAsDouble,dYmaxAsDouble

  pEnv1.QueryCoords dXmin,dYmin,dXmax,dYmax

 

   If pEnv1.IsEmptyThen

     MsgBox"envelopeisempty"

   Else

     MsgBoxdXmin &","& dYmin & "," &dXmax &","& dYmax

   End If

End Sub

 

注意!!!一般情况设置为True,来控制倍数的放大

 

Expand scales thesizeoftheEnvelope.  IfasRatio =FALSE,theexpansionisadditive.

XMin = XMin - dx

YMin = YMin - dy

XMax = XMax +dx

YMax = YMax +dy

If asRatio = TRUE,theexpansionismultiplicative.

XMin = (XMin - dx*Width)/2

YMin = (YMin - dy*Height)/2

XMax = (XMax +dx*Width)/2

YMax = (YMax +dy*Height)/2

The Envelope remainscenteredatthesameposition.

 

IEnvelope接口的第十个方法Offset (X, Y)(方法,将已有的一个边框的按照输入参数的大小来进行水平竖直的移动)

例子代码:

 

Private Sub btnOffset_Click()

  m_pEnveLope.Offset10,20

  update_props

End Sub

注意!!!

The new positionoftheEnvelopeisasfollows:

newXMin = old XMin+X

newYMin =oldYMin+Y

newXMax =oldXMax+X

newYMax =oldYMax+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 AsIFeature

Dim pGeo AsIGeometry

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 AsITable

Set pTable = pRow.Table

 

12.        关于IRow接口(esriGeoDatabase)

IRow接口的第一个方法Delete(方法,删除该行)

IRow接口的第二个属性Fields(只读,获取该Feature要素的字段集合,返回值为IFields类型)此方法类似于IFeature接口的Fields属性

IRow 接口的第三个方法Store(方法,保存该行。)此方法类似于IFeature接口的Store方法

IRow接口的第四个属性Table(只读,获取该行所在的表格,返回值为ITable类型)

例子代码:

 

Dim pTable AsITable

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 AsIFeatureClass

Dim pFeatLayer AsIFeatureLayer

Dim pDoc AsIMxDocument

Dim pMap AsIMap

 

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 haveareferencetoafeatureclass,pFC

Dim pFields AsIFields

Dim pField AsIField

Set pFields = pFC.Fields

Set pField = pFields.Field(pFields.FindField("MyField"))

 

pFC.DeleteFieldpField

 

IFeatureClass接口的第四个方法FindField(Name)(方法,去查找在该要素类里面是否含有参数名字的属性字段,如果有,则返回索引,没有,则返回-1)

IFeatureClass接口的第五个属性AreaField(只读,获取属性字段为geometry的那一个Field)

例子代码:

 

Dim pFeatcls AsIfeatureClass

Dim pFeatLayer AsIFeatureLayer

Dim pDoc AsIMxDocument

Dim pMap AsImap

 

Set pDoc = ThisDocument

Set pMap = pDoc.Maps.Item(0)

Set pFeatLayer = pMap.Layer(0)

Set pFeatcls = pFeatLayer.FeatureClass  

Dim pFld AsIField

Set pFld = pFeatcls.AreaField

 

If Not pFldIsNothingThen

MsgBox pFld.Name

End If

 

IFeatureClass接口的第六个方法Search (filter, Recycling) (方法,去得到一个IFeatureCursor类型的游标,该游标由filter来控制赛选,如果filter等于null,则返回整个featureclass的游标,再用IfeatureCursor的NextFeature的方法依次得到每一个Feature)

例子代码:

 

Dim pFeatcls AsIFeatureClass

Dim pFeatLayer AsIFeatureLayer

Dim pDoc AsIMxDocument

Dim pMap AsIMap

 

Set pDoc = ThisDocument

Set pMap = pDoc.Maps.Item(0)

Set pFeatLayer = pMap.Layer(0)

Set pFeatcls = pFeatLayer.FeatureClass

 

'+++createthequeryfilter,andgive

'+++itawhereclause

Dim pQFilt AsIQueryFilter

Dim pFeatCur AsIFeatureCursor

 

Set pQFilt = NewQueryFilter

pQFilt.WhereClause= "subtype = 'COM'"

Set pFeatCur = pFeatcls.Search(pQFilt,False)

 

'+++gettheareafield

Dim pFlds AsIFields

Dim pFld AsIField

Dim lAIndex AsLong

 

Set pFlds = pFeatcls.Fields

lAIndex =pFlds.FindField("Area")

Set pFld = pFlds.Field(lAIndex)

 

'+++avariabletoholdthetotalarea

Dim dtotArea AsDouble

dtotArea =0#

 

'+++loopthroughallofthefeaturesand

'+++calculatethesumofalloftheareas

Dim pFeat AsIFeature

Set pFeat = pFeatCur.NextFeature

Do

dtotArea =dtotArea+pFeat.Value(lAIndex)

Set pFeat = pFeatCur.NextFeature

Loop Until pFeatIsNothing

 

'+++sendthetotalareatoamessagebox

MsgBox dtotArea

 

IFeatureClass接口的第七个方法Insert(useBuffering) (方法, 去得到一个IFeatureCursor类型的游标,来用作插入新的Features,useBuffering是一个布尔型参数,当为True时即可以 插入新的Feature,再用IFeatureCursor的InsertFeature (buffer)的方法去插入一个新的Feature)

例子代码:

 

Dim pFeatcls AsIFeatureClass

Dim pFeatLayer AsIFeatureLayer

Dim pDoc AsIMxDocument

Dim pMap AsImap

 

Set pDoc = ThisDocument

Set pMap = pDoc.Maps.Item(0)

Set pFeatLayer = pMap.Layer(0)

Set pFeatcls = pFeatLayer.FeatureClass

 

Dim pFeatCur AsIFeatureCursor

Dim pFeatBuf AsIFeatureBuffer

Dim v AsVariant

 

Set pFeatCur = pFeatcls.Insert(True)

Set pFeatBuf = pFeatcls.CreateFeatureBuffer

v =pFeatCur.InsertFeature(pFeatBuf)

 

IFeatureClass接口的第八个方法CreateFeatureBuffer(方法,新建一个缓冲,返回一个IFeatureBuffer类型的变量,然后再对这个变量进行操作)

例子代码:

 

Dim pFeatcls AsIFeatureClass

Dim pFeatLayer AsIFeatureLayer

Dim pDoc AsIMxDocument

Dim pMap AsIMap

 

Set pDoc = ThisDocument

Set pMap = pDoc.Maps.Item(0)

Set pFeatLayer = pMap.Layer(0)

Set pFeatcls = pFeatLayer.FeatureClass

 

'createafeaturecursorandfeaturebufferinterface

Dim pFeatCur AsIFeatureCursor

Dim pFeatBuf AsIFeatureBuffer

 

'openthefeaturecursorandfeaturebuffer

Set pFeatCur = pFeatcls.Insert(True)

Set pFeatBuf = pFeatcls.CreateFeatureBuffer

 

'getthelistoffields

Dim pFlds AsIFields

Dim pFld AsIField

Dim i AsLong

Dim pPolygon AsIPolygon

Dim pPolyline AsIPolyline

Dim pPt AsIPoint

 

Set pPolygon = NewPolygon

Set pPolyline = NewPolyline

Set pPt = NewPoint

 

'findthegeometryfield,basedontheshapetype,

'setthevalueforthefieldtotheappropriateobject

Set pFlds = pFeatcls.Fields

For i = 1TopFlds.FieldCount1

Set pFld = pFlds.Field(i)

If (pFld.Type = esriFieldTypeGeometry)Then

Dim pGeom AsIGeometry

    Select CasepFeatcls.ShapeType

       CaseesriGeometryPolygon

          SetpGeom =pPolygon

       CaseesriGeometryPolyline

          SetpGeom =pPolyline

       CaseesriGeometryPoint

          SetpGeom =pPt

    End Select

 

   'set the valueinthefeaturebuffer

   pFeatBuf.Value(i) = pGeom

 

   'if it isnotageometrycolumn,determinewhatkindof

   'field it is,andinserttheequivalentofanullvalue

   'for that fieldtype

   Else

     IfpFld.Type =esriFieldTypeIntegerThen

      pFeatBuf.Value(i) = CLng(0)

     ElseIfpFld.Type =esriFieldTypeDoubleThen

      pFeatBuf.Value(i) = CDbl(0)

     ElseIfpFld.Type =esriFieldTypeSmallIntegerThen

      pFeatBuf.Value(i) = CInt(0)

     ElseIfpFld.Type =esriFieldTypeStringThen

      pFeatBuf.Value(i) = ""

     Else

       MsgBox"Needtohandlethisfieldtype"

     EndIf

   End If

Next i

 

'insertthefeaturefromthebufferintothedatabase

pFeatCur.InsertFeaturepFeatBuf

 

14.        关于ITable接口(esriGeoDatabase)

ITable是把要素类当成一个表格来看,每一列对应一个字段(Field),每一行对应一个要素(Feature),所以对要素类(IFeatureClass)接口的操作均可以类似的在Itable接口中找到。

两个接口可以进行如下强制转化:

VB语言

Dim pFC AsIFeatureClass

Dim pTable AsITable

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 AsIWorkspace

Dim pFact AsIWorkspaceFactory

 

' This example usesanSDEconnection.Thiscodeworksthe

'sameforanyopenIWorkspace.

 

Dim pPropset AsIPropertySet

Set pPropset = NewPropertySet

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 = NewSdeWorkspaceFactory

Set pWorkspace = pFact.Open(pPropset,Me.hWnd)

Dim pFeatureWorkspaceAsIFeatureWorkspace

Set pFeatureWorkspace =pWorkspace

 

Dim pTable AsITable

Set pTable = pFeatureWorkspace.OpenTable("Pavement")

Dim pRow AsIRow

Set pRow = pTable.GetRow(59)

Debug.Print pRow.Value(2)

 

ITable接口的第三个方法GetRows(oids, Recycling) (方法,得到一个游标ICursor,通过一个oids的OID数组参数和一个Recycling的布尔类型的参数,一般为True)此方法类似于IFeatureClass接口的GetFeatures方法

例子代码:

 

Dim iOIDList() AsLong

Dim iOIDListCount AsLong

iOIDListCount =5

 

ReDim iOIDList(iOIDListCount)

iOIDList(0) =1

iOIDList(1) =2

iOIDList(2) =3

iOIDList(3) =4

iOIDList(4) =50

 

Dim pCursor AsICursor

Set pCursor = pTable.GetRows(iOIDList,True)

Dim pRow AsIRow

Set pRow = pCursor.NextRow

While Not pRowIsNothing

  Debug.Print pRow.Value(2)

  Set pRow =pCursor.NextRow

Wend

 

ITable接口的第四个方法RowCount(QueryFilter) (方法,得到满足查询过滤器条件的行数。此方法IFeatureClass接口没有,所以是一个很好的有条件查询要素数量的一个方法)

 

15.        关于IFeatureCursor接口(esriGeoDatabase)

Dim pFeatureSelectionAsIFeatureSelection

Set pFeatureSelection =pFeatureLayer

Dim pSelectionSet AsISelectionSet

Set pSelectionSet = pFeatureSelection.SelectionSet

Dim pFeatureCursor AsIFeatureCursor

pSelectionSet.SearchNothing,True,pFeatureCursor

Dim pDataStats AsIDataStatistics

Set pDataStats = NewDataStatistics

Set pDataStats.Cursor =pFeatureCursor

pDataStats.Field ="POP1990"

MsgBox pDataStats.Statistics.Mean

 

IFeatureCursor接口的第一个方法NextFeature(方法,将游标向前跳到下一个位置,并且返回该位置的Feature)

IFeatureCursor接口的第二个方法UpdateFeature(Feature) (方法,对当前游标位置的Feature进行更新)

例子代码:

 

'Assume we alreadyhaveareferencetoafeatureclasswiththem_pFClassvariable

  Dim pFCursorAsIFeatureCursor

  Dim pFieldsAsIFields

  Dim pFeatureAsIFeature

  Dim iAsLong

 

  Set pFields =m_pFClass.Fields

  i =pFields.FindField("NAME")

 

  'Assume wehavealreadyspecifiedaqueryfilter

  Set pFCursor =m_pFClass.Update(pQueryFilter,False)

 

  Set pFeature =pFCursor.NextFeature

  Do UntilpFeatureIsNothing

    'Update theFieldNAMEwiththevalue" Southern California"

   pFeature.value(i) = "Southern California"

   pFCursor.UpdateFeature pFeature

    Set pFeature =pFCursor.NextFeature

  Loop

 

IFeatureCursor接口的第三个方法InsertFeature(buffer) (方法,用参数传进来的属性值插入一个新的要素到数据库中,返回该插入新的Feature的ID值,其中参数类型为IFeatureBuffer)

例子代码:

 

Public Sub FeatureBufferCode()

    Dim pWorFactAsIWorkspaceFactory

    Dim pWorAsIWorkspace

    Dim pFeatWorAsIFeatureWorkspace

    Dim pWorEditAsIWorkspaceEdit

    Dim pFCAsIFeatureClass

    Dim pFeatureBufferAsIFeatureBuffer

    Dim pFeatureCursorAsIFeatureCursor

    Dim pFeatureAsIFeature

    Dim pPolylineAsIPolyline

    Dim ptAsIPoint

    Dim qAsLong,iAsLong

 

    Set pWorFact =NewAccessWorkspaceFactory

    Set pWor =pWorFact.OpenFromFile("D:\Testing\Data\GDB1.mdb",0)

    Set pFeatWor =pWor

    Set pWorEdit =pWor

    Set pFC =pFeatWor.OpenFeatureClass("casing")

 

   pWorEdit.StartEditing True

   pWorEdit.StartEditOperation

        SetpFeatureBuffer= pFC.CreateFeatureBuffer

        SetpFeatureCursor= pFC.Insert(True)

        SetpFeature =pFeatureBuffer

        SetpPolyline =NewPolyline

 

        'Create100featuresusingFeatureBufferandinsertintoafeaturecursor

        Fori =0To99

           'Create the polylinegeometrytoassigntothenewfeature

           Set pt = NewPoint

           pt.X = 2213300+i

           pt.Y = 396500+i

           pPolyline.FromPoint =pt

           Set pt = NewPoint

           pt.X = 2213300+i

           pt.Y = 396500+i

           pPolyline.ToPoint =pt

 

           'Set the feature'sshape

           Set pFeature.Shape = pPolyline

 

           'Insert the featureintothefeaturecursor

           q = pFeatureCursor.InsertFeature(pFeatureBuffer)

        Nexti

        'Flushthefeaturecursortothedatabase

       pFeatureCursor.Flush

   pWorEdit.StopEditOperation

   pWorEdit.StopEditing True

End Sub

 

 

 

16.        关于IQueryFilter接口(esriGeoDatabase)

Dim pQueryFilter AsIQueryFilter

Set pQueryFilter = NewQueryFilter

 

pQueryFilter.SubFields=   "STATE_NAME,POPULATION"

pQueryFilter.WhereClause= "STATE_NAME = 'California'"

 

Dim pFeatureCursor AsIFeatureCursor

Set pFeatureCursor = pFeatureClass.Search(pQueryFilter,False)

IQueryFilter接口的第一个属性WhereClause(读写,为过滤器设置条件语句)

注意!各种不同的数据,设置条件查询语句的语法是不相同的,如一个shp文件在设置字段的时候要加“””双引号,而在SDE数据连接中,则什么都不加;在gdb文件的语句中,符号是“*”,而在SDE或者shp文件查询中则是“%”

IQueryFilter接口的第二个属性SubFields(读写,为过滤器设置提供赛选的字段,用逗号来分隔每一个逗号,如果不设置该属性,则当做所有字段均为查找字段)

 

17.        关于IFeatureLayer接口(esriCarto)

Sub AddLayer()

'createsaFeatureLayerfromashapefileandaddstothemapinArcMap

  Dim pShpWksFactAsIWorkspaceFactory

  Dim pFeatWksAsIFeatureWorkspace

  Dim pFeatClassAsIFeatureClass

  Dim pFeatLayerAsIFeatureLayer

  Dim pDataSetAsIDataset

  Dim pMxDocAsIMxDocument

  Dim pMapAsIMap

  Set pShpWksFact =NewShapefileWorkspaceFactory

  Set pFeatWks =pShpWksFact.OpenFromFile("D:\Data\CentreCounty",0)

  Set pFeatClass =pFeatWks.OpenFeatureClass("roads")

  Set pFeatLayer =NewFeatureLayer

  Set pFeatLayer.FeatureClass= pFeatClass

  Set pDataSet =pFeatClass

  pFeatLayer.Name =pDataSet.Name

  Set pMxDoc =Application.Document

  Set pMap =pMxDoc.FocusMap

  pMap.AddLayerpFeatLayer

End Sub

 

IFeatureLayer接口的第一个属性FeatureClass(读写,设置或者读取此layer的要素类)

IFeatureLayer接口的第二个属性MaximumScale(读写,设置或者读取此layer显示的最大比例尺)

IFeatureLayer接口的第三个属性MinimumScale(读写,设置或者读取此layer显示的最小比例尺)

IFeatureLayer接口的第四个方法Search (queryFilter, recycling ) (方法,创建一个游标去查询相应设置的过滤器的查询)

IFeatureLayer接口的第五个属性Visible(读写,设置或者读取此layer的可见性)

IFeatureLayer接口的第六个属性AreaOfInterest(只读,读取此layer的最大范围,回返一个IEnvelope接口的变量)

 

18.        关于IFeatureSelection接口(esriCarto)

IFeatureSelection接口的第一个方法SelectFeatures (Filter, Method, justOne ) (方法,根据指定的标准过滤器filter和方法,选择要素,第一个参数为QueryFilter类型的变量,第二个参数为esriSelectionResultEnum类型的变量,第三个参数为布尔型变量,通常为false)

 

19.        关于IMap接口(esriCarto)

IMap接口的第一个属性Layers (uid, recursive ) (只读,第二个参数为True的时候,该属性获取第一个参数uid指定的Layers,赋值给一个IEnumLayer的变量)

例子代码:

Sub GetFeatureLayers()

  Dim pMxDocumentAsIMxDocument

  Dim pMapAsIMap

  Dim pEnumLayerAsIEnumLayer

  Dim pLayerAsILayer

  Dim pIdAsNewUID

 

  Set pMxDocument =Application.Document

  Set pMap =pMxDocument.FocusMap

  pId ="{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"

  Set pEnumLayer =pMap.Layers(pId,True)

  pEnumLayer.Reset

  Set pLayer =pEnumLayer.Next

  Do WhileNotpLayerIsNothing

    MsgBox pLayer.Name

    Set pLayer =pEnumLayer.Next

  Loop

End Sub

 

其中比较常用的UID参数值如下: 

{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer

{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer

{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer

{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer

{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer

{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer

{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer

 

IMap接口的第二个属性LayerCount(只读,返回该map里面Layer的个数)

IMap接口的第三个属性Layer(Index) (只读,返回指定索引index位置的Layer)

IMap接口的第四个方法AddLayer(Layer) (方法,向该map添加一个Layer)

例子代码:

 

Public Sub AddShapeFile()

  Dim pWorkspaceFactoryAsIWorkspaceFactory

  Dim pFeatureWorkspaceAsIFeatureWorkspace

  Dim pFeatureLayerAsIFeatureLayer

  Dim pMxDocumentAsIMxDocument

  Dim pMapAsIMap

 

  Set pWorkspaceFactory= New ShapefileWorkspaceFactory

  Set pFeatureWorkspace= pWorkspaceFactory.OpenFromFile("d:\digitizing",0)

  Set pFeatureLayer =NewFeatureLayer

  Set pFeatureLayer.FeatureClass= pFeatureWorkspace.OpenFeatureClass("Parcel")

  pFeatureLayer.Name= pFeatureLayer.FeatureClass.AliasName

  Set pMxDocument =Application.Document

  Set pMap =pMxDocument.FocusMap

  pMap.AddLayerpFeatureLayer'alternatively,wecouldcallAddLayeronIMXDocument

End Sub

 

IMap接口的第五个方法AddLayers(Layers, autoArrange) (方法,添加一个EnumLayer变量的layers到该map,第一个参数为IEnumLayer类型,第二个参数为bool型变量)

IMap接口的第六个方法ClearLayers(方法,将所有的layer从map中移除)

IMap接口的第七个方法ClearSelection(方法,将该map中选择的要素清空)

IMap接口的第八个属性SelectionCount(只读,返回该map被选中要素的个数)

IMap接口的第九个方法SelectFeature(Layer,Feature) (方法,从一个Layer中选择一个Feature)

IMap接口的第十个属性MapScale(读写,获取或者设置当前map的地图比例尺,double类型)

IMap接口的第十一个方法MoveLayer(Layer,toIndex) (方法,把一个Layer从当前的位置移动到指定的索引位置)

例子代码:

 

Public Sub MoveLayer()

    Dim pMxDocumentAsIMxDocument

    Dim pMapAsIMap

    Dim pLayerAsILayer

    Set pMxDocument =Application.Document

    Set pMap =pMxDocument.FocusMap

    Set pLayer =pMxDocument.SelectedLayer

   pMap.MoveLayer pLayer, pMap.LayerCount1

End Sub

 

IMap接口的第十二个方法SelectByShape(Shape, env, justOne) (方法,从Layer中依靠一个图形的范围shape和一个选择的环境env来选择要素,而在所有图层中只从IFeatureLayer的图层中进行选择)

 

20.        关于IPropertySet接口(esriSystem)

IPropertySet接口的第一个方法SetProperties (names, values ) (方法,设置属性)

IPropertySet接口的第二个方法SetProperty (name, value ) (方法,设置属性)

例子代码:

 

Dim pPropset AsIPropertySet

Set pPropset = NewPropertySet

With pPropset

.SetProperty"Server",m_SDEServerName

.SetProperty"Instance",m_SDEServerInst

.SetProperty"user",m_SDEServerUserName

.SetProperty"password",m_SDEServerPass

.SetProperty"Database",m_SDEDatabaseName

.SetProperty"version",m_SDEVersionName

End With

 

Dim pFactSDE AsIWorkspaceFactory

Set pFactSDE = NewSdeWorkspaceFactory

 

Dim pWorkSpaceSDE AsIWorkspace

Set pWorkSpaceSDE = pFactSDE.Open(pPropset,Me.hWnd)

 

'Example of howtouseapropertysettoopenaPersonalGeodatabaseworkspace.

 

Dim pPropset AsIPropertySet

Set pPropset = NewPropertySet

 

pPropset.SetProperty"DATABASE", "d:\\data\\Access Data\\First.mdb"

 

Dim pFactAccess AsIWorkspaceFactory

Set pFactAccess = NewAccessWorkspaceFactory

 

Dim pWorkSpaceAccess AsIWorkspace

Set pWorkSpaceAccess =pFactAccess.Open(pPropset,Me.hWnd)

21.        关于IFeatureWorkspace接口esriGeoDatabase

IFeatureWorkspace接口的第一个方法OpenFeatureClass (Name) (方法,从一个要素工作空间打开一个要素,返回一个IFeatureClass类型的变量)

例子代码:

 

'This example opensashapefileasafeatureclass.

Public Sub OpenFeatureClass_Example()

  Dim pWorkspaceFactoryAsIWorkspaceFactory

  Set pWorkspaceFactory= New ShapefileWorkspaceFactory

  Dim pFeatureWorkspaceAsIFeatureWorkspace

  Set pFeatureWorkspace =pWorkspaceFactory.OpenFromFile("D:\Data\Esridata\USA", 0)

  Dim pFeatureClassAsIFeatureClass

  Set pFeatureClass =pFeatureWorkspace.OpenFeatureClass("States")

  MsgBox "Thereare" &pFeatureClass.FeatureCount(Nothing)& " states"

End Sub

 

22.        关于IWorkspaceEdit接口esriGeoDatabase

例子代码:

 

Public Sub WorkspaceEdit()

  Dim pWorkspaceFactoryAsIWorkspaceFactory

  Set pWorkspaceFactory= New esriDataSourcesGDB.AccessWorkspaceFactory

  Dim pFeatureWorkspaceAsIFeatureWorkspace

  Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("D:\Usa.mdb",0)

  Dim pFeatureClassAsIFeatureClass

  Set pFeatureClass =pFeatureWorkspace.OpenFeatureClass("States")

  Dim pWorkspaceEditAsIWorkspaceEdit

  Set pWorkspaceEdit =pFeatureWorkspace

  Dim pFeatureAsIFeature

  Dim iResponseAsInteger

  Dim bHasEditsAsBoolean

  pWorkspaceEdit.StartEditingTrue

  pWorkspaceEdit.StartEditOperation

  Set pFeature =pFeatureClass.GetFeature(1)

  pFeature.Delete

  pWorkspaceEdit.StopEditOperation

  iResponse = MsgBox("Undooperation?", vbYesNo)

  If iResponse =vbYesThen

   pWorkspaceEdit.UndoEditOperation

  End If

  pWorkspaceEdit.HasEditsbHasEdits

  If bHasEditsThen

   pWorkspaceEdit.StopEditingMsgBox("Save edits?", vbYesNo)

  End If

End Sub

 

在实际问题中,如果有多次操作需要更改Feature的时候,切记要将操作对象重新赋值,不然在pWorkspaceEdit.StopEditing(true)的时候会出现错误,报错代码为-2147467259。

(详细代码可见WindowsApplication6的代码,其中详细操作了对字段的编辑过程,其中涉及到很多对字段Field的方法 Add(Field),Delete(Field),get_value(object value),set_value(intindex, object value)等等操作,其中还涉及到Feature经过ICursor类选取与经过FeatureClass类的方法GetFeature选取的区别)

 

23.        关于IWorkspaceFactory接口esriGeoDatabase

IWorkspaceFactory接口的第一个方法Open (ConnectionProperties, hWnd ) (方法,从一个工作工厂打开一个工作空间,并返回IWorkspace类型的变量,方法中的第一个参数ConnectionProperties是IPropertySet接口的变量)

例子代码:

 

Dim pSdeWorkspaceFactoryAsIWorkspaceFactory

Dim pSdeWorkspace AsIWorkspace

Dim pConnectionPropertiesAsIPropertySet

 

Set pConnectionProperties =NewPropertySet

With pConnectionProperties

  .SetProperty"SERVER","Redarrow"

  .SetProperty"USER","vtest"

  .SetProperty"INSTANCE","sde4_ora"

  .SetProperty"PASSWORD","go"

  .SetProperty"VERSION","SDE.DEFAULT"

End With

 

Set pSdeWorkspaceFactory =NewSdeWorkspaceFactory

Set pSdeWorkspace = pSdeWorkspaceFactory.Open(pConnectionProperties,0)

 

IWorkspaceFactory接口的第二个方法OpenFromFile (fileName, hWnd ) (方法,从一个路径打开一个工作空间,并返回IWorkspace类型的变量)

例子代码:

 

Dim pSdeWorkspaceFactoryAsIWorkspaceFactory

Dim pSdeWorkspace AsIWorkspace

 

Set pSdeWorkspaceFactory =NewSdeWorkspaceFactory

Set pSdeWorkspace = pSdeWorkspaceFactory.OpenFromFile("D:\data\redarrow.sde",0)

 

24.        关于ITopologicalOperator接口esriGeometry

例子代码:

Share a linesegment

ThissampleusesITopologicalOperator::Intersecttodotheequivalentofaselectbylocationwiththesharealinesegmentoperator.

Public Sub ShareLineSegment()

 

  Dim pMxDocAsIMxDocument

  Set pMxDoc =ThisDocument

  Dim pMapAsIMap

  Set pMap =pMxDoc.FocusMap

  Dim pFL1AsIFeatureLayer,pFL2AsIFeatureLayer

  Set pFL1 =pMap.Layer(0)  'firstlayerofmap

  Set pFL2 =pMap.Layer(1)  'secondlayer

  Dim pF1AsIFeature,pF2AsIFeature

  Set pF1 =pFL1.FeatureClass.GetFeature(1)'getfeaturewith infirstlayer'sfeatureclass

 

  Dim pfsAsIFeatureSelection

  Set pfs =pFL2

  Dim idAsLong

  id =pfs.SelectionSet.IDs.Next  'getfirstidinselectionsetanactualfunctionwouldprobablyloopovertheselection

 

  Set pF2 =pFL2.FeatureClass.GetFeature(id)'thisisline2

 

'isthereanintersectionbwthetwofeatures

  Dim pTopoAsITopologicalOperator

  Set pTopo =pF1.Shape'storetheline1shapeinthetopoop

  If NotpTopo.IsSimpleThenpTopo.Simplify

  Dim pGeomAsIGeometry

  Set pGeom =pTopo.Intersect(pF2.Shape,esriGeometry1Dimension)'getintersectionwithline2

  If pGeom.IsEmptyThen

    MsgBox "Theintersectionofthe2geometriesisemptyorconsistsofpoints"

    Exit Sub

  End If

'pGeomnowcontainstheintersectionofpf1andpf2

'becausedimension =esriGeometry1Dimension

'itisageometryoftypepolyline

'iftheintersectionofthetwofeaturesconsistedofapointorpointspgeomwouldbeempty

'wecoulduseITopologicalOperator::Intersectwith0dimensiontofindaboutthosepoints

 

'ifpGeomcontainsonlytwoverticesthentheintersectionisasimplelinesegment

'3verticesmeansit'sapolylinewithtwosuccessivesegments

'>3 means we haveseveral segments or a polyline or a combination of these

  Dim ppc1AsIPointCollection

  Set ppc1 =pGeom

  Dim lAsLong,gAsLong

  l =ppc1.PointCount

  If l =2Then

    MsgBox "Thetwofeaturesshareasinglelinesegment"

  ElseIf l =3Then

    MsgBox "Theintersectionofthetwofeaturesisapolylineconsistingof2segments"

  ElseIf l > 3 Then

    Dim pGeomCollAsIGeometryCollection

    Set pGeomColl =pGeom

   g = pGeomColl.GeometryCount

    MsgBox "Theintersectionofthetwofeaturesismadeof" &g &"polyline(s)and/orsegment(s)"

  End If

End Sub

 

可以将一个Polygon类型的数据或者Polyline的数据赋值给该接口的变量,如:

 

Set pUnionedPolylines =NewPolyline

Set pTopOp = pUnionedPolylines

 

ITopologicalOperator接口的第一个方法ConstructUnion(geometries ) (方法,合并图形的工具,输入的参数是一个IEnumGeometry类型的参数,此方法效率甚高)

ITopologicalOperator接口的第二个方法Cut(cutter, leftGeom, rightGeom) (方法,剪切图形,第一个参数为剪切的线要素,为IPloyline类型,第二个第三个参数均为剪切后的图形,为输出参数)

ITopologicalOperator接口的第三个方法Boundary(方法,获取一个图形的边框,一个Polygon的boundary是一个Polyline类型的要素,一个Polyline的boundary是一个nultipoint类型的要素,一个Point的boundary是为空的)

ITopologicalOperator接口的第四个方法Buffer(distance) (方法,创造一个Polygon的要素来显示缓冲区域,参数为缓冲距离)

例子代码:

 

'This example demonstrateshowtouseITopologicalOperator::Buffer

Sub exampleITopologicalOperator_Buffer()

Dim ptc AsIPointCollection,iAsLong,paAsIArea,ptopoAsITopologicalOperator

Dim pt(4) AsIPoint,poutPolyAsIPolygon

Set ptc = NewPolygon

'The spatial referenceshouldbesethereusingIGeometry::SpatialReference(Codeskippedhere)

For i = 0To4

   Set pt(i) =NewPoint

Next

pt(0).PutCoords0,0

pt(1).PutCoords0,10

pt(2).PutCoords10,10

pt(3).PutCoords10,0

pt(4).PutCoords0,0

ptc.AddPoints5,pt(0)

Set pa = ptc

Debug.Print "Area originalpolygon" & pa.Area

Set ptopo = ptc

Set poutPoly = ptopo.Buffer(1)'Outsidebuffer

Set pa = poutPoly

Debug.Print "Area polygonpositivedistance" & pa.Area

Set poutPoly = ptopo.Buffer(-1)'Insidebuffer

Set pa = poutPoly

Debug.Print "Area polygonnegativedistance" & pa.Area

End Sub

 

ITopologicalOperator接口的第五个方法Clip (clipperEnvelope ) (方法,输入一个IEnvelope类型的变量,来获取被这个边框剪切的要素,并将切割后的变量返回给调用方法的变量)

ITopologicalOperator接口的第六个方法QueryClipped (clipperEnvelope, clippedGeometry ) (方法,与Clip类似,但是第二个参数为剪切后返回输出的参数,本身不会改变)

ITopologicalOperator接口的第七个方法ConvexHull(方法,构造一个Geometry,大部分为Polygon类型的几何要素,该要素为调用此方法的ITopologicalOperator类型的变量最小的外边框)

ITopologicalOperator接口的第八个方法SymmetricDifference (other ) (方法,并集减去交集的部分,调用次方法的变量为第一个参数,other为第二个参数,最后返回变量到一个Geometry类型的变量,该变量的范围均在两个参数范围内,但不在两个参数相交的部分)

 

25.        创建Buffer并选择里面的要素

===============================

怎样创建buffer:(来源于管网线处理删除冗余节点)

===============================

 

Dim pTopOper As ITopologicalOperator

Set pTopOper = pfeature.Shape

Dim pGeometry As IGeometry

Set pGeometry = pTopOper.Buffer(1)

 

(注意,这个pfeature是在前面定义的pfeature=pFeatureCursor.NextFeature,不用重新定义一个)

 

 

===============================

怎样在buffer里面选择要素:

===============================

Dim pSpFilter AsISpatialFilter

Set pSpFilter = NewSpatialFilter

 

Dim pTopOper AsITopologicalOperator

Set pTopOper = pfeature.Shape

Dim pGeometry AsIGeometry

Set pGeometry = pTopOper.Buffer(1)

 

Set pSpFilter.Geometry =pGeometry

pSpFilter.SpatialRel= esriSpatialRelContains

'(esriSpatialRelContains是ISpatialFilter里面SpatialRel的一种参数esriSpatialRelEnum,值为8,代表在这个区域内包含的要素) 

          

Set m_pSelGW_D = pLyr_D

m_pSelGW_D.SelectFeaturespSpFilter, esriSelectionResultNew, False

'(m_pSelGW_D是IfeatureSelection类型的变量)           

pSpFilter.SpatialRel= esriSpatialRelIntersects

 

Set m_pSelGW_X = pLyr

m_pSelGW_X.SelectFeaturespSpFilter,esriSelectionResultNew,False

 

 

26.        Merge要素Union要素

Vb语言: 

Private Sub UnionSelected()

Dim pMxDoc AsIMxDocument

Dim pFtrLyr AsIFeatureLayer

Dim pFtrCls AsIFeatureClass

Dim pFtrSel AsIFeatureSelection

Dim pFtr AsIFeature

Dim pEnumGeom AsIEnumGeometry

Dim pEnumGeomBind AsIEnumGeometryBind

Dim pTopOp AsITopologicalOperator

Dim pUnionedPolylinesAsIPolyline

 

    ' Getareftotheselectedpolylinesinthe1stlayer

    Set pMxDoc =ThisDocument

    Set pFtrLyr =pMxDoc.FocusMap.Layer(0)

    Set pFtrSel =pFtrLyr

    Set pFtrCls =pFtrLyr.FeatureClass

 

    ' Createanenumerationoftheselectedpolyines

    Set pEnumGeom =NewEnumFeatureGeometry

    Set pEnumGeomBind =pEnumGeom

   pEnumGeomBind.BindGeometrySource Nothing,pFtrSel.SelectionSet

   pEnumGeom.Reset

 

    ' Unionthepolylines

    Set pUnionedPolylines= New Polyline

    Set pTopOp =pUnionedPolylines

   pTopOp.ConstructUnion pEnumGeom

 

    ' Addthisnewunionedpolylinetothefeatureclass

    Set pFtr =pFtrCls.CreateFeature

    Set pFtr.Shape =pUnionedPolylines

   pFtr.Store

 

End Sub

 

C#语言:

if(pFC.ShapeType=ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon)

{

    IEnumGeometry pEnumGeom;

    IEnumGeometryBindpEnumGeomBind= new EnumFeatureGeometryClass();

    IFeatureLayer pLyr =newFeatureLayerClass();

   pLyr.FeatureClass = pFC;

    IFeatureSelectionpFeatSel =(IFeatureSelection)pLyr;

 

    IQueryFilter pQfliter =newQueryFilterClass();

   pQfliter.WhereClause = this.SQLText.Text;

 

   pFeatSel.SelectFeatures(pQfliter, esriSelectionResultEnum.esriSelectionResultNew,false);

 

   pEnumGeomBind.BindGeometrySource(null,pFeatSel.SelectionSet);

   pEnumGeom = (IEnumGeometry)pEnumGeomBind;

    ITopologicalOperatorpTopo =newPolygonClass();

   pTopo.ConstructUnion(pEnumGeom);

   pGeom = (IGeometry)pTopo;

}

returnpGeom;

 

 

===============================

怎样从Table中获取具体需求值的Row:

===============================

 

ITable pTable = (ITable)pFC;

               intindex =pTable.Fields.FindField("FieldName");

               IQueryFilter pQFilter = newQueryFilterClass();

               ICursor pCur;

               pCur =pTable.Search(pQFilter,false);

               IRow pRow = new Row();

               IRow pAnswerRow = new Row();

               pRow =pCur.NextRow();

               while (pRow != null)

               {

                   stringValue = pRow.get_Value(index).ToString();

                  if (Value == "Value")

                  {

                      pAnswerRow = pRow;

                      break;

                  }

                  pRow = pCur.NextRow();

               }

===============================

怎样ZoomInCenter:

===============================

 

Public Sub ZoomInCenter()

  Dim pMxDocumentAsIMxDocument

  Dim pActiveViewAsIActiveView

  Dim pDisplayTransformAsIDisplayTransformation

  Dim pEnvelopeAsIEnvelope

  Dim pCenterPointAsIPoint

 

  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 toIActiveView::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.CenterAtpCenterPoint

  pDisplayTransform.VisibleBounds= pEnvelope

  pActiveView.Refresh

End Sub

===============================

怎样读取一个字段内的所有值:

===============================

 

 

IFeatureClass pFC= m_SDEQuery.getFeatureClass();

IFeatureCursor pFeaCur= pFC.Search(null, false);

IFeature pFeature= pFeaCur.NextFeature();

 int pFieldIndex = pFC.Fields.FindField(this.m_cboQryFld.SelectedItem.Value.ToString());

 System.Collections.ArrayListpArr = new System.Collections.ArrayList();

 while (pFeature != null)

 {

     string theFieldValue = pFeature.get_Value(pFieldIndex).ToString();

     if (!pArr.Contains(theFieldValue)&& (theFieldValue.Trim() != ""))

     {

         m_cboQryText.Items.Add(theFieldValue);

         pArr.Add(theFieldValue);

     }

     pFeature = pFeaCur.NextFeature();

 }

===============================

怎样编辑更改属性字段的值:

===============================

IRow prow = (IRow)bendiFeatureC.GetFeature(1);

MessageBox.Show(prow.Table.Fields.FieldCount.ToString());              

 

ITable ptable = (ITable)bendiFeatureC;

IQueryFilter pqfilter= new QueryFilterClass();

pqfilter.WhereClause = ""dkmc"= \'北江路南郊一公里\'";

 

IFeatureCursor pfeatcur;

pfeatcur = bendiFeatureC.Search(pqfilter, false);

 

IFeature pfff = pfeatcur.NextFeature();

 

while (pfff != null)

{

    IRowprrr = (IRow)pfff;

    prrr.set_Value(prrr.Fields.FindField("dkmc"),"北江路南郊二公里");

    pfff = (IFeature)prrr;

    pfff.Store();

    pfff = pfeatcur.NextFeature();

}

 

===============================

怎样将MapControl中的Map复制到PageLayoutControl中

===============================

 

Public Sub CopyAndOverwriteMap()

        OnErrorGoToCopyAndOverwriteMap_err

 

        DimpObjectCopyAsIObjectCopy

       pObjectCopy = New ObjectCopy

 

        DimpToCopyMapAsObject

       pToCopyMap = frmMap.AxMapControl1.Map

 

        DimpCopiedMapAsObject

       pCopiedMap = pObjectCopy.Copy(pToCopyMap)

 

        DimpToOverwriteMapAsObject

       pToOverwriteMap =PrintPageLayout.AxPageLayoutControl1.ActiveView.FocusMap

 

       pObjectCopy.Overwrite(pCopiedMap, pToOverwriteMap)

       frmMap.AxMapControl1.MousePointer

=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerArrow

       frmMain.StatusMessage.Text = ""

        PrintPageLayout.ShowDialog()

        ExitSub

 

        CopyAndOverwriteMap_err:

        MsgBox(Err.Number& " - " & Err.Description,MsgBoxStyle.Critical,Application.ProductName&" CopyMap")

        ExitSub

End Sub

===============================

怎样判断是否出于编辑状态:

===============================

 

If m_pEditor.EditState =esriStateEditingThen

  m_pEditor.StartOperation

       '删除冗余节点 

        DelSubPoint pMap

  m_pEditor.StopOperation"OK"

End If

 

把m_pEditor.StartOperation放在函数里面更好

 

===============================

怎样用点创建一个Polygon:

===============================

 

Dim pPnt0 asIPoint,pPnt1asIPoint,pPnt2asIPoint

Set pPnt0 = NewPoint

Set pPnt1 = NewPoint

Set pPnt2 = NewPoint

pPnt0.PutCoordsx1,y1

pPnt1.PutCoordsx2,y2

pPnt2.PutCoordsx3,y3

Dim pPolygon as IPointCollection(注意,这里的polygon是设置为点集的)

Set pPolygon =New Polygon

pPolygon.AddPointpPnt0

pPolygon.AddPointpPnt1

pPolygon.AddPointpPnt2

pPolygon.AddPoint pPnt0(注意,这里一定要闭合回到pPnt0才能形成一个Polygon)

 

Set pFeature.Shape = pPolygon

pFeature.Store

 

用这种方法可以创建一个Polyline:

Dim pPolyline asIPointCollection

Set pPolyline =New Line

pPolyline.AddPointpPnt0

pPolyline.AddPointpPnt1

pPolyline.AddPointpPnt2(这时就是一个polyline,不是闭合的)

 

还可以用另外一种方法,画一条两点之间的线段:

Dim pLine AsILine

Set pLine = New esriGeometry.Line

pLine.PutCoords pPnt0,pPnt1(第一个为from点,第二个为to点)

)

===============================

怎样运用属性来计算总面积:

===============================

 

Dim pDoc As IMxDocument

Dim pMap As IMap

Dim pFeatureLayer As IFeatureLayer

Dim pFeatureClass As IFeatureClass

 

Set pDoc = m_pApplication.Document

Set pMap = pDoc.ActiveView.FocusMap

Set pFeatureLayer = pMap.Layer(0)

Set pFeatureClass = pFeatureLayer.FeatureClass

 

' +++ create the query filter, and give

' +++ it a where clause

Dim pQueryFilt As IQueryFilter

Dim pFeatureCursor As IFeatureCursor

 

Set pQueryFilt = New QueryFilter

pQueryFilt.WhereClause= "subtype = 'COM'"

Set pFeatureCursor =pFeatureClass.Search(pQueryFilt, False)

 

' +++ get the area field

Dim pFields As IFields

Dim pField As IField

Dim lAIndex As Long

 

Set pFields = pFeatureClass.Fields

lAIndex = pFields.FindField("Area")

Set pField = pFields.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 pFeature As IFeature

Set pFeature = pFeatureCursor.NextFeature

Do

dtotArea = dtotArea + pFeature.Value(lAIndex)

Set pFeature = pFeatureCursor.NextFeature

Loop Until pFeature Is Nothing

 

' +++ send the total area to a message box

MsgBox dtotArea

 

===============================

关于属性域的一些心得

===============================

 

Dim pField As IField

    Dim pFields As IFields

    Dim pFieldEdit As IFieldEdit

    Dim pFieldsEdit As IFieldsEdit

    Dim pGeometryDef As IGeometryDef

    Dim pGeometryDefEdit As IGeometryDefEdit

 

    Set pFields = New Fields

    Set pFieldsEdit = pFields

    pFieldsEdit.FieldCount = 35

 

    Set pField = New Field

    Set pFieldEdit = pField

    With pFieldEdit

        .Name = "OBJECTID"

        .AliasName = "OBJECTID"

        .Type = esriFieldTypeOID

    End With

    Set pFieldsEdit.Field(0)= pField

 

    Set pField = New Field

    Set pFieldEdit = pField

    pFieldEdit.Name = "SHAPE"

    pFieldEdit.Type = esriFieldTypeGeometry

 

    Set pGeometryDef = New GeometryDef

    Set pGeometryDefEdit = pGeometryDef

    With pGeometryDefEdit

        .GeometryType = esriGeometryPolygon

        Set .SpatialReference = New UnknownCoordinateSystem

    End With

    Set pFieldEdit.GeometryDef= pGeometryDef

Set pFieldsEdit.Field(1)= pField

 

个人觉得,在创建shp文件时,运用上面的方法就可以创建,但是当在GDB中创建featureclass的时候就会出现问题,有可能是空间参考的问题。所以个人觉得当在GDB中创建时,还是将已有的Ifields传进来比较保险

 

===============================

怎样实现翻折Flip方法:

===============================

 

 (这个过程主要运用了IPolyline和IPolygon的ReverseOrientation方法,使其翻折)

Private Sub FlipFeature(pFeature As IFeature)

  Dim pGeom As IGeometry

  If pFeature.Shape.GeometryType = esriGeometryPolyline Then

    Dim pPolyline As IPolyline

    Set pPolyline = pFeature.Shape

     pPolyline.ReverseOrientation

    Set pGeom = pPolyline

  ElseIf pFeature.Shape.GeometryType = esriGeometryPolygon Then

    Dim pPolygon As IPolygon

     Set pPolygon = pFeature.Shape

     pPolygon.ReverseOrientation

    Set pGeom = pPolygon

  End If

  Set pFeature.Shape = pGeom

  pFeature.Store

End Sub

 

好的,没问题,这是我随便写的,希望对你有用

 

   Dim pTopoO As ITopologicalOperator

   Set pTopoO = pFeature.Shape

   Dim pEnv As IEnvelope

   Set pEnv = New Envelope

    pEnv.XMax = 100

    pEnv.XMin = 90

    pEnv.YMax = 100

    pEnv.YMin = 90

    pTopoO.Clip epnv

    pFeature.Store

 

===============================

回答机器猫FJJ关于ISpatialFilter接口方法的问题

===============================

 

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

using ESRI.ArcGIS.Geodatabase;

using ESRI.ArcGIS.Carto;

 

namespace Solutions

{

    public partial class Form1 : Form

    {

       public Form1()

       {

           InitializeComponent();

           ILayer pLayer;

           IFeatureLayer pFeatureLayer;

           IFeatureClass pFeatureClass;

           IFeatureCursor pFeatureCursor;

           IFeature pFeature;

           ISpatialFilter pSpatialFilter;

           pSpatialFilter = new SpatialFilterClass(); 

           pSpatialFilter.Geometry = pGeom;   ///这个pGeom就是传进来的参数,就是你所谓的那个“选择了地图上的一块区域” 

           pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

 

           int layerCount = axMapControl1.LayerCount;

           for (int i = 0; i < layerCount; i++)

           {

               pLayer = axMapControl1.get_Layer(i);

               pFeatureLayer = (IFeatureLayer)pLayer;

               pFeatureClass = pFeatureLayer.FeatureClass;

               pFeatureCursor = pFeatureClass.Search((IQueryFilter)pSpatialFilter, false);

              pFeature = pFeatureCursor.NextFeature(); 

               while (pFeature != null) 

               {

                   listView1.Items.Add(pLayer.Name);

               }

           }

       }

    }

}

 

===============================

回答网友韶华响当当关于更改符号的代码

===============================

 

 

private void axTOC1_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e)

       {

           esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;

           IBasicMap basicMap = null;

           ILayer layer = null;

           object unk = null;

           object data = null;

           axTOC1.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref unk, ref data);

 

           if (e.button == 1) 

           

               if (itemType == esriTOCControlItem.esriTOCControlItemLegendClass) 

               { 

                  //取得图例

                   ILegendClass pLegendClass = ((ILegendGroup)unk).get_Class((int)data); 

 

                  //创建符号选择器SymbolSelector实例

                   FormSymbolControl SymbolSelectorFrm= new FormSymbolControl(pLegendClass,layer); 

                  if (SymbolSelectorFrm.ShowDialog()== DialogResult.OK) 

                  

                      //局部更新主Map控件

                      //m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); 

                      axMap1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null,null); 

                      //设置新的符号

                      pLegendClass.Symbol = SymbolSelectorFrm.pSymbol; 

                      //更新主Map控件和图层控件

                      this.axMap1.ActiveView.Refresh(); 

                      this.axTOC1.Refresh(); 

                  

               } 

           

       

 

===============================

回答网友韶华响当当关于显示属性的代码

===============================

 

       ///

       /// 填充DataTable中的数据

       ///

       ///

       ///

       ///

       public static DataTable CreateDataTable(ILayer pLayer, string tableName) 

       

           //创建空DataTable 

            DataTable pDataTable = CreateDataTableByLayer(pLayer, tableName); 

           //创建DataTable的行对象

            DataRow pDataRow = null; 

 

           //取得图层类型

           string shapeType = getShapeType(pLayer); 

 

           //从ILayer查询到ITable 

            ITable pTable = pLayer as ITable; 

            ICursor pCursor = pTable.Search(null, false); 

 

           //取得ITable中的行信息

            IRow pRow = pCursor.NextRow(); 

           int n = 0; 

           while (pRow != null) 

           

               //新建DataTable的行对象

               pDataRow = pDataTable.NewRow(); 

               for (int i = 0; i < pRow.Fields.FieldCount; i++) 

               { 

                  //如果字段类型为esriFieldTypeGeometry,则根据图层类型设置字段值

                  if (pRow.Fields.get_Field(i).Type== esriFieldType.esriFieldTypeGeometry) 

                  

                      pDataRow = shapeType; 

                  

 

                  //当图层类型为Anotation时,要素类中会有esriFieldTypeBlob类型的数据,

                   //其存储的是标注内容,如此情况需将对应的字段值设置为Element 

                   else if (pRow.Fields.get_Field(i).Type== esriFieldType.esriFieldTypeBlob)

                   {

                       pDataRow = "Element";

                   }

                   else

                   {

                       pDataRow= pRow.get_Value(i);

                   }

               }

 

               //添加DataRow到DataTable

               pDataTable.Rows.Add(pDataRow);

               pDataRow = null;

               n++;

               pRow = pCursor.NextRow();

               为保证效率,一次只装载最多条记录

               //if (n == 2000)

               //{

               //    pRow = null;

               //}

               //else

               //{

               //    pRow = pCursor.NextRow();

               //}

           }

           return pDataTable;

        }

 

===============================

回答gjw1015关于IFeature变量添加进List数组里的问题

===============================

 

ListlistFeature = new List();

try

{

    pFC_TER_LN = pFeatureWorkspace.OpenFeatureClass("GD10K_DLG_TER_LN_l");

}

catch

{

    richTextBox1.SelectionStart =richTextBox1.Text.Length;

    richTextBox1.SelectedText = "地形图:" + eachFolder.Name + "中没有TER_LN_l图层!\n";

    richTextBox1.Refresh();

    continue;

}

for (int i = 0; i < pFC_TER_LN.FeatureCount([color=Red]null[/color]);i++)   ///将该图层的每个Feature提取出来以供编辑时依次赋值,并且可以设置FeatureCount方法的参数为一个IQueryFilter类型的变量,就可以实现你所要求的方法了,用Search()方法,应该是不可以的,希望你们试一下。

{

    pFeatrue = pFC_TER_LN.GetFeature(i);

    listFeature.Add(pFeatrue);

}

 

  转自 :GIS论坛

http://blog.sina.com.cn/s/blog_71d88f280100mj5j.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值