王昭君哪个朝代的人:AE创建一个点文件和向文件添加一个点元素

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 21:32:04
【转】 AE创建一个点文件和向文件添加一个点元素2011-01-14 15:41转载自 623995698最终编辑 gxiang

private void button1_Click(object sender, EventArgs e)
        {
            #region 创建一个点的shape文件
            string Folderpathstr = @"E:\shape\";
            string LyrName = @"123.shp";
            string strFolder = Folderpathstr;
            string strName = LyrName;
            string strShapeFieldName = "Shape";


            IFeatureWorkspace pFWS;//ESRI.ArcGIS.Geodatabase;
            IWorkspaceFactory pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();//ESRI.ArcGIS.DataSourcesFile
            pFWS = pWorkspaceFactory.OpenFromFile(strFolder, 0) as IFeatureWorkspace;
           
            //创建一个字段集
            IFields pFields = new ESRI.ArcGIS.Geodatabase.FieldsClass();
            IFieldsEdit pFieldsEdit;
            pFieldsEdit = pFields as IFieldsEdit;

            //记录线形
            IField pField;
            IFieldEdit pFieldEdit;

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = strShapeFieldName;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            ISpatialReferenceFactory ispfac = new SpatialReferenceEnvironmentClass();
            IProjectedCoordinateSystem iprcoorsys = ispfac.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_114E);
            ISpatialReference pSpatialReference = iprcoorsys as ISpatialReference;
            IGeometryDef pGeomDef = new GeometryDefClass();
           
            IGeometryDefEdit pGeomDefEdit = pGeomDef as IGeometryDefEdit;
            pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            //pGeomDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline;
            pGeomDefEdit.SpatialReference_2 = pSpatialReference;
            //pGeomDefEdit.SpatialReference_2 = new ESRI.ArcGIS.Geometry.UnknownCoordinateSystemClass();
            //ISpatialReferenceFactory ispfac = new SpatialReferenceEnvironmentClass();
            //IProjectedCoordinateSystem iprocoorsys = ispfac.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_114E);
            pFieldEdit.GeometryDef_2 = pGeomDef;
            pFieldsEdit.AddField(pField);

            //记录线的点数
            IField pFieldz;
            IFieldEdit pFieldEditz;
            pFieldz = new FieldClass();
            pFieldEditz = pFieldz as IFieldEdit;
            pFieldEditz.Length_2 = 30;//Length_2与Length的区别是一个是只读的,一个是可写的,以下Name_2,Type_2也是一样
            pFieldEditz.Name_2 = "PointsNum";
            pFieldEditz.Type_2 = esriFieldType.esriFieldTypeInteger;

            pFieldsEdit.AddField(pFieldz);

            //记录创建线的日期
            IField pFieldzz;
            IFieldEdit pFieldEditzz;
            pFieldzz = new FieldClass();
            pFieldEditzz = pFieldzz as IFieldEdit;
            pFieldEditzz.Length_2 = 30;
            pFieldEditzz.Name_2 = "Date";
            pFieldEditzz.AliasName_2 = "时间";
            pFieldEditzz.Type_2 = esriFieldType.esriFieldTypeDate;
            pFieldsEdit.AddField(pFieldzz);

            //创建一个SEX字段
            IField pFieldzzz;
            IFieldEdit pFieldEditzzz;
            pFieldzzz = new FieldClass();
            pFieldEditzzz = pFieldzzz as IFieldEdit;
            pFieldEditzzz.Length_2 = 4;
            pFieldEditzzz.Name_2 = "sex";
            pFieldEditzzz.Type_2= esriFieldType.esriFieldTypeString;
            pFieldEditzzz.AliasName_2 = "性别";
            pFieldEditzzz.DefaultValue_2 = "男";
            pFieldsEdit.AddField(pFieldEditzzz);
      
            IFeatureClass pFeatClass;
            pFeatClass = pFWS.CreateFeatureClass(strName, pFields, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, "");

#endregion
            #region 向点层添加一个点
            //设置图形的指针与缓冲空间
            //IFeatureCursor pIFeatureCursor = pFeatClass.Insert(true);
            //IFeatureBuffer pIFeatureBuffer = pFeatClass.CreateFeatureBuffer();
            //设置工作空间与取出数据的DataSet的接口
            IWorkspaceEdit pIWorkspaceEdit=null;
            IDataset pIDataset =(IDataset)pFeatClass;

            if (pIDataset != null)
            {
                pIWorkspaceEdit = (IWorkspaceEdit)pIDataset.Workspace;
            }
            try
            {
                IPoint new_Point = new PointClass();
                new_Point.X = 100;
                new_Point.Y = 100;

                pIWorkspaceEdit.StartEditing(true);
                pIWorkspaceEdit.StartEditOperation();

                IGeometry pOut_Geometry_Point = (IGeometry)new_Point;
                IFeature pOut_Feature_Point = pFeatClass.CreateFeature();
                pOut_Feature_Point.Shape = pOut_Geometry_Point;
               
                pOut_Feature_Point.Store();
                IFeature pIFeature;
                IFeatureCursor pIFeatureCursor=pFeatClass.Search(null, false);
                pIFeature= pIFeatureCursor.NextFeature();

                    IFields pfileds;
                    pfileds = pIFeature.Fields;
                    string s=pfileds.FindField("sex").ToString();
                    MessageBox.Show("sex 在 Field中的index", s);

                pIWorkspaceEdit.StopEditOperation();
                pIWorkspaceEdit.StopEditing(true);           
            }
            catch(Exception ex)
            {
                MessageBox.Show("异常信息" + ex.Message);
            }