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();
        }
    }
}
發佈了23 篇原創文章 · 獲贊 109 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章