AE创建各种类型的featureclass代码

更新时间:2023-08-18 17:12:01 阅读量: 资格考试认证 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

代码由 ESRI 社区 GIScafe 提供

IFields pFields = new FieldsClass(); IFieldsEdit pFieldsEdit = pFields as IFieldsEdit; IField pField = new FieldClass(); IFieldEdit pFieldEdit = pField as IFieldEdit; _2 = "shape"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeome try;

//set up Geometry Definition IGeometryDef pGeometryDef = new GeometryDefClass(); IGeometryDefEdit pGeometryDefEdit = pGeometryDef a s IGeometryDefEdit;

pGeometryDefEdit.GeometryType_2 = esriGeometryType.e sriGeometryPoint; //Point,line,polygon et.

pGeometryDefEdit.SpatialReference_2 = axMapControl1. SpatialReference;

//新建字段 pField = new FieldClass(); pFieldEdit = pField as IFieldEdit; pFieldEdit.Length_2 = 15; _2 = "SchoolName"; pFieldEdit.GeometryDef_2 = pGeometryDef; pFieldsEdit.AddField(pField);

pFieldEdit.AliasName_2 = "SchoolName"; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeStrin g;

pFieldsEdit.AddField(pField);

string mappath = @"E:\data"; string layername="school"; IWorkspaceFactory pWorkspaceFactory = new ShapefileW orkspaceFactory();

IFeatureWorkspace pFeatureWorkspace = pWorkspaceFact ory.OpenFromFile(mappath,0) as IFeatureWorkspace;

pFeatureWorkspace.CreateFeatureClass(layername+".shp ",pFields,null,null,esriFeatureType.esriFTSimple,"shape","");

axMapControl1.AddShapeFile(mappath,layername+".shp ");

代码中路径名称可以自定义

posted on 2008-07-12 15:28 阅读(137) 所属分类:

AE 创建各种类型的 featureclass 代码 源码

''' <summary>

''' 创建 Annotation 类型的 featureclass

''' </summary>

''' <param name="pWorkspace">annotation 的工作空间</param>

''' <param name="pTextSymbol">annotation 的 symbol</param>

''' <param name="pRefeScale">annotation 的比例</param>

''' <param name="pAnnoFeatName">annotation 的名称</param>

''' <returns>创建的 Annotation Featureclass</returns>

''' <remarks></remarks>

Private Function CreateAnnoFeatCls(ByVal pWorkspace As IFeatureWorkspace, ByVal pTextSymbol As ITextSymbol, ByVal pRefeScale As Double, ByVal pAnnoFeatName As String, Optional ByVal pSymbolID As Integer = 0) As IFeatureClass

Dim pField As IField

Dim pFields As IFields

Dim pSource As IClone

Dim pObjectClassDesc As IObjectClassDescription

Dim pFeatClassDesc As IFeatureClassDescription

''创建 Annotation 的 Fields

pObjectClassDesc = New AnnotationFeatureClassDescription

pFeatClassDesc = pObjectClassDesc

pSource = pObjectClassDesc.RequiredFields

pFields = pSource.Clone

''创建 Annotation 的 Geometry defintion

pField = pFields.Field(pFields.FindField(pFeatClassDesc.ShapeFieldName))

''创建 Reference scale

Dim pGraphicsLayerScale As IGraphicsLayerScale

pGraphicsLayerScale = New GraphicsLayerScale

pGraphicsLayerScale.ReferenceScale = pRefeScale

pGraphicsLayerScale.Units = esriUnits.esriMeters

''创建 Symbolcollection

Dim pSymbolColl As ISymbolCollection2

Dim pSymbolIdent As ISymbolIdentifier2 = New SymbolIdentifier

pSymbolColl = New SymbolCollection

pSymbolColl.AddSymbol(pTextSymbol, "Default", pSymbolIdent)

If pSymbolID = 1 Then pSymbolColl.AddSymbol(pTextSymbol, "Default 1", pSymbolIdent)

''创建 Label 的相关属性

Dim pOverposterProperties As IOverposterProperties Dim pAnnoPropsColl As IAnnotateLayerPropertiesCollection

Dim pLabelEngineLP As ILabelEngineLayerProperties Dim

pAnnotateLayerProps As IAnnotateLayerProperties

pOverposterProperties = New BasicOverposterProperties

pAnnoPropsColl = New AnnotateLayerPropertiesCollection

pLabelEngineLP = New LabelEngineLayerProperties

pAnnotateLayerProps = pLabelEngineLP

pAnnotateLayerProps.Class = "Default"

pLabelEngineLP.Symbol = pTextSymbol

pLabelEngineLP.SymbolID = pSymbolIdent.ID

pAnnoPropsColl.Add(pLabelEngineLP)

''创建 Annotation layer

Dim pAnnoLayerFactory As IAnnotationLayerFactory

Dim pAnnoLayer As IAnnotationLayer

pAnnoLayerFactory = New FDOGraphicsLayerFactory

pAnnoLayer = pAnnoLayerFactory.CreateAnnotationLayer(pWorkspace, Nothing, pAnnoFeatName,

pField.GeometryDef, Nothing, pAnnoPropsColl, pGraphicsLayerScale, pSymbolColl, False, False, False, True, pOverposterProperties, "")

Return CType(pAnnoLayer, IFeatureLayer).FeatureClass End Function

''' <summary>

