【036】◀▶ 自己寫的程序源代碼


●·● 目錄:

A1 ………… ESRI_01.zip
A2 ………… 實現:新建shapefile文件
A3 ………… 實現:只顯示篩選的要素(IFeatureLayerDefinition)
A4 ………… 實現:高亮顯示篩選的要素(IFeatureSelection)
A5 ………… 實現:類似 ArcMap 中 Identify 工具的效果(IIdentify、IArray、IIdentifyObj)
A6 ………… 實現:在 MapControl 上繪制幾何圖形

---------------------------------------------------------------------------------------------------------

Visual Studio 2008 + ArcGIS Engine 9.3

---------------------------------------------------------------------------------------------------------

            ╔════════╗
╠════╣   返回目錄   ╠══════════════════════════════════════════════════╣
            ╚════════╝

點擊下載: >->-> ESRI_01.zip <-<-<  標注:實現了基本 GIS 功能,部分截圖如下!

 

全局:

鷹眼:


自定義工具對話框:

面符號:

線符號:

點符號:

右鍵菜單:

圖層屬性表:

--------------------------------------------------------------------------------------------------------- 

Visual Studio 2010 + ArcGIS Engine 10.0

--------------------------------------------------------------------------------------------------------- 

            ╔════════╗
╠════╣   返回目錄   ╠══════════════════════════════════════════════════╣
            ╚════════╝

點擊下載: >->-> NewPoints.zip <-<-<  標注:實現新建shapefile文件,部分截圖如下!

 

屬性截圖如下:

實現步驟:

  • 新建工作空間工廠 IWorkspaceFactory!(矢量數據)
  • 新建要素工作空間 IFeatureWorkspace!  <IWorkspaceFactory.OpenFromFile()>
  • 若指定的文件名存在了,則將其內部的數據刪除!  <IDataset.Delete()>
  • 為要素集新建字段 IFields!
  • 新建要素集 IFeatureClass!  <IFeatureWorkspace.CreateFeatureClass()>
  • 新建要素 IFeature!  <IFeatureClass.CreateFeature()>

代碼如下:

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 ESRI.ArcGIS.Carto;
using System.IO;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geometry;

namespace NewPoints
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 保存文件的完整路徑
        /// </summary>
        string saveFullPath = string.Empty;

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = Directory.GetCurrentDirectory();
            sfd.Filter = "Shp文件(*.shp)|*.shp";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                saveFullPath = sfd.FileName;
            }
            textBox1.Text = saveFullPath;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFeatureLayer = CreateShpFromPoint(saveFullPath);
            pFeatureLayer.Name = "Point";
            axMapControl1.Map.AddLayer(pFeatureLayer);
        }

        private IFeatureLayer CreateShpFromPoint(string outfileNamePath)
        {
            string folder = System.IO.Path.GetDirectoryName(outfileNamePath);
            string file = System.IO.Path.GetFileName(outfileNamePath);

            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pFWS = pWSF.OpenFromFile(folder, 0) as IFeatureWorkspace;

            if (File.Exists(outfileNamePath))
            {
                IFeatureClass featureClass = pFWS.OpenFeatureClass(file);
                IDataset pDataset = featureClass as IDataset;
                pDataset.Delete();
            }

            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            IGeometryDef pGeometryDef = new GeometryDefClass();
            IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = "X";
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            pField = new FieldClass();
            pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Name_2 = "Y";
            pFieldEdit.Length_2 = 20;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            pFieldsEdit.AddField(pField);

            IFeatureClass pFeatureClass = pFWS.CreateFeatureClass(file, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
            
            for (int i = 0; i < 100;i+=10 )
            {
                for (int j = 0; j < 100;j+=10 )
                {
                    IPoint pPoint = new PointClass();
                    pPoint.PutCoords(i, j);
                    IFeature pFeature = pFeatureClass.CreateFeature();
                    pFeature.Shape = pPoint as IPoint;
                    pFeature.set_Value(pFeature.Fields.FindField("X"), i.ToString());
                    pFeature.set_Value(pFeature.Fields.FindField("Y"), j.ToString());
                    pFeature.Store();
                }
            }

            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pFeatureClass;
            return pFeatureLayer;
        }
    }
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM