ArcEngine C# GIS开发入门作业 (六)Ex07——创建一个File Geodatabase,使用地图文件更新属性表字段

ArcEngine C# GIS开发入门作业 (六)Ex07——创建一个File Geodatabase,使用地图文件更新属性表字段

题目要求

ArcEngine C# GIS开发入门作业 (六)Ex07——创建一个File Geodatabase,使用地图文件更新属性表字段

实现效果

ArcEngine C# GIS开发入门作业 (六)Ex07——创建一个File Geodatabase,使用地图文件更新属性表字段

代码实现

注意事项先不重复了,前面几篇文章都有,直接上代码,这几天在实习要顺便写个实习的文章,这个作业就先还来不及行行注释了,为了保持作业的连续性,先把作业发完,后面再注释:
提醒一下 string FilePath = @“D:\2018大三上\GIS开发与设计\作业\EX07”; 这句话要改成你们自己文件所存的路径,当时时间不够,没完善代码功能

form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ESRI.ArcGIS;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Controls;

namespace Ex07
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ProductCode.EngineOrDesktop);
            InitializeComponent();
        }

        public IFieldsEdit CreatFields()
        {
            IFieldsEdit pFields = new FieldsClass();        //创建字段集
            IFieldEdit pField = new FieldClass();   //创建字段

            pField.Name_2 = "objectID";
            pField.Type_2 = esriFieldType.esriFieldTypeOID;
            pFields.AddField(pField);            //创建字段OID

            pField = new FieldClass();   //创建字段Shape
            pField.Name_2 = "Shape";
            pField.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDefEdit pGDE = new GeometryDefClass();
            pGDE.GeometryType_2 = esriGeometryType.esriGeometryPoint;

            ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
            esriSRGeoCSType geoSystem = esriSRGeoCSType.esriSRGeoCS_NAD1983;
            ISpatialReferenceResolution pSRR = pSRF.CreateGeographicCoordinateSystem(Convert.ToInt32(geoSystem)) as ISpatialReferenceResolution;
            ISpatialReference pSR = pSRR as ISpatialReference;      //设置地理坐标系

            pGDE.SpatialReference_2 = pSR;
            pField.GeometryDef_2 = pGDE;
            pFields.AddField(pField);

            pField = new FieldClass();   //创建字段经度
            pField.Name_2 = "Longtitude";
            pField.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFields.AddField(pField);

            pField = new FieldClass();   //创建字段纬度
            pField.Name_2 = "Latitude";
            pField.Type_2 = esriFieldType.esriFieldTypeDouble;
            pFields.AddField(pField);

            pField = new FieldClass();   //创建字段区域名
            pField.Name_2 = "State_Name";
            pField.Type_2 = esriFieldType.esriFieldTypeString;
            pField.Length_2 = 30;
            pFields.AddField(pField);

            return pFields;

        }

        private void axMapControl1_OnMapReplaced(object sender, IMapControlEvents2_OnMapReplacedEvent e)
        {
            string FilePath = @"D:\2018大三上\GIS开发与设计\作业\EX07\";
            FileGDBWorkspaceFactory pFWF = new FileGDBWorkspaceFactory();
            IWorkspaceName pWN = pFWF.Create(FilePath, "test", null, 0);
            IName pName = pWN as IName;
            IWorkspace pWS = pName.Open() as IWorkspace;    //创建FileGDB
            IFeatureWorkspace pFW = pWS as IFeatureWorkspace;
            IFeatureClass pFC = pFW as IFeatureClass;

            IFieldsEdit pFields = CreatFields();            //创建字段集,因为放一起太冗长,我放到了一个函数里,实际上可以直接复制上函数内的代码替换这一行代码
            pFC = pFW.CreateFeatureClass("store", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", null);     //创建要素类

            //-------------------------------读入文本数据
            string textPath = @"D:\2018大三上\GIS开发与设计\作业\EX07\textToFC.txt";
            System.IO.FileStream Fs = new System.IO.FileStream(textPath, FileMode.Open);
            StreamReader Sr = new StreamReader(Fs);
            while (!Sr.EndOfStream)
            {
                String[] Fields = Sr.ReadLine().Split(new char[] { ',' });
                IPoint pPoint = new PointClass();
                pPoint.X = double.Parse(Fields[0]);
                pPoint.Y = double.Parse(Fields[1]);
                IFeature pFea = pFC.CreateFeature();
                pFea.Shape = pPoint;
                pFea.set_Value(2, Fields[0]);
                pFea.set_Value(3, Fields[1]);
                pFea.Store();
            }
            Sr.Close();
            Fs.Close();

            //--------------------------------------------根据空间位置更新表属性

            IFeatureLayer pFL1 = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFC1 = pFL1.FeatureClass;             //面要素图层
            IQueryFilter pQF = new QueryFilterClass();

            IFeatureCursor pFCur2 = pFC.Update(pQF, false);
            IFeature pFea2 = pFCur2.NextFeature();               //当前点要素获取

            while (pFea2 != null)
            {
                ISpatialFilter pSF = new SpatialFilterClass();

                pSF.Geometry = pFea2.Shape;
                pSF.SpatialRel = esriSpatialRelEnum.esriSpatialRelWithin;     //空间查询类型位于
                pSF.WhereClause = "";
                IFeatureCursor pFcur = pFC1.Search(pSF, true);          //获取所处面域
                IFeature pFea3 = pFcur.NextFeature();

                pFea2.set_Value(pFC.FindField("State_Name"), pFea3.get_Value(3).ToString());
                pFea2.Store();
                pFea2 = pFCur2.NextFeature();
            }
            axMapControl1.Refresh();
            MessageBox.Show("打开usa.mxd时,创建文件点数据库成功,请打开文件夹并通过ArcMap查看属性表");
        }


    }
}

program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;

namespace Ex07
{
    static class Program
    {
        private static LicenseInitializer m_AOLicenseInitializer = new 王晓佳_1609040125_Ex07.LicenseInitializer();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            //ESRI License Initializer generated code.
            m_AOLicenseInitializer.InitializeApplication(new esriLicenseProductCode[] { esriLicenseProductCode.esriLicenseProductCodeEngine },
            new esriLicenseExtensionCode[] { });
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
            //ESRI License Initializer generated code.
            //Do not make any call to ArcObjects after ShutDownApplication()
            m_AOLicenseInitializer.ShutdownApplication();
        }
    }
}