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) { } } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理