Ejemplo n.º 1
0
        /// <summary>
        /// 更新几何对象长度到一个自定义的字段中
        /// </summary>
        /// <param name="CustomLengthField">自定义的字段</param>
        /// <returns></returns>
        public bool UpdateShapeLengthToCustomAreaField(IFeatureClass fc, string CustomLengthField)
        {
            ZhFeature zhfeat      = null;
            double    interLength = 0;
            IGeometry geo         = null;

            if (fc != null)
            {
                IFeatureCursor featCur = fc.Update(null, false);
                IFeature       feat    = featCur.NextFeature();
                while (feat != null)
                {
                    interLength = 0;
                    zhfeat      = new ZHFeaturePolygon(feat);
                    geo         = zhfeat.pFeature.ShapeCopy;
                    if (geo is ICurve)
                    {
                        interLength = (geo as ICurve).Length;
                    }
                    zhfeat.setFieldValue(CustomLengthField, interLength);
                    featCur.UpdateFeature(feat);
                    feat = featCur.NextFeature();
                }
                featCur.Flush();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(featCur);
            }

            return(true);
        }
        //开始计算
        private void Btn_Compute_Click(object sender, EventArgs e)
        {
            string tpFiledName = this.CB_Fields.Text;

            object obj = this.comboBox1.SelectedItem;

            if (obj != null && obj is CommonComboBoxItem)
            {
                CommonComboBoxItem item             = obj as CommonComboBoxItem;
                IFeatureClass      pFeatureClass    = item.Tag as IFeatureClass;
                decimal            ellipseArea      = 0;
                AnyTrapeziaEllipseAreaCompute_JF jf = new AnyTrapeziaEllipseAreaCompute_JF();
                //获取坐标系
                jf.Datum = EnumProjectionDatum.CGCS2000;
                if (this.rb2000.Checked == true)
                {
                    jf.Datum = EnumProjectionDatum.CGCS2000;
                }
                if (this.rb_xian80.Checked == true)
                {
                    jf.Datum = EnumProjectionDatum.Xian80;
                }
                if (this.rb_beijing54.Checked == true)
                {
                    jf.Datum = EnumProjectionDatum.Bejing54;
                }
                //获取大数设置
                jf.IsBigNumber = false;
                if (this.CB_IsBigNumber.Checked == true)
                {
                    jf.IsBigNumber = true;
                }
                //获取分度设置
                jf.Strip = EnumStrip.Strip6;
                if (CB_FD.Checked == true)
                {
                    jf.Strip = EnumStrip.Strip3;
                }
                //获取中央子午线
                jf.L0 = CommonClass.TDec(this.txt_DD.Text) * (int)jf.Strip;

                int             fIndex    = 0;
                int             tmpFCount = pFeatureClass.FeatureCount(null);
                frmProgressBar1 pb        = new frmProgressBar1();
                pb.Text                   = "正在进行椭球面积计算...";
                pb.KDCaption1             = "正在进行椭球面积计算...";
                pb.KDProgressBar1.Maximum = tmpFCount + 1;
                pb.KDProgressBar1.Value   = 0;
                pb.Show();
                Application.DoEvents();

                IFeatureCursor fcur = pFeatureClass.Update(null, false);
                IFeature       feat = fcur.NextFeature();
                while (feat != null)
                {
                    fIndex += 1;
                    if (fIndex % 500 == 0)
                    {
                        pb.KDCaption1           = "已计算完成要素个数:" + fIndex.ToString() + "/总" + tmpFCount.ToString();
                        pb.KDProgressBar1.Value = fIndex;
                        Application.DoEvents();
                        fcur.Flush();
                    }
                    ellipseArea = jf.Compute(feat.ShapeCopy);
                    //save ellipseArea...
                    ZhFeature zhfeat = new ZHFeaturePolygon(feat);
                    zhfeat.setFieldValue(tpFiledName, ellipseArea);

                    fcur.UpdateFeature(feat);

                    feat = fcur.NextFeature();
                }
                fcur.Flush();

                TokayWorkspace.ComRelease(fcur);
                fcur = null;


                pb.Close();
                pb.Dispose();
                pb = null;

                MessageBox.Show(this, "计算椭球面积完毕!", "提示");
            }
        }
