导入两个shp文件并合并

2 篇文章 0 订阅
2 篇文章 0 订阅
  private void simpleButton4_Click(object sender, EventArgs e)
        {
            //创建一个文件打开对话框来打开源文件
            OpenFileDialog pDlg = new OpenFileDialog();
            pDlg.Filter = "Shape(*.shp)|*.shp|All Files(*.*)|*.*";
            pDlg.Title = "Open Shapefile data";
            if (pDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox2.Text = pDlg.FileName;
            }
        }
        private void simpleButton3_Click(object sender, EventArgs e)
        {
            //创建一个文件打开对话框来打开源文件
            OpenFileDialog pDlg = new OpenFileDialog();
            pDlg.Filter = "Shape(*.shp)|*.shp|All Files(*.*)|*.*";
            pDlg.Title = "Open Shapefile data";
            if (pDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                textBox1.Text = pDlg.FileName;
            }
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if ((textBox1.Text == "") || (textBox2.Text == ""))
                return;


            //获得去除后缀名的的文件名及路径
            string strShpfile_Source = System.IO.Path.GetFileNameWithoutExtension(textBox1.Text);
            string strFileDir_Source = System.IO.Path.GetDirectoryName(textBox1.Text);


            string strShpfile_Target = System.IO.Path.GetFileNameWithoutExtension(textBox1.Text);
            string strFileDir_Target = System.IO.Path.GetDirectoryName(textBox1.Text);


            //创建工厂类以打开工作空间
            IWorkspaceFactory pWksFac = new ShapefileWorkspaceFactory();
            IWorkspace pWks_Source = pWksFac.OpenFromFile(strFileDir_Source, 0);
            IWorkspace pWks_Target = pWksFac.OpenFromFile(strFileDir_Target, 0);


            if ((pWks_Source == null) || (pWks_Target == null))
                return;


            //获得源文件的表及空间参考
            IFeatureClass pSourceFeaCls = (pWks_Source as IFeatureWorkspace).OpenFeatureClass(strShpfile_Source);
            ISpatialReference pSourceSpr = ((pSourceFeaCls as IDataset) as IGeoDataset).SpatialReference;


            IFeatureClass pTargetFeaCls = (pWks_Target as IFeatureWorkspace).OpenFeatureClass(strShpfile_Target);
            ISpatialReference pTargetSpr = ((pTargetFeaCls as IDataset) as IGeoDataset).SpatialReference;//空间参考


            if (pSourceSpr.Name != pTargetSpr.Name)
            {
                MessageBox.Show("不一致的空间参考,数据无法导入!");
                return;
            }


            string sError = "";




            int iFeaCnt = pSourceFeaCls.FeatureCount(null);


            //获得源文件的游标
            IFeatureCursor pFeaCur = pSourceFeaCls.Search(null, false);
            IFeature pSourceFea = pFeaCur.NextFeature();
            if (pSourceFea == null)
                return;




            bool blOK = false;


            //启动目标工作空间的编辑
            IWorkspaceEdit pWksEdit = pWks_Target as IWorkspaceEdit;
            pWksEdit.StartEditing(false);
            pWksEdit.StartEditOperation();


            progressBar1.Maximum = iFeaCnt;
            progressBar1.Minimum = 0;


            try
            {
                //遍历每一条记录
                while (pSourceFea != null)
                {
                    //创建对应的目标记录
                    IFeature pTargetFea = pTargetFeaCls.CreateFeature();


                    if (pTargetFea != null)
                    {
                        //复制几何
                        pTargetFea.Shape = pSourceFea.Shape;
                        //复制属性
                        CopyAttribute(pSourceFea, pTargetFea);
                        //保存编辑
                        pTargetFea.Store();


                        progressBar1.Increment(1);


                        Application.DoEvents();


                        //this.Invalidate();
                    }
                    pSourceFea = pFeaCur.NextFeature();
                }


                blOK = true;
            }
            catch (Exception ex)
            {
                sError = ex.Message;
            }
            finally
            {
                progressBar1.Increment(-iFeaCnt);//进度条


                if (blOK)
                    pWksEdit.StopEditOperation();
                else
                    pWksEdit.AbortEditOperation();


                pWksEdit.StopEditing(blOK);


                if (blOK)
                {
                    if (checkBox1.Checked)
                    {
                        IFeatureLayer pFeaLyr = new FeatureLayerClass();
                        pFeaLyr.FeatureClass = pTargetFeaCls;
                        (this.Owner as Form1).AddFeatureLayer(pFeaLyr);
                    }
                    MessageBox.Show("导入成功,共计导入:" + iFeaCnt + "条记录。");
                }
                else
                    MessageBox.Show("导入失败:" + sError);
            }
        }


        private void CopyAttribute(IFeature pSourceFea, IFeature pTargetFea)
        {
            //获取源文件的字段信息
            IFields pSourceFlds = pSourceFea.Fields;
            for (int i = 0; i < pSourceFlds.FieldCount; i++)
            {
                //筛掉OID 几何 大字段类型的字段名
                IField pSourceFld = pSourceFlds.get_Field(i);
                if ((pSourceFld.Type == esriFieldType.esriFieldTypeOID) ||
                    (pSourceFld.Type == esriFieldType.esriFieldTypeGeometry) ||
                    (pSourceFld.Type == esriFieldType.esriFieldTypeBlob))
                    continue;


                //筛掉值为空的
                string sSourceVal = pSourceFea.get_Value(i).ToString();
                if (string.IsNullOrEmpty(sSourceVal))
                    continue;


                //添加索引
                string sSourceFldName = pSourceFld.Name;
                int iTargetFldIndex = pTargetFea.Fields.FindField(sSourceFldName);
                if (iTargetFldIndex >= 0)
                    pTargetFea.set_Value(iTargetFldIndex, sSourceVal);
            }
        }  
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值