''' 创建存于 Shapefile 中的 featureclass

''' </summary> ''' <param name="sDir"></param> ''' <param name="sName"></param> ''' <param name="shapeType"></param> ''' <param name="hasM"></param> ''' <param name="hasZ"></param> ''' <param name="pSR"></param> ''' <param name="pFields"></param> ''' <returns></returns> ''' <remarks></remarks>

Public Shared Function CreateShapefile(ByVal sDir As String, ByVal sName As String, ByVal shapeType As esriGeometryType, _

ByVal hasM As Boolean, ByVal hasZ As Boolean, ByVal pSR As ISpatialReference, ByVal pFields As IFields) As IFeatureClass

Try

Dim shapeWorkspaceFactory As IWorkspaceFactory

shapeWorkspaceFactory = New ShapefileWorkspaceFactory

Dim shapeWorkspace As IFeatureWorkspace

shapeWorkspace = shapeWorkspaceFactory.OpenFromFile(sDir, 0)

If (Not shapeWorkspace Is Nothing) Then

If pFields Is Nothing Then pFields = CreateBasicFields(shapeType, hasM, hasZ, pSR) '

Dim pFClass As IFeatureClass

pFClass = shapeWorkspace.CreateFeatureClass(sName, pFields, Nothing, Nothing,

esriFeatureType.esriFTSimple, "Shape", "")

Return pFClass End If Return Nothing

Catch ex As Exception

MsgBox(ex.Message)

Return Nothing

End Try

End Function

''' <summary>

''' 创建存于内存中的 Featureclass

''' </summary> ''' <param name="pFields"></param>

''' <param name="featureClassName"></param>

''' <returns></returns>

''' <remarks></remarks>

Public Shared Function GetSimpleFeatureClass(ByVal pFields As IFields, ByVal featureClassName As String) As IFeatureClass

Try Dim pSwf As IWorkspaceFactory = New InMemoryWorkspaceFactory

Dim pWorkspaceName As IWorkspaceName = pSwf.Create("", "MyWorkspace", Nothing, 0)

Dim pFWS As IFeatureWorkspace = CType(pWorkspaceName, IName).Open()

Dim pFC As IFeatureClass = pFWS.CreateFeatureClass(featureClassName, pFields, Nothing, Nothing,

esriFeatureType.esriFTSimple, "Shape", Nothing)

Return pFC Catch ex As Exception Return Nothing End Try End Function

''' <summary>

''' 创建存于 Access 中的 featureclass

''' </summary> ''' <param name="pFields"></param> ''' <param name="pAccessPath"></param> ''' <param name="pAccessName"></param> ''' <param name="featureClassName"></param> ''' <returns></returns>