Ejemplo n.º 3
0
        //开始经纬度计算椭球面积
        private void Btn_JWDComputeElliArea_Click(object sender, EventArgs e)
        {
            try
            {
                this.Btn_JWDComputeElliArea.Enabled = false;
                this.Cursor = Cursors.WaitCursor;

                string tpFileName = this.textBox1.Text;
                if (tpFileName == "" || System.IO.File.Exists(tpFileName) == false)
                {
                    MessageBox.Show("需先选择一个shp文件", "提示");
                    return;
                }

                string tpFiledName = this.CB_Fields.Text;

                string[] sArray   = tpFileName.Split('\\');
                string   filename = sArray[sArray.Length - 1];
                string   url      = "";
                for (int i = 0; i < sArray.Length - 1; i++)
                {
                    url = url + sArray[i] + "\\";
                }
                string[] sFile    = filename.Split('.');
                string   file_ext = "";
                if (sFile.Length > 0)
                {
                    file_ext = sFile[sFile.Length - 1];
                }
                file_ext = file_ext.ToLower();
                //----
                ShapefileWorkspaceFactoryClass wsf = new ShapefileWorkspaceFactoryClass();
                IWorkspace ws = wsf.OpenFromFile(url, 0);

                IFeatureWorkspace pFeatureWorkspace;
                pFeatureWorkspace = (IFeatureWorkspace)ws;

                IFeatureClass pFeatureClass = null;
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(filename);

                decimal ellipseArea = 0;
                AnyTrapeziaEllipseAreaCompute_JF_WGS1984 jf = new AnyTrapeziaEllipseAreaCompute_JF_WGS1984();

                IFeatureCursor fcur = pFeatureClass.Search(null, false);
                IFeature       feat = fcur.NextFeature();
                while (feat != null)
                {
                    ellipseArea = jf.Compute(feat.ShapeCopy);
                    //save ellipseArea...
                    ZhFeature zhfeat = new ZHFeaturePolygon(feat);
                    zhfeat.setFieldValue(tpFiledName, ellipseArea);
                    zhfeat.setFieldValue("TXMJ", zhfeat.GeometryArea);
                    zhfeat.SaveFeature();

                    feat = fcur.NextFeature();
                }
                TokayWorkspace.ComRelease(fcur);
                fcur = null;

                TokayWorkspace.ComRelease(ws);
                ws = null;

                MessageBox.Show(this, "计算椭球面积完毕!", "提示");
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee);
                MessageBox.Show(this, ee.Message, "提示");
            }
            finally
            {
                this.Btn_JWDComputeElliArea.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }
