/// <summary> /// 获取统计信息 /// </summary> private void getStatistics() { Summarize sumarize = null; if (soilTable != null) { sumarize = new Summarize(soilTable); //统计各土壤类型的面积 AreaTable = sumarize.Sum(soilTable.Columns.IndexOf(soilArea), soilTable.Columns.IndexOf(soilType)); //统计各土壤类型的土层厚度均值 depthTable = sumarize.Avg(soilTable.Columns.IndexOf(soilDepth), soilTable.Columns.IndexOf(soilType)); //统计各土壤类型的土壤容重均值 densityTable = sumarize.Avg(soilTable.Columns.IndexOf(SoilDensity), soilTable.Columns.IndexOf(soilType)); //统计各土壤类型的土壤有机质均值 organicsTable = sumarize.Avg(soilTable.Columns.IndexOf(soilOrganics), soilTable.Columns.IndexOf(soilType)); //统计各土壤类型的土层砾石含量均值 gravelTable = sumarize.Avg(soilTable.Columns.IndexOf(soilGravel), soilTable.Columns.IndexOf(soilType)); } }
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> /// 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); } }