ArcEngine面转线

实现如下功能:输入一个面要素,对其进行面转线操作,最后输出一个线要素。
软件界面
代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;

namespace WindowsFormsApplication2
{
    public class Tool
    {
        /// <summary>
        /// 输入要素
        /// </summary>
        private string in_filePath;

        /// <summary>
        /// 输出路径
        /// </summary>
        private string out_filePath;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="in_filePath">输入路径</param>
        /// <param name="out_filePath">输出路径</param>
        public Tool(string in_filePath, string out_filePath)
        {
            this.in_filePath = in_filePath;
            this.out_filePath = out_filePath;
        }

        /// <summary>
        /// 打开要素类
        /// </summary>
        /// <returns></returns>
        private IFeatureClass OpenShapefile()
        {
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspaceFactoryLockControl pWorkspaceFactoryLockControl = pWorkspaceFactory as IWorkspaceFactoryLockControl;
            if (pWorkspaceFactoryLockControl.SchemaLockingEnabled)
            {
                pWorkspaceFactoryLockControl.DisableSchemaLocking();
            }
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(in_filePath), 0);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(System.IO.Path.GetFileName(in_filePath));
            return pFeatureClass;
        }

        /// <summary>
        /// 创建要素类
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private IFeatureClass CreateShapefile(ISpatialReference pSpatialReference)
        {
            IGeometryDef pGeometryDef = new GeometryDef();
            IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
            pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
            pGeometryDefEdit.HasM_2 = false;
            pGeometryDefEdit.HasZ_2 = false;
            pGeometryDefEdit.SpatialReference_2 = pSpatialReference;

            // 字段集合
            IFields pFields = new Fields();
            IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;

            // Shape
            IField pField = new Field();
            IFieldEdit pFieldEdit = pField as IFieldEdit;
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            pFieldEdit.GeometryDef_2 = pGeometryDef;
            pFieldEdit.AliasName_2 = "Shape";
            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.IsNullable_2 = false;
            pFieldEdit.Required_2 = true;
            pFieldsEdit.AddField(pField);

            // 创建shp
            IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
            IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(out_filePath), 0);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(System.IO.Path.GetFileName(out_filePath), pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
            return pFeatureClass;
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <returns></returns>
        public bool Execute()
        {
            IFeatureClass in_FeatureClass = OpenShapefile();
            if (in_FeatureClass.ShapeType != esriGeometryType.esriGeometryPolygon)
            {
                return false;
            }

            // 空间参考
            IGeoDataset in_GeoDataset = in_FeatureClass as IGeoDataset;
            ISpatialReference in_SpatialReference = in_GeoDataset.SpatialReference;

            // 创建要素
            IFeatureClass out_FeatureClass = CreateShapefile(in_SpatialReference);
            IFeatureBuffer out_FeatureBuffer = out_FeatureClass.CreateFeatureBuffer();
            IFeatureCursor out_FeatureCursor = out_FeatureClass.Insert(true);

            // 获取游标
            IFeatureCursor in_FeatureCursor = in_FeatureClass.Search(null, true);
            IFeature in_Feature = in_FeatureCursor.NextFeature();
            if (in_Feature == null)
            {
                return false;
            }

            // 插入要素
            IPolyline pPolyline = new Polyline() as IPolyline;
            while (in_Feature != null)
            {
                ISegmentCollection pSegmentCollection = in_Feature.ShapeCopy as ISegmentCollection;
                for (int i = 0; i < pSegmentCollection.SegmentCount; i++)
                {
                    ISegment pSegment = pSegmentCollection.get_Segment(i);
                    pPolyline.FromPoint = pSegment.FromPoint;
                    pPolyline.ToPoint = pSegment.ToPoint;
                    out_FeatureBuffer.Shape = pPolyline;
                    out_FeatureCursor.InsertFeature(out_FeatureBuffer);
                }
                in_Feature = in_FeatureCursor.NextFeature();
            }
            out_FeatureCursor.Flush();

            // 释放游标
            System.Runtime.InteropServices.Marshal.ReleaseComObject(in_FeatureCursor);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(out_FeatureCursor);
            return true;
        }

    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;

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

        // 输入文件
        private void btnInput_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "输入文件";
            openFileDialog.Filter = "Shapefile(*.shp)|*.shp";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtInput.Text = openFileDialog.FileName;
            }
        }

        // 输出文件
        private void btnOutput_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "输出文件";
            saveFileDialog.Filter = "Shapefile(*.shp)|*.shp";
            saveFileDialog.OverwritePrompt = true;
            saveFileDialog.RestoreDirectory = true;
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                txtOutput.Text = saveFileDialog.FileName;
            }
        }


        // 确定
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtInput.Text))
            {
                MessageBox.Show("请选择输入文件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!System.IO.File.Exists(txtInput.Text))
            {
                MessageBox.Show("输入文件不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(txtOutput.Text))
            {
                MessageBox.Show("请定义输出文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (System.IO.File.Exists(txtOutput.Text))
            {
                MessageBox.Show("输出文件已经存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Tool tool = new Tool(txtInput.Text, txtOutput.Text);
            bool ok = tool.Execute();
            if (ok)
            {
                MessageBox.Show("执行成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show("执行失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        // 取消
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/HerryDong/article/details/84260053
今日推荐