/// <summary> /// 获取属性表中的列名 /// </summary> /// <param name="fPath">模拟抽样文件</param> /// <param name="cmb">下拉框</param> public static void BindFields(string fPath, ComboBoxEdit cmb) { cmb.Properties.Items.Clear(); try { DataTable table = new DataTable(); IFeatureClass allVillage = EngineAPI.OpenFeatureClass(fPath); ITableConversion conver = new TableConversion(); table = conver.AETableToDataTable(allVillage); if (table == null) { return; } for (int i = 0; i < table.Columns.Count; ++i) { //Type type = table.Columns[i].DataType; //if (type != typeof(string)) //{ string f_name = table.Columns[i].ColumnName; cmb.Properties.Items.Add(f_name); //} } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void barBtnAddField_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { //save edit DataTable editedRow = _CurrentTable.GetChanges(); if (editedRow != null) { if (DialogResult.OK == XtraMessageBox.Show("确定保存所做修改?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { SaveChanges(editedRow); } } //add field frmAddField frm = new frmAddField(_FeatureLayer.FeatureClass); if (DialogResult.OK == frm.ShowDialog()) { //refresh view ITableConversion conver = new TableConversion(); _CurrentTable = null; _CurrentTable = conver.AETableToDataTable(_FeatureLayer.FeatureClass); _CurrentTable.AcceptChanges(); gridView1.Columns.Clear(); gridControlTable.DataSource = _CurrentTable; //gridControlTable.Refresh(); } }
private void frmSampleOverview_Load(object sender, EventArgs e) { pTable.Clear(); pTable.Columns.Clear(); IFeatureClass allVillage = EngineAPI.OpenFeatureClass(files); ITableConversion conver = new TableConversion(); this.CurrentTable = conver.AETableToDataTable(allVillage); for (int i = 2; i < CurrentTable.Columns.Count; i++) { if (CurrentTable.Columns[i].DataType == typeof(double)) { cBEHorizontal.Properties.Items.Add(CurrentTable.Columns[i]); cBEvertical.Properties.Items.Add(CurrentTable.Columns[i]); pTable.Columns.Add(new DataColumn(CurrentTable.Columns[i].ColumnName, CurrentTable.Columns[i].DataType)); } } DataRow drr = null; for (int i = 0; i < CurrentTable.Rows.Count; i++) { drr = pTable.NewRow(); for (int j = 0; j < pTable.Columns.Count; j++) { //转换成亩 drr[j] = double.Parse(CurrentTable.Rows[i][pTable.Columns[j].ColumnName].ToString()) / 666.67; } pTable.Rows.Add(drr); } }
private void barBtnOpenShp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (_pFtLayer != null) { EnviVars.instance.MapControl.Map.DeleteLayer(_pFtLayer); } OpenFileDialog fileDialog = new OpenFileDialog(); //fileDialog.Multiselect = true; fileDialog.Title = "请选择文件"; fileDialog.Filter = "shapefile(*.shp)|*.shp|All files(*.*)|*.*"; if (fileDialog.ShowDialog() == DialogResult.OK) { filepath = fileDialog.FileName.ToString(); //sampleSumTable.Clear();//清空表 //sampleSumTable.Columns.Clear(); IFeatureClass allVillage = EngineAPI.OpenFeatureClass(filepath); ITableConversion conver = new TableConversion(); this.sampleSumTable = conver.AETableToDataTable(allVillage); qualified = sampleSum = sampleSumTable.Rows.Count;//样本量 if (sampleSumTable.Columns.Contains("shape")) { sampleSumTable.Columns.Remove("shape"); } selectRows = sampleSumTable.Clone(); gridView1.Columns.Clear(); sampleSumTable.DefaultView.Sort = "FID ASC"; gridControlTable.DataSource = sampleSumTable; gridControlTable.Refresh(); _pFtLayer = new FeatureLayerClass(); _pFtLayer.FeatureClass = allVillage; FileInfo info = new FileInfo(filepath); //string filename = System.IO.Path .GetFileNameWithoutExtension(filepath); _pFtLayer.Name = info.Name; //将数据加载到主地图上 EnviVars.instance.MapControl.AddLayer(_pFtLayer); //MAP.AddShpFileToMap(filepath); deleted = sampleSum - qualified; barStatic.Caption = string.Format("样本总数:{0} 已删样本数 {1} 合格样本数 {2}", sampleSum, deleted, qualified); if (!(Overview == null || Overview.IsDisposed)) { Overview.Activate(); Overview.updatetable(sampleSumTable, _pFtLayer); } } }
private void frmSampleOverview_Load(object sender, EventArgs e) { IFeatureClass allVillage = EngineAPI.OpenFeatureClass(files); ITableConversion conver = new TableConversion(); this.CurrentTable = conver.AETableToDataTable(allVillage); if (allVillage != null) { Marshal.ReleaseComObject(allVillage); } for (int i = 0; i < CurrentTable.Columns.Count; i++) { if (CurrentTable.Columns[i].ColumnName.Contains("调查")) { cBEHorizontal.Properties.Items.Add(CurrentTable.Columns[i]); pTable.Columns.Add(new DataColumn(CurrentTable.Columns[i].ColumnName, CurrentTable.Columns[i].DataType)); } if (CurrentTable.Columns[i].ColumnName.Contains("分类")) { cBEvertical.Properties.Items.Add(CurrentTable.Columns[i]); pTable.Columns.Add(new DataColumn(CurrentTable.Columns[i].ColumnName, CurrentTable.Columns[i].DataType)); } } DataRow dr = null; foreach (DataRow row in CurrentTable.Rows) { dr = pTable.NewRow(); for (int j = 0; j < pTable.Columns.Count; j++) { dr[j] = row[pTable.Columns[j].ColumnName]; } pTable.Rows.Add(dr); } DataRow drr = null; for (int i = 0; i < CurrentTable.Rows.Count; i++) { drr = pTable.NewRow(); for (int j = 0; j < pTable.Columns.Count; j++) { //转换成亩 drr[j] = double.Parse(CurrentTable.Rows[i][pTable.Columns[j].ColumnName].ToString()) / 666.67; } pTable.Rows.Add(drr); } }
private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { if (filepath == null) { MessageBox.Show("错误信息:\n样本调查数据的值:是必需的"); } else { //if (selectRows.Rows.Count == 0) //{ // //加载地图 // EnviVars.instance.MapControl.AddLayer(_pFtLayer); // //MAP.AddShpFileToMap(filepath); //} //else if (DialogResult.OK == XtraMessageBox.Show("是否保存?保存后无法撤销修改!", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { string queryStr = string.Empty; for (int i = 0; i < selectRows.Rows.Count; i++) { queryStr = queryStr + "FID" + " = " + selectRows.Rows[i][0] + " OR "; } queryStr = queryStr.Substring(0, queryStr.Length - 4); SampleAduit aduit = new SampleAduit(); if (aduit.DeleFeature(_pFtLayer.FeatureClass, queryStr)) { XtraMessageBox.Show("保存成功!"); selectRows.Rows.Clear(); rowsIndex.Clear(); EnviVars.instance.MapControl.Refresh(); ITableConversion conver = new TableConversion(); this.sampleSumTable = conver.AETableToDataTable(_pFtLayer.FeatureClass); if (sampleSumTable.Columns.Contains("shape")) { sampleSumTable.Columns.Remove("shape"); } gridView1.Columns.Clear(); sampleSumTable.DefaultView.Sort = "FID ASC"; gridControlTable.DataSource = sampleSumTable; gridControlTable.Refresh(); } } } }
private void OpenAddFile(string file) { try { _FeatureLayer = null; for (int i = 0; i < _pMapControl.LayerCount; i++) { ILayer pLayer = _pMapControl.get_Layer(i); if (pLayer is IFeatureLayer) { IFeatureClass pFClass = (pLayer as IFeatureLayer).FeatureClass; string dtSource = Path.Combine((pFClass as IDataset).Workspace.PathName, (pFClass as IDataset).Name + ".shp"); if (dtSource == file) { _FeatureLayer = pLayer as IFeatureLayer; } } } if (_FeatureLayer == null) { IFeatureClass pFClass = EngineAPI.OpenFeatureClass(file); _FeatureLayer = new FeatureLayerClass(); _FeatureLayer.FeatureClass = pFClass; FileInfo fInfo = new FileInfo(file); _FeatureLayer.Name = fInfo.Name.Substring(0, fInfo.Name.Length - 4); _pMapControl.AddLayer(_FeatureLayer); } ITableConversion conver = new TableConversion(); this._CurrentTable = conver.AETableToDataTable(_FeatureLayer.FeatureClass); _CurrentTable.AcceptChanges(); gridView1.Columns.Clear(); this.gridControlTable.DataSource = _CurrentTable; } catch (Exception ex) { XtraMessageBox.Show(ex.Message); } }
private void btnOK_Click(object sender, EventArgs e) { ITableConversion conver = new TableConversion(); DataTable dt = conver.AETableToDataTable(_pFLayer.FeatureClass); //添加主键以提高处理速度 dt.PrimaryKey = new DataColumn[] { dt.Columns["FID"] }; AutoLayer auto = new AutoLayer(); _pFDt = auto.OptimalStratifying(dt, _lyrField, _lyrNum); ITable pTable = _pFLayer.FeatureClass as ITable; //添加分层字段 string fName = "FCBH"; if (pTable.FindField(fName) < 0) { IField layerField = new FieldClass(); IFieldEdit fieldEdit = layerField as IFieldEdit; fieldEdit.Name_2 = fName; fieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; pTable.AddField(layerField); } //字段赋值 ICursor pCursor = pTable.Update(null, false); IRow pRow = null; int layerIndex = pTable.FindField(fName); if (layerIndex < 0) { XtraMessageBox.Show("添加分层字段失败!"); return; } while ((pRow = pCursor.NextRow()) != null) { string value = pRow.get_Value(0).ToString(); //改用find接口以二叉树查找,效率提高百倍级别 DataRow test = _pFDt.Rows.Find(value); if (test == null) { continue; } pRow.set_Value(layerIndex, test[fName]); pCursor.UpdateRow(pRow); //原始遍历方法 //foreach(DataRow dRow in _pFDt.Rows) //{ // if (value == dRow["FID"].ToString()) // { // pRow.set_Value(layerIndex, dRow[fName]); // pCursor.UpdateRow(pRow); // } //} } Marshal.ReleaseComObject(pCursor); XtraMessageBox.Show("分层完毕!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); { SampleData.layerField = fName; MapAPI.UniqueValueRender(_pFLayer, fName); (_pMapControl.Map as IActiveView).Extent = _pFLayer.AreaOfInterest; _pMapControl.Refresh(); _pTOCControl.Update(); } }
public static bool CalLandArea(string unitFile, string framLand, out string msg) { IFeatureClass pUnitFeatureClass = null; IFeatureClass pAreaFeatureClass = null; ITable pUnitTable = null; ITable pAreaTable = null; try { msg = string.Empty; //相交处理 string interFile = System.IO.Path.Combine(GFS.BLL.ConstDef.PATH_TEMP, DateTime.Now.ToFileTime().ToString() + ".shp"); //if (!GFS.BLL.EnviVars.instance.GpExecutor.Intersect(unitFile + ";" + framLand, interFile, out msg)) // return false; EnviVars.instance.GpExecutor.Intersect(unitFile + ";" + framLand, interFile, out msg); //重算面积 string areaFile = System.IO.Path.Combine(GFS.BLL.ConstDef.PATH_TEMP, DateTime.Now.ToFileTime().ToString() + ".shp"); if (!EnviVars.instance.GpExecutor.CalArea(interFile, areaFile, out msg)) { return(false); } pUnitFeatureClass = EngineAPI.OpenFeatureClass(unitFile); if (pUnitFeatureClass == null) { msg = "打开抽样单元失败!"; return(false); } pAreaFeatureClass = EngineAPI.OpenFeatureClass(areaFile); if (pAreaFeatureClass == null) { msg = "打开耕地面积矢量失败!"; return(false); } ITableConversion conver = new TableConversion(); DataTable areaDT = conver.AETableToDataTable(pAreaFeatureClass); DataColumn refColum = new DataColumn(_unique, typeof(string)); if (!areaDT.Columns.Contains("uniqueid")) { areaDT.Columns.Add(refColum); } foreach (DataRow row in areaDT.Rows) { row[_unique] = row[SampleData.villageField].ToString() + row[_YFBH].ToString(); } Summarize sum = new Summarize(areaDT); DataTable sumDT = sum.Sum(areaDT.Columns.IndexOf("F_AREA"), areaDT.Columns.IndexOf(_unique)); pUnitTable = pUnitFeatureClass as ITable; //pAreaTable = pAreaFeatureClass as ITable; if (pUnitTable.FindField(_GDMJ) < 0) { IField areaField = new FieldClass(); IFieldEdit fieldEdit = areaField as IFieldEdit; fieldEdit.Name_2 = _GDMJ; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pUnitTable.AddField(areaField); } //int unitRows = pUnitTable.RowCount(null); //int areaRows = pAreaTable.RowCount(null); int unitNameIndex = pUnitTable.FindField(SampleData.villageField); int unitIDIndex = pUnitTable.FindField(_YFBH); //int areaNameIndex = pAreaTable.FindField(SampleData.villageField); int unitAreaIndex = pUnitTable.FindField(_GDMJ); //int areaIndex = pAreaTable.FindField("F_AREA"); ICursor pUnitCursor = pUnitTable.Update(null, false); IRow unitRow = null; while ((unitRow = pUnitCursor.NextRow()) != null) { string unitFid = unitRow.get_Value(unitNameIndex).ToString() + unitRow.get_Value(unitIDIndex).ToString(); foreach (DataRow sumRow in sumDT.Rows) { if (unitFid == sumRow[0].ToString()) { unitRow.set_Value(unitAreaIndex, sumRow[1]); pUnitCursor.UpdateRow(unitRow); } } //IRow areaRow = null; //double unitArea = 0.0; //ICursor pAreaCursor = pAreaTable.Update(null, false); //while ((areaRow = pAreaCursor.NextRow()) != null) //{ // string areaFid = areaRow.get_Value(areaNameIndex).ToString(); // if (unitFid == areaFid) // { // unitArea += (double)areaRow.get_Value(areaIndex); // unitRow.set_Value(unitAreaIndex, unitArea); // pUnitCursor.UpdateRow(unitRow); // } //} //Marshal.ReleaseComObject(pAreaCursor); } if (pUnitCursor != null) { Marshal.ReleaseComObject(pUnitCursor); } //for循环效率较低改用ICursor遍历 //for (int i = 0; i < unitRows; i++) //{ // IRow unitRow = pUnitTable.GetRow(i); // string unitFid = unitRow.get_Value(unitNameIndex).ToString(); // for (int j = 0; j < areaRows; j++) // { // IRow areaRow = pAreaTable.GetRow(j); // string areaFid = areaRow.get_Value(areaNameIndex).ToString(); // if (unitFid == areaFid) // { // object value = areaRow.get_Value(areaIndex); // unitRow.set_Value(unitAreaIndex, value); // unitRow.Store(); // } // } //} return(true); } catch (Exception ex) { throw ex; } finally { if (pUnitFeatureClass != null) { Marshal.ReleaseComObject(pUnitFeatureClass); } if (pAreaFeatureClass != null) { Marshal.ReleaseComObject(pAreaFeatureClass); } if (pUnitTable != null) { Marshal.ReleaseComObject(pUnitTable); } if (pAreaTable != null) { Marshal.ReleaseComObject(pAreaTable); } } }
/// <summary> /// 结果汇总,计算抽取的样本的均方误差值、变异系数,以及每次抽样估算的总面积 /// </summary> /// <param name="file">模拟抽样文件</param> /// <param name="sampleTimes">重复抽样次数</param> /// <param name="i_samplesNum">抽取样本量</param> /// <param name="ZoneNum">层号字段名称</param> /// <param name="Basis">抽样依据字段名称</param> /// <param name="Classic">总体样本</param> /// <param name="EstimationArea">重复抽样得到的估算面积表</param> /// <param name="MSE_Yr">均方误差值</param> /// <param name="CV">变异系数</param> /// <param name="zones">重复抽样得到的样本表汇总表</param> /// <returns>是否成功信息反馈</returns> public bool SampleEvaluating(string file, int sampleTimes, int i_samplesNum, string ZoneNum, string Basis, string Classic, DataTable EstimationArea , ref double MSE_Yr, ref double CV, List <DataTable> zones) { bool result; EstimationArea.Columns.Add("ID", typeof(Int32)); EstimationArea.Columns.Add("估算结果(万亩)", typeof(double)); try { zones.Clear(); double YrArea2 = 0; DataTable Population1 = new DataTable(); //AttributeTable(file, Population1); double sum_Area = 0; //耕地总面积 for (int j = 0; j < sampleTimes; j++) { Population1.Clear(); Population1.Columns.Clear(); IFeatureClass allVillage = EngineAPI.OpenFeatureClass(file); ITableConversion conver = new TableConversion(); Population1 = conver.AETableToDataTable(allVillage); sum_Area = Population1.AsEnumerable().Select(t => t.Field <double>(Basis)).Sum(); //sum_Area = ColumnSum(Population1, Basis); double Yr_Area = 0; //第j套样本估算得到的总面积 //得出:总体分层的层数i_ZoneCount、各层的样本量i_ZoneSamplesNum[]、按分层整合的记录ZonePopu[] int i_ZoneCount = 0; //层数 int[] i_ZoneSamplesNum = null; DataTable SamplePopu = StratifiedPPSSampling(Population1, i_samplesNum, ZoneNum, Basis); DataTable[] ZonePopu = GetStratifiedSamples(SamplePopu, i_samplesNum, ZoneNum, ref i_ZoneCount, ref i_ZoneSamplesNum); for (int h = 0; h < i_ZoneCount; h++) { double[] h_probability = new double[ZonePopu[h].Rows.Count]; for (int i = 0; i < ZonePopu[h].Rows.Count; i++) { //计算入样概率,公式为 【第h层的【层样本量】】 *【抽样单元内【耕地面积】】/【h层耕地总面积】 double SUMh_Area = ZonePopu[h].AsEnumerable().Select(t => t.Field <double>(Basis)).Sum(); //【h层耕地总面积】 //double SUMh_Area1 = ColumnSum(ZonePopu[h], Basis);//【h层耕地总面积】 h_probability[i] = ZonePopu[h].Rows.Count * Convert.ToDouble(ZonePopu[h].Rows[i][Basis]) / SUMh_Area; if (h_probability[i] != 0) { //M_chi表示第h层第i个样本村的作物分类面积 double Chi_Area = Convert.ToDouble(ZonePopu[h].Rows[i][Classic]);//classic表示表中分类面积字段名称 Yr_Area += Chi_Area / h_probability[i]; } } } if (!zones.Contains(SamplePopu)) { zones.Add(SamplePopu); } double Basis_Area = SamplePopu.AsEnumerable().Select(t => t.Field <double>(Basis)).Sum(); //抽样单元村耕地总面积; double Yr = Yr_Area * 0.0015 / 10000; //单位转化 EstimationArea.Rows.Add(new object[] { j + 1, Yr.ToString("f2") }); //YrArea2=(∑[(Y_r/n*M-Y)]^2 ),Y_r是第 r 套样本耕地总面积值,Y是【耕地总面积】,n是抽取样本村个数,M是入样总体村个数 YrArea2 += Math.Pow((Basis_Area / i_samplesNum * Population1.Rows.Count - sum_Area), 2); } //精度评价计算 //均方误差计算,公式(∑【(Y_r-Y)】^2 )⁄k,k为【重复抽样次数】,Y_r是第 r 套样本对总体面积估计值,Y是【耕地总面积】 MSE_Yr = YrArea2 / sampleTimes; //变异系数计算,公式(Sqrt(MSE(Y_r))/Y*100% CV = Math.Sqrt(MSE_Yr) / sum_Area; result = true; } catch (Exception ex) { MessageBox.Show(ex.Message); result = false; } return(result); }
/// <summary> /// Calculate the class area. /// </summary> /// <param name="unitFile">The unit file.</param> /// <param name="classFile">The class file.</param> /// <param name="classValue">The class value.</param> /// <param name="msg">The MSG.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool CalClassArea(string unitFile, string classFile, int classValue, out string msg) { IFeatureClass pUnitFeatureClass = null; IGeoDataset pRasterDst = null; ESRI.ArcGIS.SpatialAnalyst.IZonalOp pZonalOp = null; ITable pZonalTable = null; ITable pUnitTable = null; msg = string.Empty; try { int pixelValue = -1; if (!int.TryParse(SampleData.classNames[classValue], out pixelValue)) { pixelValue = classValue; } //未选中作物设为NODATA string targetClassFile = Path.Combine(GFS.BLL.ConstDef.PATH_TEMP, DateTime.Now.ToFileTime().ToString() + "SetNull.tif"); if (!EnviVars.instance.GpExecutor.SetNoData(classFile, pixelValue, targetClassFile, out msg)) { return(false); } //pRasterDst = EnviVars.instance.GpExecutor.SetNull(classFile, classValue); //区域统计 pUnitFeatureClass = EngineAPI.OpenFeatureClass(unitFile); pRasterDst = EngineAPI.OpenRasterFile(targetClassFile) as IGeoDataset; pZonalOp = new ESRI.ArcGIS.SpatialAnalyst.RasterZonalOpClass(); pZonalTable = pZonalOp.ZonalStatisticsAsTable((IGeoDataset)pUnitFeatureClass, pRasterDst, true); if (pZonalTable == null) { msg = "计算分类面积失败:区域统计结果为空!"; return(false); } ITableConversion conver = new TableConversion(); DataTable zonalDT = conver.AETableToDataTable(pZonalTable); pUnitTable = pUnitFeatureClass as ITable; //添加分类面积字段 //string fName = "YGMJ" + SampleData.classNames[classValue]; string fName = "YGMJ"; if (pUnitTable.FindField(fName) < 0) { IField areaField = new FieldClass(); IFieldEdit fieldEdit = areaField as IFieldEdit; fieldEdit.Name_2 = fName; fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; pUnitTable.AddField(areaField); } //面积字段赋值 //int unitRows = pUnitTable.RowCount(null); //int areaRows = pZonalTable.RowCount(null); int unitId = pUnitTable.FindField("FID"); //int areaId = pZonalTable.FindField("VALUE"); int unitAreaIndex = pUnitTable.FindField(fName); //int areaIndex = pZonalTable.FindField("AREA"); ICursor pUnitCursor = pUnitTable.Update(null, false); IRow unitRow = null; while ((unitRow = pUnitCursor.NextRow()) != null) { string unitFid = unitRow.get_Value(unitId).ToString(); foreach (DataRow row in zonalDT.Rows) { if (unitFid == row["VALUE"].ToString()) { unitRow.set_Value(unitAreaIndex, row["AREA"]); pUnitCursor.UpdateRow(unitRow); } } //IRow areaRow = null; //ICursor pAreaCursor = pZonalTable.Update(null, false); //while ((areaRow = pAreaCursor.NextRow()) != null) //{ // string areaFid = areaRow.get_Value(areaId).ToString(); // if (unitFid == areaFid) // { // object value = areaRow.get_Value(areaIndex); // unitRow.set_Value(unitAreaIndex, value); // pUnitCursor.UpdateRow(unitRow); // } //} //Marshal.ReleaseComObject(pAreaCursor); } if (pUnitCursor != null) { Marshal.ReleaseComObject(pUnitCursor); } //for (int i = 0; i < unitRows; i++) //{ // IRow unitRow = pUnitTable.GetRow(i); // var unitFid = unitRow.get_Value(0); // for (int j = 0; j < areaRows; j++) // { // IRow areaRow = pZonalTable.GetRow(j); // var areaFid = areaRow.get_Value(0); // if (unitFid == areaFid) // { // unitRow.set_Value(unitAreaIndex, areaRow.get_Value(areaIndex)); // unitRow.Store(); // } // } //} return(true); } catch (Exception ex) { throw ex; } finally { if (pUnitFeatureClass != null) { Marshal.ReleaseComObject(pUnitFeatureClass); } if (pRasterDst != null) { Marshal.ReleaseComObject(pRasterDst); } if (pZonalOp != null) { Marshal.ReleaseComObject(pZonalOp); } if (pUnitTable != null) { Marshal.ReleaseComObject(pUnitTable); } if (pZonalTable != null) { Marshal.ReleaseComObject(pZonalTable); } } }
//基于抽样概率估计,新 /// <summary> /// /// </summary> /// <param name="Popu_File">入样总体</param> /// <param name="Sample_File">二级样本</param> /// <param name="Popu_Basis">总体耕地</param> /// <param name="Popu_Layer">总体分层</param> /// <param name="Sample_Layer">样本分层</param> /// <param name="Sample_Basis">样本耕地</param> /// <param name="Sample_Survey">样本调查面积</param> /// <param name="Sample_CunCode">村代码</param> /// <param name="Save_File">保存文件</param> /// <returns></returns> public bool ProbabilityProcessing(string Popu_File, string Sample_File, string Popu_Basis, string Popu_Layer, string Sample_Layer, string Sample_Basis, string Sample_Survey, string Sample_CunCode, string Save_File) { try { DataTable Population = new DataTable(); DataTable Samples = new DataTable(); IFeatureClass allVillage = EngineAPI.OpenFeatureClass(Popu_File); IFeatureClass allVillage1 = EngineAPI.OpenFeatureClass(Sample_File); ITableConversion conver = new TableConversion(); Population = conver.AETableToDataTable(allVillage); Samples = conver.AETableToDataTable(allVillage1); //概率估计计算 if (Population == null || Samples == null || Popu_Basis == "" || Popu_Layer == "" || Sample_Layer == "" || Sample_Basis == "" || Sample_Survey == "" || Sample_CunCode == "" || Save_File == "") { return(false); } fUnitNum = Population.Rows.Count; SampleNum = Samples.Rows.Count; unitLandArea = double.Parse(Population.Compute(string.Format("sum({0})", Sample_Basis), "true").ToString()) / 666.67; //unitClassArea = double.Parse(Population.Compute(string.Format("sum({0})", Sample_CunCode), "true").ToString()) / 666.67; sampleLandArea = double.Parse(Samples.Compute(string.Format("sum({0})", Sample_Basis), "true").ToString()) / 666.67; //sampleClassArea = double.Parse(Samples.Compute(string.Format("sum({0})", Sample_CunCode), "true").ToString()); sampleSurveyArea = double.Parse(Samples.Compute(string.Format("sum({0})", Sample_Survey), "true").ToString()) / 666.67; double CV = 0; double M_Area = 0; if (SampleProbability(Population, Samples, Popu_Basis, Popu_Layer, Sample_Layer, Sample_Basis, Sample_Survey, Sample_CunCode, ref M_Area, ref CV)) { //保存推算结果至EXCl string p0 = " 面积估算结果"; string p1 = "估算方法:基于入样概率估计 "; string p2 = string.Format("估算面积:{0} 亩 CV值:{1}", (M_Area / 666.667).ToString("#0.00"), CV); string p3 = string.Format("一级抽样单元总数:{0} 样方总数:{1}", fUnitNum, SampleNum); //string p4 = string.Format("区域耕地总面积:{0} 亩 ", unitLandArea.ToString("#0.00")); string p5 = string.Format("二级样方总耕地面积:{0} 亩 二级样方总调查面积:{1} 亩", sampleLandArea.ToString("#0.00"), sampleSurveyArea.ToString("#0.00")); string p6 = string.Format("耕地面积与调查面积相关系数:{0}", coefficient);; List <string> list = new List <string>() { p0, p1, p2, p3, p5, p6 }; if (File.Exists(Save_File)) { File.Delete(Save_File); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(Save_File, true)) { foreach (string line in list) { file.WriteLine(line);// 直接追加文件末尾,换行 file.WriteLine(); } file.Flush(); file.Close(); return(true); } //DataTable ptable = new DataTable(); //ptable.Columns.Add ( "估算总面积",typeof(double)); //ptable.Columns.Add("CV值", typeof(double)); //ptable.Rows.Add(); //DataRow dr = ptable.NewRow(); //dr[0] = M_Area; //dr[1] = CV; //ptable.Rows.Add(dr); //if (ptable == null) // return false; //ExcelHelper excel = new ExcelHelper(); //if (excel.DataTableToExcel(Save_File, "sheet1", ptable, true)) //{ // MessageBox.Show("成功"); // return true; //} //else //{ // MessageBox.Show("失败"); // return false ; //} } else { return(false); } } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } }
//分层比率估计 /// <summary> /// 比率估计预处理(读写文件) /// </summary> /// PopulationPath 入样总体文件地址 /// SamplesPath 抽样样本文件地址 /// FieldName 测量结果字段名称 /// Layer_Population 入样总体的层号 /// Layer_Samples 抽样样本的层号 /// Classify_Population 入样总体的分类结果 /// Classify_Samples 抽样样本的分类结果 /// SavePath 推算结果保存地址 /// <returns>是否计算成功</returns> public bool RatioPreprocessing(string PopulationPath, string SamplesPath, string Survey_Samples, string Layer_Population, string Layer_Samples, string Classify_Population, string Classify_Samples, string SavePath, string Sample_Basis, string Sam_CunCode) { try { DataTable Population = new DataTable(); DataTable Samples = new DataTable(); IFeatureClass PopuIFeaClass = EngineAPI.OpenFeatureClass(PopulationPath); IFeatureClass SamIFeaClass = EngineAPI.OpenFeatureClass(SamplesPath); ITableConversion conver = new TableConversion(); Population = conver.AETableToDataTable(PopuIFeaClass); Samples = conver.AETableToDataTable(SamIFeaClass); //比率估计计算 if (Population == null || Samples == null || Survey_Samples == "" || Layer_Population == "" || Layer_Samples == "" || Classify_Population == "" || Classify_Samples == "" || SavePath == "" || Sample_Basis == "" || Sam_CunCode == "") { return(false); } fUnitNum = Population.Rows.Count; SampleNum = Samples.Rows.Count; unitLandArea = double.Parse(Population.Compute(string.Format("sum({0})", Sample_Basis), "true").ToString()) / 666.67; unitClassArea = double.Parse(Population.Compute(string.Format("sum({0})", Classify_Population), "true").ToString()) / 666.67; sampleLandArea = double.Parse(Samples.Compute(string.Format("sum({0})", Sample_Basis), "true").ToString()) / 666.67; sampleClassArea = double.Parse(Samples.Compute(string.Format("sum({0})", Classify_Samples), "true").ToString()) / 666.67; sampleSurveyArea = double.Parse(Samples.Compute(string.Format("sum({0})", Survey_Samples), "true").ToString()) / 666.67; double CV = 0; double d_result = RatioEstimating(Population, Samples, Survey_Samples, Layer_Population, Layer_Samples, Classify_Population, Classify_Samples, Sample_Basis, Sam_CunCode, ref CV) / 666.667; //保存推算结果至EXCl string p0 = " 面积估算结果"; string p1 = "估算方法:分层比率估计 "; string p2 = string.Format("估算面积:{0} 亩 CV值:{1}", d_result.ToString("#0.00"), CV); string p3 = string.Format("一级抽样单元总数:{0} 样方总数:{1}", fUnitNum, SampleNum); //string p4 = string.Format("区域耕地总面积:{0} 亩 区域分类总面积:{1} 亩", unitLandArea.ToString("#0.00"), unitClassArea.ToString("#0.00")); string p5 = string.Format("二级样方总耕地面积:{0} 亩 二级样方总分类面积:{1} 亩 二级样方总调查面积:{2} 亩", sampleLandArea.ToString("#0.00"), sampleClassArea.ToString("#0.00"), sampleSurveyArea.ToString("#0.00")); string p6 = string.Format("分类面积与调查面积相关系数:{0}", coefficient);; List <string> list = new List <string>() { p0, p1, p2, p3, p5, p6 }; if (File.Exists(SavePath)) { File.Delete(SavePath); } using (System.IO.StreamWriter file = new System.IO.StreamWriter(SavePath, true)) { foreach (string line in list) { file.WriteLine(line);// 直接追加文件末尾,换行 file.WriteLine(); } file.Flush(); file.Close(); return(true); } //DataTable ptable = new DataTable(); //ptable.Columns.Add ( "估算总面积",typeof(double)); //ptable.Columns.Add("CV值", typeof(double)); //ptable.Rows.Add(); //DataRow dr = ptable.NewRow(); //dr[0] = d_result; //dr[1] = CV; //ptable.Rows.Add(dr); //if (ptable == null) // return false; //ExcelHelper excel = new ExcelHelper(); //if (excel.DataTableToExcel(SavePath, "sheet1", ptable, true)) //{ // MessageBox.Show("成功"); // return true; //} //else //{ // MessageBox.Show("失败"); // return false ; //} } catch (Exception ex) { MessageBox.Show(ex.Message); return(false); } }
//抽样仿真 private void siBSimulation_Click(object sender, EventArgs e)//抽样仿真 { sampleSumTable.Clear(); sampleSumTable.Columns.Clear(); if (string.IsNullOrEmpty(cBFile.Text.TrimEnd()) || string.IsNullOrEmpty(cBEBasis.Text.TrimEnd()) || string.IsNullOrEmpty(cBELayer.Text.TrimEnd())) { MessageBox.Show("错误信息:\n入样总体文件是必需的!\n抽样依据的值:是必需的!\n分层的值是:必需的!"); } else { IFeatureClass allVillage = EngineAPI.OpenFeatureClass(cBFile.Text); ITableConversion conver = new TableConversion(); sampleSumTable = conver.AETableToDataTable(allVillage); SamplingSum = sampleSumTable.Rows.Count; if (string.IsNullOrEmpty(tENum.Text.TrimEnd()) && string.IsNullOrEmpty(tEProportion.Text.TrimEnd()) || string.IsNullOrEmpty(tETimes.Text.TrimEnd())) { MessageBox.Show("错误信息:\n样本量与抽样比例的值二选一,必须有一个有值!\n抽样次数值是:必需的!"); } else { if (cENum.Checked == true) { sampleNum = Convert.ToInt32(tENum.Text); } else { sampleNum = Convert.ToInt32(SamplingSum * (Convert.ToDouble(tEProportion.Text) / 100)); } if (0 > sampleNum || sampleNum > SamplingSum) { MessageBox.Show("输入的样本量超出范围,请重新输入!"); return; } //显示 groupCResult.Visible = true; groupC.Visible = true; this.MinimumSize = new Size(755, 425); groupCHelp.Location = new Point(758, 12); //结果汇总 lBCResult.Items.Clear();//清空 lBCResult.Items.Add("抽样单元的总数:" + SamplingSum); lBCResult.Items.Add("抽中样本数:" + sampleNum); int sampleTimes = Convert.ToInt32(tETimes.Text); double Cv = 0.0; double MSEYr = 0.0; //EstArea 重复抽样得到的估算面积表 DataTable EstArea = new DataTable(); SampleSimulation Sim = new SampleSimulation(); if (Sim.SampleEvaluating(cBFile.Text, sampleTimes, sampleNum, cBELayer.Text, cBEBasis.Text, cBEClassic.Text, EstArea, ref MSEYr, ref Cv, SampleZones)) { lBCResult.Items.Add("均方误差(MSE):" + (MSEYr).ToString("f2")); lBCResult.Items.Add("变异系数:" + (Cv * 100).ToString("f1") + "%"); gridControl1.DataSource = EstArea; //chartData 绑定估计结果 chartData.Series.Clear(); Series dataTable1Series = new Series("", ViewType.Spline); dataTable1Series.ArgumentDataMember = EstArea.Columns[0].ColumnName; dataTable1Series.ValueDataMembers[0] = EstArea.Columns[1].ColumnName; dataTable1Series.DataSource = EstArea; chartData.Series.Add(dataTable1Series); } } } }
/// <summary> /// survey sample summaries . /// </summary> /// <param name="msg">The MSG.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> /// <exception cref="System.Exception"> /// </exception> public bool Summary(out string msg) { if (!ChkPara(out msg)) { return(false); } IFeatureClass pSampleFC = _pSampleLyr.FeatureClass; IFeatureClass pSurveyFC = _pSurveyLyr.FeatureClass; try { //copy second sample to tempfile; //string tempSummary = Path.Combine(ConstDef.PATH_TEMP, DateTime.Now.ToFileTime().ToString() + ".shp"); //CommonAPI.CopyShpFile(_sampleFile, tempSummary); ////open tempfile and add field //pOutFC = EngineAPI.OpenFeatureClass(tempSummary); IField newField = new FieldClass(); IFieldEdit pFieldEdit = newField as IFieldEdit; pFieldEdit.Name_2 = _fieldArea; pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; if (pSampleFC.FindField(_fieldArea) < 0) { pSampleFC.AddField(newField); } //open survey sample and summarize area //pFieldFC = EngineAPI.OpenFeatureClass(_fieldFile); TableConversion conver = new TableConversion(); DataTable fieldDT = conver.AETableToDataTable(pSurveyFC); //remove unSelected crop DataRow[] rows = fieldDT.Select(_fieldCrop + " <> '" + _cropSelected + "'"); foreach (DataRow row in rows) { fieldDT.Rows.Remove(row); } //add unique column DataColumn refColum = new DataColumn(_unque, typeof(string)); if (!fieldDT.Columns.Contains("uniqueid")) { fieldDT.Columns.Add(refColum); } foreach (DataRow row in fieldDT.Rows) { row[_unque] = row[_fieldVillage].ToString() + row[_fieldID.ToString()].ToString(); } Summarize sum = new Summarize(fieldDT); DataTable sumDT = sum.Sum(fieldDT.Columns.IndexOf(_fieldArea), fieldDT.Columns.IndexOf(_unque)); //set tempfile area field value IFeatureCursor pCursor = null; pCursor = pSampleFC.Update(null, false); IFeature pFeature = null; int villageIndex = pSampleFC.FindField(_fieldVillage); if (villageIndex < 0) { throw new Exception(string.Format("二级样本不包含名为:{0} 的字段", _fieldVillage)); } int idIndex = pSampleFC.FindField(_fieldID); if (idIndex < 0) { throw new Exception(string.Format("二级样本不包含名为:{0} 的字段", _fieldID)); } int areaIndex = pSampleFC.FindField(_fieldArea); while ((pFeature = pCursor.NextFeature()) != null) { foreach (DataRow row in sumDT.Rows) { string unique = pFeature.get_Value(villageIndex).ToString() + pFeature.get_Value(idIndex).ToString(); if (unique == row[_unque].ToString()) { pFeature.set_Value(areaIndex, row[1]); pFeature.Store(); } } } if (pCursor != null) { Marshal.ReleaseComObject(pCursor); } return(true); } catch (Exception ex) { throw ex; } finally { //if(pOutFC!=null) // Marshal.ReleaseComObject(pOutFC); //if (pFieldFC != null) // Marshal.ReleaseComObject(pFieldFC); } }