Ejemplo n.º 4
0
        //开始计算
        private void Btn_Compute_Click(object sender, EventArgs e)
        {
            try
            {
                this.Btn_Compute.Enabled = false;
                this.Cursor = Cursors.WaitCursor;

                string tpFileName = this.textBox1.Text;
                if (tpFileName == "" || System.IO.File.Exists(tpFileName) == false)
                {
                    MessageBox.Show("需先选择一个shp文件", "提示");
                    return;
                }

                string tpFiledName = this.CB_Fields.Text;

                string[] sArray   = tpFileName.Split('\\');
                string   filename = sArray[sArray.Length - 1];
                string   url      = "";
                for (int i = 0; i < sArray.Length - 1; i++)
                {
                    url = url + sArray[i] + "\\";
                }
                string[] sFile    = filename.Split('.');
                string   file_ext = "";
                if (sFile.Length > 0)
                {
                    file_ext = sFile[sFile.Length - 1];
                }
                file_ext = file_ext.ToLower();
                //----
                ShapefileWorkspaceFactoryClass wsf = new ShapefileWorkspaceFactoryClass();
                IWorkspace ws = wsf.OpenFromFile(url, 0);

                IFeatureWorkspace pFeatureWorkspace;
                pFeatureWorkspace = (IFeatureWorkspace)ws;

                IFeatureClass pFeatureClass = null;
                pFeatureClass = pFeatureWorkspace.OpenFeatureClass(filename);

                decimal ellipseArea = 0;
                AnyTrapeziaEllipseAreaCompute_JF jf = new AnyTrapeziaEllipseAreaCompute_JF();
                jf.Datum = EnumProjectionDatum.Bejing54;
                if (CB_Datum.Checked == true)
                {
                    jf.Datum = EnumProjectionDatum.Xian80;
                }
                jf.IsBigNumber = false;
                if (this.CB_IsBigNumber.Checked == true)
                {
                    jf.IsBigNumber = true;
                }
                jf.Strip = EnumStrip.Strip6;
                if (CB_FD.Checked == true)
                {
                    jf.Strip = EnumStrip.Strip3;
                }

                jf.L0 = CommonClass.TDec(this.txt_DD.Text) * (int)jf.Strip;

                int             fIndex    = 0;
                int             tmpFCount = pFeatureClass.FeatureCount(null);
                frmProgressBar1 pb        = new frmProgressBar1();
                pb.Text                 = "正在进行椭球面积计算...";
                pb.Caption1.Text        = "正在进行椭球面积计算...";
                pb.progressBar1.Maximum = tmpFCount + 1;
                pb.progressBar1.Value   = 0;
                pb.Show();
                Application.DoEvents();

                IFeatureCursor fcur = pFeatureClass.Update(null, false);
                IFeature       feat = fcur.NextFeature();
                while (feat != null)
                {
                    fIndex += 1;
                    if (fIndex % 500 == 0)
                    {
                        pb.Caption1.Text      = "已计算完成要素个数:" + fIndex.ToString() + "/总" + tmpFCount.ToString();
                        pb.progressBar1.Value = fIndex;
                        Application.DoEvents();
                        fcur.Flush();
                    }
                    ellipseArea = jf.Compute(feat.ShapeCopy);
                    //save ellipseArea...
                    ZhFeature zhfeat = new ZHFeaturePolygon(feat);
                    zhfeat.setFieldValue(tpFiledName, ellipseArea);
                    zhfeat.setFieldValue("TXMJ", zhfeat.GeometryArea);

                    fcur.UpdateFeature(feat);
                    //zhfeat.SaveFeature();

                    feat = fcur.NextFeature();
                }
                fcur.Flush();

                TokayWorkspace.ComRelease(fcur);
                fcur = null;

                TokayWorkspace.ComRelease(ws);
                ws = null;

                pb.Close();
                pb.Dispose();
                pb = null;

                MessageBox.Show(this, "计算椭球面积完毕!", "提示");
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee);
                MessageBox.Show(this, ee.Message, "提示");
            }
            finally
            {
                this.Btn_Compute.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }
        //自定义叠加传值
        private void CustomOverlapTranValue()
        {
            frmProgressBar1 pb = null;
            //
            IFeatureCursor fCur     = null;
            IFeatureCursor fCur_fc2 = null;

            //
            try
            {
                this.button1.Enabled = false;
                this.Cursor          = Cursors.WaitCursor;

                //获取参数
                string updateField  = this.CB_Fields1.Text.Trim();
                string valFromField = this.CB_Fields2.Text.Trim();

                pb                      = new frmProgressBar1();
                pb.Text                 = "空间叠加传值进度";
                pb.Caption1.Text        = "预处理中......";
                pb.progressBar1.Maximum = 10;
                pb.progressBar1.Value   = 0;
                pb.Show(this);
                Application.DoEvents();

                //2 开始空间叠加操作
                pb.Caption1.Text       = "空间叠加中...";
                pb.progressBar1.Value += 1;
                Application.DoEvents();
                //
                IFeatureClass fc1 = null;
                IFeatureClass fc2 = null;
                //
                object objval       = "";
                double geo_area     = 0.0;
                double geo_area_max = 0.0;
                //目标图层
                object obj = this.CB_LayerList1.SelectedItem;
                if (obj != null && obj is CommonComboBoxItem)
                {
                    CommonComboBoxItem item = obj as CommonComboBoxItem;
                    fc1 = item.Tag as IFeatureClass;
                }
                //源图层
                obj = this.CB_LayerList2.SelectedItem;
                if (obj != null && obj is CommonComboBoxItem)
                {
                    CommonComboBoxItem item = obj as CommonComboBoxItem;
                    fc2 = item.Tag as IFeatureClass;
                }
                //
                if (fc1 != null && fc2 != null)
                {
                    pb.progressBar1.Maximum = fc1.FeatureCount(null);
                    Application.DoEvents();
                    //目标图层
                    fCur = fc1.Update(null, false);
                    if (fCur != null)
                    {
                        int      recIndex = 0;
                        IFeature feat_fc1 = fCur.NextFeature();
                        while (feat_fc1 != null)
                        {
                            ZhFeature zhfeat_fc1 = new ZHFeaturePolygon(feat_fc1);
                            objval       = "";
                            geo_area     = 0.0;
                            geo_area_max = 0.0;
                            //获取面积最大的面积值
                            #region //与fc2相交运算功能
                            ISpatialFilter sFilter = new SpatialFilterClass();
                            sFilter.Geometry      = feat_fc1.ShapeCopy;
                            sFilter.GeometryField = fc2.ShapeFieldName;
                            sFilter.SpatialRel    = esriSpatialRelEnum.esriSpatialRelIntersects;
                            fCur_fc2 = fc2.Search(sFilter, false);
                            IFeature feat_fc2 = fCur_fc2.NextFeature();
                            while (feat_fc2 != null)
                            {
                                ZhFeature zhfeat_fc2 = new ZHFeaturePolygon(feat_fc2);
                                IGeometry t_geo      = null;
                                #region //获取相交对象
                                IGeometry            topGeo      = feat_fc1.ShapeCopy;
                                IGeometry            topGeo2     = feat_fc2.ShapeCopy;
                                ITopologicalOperator Topoperator = (ITopologicalOperator)topGeo;
                                Topoperator.Simplify();
                                ITopologicalOperator TopGeometry = (ITopologicalOperator)topGeo2;
                                TopGeometry.Simplify();
                                try
                                {
                                    t_geo = Topoperator.Intersect(topGeo2, topGeo.Dimension);
                                }
                                catch (Exception ee)
                                {
                                    t_geo = null;
                                }
                                #endregion
                                if (t_geo is IArea)
                                {   //面积
                                    geo_area = (t_geo as IArea).Area;
                                }
                                else if (t_geo is ICurve)
                                {   //长度
                                    geo_area = (t_geo as ICurve).Length;
                                }
                                if (geo_area >= geo_area_max)
                                {
                                    objval       = zhfeat_fc2.getFieldValue(valFromField);
                                    geo_area_max = geo_area;
                                }
                                //下一个要素
                                feat_fc2 = fCur_fc2.NextFeature();
                            }
                            if (fCur_fc2 != null)
                            {
                                TokayWorkspace.ComRelease(fCur_fc2);
                                fCur_fc2 = null;
                            }
                            #endregion
                            //设置值
                            zhfeat_fc1.setFieldValue(updateField, objval);
                            fCur.UpdateFeature(feat_fc1);
                            //
                            recIndex += 1;
                            pb.progressBar1.Value = recIndex;
                            pb.Caption1.Text      = "正在叠加传值......... 第" + pb.progressBar1.Value + "个/共" + pb.progressBar1.Maximum + "个";
                            Application.DoEvents();
                            //
                            //下一个要素
                            feat_fc1 = fCur.NextFeature();
                        }
                    }
                    if (fCur != null)
                    {
                        fCur.Flush();
                    }
                    if (fCur != null)
                    {
                        TokayWorkspace.ComRelease(fCur);
                        fCur = null;
                    }
                    if (pb != null)
                    {
                        pb.Close();
                        pb.Dispose();
                        pb = null;
                    }
                    MessageBox.Show("传值完毕!", "提示");
                }
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee);
                MessageBox.Show(ee.Message, "提示");
            }
            finally
            {
                this.button1.Enabled = true;
                this.Cursor          = Cursors.Default;

                if (fCur != null)
                {
                    TokayWorkspace.ComRelease(fCur);
                    fCur = null;
                }
                if (fCur_fc2 != null)
                {
                    TokayWorkspace.ComRelease(fCur_fc2);
                    fCur_fc2 = null;
                }
                if (pb != null)
                {
                    pb.Close();
                    pb.Dispose();
                    pb = null;
                }
            }
        }
        //快速叠加传值功能
        private void QuickOverlapTranValue()
        {
            IFeatureCursor      fcur  = null;
            mdbAccessLayerClass mdbOp = null;
            frmProgressBar1     pb    = null;

            try
            {
                this.button1.Enabled = false;
                this.Cursor          = Cursors.WaitCursor;

                //获取参数
                string updateField  = this.CB_Fields1.Text.Trim();
                string valFromField = this.CB_Fields2.Text.Trim();

                pb                      = new frmProgressBar1();
                pb.Text                 = "空间叠加传值进度...";
                pb.Caption1.Text        = "预处理中...";
                pb.progressBar1.Maximum = 10;
                pb.progressBar1.Value   = 0;
                pb.Show(this);
                Application.DoEvents();

                //1 获取和生成临时空间数据库
                string tmpdir = Application.StartupPath + "\\TmpDir";
                if (System.IO.Directory.Exists(tmpdir) == false)
                {
                    System.IO.Directory.CreateDirectory(tmpdir);
                }
                this.tmpMdbFilePath = tmpdir + "\\GeoTransValue.mdb";
                if (System.IO.File.Exists(this.tmpMdbFilePath) == true)
                {
                    CommonClass.DeleteFile(this.tmpMdbFilePath);
                }
                //--
                TokayWorkspace.CreateGeodatabase(this.tmpMdbFilePath);
                //2 开始空间叠加操作
                pb.Caption1.Text       = "空间叠加中...";
                pb.progressBar1.Value += 1;
                Application.DoEvents();
                //两个图层作叠加相交运算输出到一个mdb空间数据库中
                string        FID_fcName1   = "";
                string        fcName1       = "";
                string        fcName2       = "";
                string        fd_Shape_Area = "shape_area";
                IFeatureClass fc1           = null;
                IFeatureClass fc2           = null;
                //目标图层
                object obj = this.CB_LayerList1.SelectedItem;
                if (obj != null && obj is CommonComboBoxItem)
                {
                    CommonComboBoxItem item = obj as CommonComboBoxItem;
                    fc1     = item.Tag as IFeatureClass;
                    fcName1 = (fc1 as IDataset).Name;
                }
                //源图层
                obj = this.CB_LayerList2.SelectedItem;
                if (obj != null && obj is CommonComboBoxItem)
                {
                    CommonComboBoxItem item = obj as CommonComboBoxItem;
                    fc2           = item.Tag as IFeatureClass;
                    fcName2       = (fc2 as IDataset).Name;
                    fd_Shape_Area = fc2.ShapeFieldName + "_area";
                }
                //生成的相交图层名称
                tmpLayerName = fcName1 + "_" + fcName2;
                FID_fcName1  = "FID_" + fcName1;

                double TopoTolerance             = 0.0001;
                IntersectCalculateMdbEx mdbIsCal = new IntersectCalculateMdbEx();
                if (mdbIsCal.Execute(fc1, fc2, this.tmpMdbFilePath + "\\" + tmpLayerName, "INPUT", TopoTolerance) == true)
                {
                    #region 空间传值中
                    pb.Caption1.Text       = "空间传值中...";
                    pb.progressBar1.Value += 1;
                    Application.DoEvents();
                    //
                    ZhFeature zhfeat    = null;
                    int       index_fc1 = fc1.Fields.FindField(valFromField);
                    if (index_fc1 >= 0)
                    {
                        valFromField = valFromField + "_1";
                    }

                    mdbOp = new mdbAccessLayerClass(this.tmpMdbFilePath);
                    string x = "select " + FID_fcName1 + "," + valFromField + ",sum(" + fd_Shape_Area + ") as geo_area ";
                    x += " from " + tmpLayerName + " ";
                    x += " group by " + FID_fcName1 + "," + valFromField + "";
                    DataTable dt = mdbOp.GetMdbDB.ExecuteDataTable(x);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        #region  值中
                        string oid          = "";
                        int    fc1_count    = 0;
                        int    index        = 0;
                        string objval       = "";
                        double geo_area     = 0.0;
                        double geo_area_max = 0.0;
                        //更新值
                        fc1_count               = fc1.FeatureCount(null);
                        pb.progressBar1.Value   = 0;
                        pb.progressBar1.Maximum = fc1_count + 1;
                        Application.DoEvents();
                        //
                        fcur = fc1.Update(null, false);
                        IFeature feat = fcur.NextFeature();
                        while (feat != null)
                        {
                            index += 1;
                            if (index % 50 == 0)
                            {
                                pb.Caption1.Text      = "空间叠加传值中...第[" + index + "]个/共[" + fc1_count + "]个";
                                pb.progressBar1.Value = index;
                                Application.DoEvents();
                                fcur.Flush();
                            }
                            zhfeat = new ZHFeaturePolygon(feat);
                            oid    = zhfeat.getObjectID();
                            //
                            DataRow[] drArray = dt.Select(FID_fcName1 + "=" + oid);
                            if (drArray != null && drArray.Length >= 1)
                            {
                                objval       = "";
                                geo_area     = 0.0;
                                geo_area_max = 0.0;
                                //获取面积最大的面积值
                                foreach (DataRow dr in drArray)
                                {
                                    geo_area = CommonClass.TNum(dr["geo_area"].ToString());
                                    if (geo_area >= geo_area_max)
                                    {
                                        objval       = dr[valFromField].ToString();
                                        geo_area_max = geo_area;
                                    }
                                }
                                //设置值
                                zhfeat.setFieldValue(updateField, objval);
                                fcur.UpdateFeature(feat);
                            }
                            feat = fcur.NextFeature();
                        }
                        fcur.Flush();
                        if (fcur != null)
                        {
                            TokayWorkspace.ComRelease(fcur);
                            fcur = null;
                        }
                        #endregion
                    }
                    if (mdbOp != null)
                    {
                        mdbOp.GetMdbDB.Dispose();
                        mdbOp = null;
                    }
                    #endregion
                    if (pb != null)
                    {
                        pb.Close();
                        pb.Dispose();
                        pb = null;
                    }
                    MessageBox.Show("传值完毕!", "提示");
                }
                else
                {
                    MessageBox.Show("空间叠加失败!" + mdbIsCal.Message, "提示");
                }
            }
            catch (Exception ee)
            {
                Log.WriteLine(ee);
                MessageBox.Show(ee.Message, "提示");
            }
            finally
            {
                this.button1.Enabled = true;
                this.Cursor          = Cursors.Default;

                if (fcur != null)
                {
                    TokayWorkspace.ComRelease(fcur);
                    fcur = null;
                }
                if (mdbOp != null)
                {
                    mdbOp.GetMdbDB.Dispose();
                    mdbOp = null;
                }
                if (pb != null)
                {
                    pb.Close();
                    pb.Dispose();
                    pb = null;
                }
            }
        }