zoukankan      html  css  js  c++  java
  • 重建MDB空间网格大小的工具

    本帖最后由 nnslu 于 2009-7-22 17:58 编辑

    自己写给自己的,也就没加什么注释
    大家看得懂的看吧
    这贴是Design的代码
    下帖是Form的代码
    Initialize自己写吧

    namespace RebuildSpatialIndex
    {
        partial class FrmRebuildSpatialGrid
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows 窗体设计器生成的代码

            /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.btnClose = new System.Windows.Forms.Button();
                this.progressBar1 = new System.Windows.Forms.ProgressBar();
                this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
                this.btnStart = new System.Windows.Forms.Button();
                this.groupBox1 = new System.Windows.Forms.GroupBox();
                this.trvLayers = new System.Windows.Forms.TreeView();
                this.label1 = new System.Windows.Forms.Label();
                this.groupBox2 = new System.Windows.Forms.GroupBox();
                this.txtPath = new System.Windows.Forms.TextBox();
                this.btnOpen = new System.Windows.Forms.Button();
                this.groupBox1.SuspendLayout();
                this.groupBox2.SuspendLayout();
                this.SuspendLayout();
                //
                // btnClose
                //
                this.btnClose.Location = new System.Drawing.Point(315, 230);
                this.btnClose.Name = "btnClose";
                this.btnClose.Size = new System.Drawing.Size(75, 21);
                this.btnClose.TabIndex = 0;
                this.btnClose.Text = "关闭";
                this.btnClose.UseVisualStyleBackColor = true;
                this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
                //
                // progressBar1
                //
                this.progressBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.progressBar1.Location = new System.Drawing.Point(0, 257);
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(393, 10);
                this.progressBar1.Step = 1;
                this.progressBar1.TabIndex = 1;
                //
                // openFileDialog1
                //
                this.openFileDialog1.Filter = "*.mdb(Access文件)|*.mdb";
                //
                // btnStart
                //
                this.btnStart.Location = new System.Drawing.Point(235, 230);
                this.btnStart.Name = "btnStart";
                this.btnStart.Size = new System.Drawing.Size(75, 21);
                this.btnStart.TabIndex = 3;
                this.btnStart.Text = "开始更新";
                this.btnStart.UseVisualStyleBackColor = true;
                this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
                //
                // groupBox1
                //
                this.groupBox1.Controls.Add(this.trvLayers);
                this.groupBox1.Location = new System.Drawing.Point(0, 44);
                this.groupBox1.Name = "groupBox1";
                this.groupBox1.Size = new System.Drawing.Size(393, 180);
                this.groupBox1.TabIndex = 4;
                this.groupBox1.TabStop = false;
                //
                // trvLayers
                //
                this.trvLayers.CheckBoxes = true;
                this.trvLayers.Dock = System.Windows.Forms.DockStyle.Fill;
                this.trvLayers.FullRowSelect = true;
                this.trvLayers.HideSelection = false;
                this.trvLayers.ItemHeight = 16;
                this.trvLayers.Location = new System.Drawing.Point(3, 17);
                this.trvLayers.Name = "trvLayers";
                this.trvLayers.Size = new System.Drawing.Size(387, 160);
                this.trvLayers.TabIndex = 0;
                this.trvLayers.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.trvLayers_AfterCheck);
                //
                // label1
                //
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(7, 231);
                this.label1.Name = "label1";
                this.label1.Size = new System.Drawing.Size(0, 12);
                this.label1.TabIndex = 5;
                //
                // groupBox2
                //
                this.groupBox2.Controls.Add(this.btnOpen);
                this.groupBox2.Controls.Add(this.txtPath);
                this.groupBox2.Location = new System.Drawing.Point(3, 1);
                this.groupBox2.Name = "groupBox2";
                this.groupBox2.Size = new System.Drawing.Size(387, 45);
                this.groupBox2.TabIndex = 6;
                this.groupBox2.TabStop = false;
                this.groupBox2.Text = "MDB文件路径";
                //
                // txtPath
                //
                this.txtPath.Dock = System.Windows.Forms.DockStyle.Left;
                this.txtPath.Enabled = false;
                this.txtPath.Location = new System.Drawing.Point(3, 17);
                this.txtPath.Name = "txtPath";
                this.txtPath.Size = new System.Drawing.Size(304, 21);
                this.txtPath.TabIndex = 0;
                //
                // btnOpen
                //
                this.btnOpen.Location = new System.Drawing.Point(312, 16);
                this.btnOpen.Name = "btnOpen";
                this.btnOpen.Size = new System.Drawing.Size(75, 21);
                this.btnOpen.TabIndex = 1;
                this.btnOpen.Text = "...";
                this.btnOpen.UseVisualStyleBackColor = true;
                this.btnOpen.Click += new System.EventHandler(this.btnOpen_Click);
                //
                // FrmRebuildSpatialGrid
                //
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(393, 267);
                this.Controls.Add(this.groupBox2);
                this.Controls.Add(this.label1);
                this.Controls.Add(this.groupBox1);
                this.Controls.Add(this.btnStart);
                this.Controls.Add(this.progressBar1);
                this.Controls.Add(this.btnClose);
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
                this.MaximizeBox = false;
                this.MinimizeBox = false;
                this.Name = "FrmRebuildSpatialGrid";
                this.ShowIcon = false;
                this.ShowInTaskbar = false;
                this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
                this.Text = "重新计算空间网格大小";
                this.Load += new System.EventHandler(this.RebuildSpatialGrid_Load);
                this.groupBox1.ResumeLayout(false);
                this.groupBox2.ResumeLayout(false);
                this.groupBox2.PerformLayout();
                this.ResumeLayout(false);
                this.PerformLayout();

            }

            #endregion

            private System.Windows.Forms.Button btnClose;
            private System.Windows.Forms.ProgressBar progressBar1;
            private System.Windows.Forms.OpenFileDialog openFileDialog1;
            private System.Windows.Forms.Button btnStart;
            private System.Windows.Forms.GroupBox groupBox1;
            //private Gisquest.SurveySystem.GisqSelLayerCtrl gisqSelLayerCtrl1;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.TreeView trvLayers;
            private System.Windows.Forms.GroupBox groupBox2;
            private System.Windows.Forms.Button btnOpen;
            private System.Windows.Forms.TextBox txtPath;
        }
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using ESRI.ArcGIS.Carto;
    using ESRI.ArcGIS.esriSystem;
    using ESRI.ArcGIS.Geodatabase;
    using ESRI.ArcGIS.Geometry;
    using ESRI.ArcGIS.Geoprocessing;
    using ESRI.ArcGIS.DataSourcesGDB;

    namespace RebuildSpatialIndex
    {
        public partial class FrmRebuildSpatialGrid : Form
        {
            public FrmRebuildSpatialGrid()
            {
                InitializeComponent();
                
            }

            private IWorkspace m_pWks = null;

            private IGeoProcessor m_pGp = new GeoProcessorClass();

            

            private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
            }

            private void btnStart_Click(object sender, EventArgs e)
            {
                try
                {
                    //Gisquest.SurveySystem.SystemExchangeData.UseWaitCursor(true);
                    label1.Text = "开始处理...";
                    Application.DoEvents();
                    List<object> oLst = null;//gisqSelLayerCtrl1.SelLayers;
                    for (int i = 0; i < trvLayers.Nodes[0].Nodes.Count; i++)
                    {
                        TreeNode oNode = trvLayers.Nodes[0].Nodes[i];
                        if (oNode.Tag is IFeatureClass)
                        {
                            string strPath = m_pWks.PathName;
                            if (oNode.Checked)
                            {
                                IDataset pDataset = oNode.Tag as IDataset;
                                IFeatureClass pFeatClass = pDataset as IFeatureClass;
                                strPath = strPath + "\\" + pDataset.Name;
                                int nCount = pFeatClass.FeatureCount(null);
                                if (nCount > 0)
                                {
                                    double dblSize = CalcGridSize(strPath);
                                    if (dblSize > -1)
                                    {
                                        label1.Text = string.Format("处理:{0},要素个数:{1}\r\n最佳网格大小{2:F2}", pFeatClass.AliasName, nCount, dblSize);
                                        Application.DoEvents();
                                        string strQry = string.Format("update GDB_GeomColumns set IdxGridSize={0} where TableName = '{1}'", dblSize, pDataset.Name);
                                        m_pWks.ExecuteSQL(strQry);
                                        string strName = pDataset.Name + "_SHAPE_Index";
                                        UpdateIndex(strName, pDataset as IFeatureClass, dblSize);
                                    }
                                }
                            }
                        }
                        else if (oNode.Tag is IDataset)
                        {
                            for (int j = 0; j < oNode.Nodes.Count; j++)
                            {
                                string strPath = m_pWks.PathName;
                                TreeNode oSubNode = oNode.Nodes[j];
                                IDataset pDataset = oSubNode.Tag as IDataset;
                                IFeatureClass pFeatClass = pDataset as IFeatureClass;
                                strPath = strPath + "\\" + oNode.Text + "\\" +  pDataset.Name;
                                int nCount = pFeatClass.FeatureCount(null);
                                if (nCount > 0)
                                {
                                    double dblSize = CalcGridSize(strPath);
                                    if (dblSize > -1)
                                    {
                                        label1.Text = string.Format("处理:{0},要素个数:{1}\r\n最佳网格大小{2:F2}", pFeatClass.AliasName, nCount, dblSize);
                                        Application.DoEvents();
                                        string strQry = string.Format("update GDB_GeomColumns set IdxGridSize={0} where TableName = '{1}'", dblSize, pDataset.Name);
                                        m_pWks.ExecuteSQL(strQry);
                                        string strName = pDataset.Name + "_SHAPE_Index";
                                        UpdateIndex(strName, pDataset as IFeatureClass, dblSize);
                                    }
                                }
                            }
                        }
                        
                    }
                    //for (int i = 0; i < oLst.Count; i++)
                    //{
                    //    object obj = oLst[i];
                    //    if (obj is IFeatureLayer)
                    //    {
                    //        IFeatureLayer pFeatLayer = obj as IFeatureLayer;
                    //        IFeatureClass pFeatClass = pFeatLayer.FeatureClass;
                    //        IDataset pDataset = pFeatClass as IDataset;
                    //        m_pWks = pDataset.Workspace;
                    //        if (m_pWks.WorkspaceFactory is AccessWorkspaceFactory)
                    //        {
                    //            string strPath = m_pWks.PathName;
                    //            if (pFeatClass.FeatureDataset != null)
                    //            {
                    //                strPath = strPath + "\\" + pFeatClass.FeatureDataset.Name + "\\" + pDataset.Name;
                    //            }
                    //            else
                    //            {
                    //                strPath = strPath + "\\" + pDataset.Name;
                    //            }
                    //            int nCount = pFeatClass.FeatureCount(null);
                    //            if (nCount > 0)
                    //            {
                    //                double dblSize = CalcGridSize(strPath);
                    //                if (dblSize > -1)
                    //                {
                    //                    label1.Text = string.Format("处理:{0},要素个数:{1}\r\n最佳网格大小{2:F2}", pFeatLayer.Name, nCount, dblSize);
                    //                    Application.DoEvents();
                    //                    string strQry = string.Format("update GDB_GeomColumns set IdxGridSize={0} where TableName = '{1}'", dblSize, pDataset.Name);
                    //                    m_pWks.ExecuteSQL(strQry);
                    //                    string strName = pDataset.Name + "_SHAPE_Index";
                    //                    UpdateIndex(strName, pDataset as IFeatureClass, dblSize);
                    //                }
                    //            }
                    //        }
                            
                    //    }
                    //}
                    label1.Text = "全部图层计算完毕!";
                }
                catch (Exception Err)
                {
                    //new SurveyErrException("重建网格错误:", Err);
                    label1.Text = "重建网格错误!";
                }
                finally
                {
                    progressBar1.Value = 0;
                    //Gisquest.SurveySystem.SystemExchangeData.UseWaitCursor(false);
                }
            }

            private double CalcGridSize(string strPath)
            {
                double dblSize = -1;
                try
                {
                    IVariantArray pVarray = new VarArrayClass();
                    pVarray.Add(strPath);
                    IGeoProcessorResult pGeoPrcResult = m_pGp.Execute("CalculateDefaultGridIndex", pVarray, null);
                    object obj = pGeoPrcResult.ReturnValue;
                    char[] aa={';'};
                    string[] strTemp=obj.ToString().Split(aa);
                    dblSize = Convert.ToDouble(strTemp.GetValue(0));
                     
                }
                catch (Exception)
                {
                    throw;
                     
                }
                return dblSize;
            }

            private void UpdateIndex(string strName, IFeatureClass pFeatClass,double dblSize)
            {
                try
                {
                    int nTotal = pFeatClass.FeatureCount(null);
                    int nNow = 0;
                    IFeatureCursor pFeatCursor = pFeatClass.Search(null, true);
                    IFeature pFeature = pFeatCursor.NextFeature();
                    double xMin, xMax, yMin, yMax;
                    string strQry = string.Empty;
                    while (pFeature!=null)
                    {
                        progressBar1.Value = 100 * nNow / nTotal;
                        if (pFeature.Shape==null)
                        {
                            nNow = nNow + 1;
                            pFeature = pFeatCursor.NextFeature();
                            continue;
                        }
                        IEnvelope pEnv = pFeature.Shape.Envelope;
                        xMin =Math.Floor( pEnv.XMin / dblSize);
                        xMax =Math.Ceiling( pEnv.XMax / dblSize);
                        yMin =Math.Floor( pEnv.YMin / dblSize);
                        yMax = Math.Ceiling(pEnv.YMax / dblSize);
                        strQry = string.Format("Update {0} set MinGX={1},MinGY={2},MaxGX={3},MaxGY={4} where IndexedObjectId={5}", strName, xMin, yMin, xMax, yMax,pFeature.OID);
                        try
                        {
                            m_pWks.ExecuteSQL(strQry);
                        }
                        catch (Exception)
                        {
                            
                        }
                        nNow = nNow + 1;
                        pFeature = pFeatCursor.NextFeature();
                    }
                }
                catch (Exception)
                {
                   
                }
            }

            private void RebuildSpatialGrid_Load(object sender, EventArgs e)
            {
                //gisqSelLayerCtrl1.ViewLayers(0, true,false, true, true, true);
                //gisqSelLayerCtrl1.SetNodeChecked("线层", "", true);
                //gisqSelLayerCtrl1.SetNodeChecked("面层", "", true);
                //gisqSelLayerCtrl1.SetNodeChecked("点层", "", true);
                //gisqSelLayerCtrl1.SetNodeChecked("注记层", "", true);
                
            }

            private void btnOpen_Click(object sender, EventArgs e)
            {
                try
                {
                    openFileDialog1.ShowDialog();
                    if (openFileDialog1.FileName!="")
                    {
                        txtPath.Text = openFileDialog1.FileName;
                        OpenWks();
                    }
                }
                catch (Exception)
                {
                   
                   
                }
            }

            private void OpenWks()
            {
                try
                {
                    trvLayers.Nodes.Clear();
                    IWorkspaceFactory pWksF = new AccessWorkspaceFactoryClass();
                    m_pWks = pWksF.OpenFromFile(txtPath.Text,0);
                    trvLayers.Nodes.Add(m_pWks.PathName);
                    TreeNode oRootNode = trvLayers.Nodes[0];
                    IEnumDataset pEnumDt = m_pWks.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                    IDataset pDataset = pEnumDt.Next();
                    while (pDataset != null)
                    {
                        TreeNode oNode = new TreeNode();
                        oNode.Text = pDataset.BrowseName;
                        oNode.Name = pDataset.BrowseName;
                        oNode.Tag = pDataset;
                        oRootNode.Nodes.Add(oNode);
                        IEnumDataset pSubEnumDt = pDataset.Subsets;
                        IDataset pSubDt = pSubEnumDt.Next();
                        while (pSubDt != null )
                        {
                            if (pSubDt.Type==esriDatasetType.esriDTFeatureClass)
                            {
                                TreeNode oSubNode = new TreeNode();
                                oSubNode.Text = pSubDt.BrowseName;
                                oSubNode.Name = pSubDt.BrowseName;
                                oSubNode.Tag = pSubDt;
                                oNode.Nodes.Add(oSubNode);
                            }
                            pSubDt = pSubEnumDt.Next();
                        }
                        pDataset = pEnumDt.Next();
                    }
                    pEnumDt = m_pWks.get_Datasets(esriDatasetType.esriDTFeatureClass);
                    pDataset = pEnumDt.Next();
                    while (pDataset!=null)
                    {
                        if (pDataset.Type==esriDatasetType.esriDTFeatureClass)
                        {
                            TreeNode oNode = new TreeNode();
                            oNode.Text = pDataset.BrowseName;
                            oNode.Name = pDataset.BrowseName;
                            oNode.Tag = pDataset;
                            oRootNode.Nodes.Add(oNode);
                        }
                        pDataset = pEnumDt.Next();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                trvLayers.ExpandAll();
            }

            private void trvLayers_AfterCheck(object sender, TreeViewEventArgs e)
            {
                try
                {
                    TreeNode oNode = e.Node;
                    if ( oNode.Nodes.Count>0)
                    {
                        for (int i = 0; i < oNode.Nodes.Count; i++)
                        {
                            oNode.Nodes[i].Checked = oNode.Checked;
                        }
                    }
                }
                catch (Exception)
                {
                   
                   
                }
            }
        }
    }
  • 相关阅读:
    C#中的String与string
    类和结构的异同点?
    HTTP 无状态啊无状态啊
    重载运算符
    Lambda与委托
    Js与正则表达式
    字符函数PATINDEX()与STUFF()
    C#中的托管与非托管
    原码,反码,补码
    日期函数与转型
  • 原文地址:https://www.cnblogs.com/gisoracle/p/1535983.html
Copyright © 2011-2022 走看看