''' <remarks></remarks>

Public Function CreateAccessFeatureClass(ByVal pFields As IFields, ByVal pAccessPath As String, ByVal pAccessName As String, ByVal featureClassName As String) As IFeatureClass

Try

Dim pSwf As IWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory

Dim pWorkspaceName As IWorkspaceName = pSwf.Create(pAccessPath, pAccessName, Nothing, 0)

Dim pFWS As IFeatureWorkspace = CType(pWorkspaceName, IName).Open()

pFields = CreateBasicFields(esriGeometryType.esriGeometryPoint, False, False, New

UnknownCoordinateSystem)

Dim pFC As IFeatureClass = pFWS.CreateFeatureClass(featureClassName, pFields, Nothing, Nothing,

esriFeatureType.esriFTSimple, "Shape", Nothing)

Return pFC Catch ex As Exception Return Nothing End Try End Function

vs2005+AE9.2 实现,主要功能:鹰眼视图里面鼠标左键画红框,右键拖动

代码中主视图为 Mapcontrol1,鹰眼视图为 MapControl2。 主要

利用了 Envelope 进行视图范围传递,从而控制视图同步等。 代码

如下:

几个变量声明 :

//变量 IMapDocument pMapDocument = new MapDocumentClass(); IEnvelope pEn = new EnvelopeClass(); object oFillobject = new object(); private void CreateOverviewSymbol()

{ IRgbColor iRgb = new RgbColorClass(); iRgb.RGB = 255; ILineSymbol pOutline = new SimpleLineSymbolClass(); pOutline.Color = iRgb; pOutline.Width = 2.3; ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass(); pSimpleFillSymbol.Outline = pOutline; pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSHollow; oFillobject = pSimpleFillSymbol;

}

控制拖动变量:

private IMoveEnvelopeFeedback pSmallViewerEnvelope;//鹰眼小地图的红框,IMov eEnvelopeFeedback,用来移动 Envelope 的接口

private IPoint pSmallViewerMouseDownPt;//拖动时鼠标落点 private bool isTrackingSmallViewer = false; //标识是否在拖动 static int moveCount = 0;//记录移动的个数,为移动过程中显示红框用。

axMapControl1_OnMapReplaced 事件:

axMapControl2.LoadMxFile(axMapControl1.DocumentFilename); 两个视图加载数据可能存在鹰眼视图数据显示不完全(不知道是不是我电脑的毛病。。。),这 样加载可以控制数据的同步更新

axMapControl1_OnExtentUpdated 事件:

pEn = e.newEnvelope as IEnvelope; axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewForegro

und, null, null); axMapControl1_OnAfterDraw:

{ esriViewDrawPhase viewDrawPhase = (esriViewDrawPhase)e.viewDrawPhase; if (viewDrawPhase == esriViewDrawPhase.esriViewForeground)

axMapControl2.DrawShape(pEn, ref oFillobject);

}

axMapControl2_OnMouseDown:

{ if(e.button==1)//左键画红框

pEn = axMapControl2.TrackRectangle(); axMapControl1.Extent = pEn;

axMapControl2.DrawShape(pEn, ref oFillobject);

if (e.button == 2)//右键拖动红框 { pSmallViewerMouseDownPt = new PointClass(); pSmallViewerMouseDownPt.PutCoords(e.mapX, e.mapY);

axMapControl1.CenterAt(pSmallViewerMouseDownPt);

isTrackingSmallViewer = true; if (pSmallViewerEnvelope == null) { pSmallViewerEnvelope = new MoveEnvelopeFeedbackClass(); pSmallViewerEnvelope.Display = axMapControl2.ActiveView.ScreenDisplay; pSmallViewerEnvelope.Symbol = (ISymbol)oFillobject; }

pSmallViewerEnvelope.Start(pEn, pSmallViewerMouseDownPt);

axMapControl2_OnMouseMove:

if (isTrackingSmallViewer)

{ moveCount++; if (moveCount % 4 == 0)//因为一刷新,红框就没了。所以每移动 4 次就刷新一下,

保持红框的连续性。

axMapControl2.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewFore ground, null, null); pSmallViewerMouseDownPt.PutCoords(e.mapX, e.mapY); pSmallViewerEnvelope.MoveTo(pSmallViewerMouseDownPt);

}

axMapControl2_OnMouseUp:

{ if (pSmallViewerEnvelope != null)

pEn = pSmallViewerEnvelope.Stop(); axMapControl1.Extent = pEn; isTrackingSmallViewer = false;

}

Form 初始化:

axMapControl1.LoadMxFile(@"load data"); pEn = axMapControl1.Extent;//鹰眼红框初始化 CreateOverviewSymbol();

经过测试可以实现鹰眼中画红框和拖动功能。 现在存在问题是,拖动功能还不太完善,抓紧研究,有了结果在贴上来。 其中,拖动部分思路即代码来自 ESRI 中国社区 AE 版。

本文来源:https://www.bwwdw.com/article/qvpj.html

Top