|
阅读:1245回复:2
向图层里增加XY字段数值[函数代码]
<P>Public Function AddXY(sPath As String, sName As String)<BR> <BR> ' Open the folder to contain the shapefile as a workspace<BR> Dim pFWS As IFeatureWorkspace<BR> Dim pWorkspaceFactory As IWorkspaceFactory<BR> Set pWorkspaceFactory = New ShapefileWorkspaceFactory<BR> Set pFWS = pWorkspaceFactory.OpenFromFile(sPath, 0)<BR> <BR> Dim pFeatClass As IFeatureClass<BR> Set pFeatClass = pFWS.OpenFeatureClass(sName)<BR> <BR> If pFeatClass.ShapeType <> esriGeometryPoint Then Exit Function<BR> <BR> ' check to see if X, Y fields are on the class, if not add them<BR> Dim pField As IField<BR> Dim pFieldEdit As IFieldEdit<BR> <BR> <BR> ' add the fields if they do not already exist<BR> If pFeatClass.FindField("textX") = -1 Then<BR> Set pField = New Field<BR> Set pFieldEdit = pField<BR> pFieldEdit.Name = "textX"<BR> pFieldEdit.Type = esriFieldType.esriFieldTypeDouble<BR> pFeatClass.AddField pFieldEdit<BR> End If<BR> <BR> If pFeatClass.FindField("textY") = -1 Then<BR> Set pField = New Field<BR> Set pFieldEdit = pField<BR> pFieldEdit.Name = "textY"<BR> pFieldEdit.Type = esriFieldType.esriFieldTypeDouble<BR> pFeatClass.AddField pFieldEdit<BR> End If</P>
<P> ' loop through the features and populate the new fields<BR> Dim pFeatCursor As IFeatureCursor<BR> Set pFeatCursor = pFeatClass.Update(Nothing, False)<BR> Dim lXField As Long<BR> Dim lYField As Long<BR> <BR> lXField = pFeatCursor.FindField("textX")<BR> lYField = pFeatCursor.FindField("textY")<BR> <BR> Dim pFeature As IFeature<BR> Dim pPoint As IPoint<BR> <BR> Set pFeature = pFeatCursor.NextFeature<BR> While Not pFeature Is Nothing<BR> Set pPoint = pFeature.Shape<BR> pFeature.Value(lXField) = pPoint.X<BR> pFeature.Value(lYField) = pPoint.Y<BR> pFeatCursor.UpdateFeature pFeature<BR> Set pFeature = pFeatCursor.NextFeature<BR> Wend<BR> <BR>End Function</P> |
|
|
|
1楼#
发布于:2005-06-11 09:49
顶!太好了<img src="images/post/smile/dvbbs/em02.gif" /><img src="images/post/smile/dvbbs/em01.gif" /><img src="images/post/smile/dvbbs/em06.gif" /><img src="images/post/smile/dvbbs/em05.gif" />
|
|
|
2楼#
发布于:2005-06-17 09:59
<img src="images/post/smile/dvbbs/em01.gif" />
|
|
|